You are on page 1of 2

CS 454 Assignment 1 Syntax

Due: RamCT Checkin 11:55PM Wednesday January 29, 2014 Hardcopy due: Classtime Thursday January 30, 2014
Turn in hardcopy of your solutions in class, and check in an electronic copy of your solutions via the RamCT dropbox. Electronic copy should be in either text (.txt), pdf, or MSWord format. Include the following honor pledge on your submitted assignment: I have not given, received, or used any unauthorized assistance. Type the pledge and your signature on your submission via RamCT. Add your real signature to the hard copy of the assignment that you submit in class. A total of 100 homework points is possible for this assignment. 1. (18 points) For each of the following inx expressions (assume the normal order of arithmetic evaluation used in Java and C): (a) a + b + c * d (b) a * b * (c + d) (c) a + b * c * d i Draw abstract syntax trees (evaluation trees) for each arithmetic expression. The evaluation trees should be structured so that they precisely match the normal order of evaluation used by Java and C. ii Rewrite the expressions in postx notation, again with the same order of evaluation used by Java and C. 2. (18 points) Consider the following grammar for arithmetic expressions: E ::= E + T | E - T | T T ::= T * F | T/F | F F ::= number | name | ( E ) where number represents any integer, and name represents a character string that starts with a letter and consists of letters without spaces or punctuation. Draw a (i) parse tree and (ii) abstract syntax tree (evaluation tree) for each of the following expressions. (a) 2 + 3 * 5 (b) (2 + 3) * 5 (c) 2 + (3 * 5) 3. (20 points) Write a context-free grammar describing the syntax of a language that consists of an even number of as followed by an odd number of bs. Thus, the grammar must allow aab, aaaab, and aaaaaabbbbb, but not abb, aabb, abbbb, or aaaaabbbbbb. For this problem zero is neither even nor odd. Do not be concerned with ambiguity or associativity.

4. (24 points) Expressions in a certain language can be described using the following grammar: E T F D (a) ::= ::= ::= ::= T F D a | | | | E T/F F * b | T | T + E | (E) | -T D c | ... | z

i. ii. iii. iv. v. vi.

List the non-terminal symbols in the grammar. List the terminal symbols in the grammar. Which operator(s) has the highest precedence? Which operator(s) has the lowest precedence? What is the associativity of +? What is the associativity of ? a+bcd

(b) Consider the expression:

i. Draw the parse tree. ii. Draw the evaluation tree based on your parse tree. iii. Show that the grammar is ambiguous. (c) Revise the grammar to make it non-ambiguous. 5. (20 points) Write a BNF grammar for a language of Boolean expressions. Your grammar should not be ambiguous. Use the following in your grammar: Alphabet (or vocabulary): true, false, and, or, not, (, ) Inx Operators: and, or and has higher precedence than or Unary Operator: not not has higher precedence than and Grouping operator: ( ) ( ) has higher precedence than not Associativity: left associative

You might also like