![]() |
Home | Libraries | People | FAQ | More |
In the following section, we will deal with Parsing Expression Grammars (PEG), a variant of Extended Backus-Naur Form (EBNF) with a different interpretation. It is easier to understand PEG using Syntax Diagrams. Syntax diagrams represent a grammar graphically. It was used extensibly by Niklaus Wirth [1] in the "Pascal User Manual". Syntax Diagrams are easily understandable with programmers due to its similarity to flow charts. Also, the one to one mapping between the diagrams and functions make it ideal for representing [Recursive Descent parsers.
All diagrams have one entry and exit point. Arrows connect all possible paths through the grammar from the entry point to the exit point.
Terminals are represented by round boxes. Terminals are atomic and usually represent plain characters, strings or tokens.
Non-terminals are represented by boxes. Diagrams are modularized using named non-terminals. A complex diagram can be broken down into a set of non-terminals. Non-terminals also allow recursion (i.e. a non-terminal can call itself).
The most basic composition is the Sequence. B follows A:
The ordered choice, hencforth we will call alternatives. In PEG, ordered choice and alternatives are not quite the same. PEG allows ambiguity of choice where one ore more branches can succeed. In PEG, in case of ambiguity, the first one always wins.
The optional (can be thought of as zero-or-one):