Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Abstracts

Syntax Diagram
Parsing Expression Grammar

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.

Elements of a Syntax Diagram

All diagrams have one entry and exit point. Arrows connect all possible paths through the grammar from the entry point to the exit point.

start_stop

Terminals are represented by round boxes. Terminals are atomic and usually represent plain characters, strings or tokens.

terminal

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).

non-terminal

The most basic composition is the Sequence. B follows A:

sequence

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.

alternative

The optional (can be thought of as zero-or-one):

optional



[1] Niklaus Wirth: The Programming Language Pascal. (July 1973)


PrevUpHomeNext