Download Shift-reduce parsing and LR parsing and more Lecture notes Compiler Design in PDF only on Docsity!
CS 4120
Introduction to Compilers
Andrew Myers
Cornell University
Lecture 7: LR parsing and parser generators
11 Sep 09
2
Shift-reduce parsing
(1+2+(3+4))+5! (1+2+(3+4))+5 shift (1+2+(3+4))+5! ( 1+2+(3+4))+5 shift (1+2+(3+4))+5! (1 +2+(3+4))+5 reduce E "num ( E +2+(3+4))+5! (E +2+(3+4))+5 reduce S " E ( S +2+(3+4))+5! (S +2+(3+4))+5 shift ( S +2+(3+4))+5! (S+ 2+(3+4))+5 shift ( S +2+(3+4))+5! (S+2 +(3+4))+5 reduce E "num ( S + E +(3+4))+5! (S+E +(3+4))+5 reduce S " S + E ( S +(3+4))+5! (S +(3+4))+5 shift ( S +(3+4))+5! (S+ (3+4))+5 shift ( S +(3+4))+5! (S+( 3+4))+5 shift ( S +(3+4))+5! (S+(3 +4))+5 reduce E "num S " S + E | E
E " number | ( S )
derivation stack input stream action 3
LR(0) states
- (^) A state is a set of items keeping track of progress on
possible upcoming reductions
- (^) An LR(0) item is a production from the language
with a separator “. ” somewhere in the RHS of the
production
- (^) Stuff before “. ” is already on stack (beginnings of
possible #’s to be reduced)
- (^) Stuff after “. ” : what we might see next
- The prefixes^ $^ represented by state itself
E " num.
E " (. S )
state item
• As much power as possible out of parsing
table with k look-ahead symbols
• LR(1) grammar = recognizable by a shift/
reduce parser with 1 look-ahead.
• LR(1) item = LR(0) item + look-ahead
symbols possibly following production
LR(0): S ". S + E
LR(1): S ". S + E +
CS 4120 Introduction to Compilers 4
LR(k) parsing
Remaining input will reduce to S + E + ...
CS 4120 Introduction to Compilers 5
LR(1) state
• LR(1) state = set of LR(1) items
• LR(1) item = LR(0) item + set of look-
ahead symbols
• No two items in state have same
production + dot configuration
S " S. + E +
S " S. + E $
S " S +. E num
S " S. + E +, $
S " S +. E num
Consider closure of item
Closure formed just as for LR(0) except
- Lookahead symbols include characters following the non-terminal symbol to the right of dot: FIRST(%)
- If non-terminal symbol may produce last symbol of production (% is nullable), lookahead symbols include lookahead symbols of production (&) 2
CS 4120 Introduction to Compilers 6
LR(1) closure
S " E + S | E
E " num | ( S )
S !". S $
S ". E + S $
S ". E $
E ". num +,$ E ". ( S ) +,$
A " '. C % &
LR(1) construction
S " E + S | E
E " num | ( S ) S’ ". S $ S ". E + S $ S ". E $ E ". num +,$ E ". ( S ) +,$
S " E. + S $
S " E. $
E
+ $ E
2 s3 S " E
Know what to do if:
- reduce look-aheads distinct
- not to right of any dot
LALR grammars
• Problem with LR(1): too many states
• LALR(1) (Look-Ahead LR)
– Merge any two LR(1) states whose items are
identical except for look-ahead
– Results in smaller parser tables—works
extremely well in practice
– The usual technology for automatic parser
generators S " id. +
S " E. $
S " id. $ S " E. +
CS 4120 Introduction to Compilers 13
Grammar in CUP
non terminal E; terminal PLUS, LPAREN...
precedence left PLUS;
E ::= E PLUS E
| LPAREN E RPAREN
| NUMBER ;
“When shifting + conflicts with reducing a production containing +, choose reduce” CS 4120 Introduction to Compilers 14
Precedence
• Also can handle operator precedence
E " E + E | T
T " T ) T | num | ( E )
E " E + E | E ) E
| num | ( E )
Conflicts w/o precedence
E " E + E | E ) E
| num | ( E )
E " E. + E …
E " E ) E. +
E " E + E. )
E " E. ) E …
Predecence in CUP
E " E. + E …
E " E ) E. +
E " E + E. )
E " E. ) E …
precedence left PLUS;
precedence left TIMES; // TIMES > PLUS
E ::= E PLUS E | E TIMES E | ...
Rule: in conflict, choose reduce if production symbol higher precedence than shifted symbol; choose shift if vice-versa
CS 4120 Introduction to Compilers 17
Summary
• Look-ahead information makes SLR(1),
LALR(1), LR(1) grammars expressive
• Automatic parser generators support
LALR(1)
• Precedence, associativity declarations
simplify grammar writing
• Easiest and best way to read structured
human-readable input
CS 4120 Introduction to Compilers 18
Compiler ‘main program’
class Compiler {
void compile() throws CompileError {
Lexer l = new Lexer(input);
Parser p = new Parser(l);
AST tree = p.parse();
// calls l.getToken() to read tokens
if (typeCheck(tree))
IR = genIntermediateCode(tree);
IR.emitCode();
Thread of Control
Compiler.compile
Parser.parse
Lexer.getToken
InputStream.read
easier to make re-entrant
AST
bytes/chars tokens
Semantic Analysis
Source code
lexical analysis
parsing
semantic analysis
tokens abstract syntax tree valid programs: decorated AST semantic errors lexical errors syntax errors
CS 4120 Introduction to Compilers 25
Building the AST bottom-up
- Semantic actions are attached to grammar statements
- E.g. CUP: Java statement attached to each production non terminal Expr expr; ... expr ::= expr:e1 PLUS expr:e {: RESULT = new Add(e1,e2); :}
- Semantic action executed when parser reduces a
production
- (^) Variable RESULT is value of non-terminal symbol
being reduced (in yacc: $$)
- AST is built bottom-up along with parsing grammar production semantic action CS 4120 Introduction to Compilers 26
Actions in S-R parser
non terminal Expr expr; ... expr ::= expr:e1 PLUS expr:e {: RESULT = new Add(e1,e2); :}
- Parser stack stores value of each non-terminal (1 + 2) + 3 (1 +2)+ (E +2)+3 RESULT=new Num(1) (E+2 )+ (E+E )+3 RESULT=new Num(2) (E )+3 RESULT=new Add(e1,e2) (E) + E +3 RESULT=e
E " num | ( E ) | E + E
Num(1) Add( , ) Num(2)
How not to design an AST
• Introduce a tree node for every node in parse
tree
– not very abstract
– creates a lot of useless nodes to be dealt with
later
S " E R
R " * | + E R
E " num | ( S ) (1 + 2) + 3
S
E R
( S )
E R
+ E R
+ E R
Add 1 2 Add 3 Sum Expr Add ParenExpr 1 Add 2 3 EmptyR EmptyR
How not to design the AST, part II
- Simple(minded) approach: have one class
AST_node
- E.g. need information for if, while, +, *, ID, NUM class AST_node { int node_type; AST_node[ ] children; String name; int value; …etc… }
- Problem: must have fields for every different kind of
node with attributes
- Not extensible, Java type checking no help
CS 4120 Introduction to Compilers 29
Using class hierarchy
• Can use subclassing to solve problem
- write abstract class for each “interesting” non-
terminal in grammar
- write non-abstract subclass for (almost) every
prod’n
E " E + E | E * E | - E | ( E )
abstract class Expr { … } // E class Add extends Expr { Expr left, right; … } class Mult extends Expr { Expr left, right; … } // or: class BinExpr extends Expr { Oper o; Expr l, r; } class Negate extends Expr { Expr e; …} CS 4120 Introduction to Compilers 30
Creating the AST
non terminal Expr expr; …
expr ::= expr:e1 PLUS expr:e
{: RESULT = new BinaryExpr(plus, e1, e2); :}
| expr:e1 TIMES expr:e
{: RESULT = new BinaryExpr(times, e1, e2); :}
| MINUS expr:e
{: RESULT = new UnaryExpr(negate, e); :}
| LPAREN expr:e RPAREN
{: RESULT = e; :}
Expr plus, times, negate: Oper^ BinaryExpr^ UnaryExpr “RESULT has type Expr in all semantic actions for expr”
Another Example
expr ::= num | ( expr ) | expr + expr | id
stmt ::= expr ; | if ( expr ) stmt |
if (expr) stmt else stmt | id = expr ; | ;
abstract class Expr { … } class Num extends Expr { Num(int value) … } class Add extends Expr { Add(Expr e1, Expr e2) … } class Id extends Expr { Id(String name) … } abstract class Stmt { … } class If extends Stmt { If(Expr cond, Stmt s1, Stmt s2) } class EmptyStmt extends Stmt { EmptyStmt() … } class Assign extends Stmt { Assign(String id, Expr e)…}
And…top-down
- parse_ X method for each non-terminal X
- (^) Return type is abstract class for X Stmt parseStmt() { switch (next_token) { case IF: eat(IF); eat(LPAREN); Expr e = parseExpr; eat(RPAREN); Stmt s2, s1 = parseStmt(); if (next_token == ELSE) { eat(ELSE); s2 = parseStmt(); } else s2 = new EmptyStmt(); return new IfStmt(e, s1, s2); } case ID: …