Chapter 3 - Part2
Chapter 3 - Part2
Chapter 3 - Part2
Part 2
Describing Syntax
and Semantics
Ambiguity in Grammars
A=B +C *A
Which operation is
A=B +C *A done first?
1-3
<assign> <id> = <expression>
<id> A | B| C
<expression> < expression > + <expression>
| < expression > * <expression>
| (<expression>) | <id>
<id> <id>
< expression > * <expression> < expression > + <expression>
C A B C
A=B+C*A
What gets evaluated first the * or +?
1-4
<assign> <id> = <expression>
<id> A | B| C
<expression> < expression > + <expression>
| < expression > * <expression>
| (<expression>) | <id>
<id> <id>
< expression > * <expression> < expression > + <expression>
C A B C
A=B+C*A
19 = 4 + 5 * 3 1-5
<assign> <id> = <expression>
<id> A | B| C
<expression> < expression > + <expression>
| < expression > * <expression>
| (<expression>) | <id>
A=3; B=4; C=5;
<id> <id>
< expression > * <expression> < expression > + <expression>
C A B C
A=B+C*A A=B+C*A
19 = 4 + 15 27 = 9 * 3 1-6
An ambiguous Expression Grammar
(Operator Precedence)
• The problem we just saw relates to Operator
Precedence.
• A grammar can be written to ensure that the
addition and multiplication operators are
consistently in a higher-to-lower ordering,
respectively in the parse tree.
• The correct ordering is specified by using
separate abstractions for the operands of the
operators that have different precedence.
• This requires additional non-terminals and rules
1-7
An ambiguous Expression Grammar
(Operator Precedence)
Ambiguous:
A <expression> + <term>
<id> <id> A
1-9
B C
An Unambiguous Expression Grammar
1-10
Parse trees and derivations
<id>
A = B + C * <id>
<id> A
A = B + C *A
B C
1-12
Rightmost derivation of A = B + C * A
<assign> <id> = <expression> <assign> => <id> = <expr>
<id> A | B| C
A = <expr>
<expression> <expression> + <term> | <term>
<term> <term> * <factor> | <factor> A = <expr> + <term>
<factor> (<expression>) | <id>
A = <expr> + <term>*<factor>
< assign >
A = <expr> + <term>*<id>
A = <expr> +<term>*A
<id> = <expression>
A = <expr> + <factor>*A
A <expression> + <term> A = <expr> + <id>*A
A = <expr> + C*A
< term >
< term > * < factor > A = <term> + C*A
< factor > < factor > <id> A = <factor> + C*A
<id>
A = <id> + C*A
<id> A
A = B + C *A
B C
1-13
Associativity of Operators
1-14
Left Associativity
• When a grammar rule has its LHS also appears at the beginning
of its RHS, the rule is said to be left recursive.
• Left recursion specify operator left associativity.
• Given 2 left associative operators with equal precedence the first
will be lower in the parse tree and hence will be evaluated first.
1-15
Right Associativity
• When a grammar rule has its LHS also appears at the end
of its RHS, the rule is said to be right recursive.
• Right recursion specify operator right associativity.
• Given 2 right associative operators with equal precedence
the second will be lower in the parse tree and hence will
be evaluated first.
A
A = 2, B = 3, C = 4 <id> ** <expression>
1-17
<assign> <id> = <expression> Tree is skewed
<id> A | B| C
< assign > left
<expression> < expression >**<id> |
<id>
<id> = <expression>
A = B ** C ** A
A <expression> ** <id>
A = 2, B = 3, C = 4
< expresion > ** <id>
A = (3**4)**2 = 6561 A
C
<id>
1-18
If Then Else: Ambiguous Grammar
1-19
<if_stmt> if <logic_expr> then <stmt> | if <logic_expr> then <stmt> else <stmt>
<stmt> <if_stmt>…
x = 1; y = -1;
if (x <0)
<if_stmt> if (y <0) {y = y-1;}
else y = 0;
<if_stmt>
if<logic_expr> then<stmt> else <stmt>
y = -1 <if_stmt>
if<logic_expr> then <stmt>
1-21
Unambiguous Grammar for the
if-then-else
<if_stmt> <stmt>
<stmt> <matched> | <unmatched>
< matched > if <logic_expr> then <matched> else
<matched> | non-if-statement
< unmatched > if <logic_expr> then < stmt >
1-22
<if_stmt> <stmt>
<stmt> <matched> | <unmatched>
< matched > if <logic_expr> then <matched> else
<stmt> <matched> | non-if-statement
< unmatched > if <logic_expr> then < stmt >
<unmatched>
x = 1; y = -1;
if x < 0 then <stmt> if (x <0)
if (y <0) {y = y-1;}
<matched>
else y = 0;
if y < 0 then <matched> else <macthed>
<matched>
x = 1; y = -1;
if x < 0 then <matched> else <macthed> if (x <0)
???? y=0 if (y <0) {y = y-1;}
1-25
BNF and EBNF
• BNF
<expr> <expr> + <term>
| <expr> - <term>
| <term>
<term> <term> * <factor>
| <term> / <factor>
| <factor>
• EBNF
<expr> <term> {(+ | -) <expr>}
<term> <factor> {(* | /) <term>}
1-26