COMP 4426421 Compiler Design 1 Click to edit

  • Slides: 25
Download presentation
COMP 442/6421 – Compiler Design 1 Click to edit Master title style COMPILER DESIGN

COMP 442/6421 – Compiler Design 1 Click to edit Master title style COMPILER DESIGN Syntactic analysis – part II First and follow sets Recursive descent and table-driven predictive parsing Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 2 Click to edit Master title style Generating FIRST

COMP 442/6421 – Compiler Design 2 Click to edit Master title style Generating FIRST and FOLLOW sets Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 3 Generating FIRST sets • If , where begins

COMP 442/6421 – Compiler Design 3 Generating FIRST sets • If , where begins with a terminal symbol x, then x FIRST( ). • Algorithmic definition: FIRST(A) = 1. if ( (A T) (A is ) ) then FIRST(A) {A} 2. if ( (A N) (A S 1 S 2…Sk R) | Si (N T) ) then 2. 1. FIRST(A) (FIRST(S 1) – { }) 2. 2. if i<k ( FIRST(S 1), …, FIRST(Si) ) then FIRST(A) FIRST(Si+1) 2. 3. if ( FIRST(S 1), …, FIRST(Sk) ) then FIRST(A) { } • Or, generate the lookahead tree for A Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 5 Example: lookahead trees 0 E TE’ FT’E’ 1

COMP 442/6421 – Compiler Design 5 Example: lookahead trees 0 E TE’ FT’E’ 1 E’ +TE’ ( FIRST(E) = {0, 1, (} E E T T F TE | +TE FT | FT ( E ) | 0 | 1 FIRST(E’) = { , +} 0 T FT’ 1 ( FIRST(T) = {0, 1, (} T’ FIRST(T’) = { , } 0 F 1 ( FIRST(F) = {0, 1, (} Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 6 Generating the FOLLOW sets • FOLLOW(A) is the

COMP 442/6421 – Compiler Design 6 Generating the FOLLOW sets • FOLLOW(A) is the set of terminals that can come right after an A in any sentential form derivable from the grammar of the language. • Algorithmic definition: FOLLOW( 1. if ( then 2. if ( then 3. if ( then Concordia University A | A N ) = A == S ) ( FOLLOW(A) {$}) B A R ) ( FOLLOW(A) (FIRST( ) - { }) ) (B A R) ( ) ) ( FOLLOW(A) FOLLOW(B) ) Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 7 Generating the FOLLOW sets: example E E T

COMP 442/6421 – Compiler Design 7 Generating the FOLLOW sets: example E E T T F TE | +TE FT | FT ( E ) | 0 | 1 1. (1) : 2. (2) : 3. (2) : 4. (2) : 5. (3) : 6. (3) : 7. (3) : 8. (3) : 9. (3) : 10. (3) : E TE’ E’ +TE’ T FT’ T’ *FT’ F (E) E TE’ E’ +TE’ T FT’ T’ *FT’ : : : : FST(E) FST(E’) FST(T’) FST(F) : : : { { { FLW(E) FLW(E’) FLW(T’) FLW(F) (A == S) ( FLW(A) ‘$’) (B A ) ( FLW(A) (FST( ) - { }) ) (B A ) ( FLW(A) (FST( ) - { }) ) ( (B A ) ( FST( ) ) ( FLW(A) ( (B A ) ( FST( ) ) ( FLW(A) FOLLOW( 1. if ( then 2. if ( then 3. if ( then Concordia University 0, 1, ( } , + } 0, 1, ( } , * } 0, 1, ( } : : : { { { $1, )4 } {$, )}6 } {+}2, {$, )}5, 7 } {+, $, )}9 } { }3, {+, $, )}8, 10 } FLW(B) FLW(B) ) ) ) ) : : : : FLW(E) FLW(T) FLW(F) FLW(E) FLW(T) FLW(E’) FLW(F) FLW(T’) : : : { { { $, ) } +, $, ) } , +, $, ) } {$} (FST(E’) (FST(T’) (FST( )) FLW(E) FLW(E’) FLW(T) FLW(T’) – – – { }) { }) A | A N ) = A == S ) ( FOLLOW(A) {$}) B A R ) ( FOLLOW(A) (FIRST( ) - { }) ) (B A R) ( ) ) ( FOLLOW(A) FOLLOW(B) ) Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 8 Click to edit Master title style Recursive descent

COMP 442/6421 – Compiler Design 8 Click to edit Master title style Recursive descent predictive parsing Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 9 Method • Build FIRST and FOLLOW sets •

COMP 442/6421 – Compiler Design 9 Method • Build FIRST and FOLLOW sets • For each non-terminal, we have a corresponding function • In each function, for each possible right-hand-side of the corresponding • • productions, we have a possible path to follow. The choice is made according to the FIRST set of the right hand sides. If one of the alternatives is of the form A , the path is followed according to the FOLLOW set of the left-hand-side of the production. If no valid path is found, the function returns false. If any of the paths is followed without error, the function returns true. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 10 Constructing the parser • Main parser function is:

