DV1465 / DV1505 / DV1511:

Compiler and Interpreter Technology

Wednesday, January 27th, 2016

LR Languages and Bison

Table of Contents

1. Overview

2. Notes on presentation

3. A note on the assignment

4. Examples

expr : expr '+' term | expr '-' term | term term : term '*' factor | term '/' factor | factor factor : '(' expr ')' | id
e.g. (x+y)/z-f
tuple : '(' list ')' list : item | list ',' item item : tuple | num
e.g. ((1,2),3)

5. Refactoring by example

6. Grammar Functions: overview

7. Grammar Functions: First

tuple := '(' list ')' list := item | list ',' item item := tuple | num

8. Rules Functions: First

tuplelistitem
Step 1{'('}\(\{ \}\){num}
Step 2{'('}{num}{num, '('}
Step 3{ '(' }{num, '('}{num, '('}

9. Rules Function : Follow

tuple := '(' list ')' list := item | list ',' item item := tuple | num

10. Rules Functions : Follow

tuplelistitem
Step 1{}{')'','}{}
Step 2{}{')'','}{')'','}
Step 3{')'','}{')'','}{')'','}

11. Important subsets of the CFGs

LL(1)
A grammar that can be parsed left-to-right, building a left-most-derivation, only by looking at the next terminal in the input string.
notll1 : 'f' 'o' 'o' 'd' | 'f' 'o' 'o' 'l'
ll1 : 'f' 'o' 'o' choice; choice : 'd' | 'l'

12. CFG Subsets: LL(1)

13. CFG Subsets: LR(1)

14. CFG Subsets: All the rest.

Break (15mins)





Intermission

15. Top-down parsing

tuple : '(' list ')' list : item list2 list2 : ',' list | /*empty* item : num | tuple
bool item() { if( input[pos]==NUM ) { pos++; // Consume return true; // Rule succeeded } if( input[pos]==POPEN ) // First(tuple) return tuple(); return false; }

16. Top-down: Recursive descent

17. Bottom-up parsing

18. Bottom-up parsing : Shift-reduce

19. Bottom-up parsing : Shift-reduce example

tuple : '(' list ')' (R1) list : item (R2) | list ',' item (R3) item : tuple (R4) | num (R5)
StackInputAction
$( ( num , num ) , num )Shift 3 times
$ ( ( num, num ) , num )Reduce (R5)
$ ( ( item, num ) , num )Reduce (R2)
$ ( ( list, num ) , num )Shift 2 times
$ ( ( list , num) , num )Reduce (R5)
$ ( ( list , item) , num )Reduce (R3)
$ ( ( list) , num )Shift
$ ( ( list ), num )Reduce (R1)
$ ( tuple, num )Reduce (R4)
$ ( item, num )Reduce (R2)
......Shift, Shift, (R3), Shift, (R1)
$ tuple$Accept (start symbol, end of input)

20. LR(0) parser example

%3 0 0 accept accept 0->accept tuple 1 1 0->1 ( 1->1 ( 2 2 1->2 num 3 3 1->3 item 4 4 1->4 list 5 5 1->5 tuple 6 6 4->6 ) 7 7 4->7 , 7->2 num 7->5 tuple

21. LR(0) parser trace

%3 0 0 accept accept 0->accept tuple 1 1 0->1 ( 1->1 ( 2 2 1->2 num 3 3 1->3 item 4 4 1->4 list 5 5 1->5 tuple 6 6 4->6 ) 7 7 4->7 , 7->2 num 7->5 tuple
StackInputAction
[0]"((num,num),num)"Shift 1
[0, 1(]"(num,num),num)"Shift 1
[0, 1(, 1(]"num,num),num"Shift 2
[0, 1(, 1(, 2num]",num),num)"Reduce item:=num ; Goto 3

22. LR(0) parser trace II

%3 0 0 accept accept 0->accept tuple 1 1 0->1 ( 1->1 ( 2 2 1->2 num 3 3 1->3 item 4 4 1->4 list 5 5 1->5 tuple 6 6 4->6 ) 7 7 4->7 , 7->2 num 7->5 tuple
StackInputAction
[0, 1(, 1(, 4list, 6)]",num)"Reduce tuple := '(' list ')' ; Goto 5
[0, 1(, 5tuple]",num)"Reduce item := tuple ; Goto 3
[0, 1(, 3item]",num)"Reduce list:=item ; Goto 4
[0, 1(, 4list]",num)"Shift 7

23. Ambiguity in grammars

24. Ambiguity: Trees of operators

%3 1 1 op1 + op1->1 op2 * op1->op2 2 2 op2->2 op3 + op2->op3 3 3 op3->3 4 4 op3->4

25. Ambiguity: Undefined parse.

%3 1 1 op1 + op1->1 op2 * op1->op2 2 2 op2->2 op3 + op2->op3 3 3 op3->3 4 4 op3->4
%3 1 1 op1 + op1->1 2 2 op1->2 op2 * op2->op1 op3 + op2->op3 3 3 op3->3 4 4 op3->4
%3 1 1 op1 + op1->1 2 2 op1->2 op2 * op2->op1 3 3 op2->3 op3 + op3->op2 4 4 op3->4
%3 1 1 op1 + op1->1 op3 + op1->op3 2 2 op2 * op2->2 3 3 op2->3 op3->op2 4 4 op3->4
%3 1 1 op1 + op1->1 op2 * op1->op2 2 2 op2->2 3 3 op2->3 op3 + op3->op1 4 4 op3->4

26. Ambiguity: Operator-nesting encodes precedence

llexpr : llterm | llterm '+' llexpr; llterm : num | num '*' llterm
lrexpr : lrterm | lrexpr '+' lrterm; lrterm : num | lrterm '*' num
%3 1 1 op1 + op1->1 op3 + op1->op3 2 2 op2 * op2->2 3 3 op2->3 op3->op2 4 4 op3->4
%3 1 1 op1 + op1->1 op2 * op1->op2 2 2 op2->2 3 3 op2->3 op3 + op3->op1 4 4 op3->4

27. Ambiguity: Dangling else

stmt : ... | IF expr stmt | IF expr stmt ELSE stmt...

28. Summary