CIT 594 BNF Example
Spring 2013, David Matuszek
The BNF assignment is correct as written. It's somewhat different than my usual assignments (hence part of my confusion), in that the parsing is done at a different stage.
I'll try to clarify things with an example.
read statements, followed by some number of write statements. The read statement reads in one var at a time; the write statement can write out any number (but at least one) of vars.<program> ::= <reads> <writes>.
<reads> ::= { read
<var> }.
<writes> ::= { write <var> {, <var>}}.
<var> ::= a | b | c | d | e.| Key | value | alternate value* |
|---|---|---|
<program> |
||
<reads> |
![]() |
|
<writes> |
![]() |
|
<var> |
SEQUENCE token." ANYNUM token." ANYNUM node may have many children (in an implicit sequence), or only one. I prefer the first (fewer nodes overall), but the second is also plausible, so I will accept either. <program>", repeatedly expand each nonterminal in the list (that is, replace it by its definition) until there are no nonterminals left. For example,| start with | <program> |
| expand <program> | <reads><writes> |
| expand <reads> |
|
| expand some <var> | read <var> read d read <var> <writes> |
| expand some <var> | read e read d read <var> <writes> |
| expand <writes> | read e read d read <var> write <var>,<var> |
| expand some <var> | read e read d read <var> write a, <var> |
| expand some <var> | read e read d read <var> write a, d |
| expand some <var> | read e read d read e write a, d |
| quit, because no more nonterminals | Write out the generated program. |
a) that it has not read in. This cannot be corrected by BNF alone! BNF is a context-free grammar, which is another way of saying that you cannot use information outside the nonterminal to decide how to expand the nonterminal.