Docsity

Homework 1 Problems for Programming Languages | CS 3723, Assignments of Programming Languages

Toggle sidebar
bg1
Homework 1
due Sep 10, 2008
1. (15pts) Suppose we have the following grammar.
E ::= E ‘+’ E |E ‘(’ E ‘)’ |var
where var is a terminal which can be mapped to an arbitrary variable name (e.g., x,y,z, etc.).
(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+cshould 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).
2. (10pts) Select and finish one of the following problems — you do not need to work on both
of them, as each is worth 10pts, and adding them together won’t exceed 10pts. Problem (2.a)
is easy to start with (to write an ambiguous grammar for the given language), but making the
grammar non-ambiguous is hard. Problem (2.b) is harder to start with than problem (2.a),
but is straightforward once you’ve identified the concepts. You can work on both problems if
you feel you can solve only part of each.
(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, NE I,N N EN E I,I EE IN N are all valid strings in the refined
language, but NI is not a valid string (it does not contain E), neither is N(which does
not contain Ior E). Give a parse tree for NN E NI 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 →
(child1child2... childn). For example, a→(b c d) defines the following tree structure
a
/|\
bcd
a→(b→(x y)c d) defines
a
/|\
bcd
/ \
x y
and a→(b→(x y)c→(m n →(k)) d→(e→(f))) defines
1
pf2

Partial preview of the text

Download Homework 1 Problems for Programming Languages | CS 3723 and more Assignments Programming Languages in PDF only on Docsity!

Homework 1

due Sep 10, 2008

  1. (15pts) Suppose we have the following grammar. E ::= E ‘+’ E | E ‘(’ E ‘)’ | var where var is a terminal which can be mapped to an arbitrary variable name (e.g., x, y,z, etc.).

(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).

  1. (10pts) Select and finish one of the following problems — you do not need to work on both of them, as each is worth 10pts, and adding them together won’t exceed 10pts. Problem (2.a) is easy to start with (to write an ambiguous grammar for the given language), but making the grammar non-ambiguous is hard. Problem (2.b) is harder to start with than problem (2.a), but is straightforward once you’ve identified the concepts. You can work on both problems if you feel you can solve only part of each.

(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).