Syntax diagrams

Before trying the quiz, please read section 3.3.3 of Sebesta.

Niklaus Wirth, the developer of the Pascal group of languages, used a diagrammatic form of EBNF to show a grammar. The three main additions to BNF that make it into EBNF are shown in pictures. The principles of a syntax diagram are as follows:

1. each diagram corresponds to an EBNF rule.

2. terminals are enclosed in oval boxes, non-terminals in rectangles.

3. directed arcs (arrows) join the boxes in the direction shows left to right placement in the right hand side of a rule.

4. repetition is shown as a loop

5. alternatives are hown by branching arcs.

6. options are shown by a by-pass technique.

7. recursion is allowed where it cannot be replaced by repetition

Consider the rule for an expression:

expr = term { (+|-) term }

The corresponding diagram is:

Notice how by following the arrows the intent of the rule is preserved. The loop in the diagram allows any number of repetitions of the elements inside the braces in the rule, and the branching of the arcs allows for choices of either + or -. Notice also how there can be no repetitions by taking the "out" arc after passing through the first term box.

Options easily shown as an alternative that passes through no elements, as in the if rule from the section on EBNF:

if_stmt = 'if' expr stmt [ 'else' stmt ]

Here the by-pass branch after the first stmt box corresponds to the use of square brackets in EBNF.

Now try the self-test called diagrams by clicking on Self-Test in the Action Menu