You are on page 1of 2

Cairo University Compilers and Languages Computer Eng. Dept.

Faculty of Engineering Problem Set #4 Spring 2016

1. Write an unambiguous grammar for the language of balanced parentheses, i.e. the
language that contains (among other) the sequences

ε (i.e. the empty string)


()
(())
()()
(()(()))

but none of the following


(
)
)(
(()
()())

2. Write grammars for each of the following languages:

a) All sequences of as and bs that contain the same number of as and bs (in any order).
b) All sequences of as and bs that contain strictly more as than bs.
c) All sequences of as and bs that contain a different number of as and bs.
d) All sequences of as and bs that contain twice as many as as bs.

3. Show that the grammar

A → −A
A →A − id
A → id

is ambiguous by finding a string that has two different syntax trees. Now make two
different unambiguous grammars for the same language:

a) One where prefix minus binds stronger than infix minus.


b) One where infix minus binds stronger than prefix minus.

4. Consider the following grammars which recognize the same language:

Grammar A:
S → aSb
S → aS
S→ 
Grammar B:
S → aSb
S→T
T → aT
T→

(a) Give a one-sentence description of the language generated by both grammars.


(b) Perform a parse for the string aab using both grammars. Which one of these
grammars is ambiguous? Support your answer by showing two different parse trees that
can be derived for the same string using the ambiguous grammar.
(c) Which Grammar would you choose for your parser implementation? Why?

Show the syntax trees using the new grammars for the string you used to prove the
original grammar ambiguous.

You might also like