12.07.2015 Views

[PDF] Fun with Syntax - ezekiel

[PDF] Fun with Syntax - ezekiel

[PDF] Fun with Syntax - ezekiel

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Assignment #1, <strong>Fun</strong> <strong>with</strong> <strong>Syntax</strong>CS 355Due Tuesday Jan 25, 2011You may need more room than the space provided; use the back of each page or attach separate sheets.1. Say we wish use a caret ^ as an exponentiation operator which has a higher precedence than additionand multiplication and is right associative. Here is the initial ambiguous grammar:E → E + EE → E ∗ EE → E ^ EE → ( E )E → NUM(a) Draw two different parse trees for 4 ∗ 5^2^3.(b) Fix the grammar for expressions so that it is both unambiguous and reflects the proper order ofoperations. The expression 3+4∗5^2^3 should be evaluated as 3+(4∗(5^(2^3))). (Left recursionis okay).(c) Draw a parse tree for 4 ∗ 5^2^3 using your new grammar.1


2. Below is a grammar involving C’s ternary operator ?: which has lower precedence than addition.E → E ? E : EE → E + T→ TT → ( E )→ NUM(a) Draw two parse trees for NUM ? NUM ? NUM : NUM : NUM + NUM(b) Modify the grammar so that it is unambiguous; ?: is right associative and has lower precedencethan addition.(c) Draw a parse tree for NUM ? NUM ? NUM : NUM : NUM + NUM using your new grammar.2


3. C also has a comma operator that is left associative and has the lowest precedence of all operators.The grammar below uses commas for two different purposes: one as an operator (top production), andanother to separate arguments to a function call (second to last production).E → E , F→FF → F + T→TT → ( E )→NUM→ ID ( A )A → B→ɛB → B , F(a) Draw a parse tree for ID(NUM,NUM+NUM) (two parameter function call).→F(b) Draw a parse tree for ID((NUM,NUM+NUM)) (one parameter function call).(c) How does this grammar syntactically differentiate between the two uses of commas?3

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!