Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ID : ITITIU17022
Principles of Programming Languages Exercises 4
Associativity
1. Assume that ADDOP is left-associative binary operator, which grammar is
unambiguous and ensures the associativity of ADDOP?
a. exp exp ADDOP term | term
Answer: b and c
2. Assume that RELOP is none-associative binary operator, which grammar is
unambiguous and ensures the associativity of RELOP?
Ambigous Grammars
1. Choose the ambigous grammars
a.A -> AcA | b
b. S -> aS | bS | a | b
c. S -> SS | a | b
d. A -> AcB | B
e. B -> yBz | t | xB
Answer: c
2. Which is NOT the unambiguous grammar of a list (no separator) of
declarations where decl is a non-terminal symbol representing a
declaration?
a. decls decl+
idlist CM ID idlist |
c. idl ID CM idl | ID |
d. idl ID CM idl | ID
Answer: a
Practice
1. Consider the CFG:
S -> ( L ) | a
L -> L , S | S
Draw parse trees for:
a/ (a, a)
S
L S
S a
a
b/ (a, ((a, a), (a, a)))
S
L S
S L
a L S
S L
L L S
L S S a
S a a
n m
a. L = {a b : n = m}
n m
b. L = {a b : n = 2m}
n m
c. L = {a b : n m}
n m
d. L = {a b : n > m}
n m
e. L = {a b : n m}
a/S → aSb | ɛ
b/S → abbS | ɛ
c/S → aSb | Sa | ɛ
d/S → aSb | Sa | a
e/S → S1 | S2
S1 → aS1b | S1b | b
S2 → aS2b | aS2 | a