Precedence and associativity
•	Precedence and grammars
•	Associativity and grammars
Precedence and grammars
•	Determines which operation gets done first if no parentheses
•	Operation done first is closer to leaves
–	Production using the operator should be "further" from start symbol
Example [Pratt, Fig. 3.3 vs. Fig 3.5]
•	Addition, subtraction in same production
•	<arith expr> -> <arith expr> + <term> | <arith expr> - <term> | <term>
•	<term> -> <term> / <primary>
–	Separate nonterminal for each level of precedence
–	Division production is "further" -- need to first use <arith expr> production 
Precedence Example
•	<expr> -> <expr> + <term> | <term>
•	<term> -> <term> * <factor> | <factor>
•	<factor> -> ( <expr> ) | <id>
•	<id> -> A | B | C

Precedence example
•	A + B  * (C + B)
–	Note how derivation “groups” B with (C + B)
–	Attribute grammars will show how this means (C + B) is evaluated first, then the multiplication, and finally A is added
Associativity
•	In above rules, for 2 additions in a row, which would be done first?
•	How should things change for a right-associative operator?
–	Look at relative position of <expr> and <term> on right side
–	Ex: assignment, exponentiation (when supported)