Answer each of the following problems. If a grammar is requested, you may express it as a formal context-free grammar, or in an equivalent BNF form. (BNF is just a shorthand notation for context-free grammars.)
1. Consider the following BNF grammar:
NEW STRUCTURE:
<expression> ::= <term> | <expression> "+" <term> | <expression> "-" <term>
<term> ::= <factor> | <term> "*" <factor> | <term> "/" <factor>
<factor> ::= <number> | "(" <expression> ")"
<number> ::= <digit> | <number> <digit>
<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
Terminals: + - * / ( )
Non-Terminals: <expression> <term> <factor> <number> <digit>
Now consider the function F defined by the following structures:
F ::= 0 | 1 | 1 <V> 1 <E> 1 (<E> / <E>) 1 (<E> / <E>) x1x1 11O1
Part i: Give both the terminal and non-terminal vocabulary of this grammar. Describe the language that this grammar defines, and discuss whether or not you think the grammar is ambiguous.
Terminal vocabulary: + - * / ( )
Non-terminal vocabulary: <expression> <term> <factor> <number> <digit>
The language defined by this grammar is arithmetic expressions with addition, subtraction, multiplication, and division operations. The grammar is ambiguous because there are multiple ways to derive the same expression.
Part 2: Show a parse tree for the sentence "( (P / Q) / R )".
<expression>
|
<expression>
|
<term>
|
<factor>
|
"("
|
<expression>
|
"("
|
<expression>
|
<term>
|
<factor>
|
"P"
|
"/"
|
"Q"
|
")"
|
"/"
|
"R"
|
")