POSIX sh EBNF grammar

2019-03-19 09:32发布

问题:

Is there an existing POSIX sh grammar available or do I have to figure it out from the specification directly?

Note I'm not so much interested in a pure sh; an extended but conformant sh is also more than fine for my purposes.

回答1:

The POSIX standard defines the grammar for the POSIX shell. The definition includes an annotated Yacc grammar. As such, it can be converted to EBNF more or less mechanically.

If you want a 'real' grammar, then you have to look harder. Choose your 'real shell' and find the source and work out what the grammar is from that.

Note that EBNF is not used widely. It is of limited practical value, not least because there are essentially no tools that support it. Therefore, you are unlikely to find an EBNF grammar (of almost anything) off-the-shelf.



回答2:

I have done some more digging and found these resources:

  1. An sh tutorial located here

  2. A Bash book containing Bash 2.0's BNF grammar (gone from here) with the relevant appendix still here

I have looked through the sources of bash, pdksh, and posh but haven't found anything remotely at the level of abstraction I need.



回答3:

I'd expect that sh, csh, ash, bash, would contain parsers. GNU versions of these are open source; you might just go check there.