COMP 442/6421 – Compiler Design 10 Constructing the parser • Main parser function is: parse(){ lookahead = next. Token() if (start. Symbol() match(“$”) ) return(true) else return(false)} • function to match tokens with the lookahead symbol (next token in input): match(token){ if (lookahead == token) lookahead = next. Token(); return(true) else lookahead = next. Token(); return(false)} Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 11 Constructing the parser • For each non-terminal in

COMP 442/6421 – Compiler Design 11 Constructing the parser • For each non-terminal in the grammar, we construct a parsing function. All parsing functions have the same form: LHS(){ if (lookahead FIRST(RHS 1) ) if (non-terminals() match(terminals) ) write(“LHS RHS 1”) ; return(true) else return(false) else if (lookahead FIRST(RHS 2) ) if (non-terminals() match(terminals) ) write(“LHS RHS 2”) ; return(true) else return(false) else if … else if (lookahead FOLLOW(LHS) ) write(“LHS ”) ; return(true) else return(false)} Concordia University // LHS RHS 1 | RHS 2 | … | // LHS RHS 1 // LHS RHS 2 // other right hand sides // only if LHS exists Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 12 Example E E T T F TE |

COMP 442/6421 – Compiler Design 12 Example E E T T F TE | +TE FT | FT ( E ) | 0 | 1 E(){ // E TE’ if (lookahead FIRST(TE’) ) if (T() E’() ) write(“E TE’ ”) ; return(true) else return(false)} Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 13 Example E E T T F TE |

COMP 442/6421 – Compiler Design 13 Example E E T T F TE | +TE FT | FT ( E ) | 0 | 1 E’(){ // E’ +TE’ | if (lookahead FIRST(+TE’) ) if ( match(‘+’) T() E’() ) write(“E’ +TE’ ”) ; return(true) else return(false) else if (lookahead FOLLOW(E’) ) write(“E’ ”) ; return(true) else return(false)} Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 14 Example E E T T F TE |

COMP 442/6421 – Compiler Design 14 Example E E T T F TE | +TE FT | FT ( E ) | 0 | 1 T(){ // T FT’ if (lookahead FIRST(FT’) ) if (F() T’() ) write(“T FT’ ”) ; return(true) else return(false)} Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 15 Example E E T T F TE |

COMP 442/6421 – Compiler Design 15 Example E E T T F TE | +TE FT | FT ( E ) | 0 | 1 T’(){ // T’ FT’ | if (lookahead FIRST( FT’) ) if ( match(‘ ’) F() T’() ) write(“T’ FT’ ”) ; return(true) else return(false) else if (lookahead FOLLOW(T’) ) write(“T’ ”) ; return(true) else return(false)} Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 16 Example E E T T F TE |

COMP 442/6421 – Compiler Design 16 Example E E T T F TE | +TE FT | FT ( E ) | 0 | 1 F(){ // F 0 | 1 | (E) if (lookahead FIRST(0) ) if ( match(‘ 0’) ) write(“F 0”) ; return(true) else return(false) else if (lookahead FIRST(1) ) if ( match(‘ 1’) ) write(“F 1”) ; return(true) else return(false) else if (lookahead FIRST((E)) ) if ( match(‘(’) E() match(‘)’) ) write(“F (E)”) ; return(true) else return(false)} Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 17 Click to edit Master title style Table-driven predictive

COMP 442/6421 – Compiler Design 17 Click to edit Master title style Table-driven predictive parsing Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 18 Method • Build FIRST and FOLLOW sets •

COMP 442/6421 – Compiler Design 18 Method • Build FIRST and FOLLOW sets • Build the parser table • Implement the parser algorithm Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 19 Building the parsing table • Algorithm: 1. p

COMP 442/6421 – Compiler Design 19 Building the parsing table • Algorithm: 1. p : ( (p R) (p : A ) ) do steps 2 and 3 2. t : ( (t T) (t FIRST( )) ) add A to TT[A, t] 3. if ( FIRST( ) ) t : ((t T) (t FOLLOW(A)) ) add A to TT[A, t] 4. e : ( (e TT) (e == ) ) add “error” to e • Note: TT is the parsing table, with one row for each non-terminal, and one column for each terminal. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 20 Building the parsing table E E T T

COMP 442/6421 – Compiler Design 20 Building the parsing table E E T T F FST(E) FST(E’) FST(T’) FST(F) r 1: r 2: r 3: r 4: r 5: r 6: r 7: r 8: r 9: : : : { { { 0, 1, ( } , + } 0, 1, ( } , * } 0, 1, ( } E E’ E’ T T’ T’ F F F Concordia University TE | +TE FT | FT ( E ) | 0 | 1 TE’ +TE’ FT’ *FT’ 0 1 (E) FLW(E’) FLW(T’) FLW(F) : : : : : { { { $, ) } +, $, ) } , +, $, ) } FIRST(TE’) = FIRST(+TE’)= FOLLOW(E’) = FIRST(FT’) = FIRST(*FT’)= FOLLOW(T’) = FIRST(0) = FIRST(1) = FIRST((E)) = 1. p : ( (p R) (p : A ) ) do steps 2 and 3 2. t : ( (t T) (t FIRST( )) ) add A to TT[A, t] 3. if ( FIRST( ) ) t : ( (t T) (t FOLLOW(A)) ) add A to TT[A, t] 4. e : ( (e TT) (e == ) ) add “error” to e {0, 1, (} : {+} : {$, )} : {0, 1, (} : {*} : {+, $, )} : {0} : {1} : {(} : TT[E, 0][E, 1][E, (] TT[E’, +] TT[E’, $][E’, )] TT[T, 0][T, 1][T, (] TT[T’, *] TT[T’, +][T’, $][T’, )] TT[F, 0] TT[F, 1] TT[F, (] Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 21 Building the parsing table r 1: r 2:

COMP 442/6421 – Compiler Design 21 Building the parsing table r 1: r 2: r 3: r 4: E E’ E’ T TE’ +TE’ FT’ TT 0 1 ( E r 1 r 1 E’ T r 4 Concordia University r 7 r 8 T’ T’ F F F *FT’ 0 1 (E) ) + * r 3 r 2 r 6 $ r 3 r 4 T’ F r 5: r 6: r 7: r 8: r 9: r 5 r 6 r 9 Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 22 Parsing algorithm parse(){ push($) push(S) a = next.

COMP 442/6421 – Compiler Design 22 Parsing algorithm parse(){ push($) push(S) a = next. Token() while ( top() $ ) do x = top() if ( x T ) if ( x == a ) pop() ; a = next. Token() else skip. Errors() ; error = true else if ( TT[x, a] ‘error’ ) pop() ; inverse. RHSMultiple. Push(TT[x, a]) else skip. Errors() ; error = true if ( ( a $ ) ( error == true ) ) return(false) else return(true)} *: skip. Errors() will be explained in lecture 5 Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 23 Table parsing example Stack Input Production Derivation 1

COMP 442/6421 – Compiler Design 23 Table parsing example Stack Input Production Derivation 1 $E (0+1)*0$ E 2 $E (0+1)*0$ r 1: E TE’ 3 $E’T (0+1)*0$ r 4: T FT’E’ 4 $E’T’F (0+1)*0$ r 9: F (E)T’E’ 5 $E’T’)E( (0+1)*0$ 6 $E’T’)E 0+1)*0$ r 1: E TE’ (TE’)T’E’ 7 $E’T’)E’T 0+1)*0$ r 4: T FT’ (FT’E’)T’E’ 8 $E’T’)E’T’F 0+1)*0$ r 7: F 0 (0 T’E’)T’E’ 9 $E’T’)E’T’ 0 0+1)*0$ 10 $E’T’)E’T’ +1)*0$ r 6: T’ (0 E’)T’E’ 11 $E’T’)E’ +1)*0$ r 2: E’ +TE’ (0+TE’)T’E’ 12 $E’T’)E’T+ +1)*0$ Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 24 Table parsing example Stack Input Production Derivation 13

