Docsity

Homework Solution: Language of Integer Expressions and Collections, Assignments of Programming Languages

Toggle sidebar
bg1
Homework 3 (20pts)
due before class Feb 11, 2009
1. Suppose we have a language that includes all the expressions over the set of terminals {N,
+, -, **}, where Nrepresents all integer numbers, and +, −, ** are operators that must be
supported by your grammar. In particular, suppose aand bare integer numbers, the language
includes all expressions composed through a+b(binary addition), −a(unary minus), and a**b
(binary exponentiation) operations. For example, 3 + 5 ** 7 and 7 + −2 ** 3 + 34 are both
expressions in the language, but 3 + +5 and 2 ** +2 are not. The precedence order of the
operators is + << −<<**; that is, ** has the highest precedence. The operator + is left
associative, and ** is right-associative ( 2 ** 3 ** 2 = 2 ** 9 rather than 8 ** 2).
(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
| / | \
NE**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
2. Give a context-free grammar (BNF) for a language that expresses collections of integers. The
terminals of your grammar include {,}, N, (, ), ∞, and −∞, where Nstands for all integer
numbers, ∞stands for positive infinity, and −∞ stands for negative infinity. Each sentence in
the language must start with {, end with }, and in-between {and }must include a sequence of
three-integer tuples in the form of (lb ub incr). Here each (lb ub incr ) specifies the collection
of integers obtained by starting from lb and each time incrementing the previous integer by
1
pf2

Partial preview of the text

Download Homework Solution: Language of Integer Expressions and Collections and more Assignments Programming Languages in PDF only on Docsity!

Homework 3 (20pts)

due before class Feb 11, 2009

  1. Suppose we have a language that includes all the expressions over the set of terminals {N, +, -, }, where N represents all integer numbers, and +, −, ** are operators that must be supported by your grammar. In particular, suppose a and b are integer numbers, the language includes all expressions composed through a + b(binary addition), −a (unary minus), and ab (binary exponentiation) operations. For example, 3 + 5 ** 7 and 7 + −2 ** 3 + 34 are both expressions in the language, but 3 + +5 and 2 ** +2 are not. The precedence order of the operators is + << − <<**; that is, ** has the highest precedence. The operator + is left associative, and ** is right-associative ( 2 ** 3 ** 2 = 2 ** 9 rather than 8 ** 2).

(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

  1. Give a context-free grammar (BNF) for a language that expresses collections of integers. The terminals of your grammar include {, }, N, (, ), ∞, and −∞, where N stands for all integer numbers, ∞ stands for positive infinity, and −∞ stands for negative infinity. Each sentence in the language must start with {, end with }, and in-between { and } must include a sequence of three-integer tuples in the form of (lb ub incr). Here each (lb ub incr) specifies the collection of integers obtained by starting from lb and each time incrementing the previous integer by

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