

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) Give a context-free grammar to describe the given language. Solution: E ::= E + E | - E | E ** E | N (b) Based on your grammar, give a parse tree and AST for the input 3 + 5 ∗ ∗7. Solution: parse tree: E / |
E + E | / |
N E ** E | | | 3 N N | | 5 7 AST:
/
3 ** /
5 7 (c) Is your grammar ambiguous? If yes, rewrite your grammar to be non-ambiguous. Non-ambiguous grammar: E ::= E + T | T T ::= - T | F F ::= N ** F | N
incr until reaching ub. For example, “{ (1 ∞ 2) }” specifies the set of all positive odd integers (1,3,5,...), and “{ (-3 3 1) (5 6 1) }” specifies the collection of integers -3,-2,-1,0,1,2,3,5,6. Examples of invalid specifications include “{ (1 3) }” (the tuple (1 3) does not include three integers) and “(1 5 3)” (the sentence does not start with { or end with }), both of which should be rejected by your grammar. Based on your grammar, give a parse tree and an abstract syntax tree for “{ (-3 3 1) (5 6 1) }”.
Solution:
grammar: set ::= { tuples } tuples ::= tuple | tuples tuple tuple ::= ( num num num ) num ::= N | infty | -infty
parse tree: set / |
{ tuples } /
tuples tuple | / / | \
tuple ( num num num ) / | \ | \
( num num num ) N N N | | | | | | N N N 5 6 1 | | | -3 3 1
AST: set | tuples /
tuple tuple / | \ / |
-3 3 1 5 6 1