{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# hw3 - x 1 else if b == 0 then x = 3 x 2 else x = 3 x-1 When...

This preview shows page 1. Sign up to view the full content.

VERSION: September 16, 2010 CS-441 Fall 2010 JWJ CS UK Homework Assignment # 3 —This material is only for use in CS441-Fall 2010 in the Department of Computer Science at the University of Kentucky.— —Please, do not distribute.— Due: solve by Thursday, Sep 23, 2010 Learning outcomes: This assignments relates to the material on CFG grammars and their properties. The assignment contributes to the following learning outcomes for this course: LO2 specify parsing elements for algorithmic languages LO4 design and implement a complete algorithmic language compiler hw 3.1: Consider the following two CF grammars: < Stmt > ::= if < Expr > then < Stmt > | if < Expr > then < Stmt > else < Stmt > | < other stmts > < Stmt > ::= if < Expr > then < Stmt > | if < Expr > then < IElse > else < Stmt > | < other stmts > < IElse > ::= if < Expr > then < IElse > else < IElse > | < other stmts > Table 1: Grammar G 1 (left) and G 2 (right) 3.1.1 (a) Consider the following statement: if ( a == 0) then x = 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: * x + 1 else if ( b == 0) then x = 3 * x + 2 else x = 3 * x-1. When E is written in place of Boolean expressions and S is written in place of statements, this can be written as if (E1) then S1 else if (E2) then S2 else S3. Show a parse tree for this statement using grammar G 1 . (b) Draw two diﬀerent parse trees for grammar G 1 and string if (E1) then if (E2) then S2 else S3. 3.1.2 Draw a parse tree for the same string if (E1) then if (E2) then S2 else S3 using grammar G 2 . 3.1.3 Answer the following questions: • Is the grammar G 1 ambiguous? Explain. • Which of the two trees generated with grammar G 1 correspond to the meaning of the statement in a typical programming language (such as C++)? • Is grammar G 2 ambiguous? How could you justify your answer? 1...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online