NOTE: The midterm exam is on Thursday, March 29.
The exam will cover the material on expression grammars and recursive descent parsers from the following zip file.
- Problem 2:
- For each of the following expressions, rewrite the expression in both prefix and postfix notation, and draw an abstract syntax tree (i.e., an expression tree) for the expression.
(a) a * b + c
(b) a * (b + c)
(c) a * b + c * d
(d) a * (b + c) * d
- Problem 3:
- Describe in English the language defined by the following grammar. (Note: Do not use English to describe the grammar, describe the language defined by the grammar.)
<S> -> <A> <B> <C>
<A> -> 'a' <A> | 'a'
<B> -> 'b' <B> | 'b'
<C> -> 'c' <C> | 'c'
- Problem 4:
- Consider the following grammar.
<S> -> <A> 'a' <B> 'b'
<A> -> <A> 'b' | 'b'
<B> -> 'a' <B> | 'a'
Which of the following words are in the language generated by this grammar?
(a) baab
(b) bbbab
(c) bbaaaaa
(d) bbaab
- Problem 5:
- Consider the following grammar.
<S> -> 'a' <S> 'c' <B> | <A> | 'b'
<A> -> 'c' <A> | 'c'
<B> -> <A> | 'd'
Which of the following words are in the language generated by this grammar?
(a) abcd
(b) acccbd
(c) acccbcc
(d) acd
(e) accc
- Problem 6:
- For each of the following parts, define a grammar for the words, over the alphabet containing just 0 and 1, that are described.
(a) All words ending with a 1.
(b) All words of length exactly 8.
(c) All words that contain the string 0101.
(d) All words with odd length. Show how to parse the word 00100 using your grammar.
- Problem 7:
- Show that the grammar
expr -> '-' expr
| expr '-' ( 'x' | 'y' | 'z' )
| 'x' | 'y' | 'z'
is ambiguous by finding a string that has two different parse trees.
- Problem 8:
- The following grammar is motivated by declarations in C.
Declaration ::= Type Declarator ';'
Type ::= 'int' | 'char'
Declarator ::= '*' Declarator
| Declarator '[' Digit ']'
| Declarator '(' Type ')'
| '(' Declarator ')'
| Letter
Letter ::= 'a' | 'b' | ... | 'y' | 'z'
Digit ::= '1' | '2' | ... | '8' | '9'
(a) Give 5 examples of strings generated by this grammar.
(b) Show that this grammar is ambiguous.
- Problem 9:
- Consider the following grammar for postfix expressions.
Postfix -> Postfix Postfix BinOp
| Postfix UnOp
| Number
BinOp -> '+' | '-' | '*' | '/'
UnOp -> 'neg' | 'sqrt'
Number -> any valid Java double
- Part (a) Draw the parse tree (not the expression tree) for the expression
4 5 + sqrt 3 *
- Part (b) Explain why you cannot write a recursive descent parser for this grammar.
- Part (c) Draw the abstract syntax tree for the expression
4 5 + sqrt 3 *
- Problem 10:
- Consider the following grammar for postfix expressions. (The symbol EMPTY in the grammar means that we produce an empty string.)
Postfix -> Number Postfix2
Postfix2 -> Postfix BinOp Postfix2
| UnOp Postfix2
| EMPTY
BinOp -> '+' | '-' | '*' | '/'
UnOp -> 'neg' | 'sqrt'
Number -> any valid Java double
- Part (a) Draw the parse tree (not the expression tree) for the expression
4 5 + sqrt 3 *
- Part (b) Explain why the abstract syntax tree for the expression
4 5 + sqrt 3 *
is the same as it was for the previous problem, even though the grammar has changed.
- Part (c) Explain why you can write a recursive descent parser for this grammar.
- Part (d) Write the Java code for the
getPostfix() and getPostfix2() methods that would implement a recognizing parser (assume the usual interface to the Tokenizer ).
private static void getPostfix(Tokenizer tokens)
{
}//getPostfix()
private static void getPostfix2(Tokenizer tokens)
{
}//getPostfix2()
- Problem 11:
-
Suppose that a grammar for some language has the following production,
P3 . Use Java to write the function that parses this production in a recognizing recusive descent parser. Assume the usual interface to the Tokenizer class.
P3 -> '*' P4
| '@' P4 P5
| '#' P5 ( '#' P5 )*
| P6
|