CS3012: Formal Languages and Compilers

advertisement
CS3012 Formal Languages
Exercises
CS3012 Formal Languages
Exercises 7: More Grammars
1.
Parse a * (a + a) using the grammar G and the LR(1) parse table from the notes.
Answer:
2.
Using the grammar G and the LR(1) parse table from the notes, show that (7+8)*(4+6) is in the
language of the grammar. Assume “a” in the table stands for any digit from 0 to 9.
Answer:
3.
Grammar: S -> S(S) | 
Parse Table:
S
0: 1
1:
2: 3
3:
4:
(
R2
S2
R2
S2
R1
)
R2
S4
R1
#
R2
A
R2
R1
Use the grammar, parse table and the LR(1) algorithm to parse "(())()" and ")".
Answer:
4.
A phrase of a sentential form w is a substring u s.t.
S => ... => A => ... => u = w
That is, there exists a derivation tree for w with a subtree T s.t. the leaf nodes of T form u and the
root of T is A.
A simple phrase is a phrase for which S => ... => A => u = w.
An alternative definition of a handle is then a leftmost simple phrase.
For the grammar
S -> aTSb | *
T-> * | S+
find a) the phrases, b) the simple phrases, and c) the handle of:
(i)
(ii)
(iii)
(iv)
Answer:
a*+*b
aaTSb+Sb
aS+aT*bb
aTaT*bb
Download