08.07.2015 Views

[PDF] Syntax and Semantics

[PDF] Syntax and Semantics

[PDF] Syntax and Semantics

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5. Operator Precedence• Operators that appear lower in the parse tree are evaluated first, so we want to force our grammarsto push higher precedent operators lower down in the parse tree.• Example: increasing operator precedence(a) +, -(b) * /• Resulting grammar:6. Associativity ::= ::= ::= ::= ::= ’(’ + + ’)’ ::= num | id ::= ’+’ | ’-’ ::= ’*’ | ’/’• Are the parse trees for expressions with two or more adjacent occurrences of the same precedencecorrect?• Example3+4+5 = (3+4)+5 (left associative)3^4^5 = 3^(4^5) (right associative)• New grammar7. Dangling else ::= ::= ::= ::= ::= ’(’ ’)’ ::= num | id ::= ’+’ | ’-’ ::= ’*’ | ’/’• The following grammar is ambiguous ::= | whatever ::= if then else ::= if then • The unambiguous grammar. key observation: between a then <strong>and</strong> its matching else, there cannot be an if without an else.4

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

Saved successfully!

Ooh no, something went wrong!