Explore BrainMass
Share

# BNF grammar and parse tree

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

1. Write a BNF grammar that describes the structure of a nonterminal called <number>. Assume that <number> contains an optional + sign followed by exactly 2 decimal digits, the first of which cannot be a 0. Thus 23, +91, and +40 are legal, but 9, +01, and 123 are not.

Using this grammar, show a parse tree for the value +90.

2. Write a BNF grammar for identifiers that consist of an arbitrarily long string of letters and digits, the first one of which must be a letter.

Using this grammar, show a parse tree for the identifier AB5C8.

https://brainmass.com/computer-science/trees/bnf-grammar-parse-tree-191762

#### Solution Preview

1. BNF grammar :

<number> ::= + <nzdigit> <digit> | <nzdigit> <digit>
<digit> ::= 0 | <nzdigit>
<nzdigit> ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Parse tree for +90 :

number
/ |
+ |
nzdigit
| digit
9 |
...

#### Solution Summary

Answer shows the parse trees (drawn neatly) in text mode.

\$2.19