COMP 442/6421 – Compiler Design 24 Table parsing example Stack Input Production Derivation 13 $E’T’)E’T 1)*0$ r 4: T FT’ (0+FT’E’)T’E’ 14 $E’T’)E’T’F 1)*0$ r 8: F 1 (0+1 T’E’)T’E’ 15 $E’T’)E’T’ 1 1)*0$ 16 $E’T’)E’T’ )*0$ r 6: T’ (0+1 E’)T’E’ 17 $E’T’)E’ )*0$ r 3: E’ (0+1)T’E’ 18 $E’T’) )*0$ 19 $E’T’ *0$ r 5: T’ *FT’ 20 $E’T’F* *0$ 21 $E’T’F 0$ r 7: F 0 22 $E’T’ 0 0$ 23 $E’T’ $ r 6: T’ (0+1)*0 E’ 24 $E’ $ r 3: E’ (0+1)*0 25 $ $ success Concordia University Department of Computer Science and Software Engineering (0+1)*FT’E’ (0+1)*0 T’E’ Joey Paquet, 2000 -2020

COMP 442/6421 – Compiler Design 25 References • C. N. Fischer, R. K. Cytron,

COMP 442/6421 – Compiler Design 25 References • C. N. Fischer, R. K. Cytron, R. J. Le. Blanc Jr. , Crafting a Compiler, Adison-Wesley, 2010 – Chapter 4 and 5. • A. V. Aho, R. Sethi and J. D. Ullman. Compilers, Principles, Techniques, and Tools, Addison-Wesley, 1986 – Chapter 4. • T. W. Parsons. Introduction to Compiler Construction, W. H. Freeman and Company, 1992 – Chapter 3 and 4. • K. C. Louden. Compiler Construction: Principles and Practice, International Thomson Publishing Inc. , 1997 – Chapter 3 and 4. Concordia University Department of Computer Science and Software Engineering Joey Paquet, 2000 -2020