In this lesson, we take a closer look at syntax diagrams. We discuss features of syntax diagrams, consider some examples, and test your knowledge with a few exercises. The lesson covers the following concept: How to specify a language using syntax diagrams.
Choices in syntax diagrams
Sequences of items in syntax diagrams
Exercise
The syntax diagram in lesson 1 specifies a language consisting of only a single sentence. This is not of much use. Most languages contain an infinite number of sentences.
Before considering the infinite, we discuss how to generate two sentences by encoding a list of options, or choices, in a syntax diagram. In simple terms, the choice notation (branching arrows) allows us to follow different paths to generate different sentences. This makes the language richer, e.g., composed of more sentences.
Let us consider an example to understand the choice notation better.

This syntax diagram generates sentences of 3 types
7 + 8
| Digit | 7 is a valid Digit |
| Digit + | + is a valid symbol |
| Digit + Digit | 8 is a valid Digit |
| Sentence | Digit + Digit is a valid sentence |
8 - 5
| Digit | 8 is a valid Digit |
| Digit - | - is a valid symbol |
| Digit - Digit | 5 is a valid Digit |
| Sentence | Digit - Digit is a valid Sentence |
Now let us consider an example of a sentence that cannot be recognized using this grammar, ie, a sentence that cannot be parsed.
Let us see if we can parse the sentence 2+-3 with the above grammar.
2 + -3
| Digit | 2 is a valid Digit |
| Digit + | + is a valid symbol |
| Digit + - | Error. - is not a Digit |
| Digit + - Digit | Invalid sentence |
There are no further options to parse the remaining portion of the sentence "-3". Thus the given sentence 2+-3 is not a sentence in the language specified by the grammar.
Let us discuss another feature, namely, Sequences of Symbols. The problem we encountered in Example 2, ie, parsing more than 2 variables can be solved using this feature. The backwards-pointing arrow, or loop, indicates that the portion of the grammar so enclosed can be repeated an arbitrary number of times. Thus, sequences of symbols can be used to generate sentences of any length. In the example, the "+ Letter" sequence can be repeated by following the loop.
Example 3:
This syntax diagram produces sentences that contain multiple addition operations.
A + B + C
| Letter | A is a valid Letter |
| Letter + | + is a valid symbol |
| Letter + Letter | B is a valid Letter |
| Letter + Letter + | + is a valid symbol |
| Letter + Letter + Letter | C is a valid Letter |
| Sentence | Letter + Letter + Letter is a valid Sentence |
Thus this grammar can accept sentences of the form
Write Sentences ( and their derivations ) for this grammar.

The sentences accepted by this grammar are X, AB9D, A23452, ABGFEDC, etc.; that is, any letter optionally followed by any number of letters or digits. Let us parse one of these sentences to understand the grammar better.
AB9D
| Letter | A is a valid Letter |
| Letter Letter | B is a valid Letter |
| Letter Letter Digit | 9 is a valid Digit |
| Letter Letter Digit Letter | D is a valid Letter |
| Sentence | Letter Letter Digit Letter is a valid Sentence |
Write sentences ( and their derivations ) for this grammar.
There are 2 types of sentences for this grammar.
47.85 * 5 * 6.7
| Digit | 4 is a valid Digit |
| Digit Digit | 7 is a valid Digit |
| Digit Digit . | . is a valid symbol |
| Digit Digit . Digit | 8 is a valid Digit |
| Digit Digit . Digit Digit | 5 is a valid Digit |
| Number | Digit Digit . Digit Digit is a valid Number |
| Number * | * is a valid symbol |
| Number * Digit | 5 is a valid Digit |
| Number * Number | Digit is a valid Number |
| Number * Number * | * is a valid symbol |
| Number * Number * Digit | 6 is a valid Digit |
| Number * Number * Digit . | . is a valid symbol |
| Number * Number * Digit . Digit | 7 is a valid Digit |
| Number * Number * Number | Digit . Digit is a valid Number |
| Sentence | Number * Number * Number is a valid Sentence |
980 / 5
| Digit | 9 is a valid Digit |
| Digit Digit | 8 is a valid Digit |
| Digit Digit Digit | 0 is a valid Digit |
| Number | Digit Digit Digit is a valid Number |
| Number / | / is a valid symbol |
| Number / Digit | 5 is a valid Digit |
| Number / Number | Digit is a valid Number |
| Sentence | Number / Number is a valid Sentence |
From this exercise we also learn how to construct a number containing many digits.