Toc 1
Toc 1
2. (a) Consider the following (already augmented) grammars for the language a* [5]
S A
A Aa | ε
and
S A
A aA | ε
Both of these grammars are SLR(1). However, the SLR(1) parser for one of these
grammars will use O(n) space in its parsing stack when run on the string an, while the
other parser will only use O(1) stack space.
Identify which grammar's parser uses O(n) stack space and which grammar's parser uses
O(1) stack space. Justify your answer by making specific references to how an SLR(1)
parser for each grammar parses strings of the form an. In other words, even if you can
figure out why one parser uses O(n) stack space, you should still explain why the other
parser uses only O(1) stack space.
(b) Consider the following expression grammar [5]
exprs := exprs + expr | exprs * expr | expr
expr := x
(a) Is the grammar ambiguous or unambiguous? If ambiguous show two different
parse trees for the same string. If it is unambiguous give an informal argument?
(b) Does this grammar properly capture the normal precedence of arithmetic operators *
and +. If so, give a brief argument as to why it does, if not, give an example that shows
where it fails to capture the correct precedence relationship.
3. Let G be an LL(1) grammar with a set of terminal symbols and as the set of non-
terminal symbols (Note: For each of the following the answer should be given in big-O
notation. For example O(x), if the quantity is linear in x)
(a) What is the maximum size of LL(1) parsing table for G? Why?
(b) What is the size of the largest parse tree produced the grammar in CNF for a
string of length n? Why? (Assume that the grammar G is not having -
productions)
(c) How long does it take to parse a string of length n with respect to G using LL(1)
parsing algorithm? Why?
(d) What is the maximum size of SLR(1) parsing table for G? Why?
4. (a) Suppose we want to add the following conditional statement to MiniJava: [5]
ifequal (exp1, exp2)
statement1
smaller
statement2
larger
statement3
The meaning of this is that statement1 is executed if the integer expressions exp1 and exp2
are equal; statement2 is executed if exp1 < exp2, and statement3 is executed if exp1 >
exp2.
Give context-free grammar production(s) for the ifequal statement that allows either or
both of the smaller and larger parts of the statement to be omitted. If both the smaller and
larger parts of the statement appear, they should appear in that order.
(b) Consider the following grammar for a simplified Clike function prototype. [5]
The terminals are {T_Ident, T_Double, T_Char, (, ), ; ,}. The tokens will be recognized by
the scanner and passed to the parser.
Proto → Type T_Ident ( ParamList ) ;
Type → T_Int | T_Double | T_Char
ParamList → ParamList , Param | Param
Param → Type T_Ident
Why does the LR(0) parser not attempt a reduction to Param after pushing the first
sequence of type and identifier onto the parse stack? Doesn’t the sequence on top of the
stack match the right side. What other requirements must be met before a reduction is
performed?
5. (a) Give a formal description and the corresponding state diagram of a PDA that recognizes [5]
the language L = {w | 2#a(w) 3#b(w), w ∈ {a, b} ∗ } , where #a(w) and #b(w) denotes the
number of a’s and b’s occurring in the string w.
(b) Let L1 = {0n1m | 0 < n ≤ m < 2n}. Let G1 be the grammar with starting symbol S and the [5]
following rules:
Rule 1: S → 0S11
Rule 2: S → T
Rule 3: T → 0T1
Rule 4: T → 01
For each n and m satisfying 0 < n ≤ m < 2n, describe a leftmost derivation of 0n1m using
the grammar G1. (That is, say how many times to apply each rule and in what order).