

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


(a) (5pts) Give a parse tree and an abstract syntax tree for expression x + y + z. (b) (5pts) Give a parse tree and an abstract syntax tree for expression x + f (g(y)). (c) (5pts) Rewrite the grammar to make it non-ambiguous. In particular, the ‘+’ operator is left-associative (that is, a + b + c should be interpreted as (a + b) + c, NOT a + (b + c)) and should have lower precedence than function call (i.e., a + b(c)) should be interpreted as a + (b(c)), NOT (a + b)(c).
(a) (10pts) First write a context-free grammar describing the set of all strings over three terminals: N, E, and I. Then, refine your grammar so that your language accepts only strings that contain at least one appearance of each terminal (repetitions of terminals are allowed). For example, N EI, N N EN EI, IEEIN N are all valid strings in the refined language, but N I is not a valid string (it does not contain E), neither is N (which does not contain I or E). Give a parse tree for N N EN I using your refined grammar (if you don’t know how to do the refined grammar, use the primitive one). Is your refined grammar ambiguous? Can you make it non-ambiguous? (2pts for non-ambiguity). (b) (10pts) Write a context-free grammar describing the syntax of a tree language. The terminals of your grammar includes var (which can be mapped to an arbitrary variable name, e.g., x, y,z, etc.), ‘→’, ‘(’ and ‘)’. Each tree structure defines a nested collec- tion of parent/children relations, each relation expressed using the format parent → (child 1 child 2 ... childn). For example, a → (b c d) defines the following tree structure a / |
b c d a → (b → (x y) c d) defines a / |
b c d /
x y and a → (b → (x y) c → (m n → (k)) d → (e → (f ))) defines
a / |
b c d /| / \
x y m n e | | k f
Give a parse tree for the last tree sentence — that is, your CFG should be able to generate a → (b → (x y) c → (m n → (k)) d → (e → (f ))) (these are the tokens at the leaves of your parse tree).