Presentation is loading. Please wait.

Presentation is loading. Please wait.

241-437 Compilers: topDown/5 1 Compiler Structures Objective – –look at top-down (LL) parsing using recursive descent and tables – –consider a recursive.

Similar presentations


Presentation on theme: "241-437 Compilers: topDown/5 1 Compiler Structures Objective – –look at top-down (LL) parsing using recursive descent and tables – –consider a recursive."— Presentation transcript:

1 241-437 Compilers: topDown/5 1 Compiler Structures Objective – –look at top-down (LL) parsing using recursive descent and tables – –consider a recursive descent parser for the Expressions language 241-437, Semester 1, 2011-2012 5. Top-down Parsing

2 241-437 Compilers: topDown/5 2 Overview 1. Parsing with a Syntax Analyzer 2. Creating a Recursive Descent Parser 3. The Expressions Language Parser 4. LL(1) Parse Tables 5. Making a Grammar LL(1) 6.Error Recovery in LL Parsing

3 241-437 Compilers: topDown/5 3 In this lecture Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator Back End Int. Code Generator Intermediate Code but concentrating on top-down parsing

4 241-437 Compilers: topDown/5 4 1. Parsing with a Syntax Analyzer Lexical Analyzer (using chars) Syntax Analyzer (using tokens) Source Program 3. Token, token value 1. Get next token lexical errors syntax errors 2. Get chars to make a token parse tree

5 241-437 Compilers: topDown/5 5 1.1. Top Down (LL) Parsing begin simplestmt ; simplestmt ; end S S SS B 1 2 3 4 5  6 B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end

6 241-437 Compilers: topDown/5 6 1.2. LL Parsing Definition An LL parser is a top-down parser for a context-free grammar. It parses input from Left to right, and constructs a Leftmost derivation of the input.

7 241-437 Compilers: topDown/5 7 A Leftmost Derivation In a leftmost derivation, the leftmost non- terminal is chosen to be expanded. – –this builds the parse tree top-down, left-to-right Example grammar: L => ( L ) L L => 

8 241-437 Compilers: topDown/5 8 Leftmost Derivation for (())() L // L => ( L ) L   ( L ) L // L => ( L ) L  ( ( L ) L ) L // L =>   ( ( ) L ) L // L =>   ( ( ) ) L // L =>   ( ( ) ) ( L ) L // L =>( L ) L   ( ( ) ) ( ) L // L =>    ( ( ) ) ( ) (())() input

9 241-437 Compilers: topDown/5 9 1.3. LL(1) and LL(k) An LL(1) parser uses the current token only to decide which production to use next. An LL(k) parser uses k tokens of input to decide which production to use – –this make the grammar easier to write – –adds no 'power' compared to LL(1) – –harder to implement efficiently

10 241-437 Compilers: topDown/5 10 1.4. Two LL Implementation Approaches Recursive Descent parsing – –all the compiler code is generated (automatically) from the grammar Table Driven parsing – –a table is generated (automatically) from the grammar – –the table is 'plugged' into an existing compiler

11 241-437 Compilers: topDown/5 11 2. Creating a Recursive Descent Parser Each non-terminal (e.g. A) is translated into a parsing function (e.g. A()). The A() function is generated from all the productions for A: – –A => B, A => a C, etc.

12 241-437 Compilers: topDown/5 12 2.1. Basic Translation Rules I'll start by assuming a production body doesn't use *, [], or . – –I'll add to the translation rules later to deal with these extra features S => Body becomes void S() { translate }

13 241-437 Compilers: topDown/5 13 If Body is B1 B2... Bn then it becomes: translate ; translate ; : translate ;

14 241-437 Compilers: topDown/5 14 If Body is B1 | B2... | Bn then it becomes: if (currToken in FIRST_SEQ ) translate ; else if (currToken in FIRST_SEQ ) translate ; : else if (currToken in FIRST_SEQ ) translate ; else error();

15 241-437 Compilers: topDown/5 15 currToken is the current token, which is obtained from the lexical analyzer: Token currToken; // global void nextToken(void) { currToken = scanner(); }

16 241-437 Compilers: topDown/5 16 The first token is read when the parser first starts. main() also calls the function representing the start symbol: int main(void) { nextToken(); S(); // S is the grammar's start symbol : // other code return 0; }

17 241-437 Compilers: topDown/5 17 error() reports that the current token cannot be matched against any production: int lineNum; // global void error() { printf("\nSyntax error at \'%s\' on line %d\n", currentToken, lineNum); exit(1); }

18 241-437 Compilers: topDown/5 18 In a body, if B is a non-terminal, it is translated into the function call: B(); In a body, if b is a terminal, it is translated into a match() call: match(b);

19 241-437 Compilers: topDown/5 19 match() checks that the current token is what is expected (e.g. b), and reads in the next one for future testing: void match(Token expected) { if(currToken == expected) currToken = scanner(); else error(); }

20 241-437 Compilers: topDown/5 20 Special '|' Body case. If Body is a1 B1 | a2 B2... | an Bn // ai's are terminals then it becomes: if (currToken == a1) { match(a1); translate ; } else if (currToken == a2) { match(a2); translate ; } : else if (currToken == an) { match(an); translate ; } else error(); a1, a2,..., an must be different

21 241-437 Compilers: topDown/5 21 void S() {// S => a B | b C if (currToken == a) { match(a); B(); } else if (currToken == b) { match(b); C(); } else error(); } void B() {// B => b b C match(b); C(); } void C() {// C => c c match(c); } 2.2. Example Translation And main(), nextToken(), match(), and error().

22 241-437 Compilers: topDown/5 22 Parsing "abbcc" S a B b b C c Function calls: main() --> S() --> match(a); B() --> match(b); match(b); C() --> match(c); match(c) abbcc input

23 241-437 Compilers: topDown/5 23 2.3. When can we use Recursive Descent? A fast/efficient recursive descent parser can be generated for a LL(1) grammar. So we must first check if the grammar is LL(1). – –the check will generate information that can be used in constructing the parser – –e.g. FIRST_SEQ

24 241-437 Compilers: topDown/5 24 Dealing with "if" A tricky part of LL(1) is making sure that branches can be coded A tricky part of LL(1) is making sure that branches can be coded –each branch must start differently so it's easy (and also fast) to decide which branch to use based only on the current input token (currToken value) continued

25 241-437 Compilers: topDown/5 25 e.g. e.g. –A --> a B1 A --> b B2 –is okay since the two branches start differently (a and b) –A --> a B1 A --> a B2 –not okay since both branches start the same way a.. currToken continued

26 241-437 Compilers: topDown/5 26 In non-mathematical words, a grammar is LL(1) if the choice between productions can be made by looking only at the start of the production bodies and the current input token (currToken). In non-mathematical words, a grammar is LL(1) if the choice between productions can be made by looking only at the start of the production bodies and the current input token (currToken).

27 241-437 Compilers: topDown/5 27 Is a Grammar LL(1)? For every non-terminal in the language (e.g. A, B, C), generate the PREDICT set for all the productions: PREDICT( A =>  1)PREDICT( A =>  2 ) PREDICT( A =>  3 ) PREDICT( B =>  1 )PREDICT( B =>  2 ) PREDICT( C =>  1 )... in maths continued

28 241-437 Compilers: topDown/5 28 Take the intersection of all pairs of sets for A: Take the intersection of all pairs of sets for A: PREDICT( A =>  1) ∩ PREDICT( A =>  2 ) ∩ PREDICT( A =>  1) ∩ PREDICT( A =>  2 ) ∩ PREDICT( A =>  1) ∩ PREDICT( A =>  3 ) ∩ PREDICT( A =>  2) ∩ PREDICT( A =>  3 ) ∩ –the intersection of every pair must be empty (disjoint) continued

29 241-437 Compilers: topDown/5 29 Repeat for all the sets for B, C, etc.: Repeat for all the sets for B, C, etc.: –B -->  1B -->  2 –C -->  1C -->  2C -->  3 If every PREDICT intersection pair is disjoint then the grammar is LL(1). If every PREDICT intersection pair is disjoint then the grammar is LL(1). continued

30 241-437 Compilers: topDown/5 30 If there's only one PREDICT set for a non- terminal (e.g. D --> d1), then it's automatically disjoint.

31 241-437 Compilers: topDown/5 31 Calculating PREDICT PREDICT(A =>  ) = (FIRST_SEQ(  ) – {   FOLLOW(A) if  in FIRST_SEQ(  ) or = FIRST_SEQ(  )if  not in FIRST_SEQ(  ) FIRST_SEQ() and FOLLOW() are the set functions I described in chapter 4.

32 241-437 Compilers: topDown/5 32 Short Example 1 S => a S | a ProductionPredict – –S => a S {a} – –S => a {a} PREDICT(S) = {a} ∩ {a } == {a} – –not disjoint – –the grammar is not LL(1)

33 241-437 Compilers: topDown/5 33 Short Example 2 S => a S | b ProductionPredict – –S => a S {a} – –S => b {b} PREDICT(S) = {a} ∩ {b } == {} – –disjoint – –the grammar is LL(1)

34 241-437 Compilers: topDown/5 34 Larger Example Is this grammar LL(1)? E => T E1 E1 => + T E1 |  T => F T1 T1 => * F T1 |  F => id | '(' E ')' FIRST(F) = {(,id} FIRST(T) = {(,id} FIRST(E) = {(,id} FIRST(T1) = {*,  } FIRST(E1) = {+,  } FOLLOW(E) = {$,)} FOLLOW(E1) = {$,)} FOLLOW(T) = {+$,)} FOLLOW(T1) = {+,$,)} FOLLOW(F) = {*,+,$,)}

35 241-437 Compilers: topDown/5 35 ProductionPredict E => T E1 = FIRST(T) = {(,id} E1 => + T E1 {+} E1 =>  = FOLLOW(E1) = {$,)} T => F T1 = FIRST(F) = {(,id} T1 => * F T1 {*} T1 =>  = FOLLOW(T1) = {+,$,)} F => id {id} F => ( E ) {(} FIRST(F) = {(,id} FIRST(T) = {(,id} FIRST(E) = {(,id} FIRST(T1) = {*,  } FIRST(E1) = {+,  } FOLLOW(E) = {$,)} FOLLOW(E1) = {$,)} FOLLOW(T) = {+$,)} FOLLOW(T1) = {+,$,)} FOLLOW(F) = {*,+,$,)}

36 241-437 Compilers: topDown/5 36 Are the PREDICT sets disjoint for all the non-terminals? – –PREDICT(E): {(,id} yes – –PREDICT(E1): {+} ∩ {$,)}yes – –PREDICT(T): {(,id}yes – –PREDICT(T1): {*} ∩ {+,$,)}yes – –PREDICT(F): {id} ∩ {(}yes All disjoint, so the grammar is LL(1).

37 241-437 Compilers: topDown/5 37 2.4. Extended Translation Rules These extra rules allow a production body to use *, [], or . S => Body becomes void S() { translate } same as before

38 241-437 Compilers: topDown/5 38 If Body is B1 | B2... | Bn |  then it becomes: if (currToken in FIRST_SEQ(B1)) translate ; else if (currToken in FIRST_SEQ(B2)) translate ; : else if (currToken in FIRST_SEQ(Bn)) translate ; else error(); optional  part include if there's no  part in the grammar

39 241-437 Compilers: topDown/5 39 If Body is [ B1 B2... Bn ] then it becomes: if (currToken in FIRST_SEQ(B1)) { translate ; translate ; : translate ; } – –[ B1 B2... Bn ] is the same as ( B1 B2... Bn ) |  rule []-1

40 241-437 Compilers: topDown/5 40 A variant [] translation. If the body is [ B1 B2... Bn ] C then it can become: if (currToken not in FIRST_SEQ(C)) translate ; translate ; : translate ; } translate ; rule []-2 This may be simpler code than FIRST_SEQ(B1)

41 241-437 Compilers: topDown/5 41 Another variant [] translation. If the grammar rule is A => [ B1 B2... Bn ] then it becomes: void A() { if (currToken not in FOLLOW(A)) translate ; translate ; : translate ; } } rule []-3 This may be simpler code than FIRST_SEQ(B1)

42 241-437 Compilers: topDown/5 42 If Body is ( B1 B2... Bn )* then it becomes: while (currToken in FIRST_SEQ(B1)) translate ; translate ; : translate ; } rule *-1

43 241-437 Compilers: topDown/5 43 A variant * translation. If the body is ( B1 B2... Bn )* C then it becomes: while (currToken not in FIRST_SEQ(C)) translate ; translate ; : translate ; } translate ; rule *-2 This may be simpler code than FIRST_SEQ(B1)

44 241-437 Compilers: topDown/5 44 Another variant * translation. If the grammar rule is A => ( B1 B2... Bn )* then it becomes: void A() { while (currToken not in FOLLOW(A)) translate ; translate ; : translate ; } } rule *-3 This may be simpler code than FIRST_SEQ(B1)

45 241-437 Compilers: topDown/5 45 match() is slightly changed to deal with the end of input symbol, $: void match(Token expected) { if(currToken == expected) { if (currToken != $) currToken = scanner(); } else error(); }

46 241-437 Compilers: topDown/5 46 Translation Example 1 The LL(1) Grammar: E => T E1 E1 => [ '+' T E1 ] T => F T1 T1 => [ '*' F T1 ] F => id | '(' E ')' This is the same grammar as on slides 34-36, so we know it's LL(1).

47 241-437 Compilers: topDown/5 47 Generated Parser void E()// E => T E1 { T(); E1(); } void E1()// E1 => ['+' T E1 ] { if (currToken == '+') { match('+'); T(); E1(); } } use rule []-1 This is C code for "currToken in FIRST_SEQ(+)"

48 241-437 Compilers: topDown/5 48 void T()// T => F T1 { F(); T1(); } void T1()// T1 => ['*' F T1 ] { if (currToken == '*') { match('*'); F(); T1(); } } rule []-1 This is C code for "currToken in FIRST_SEQ(*)"

49 241-437 Compilers: topDown/5 49 void F()// F => id | '(' E ')' { if (currToken == ID) match(ID); else if (currToken == '(') { match('('); E(); match(')'): } else error(); }

50 241-437 Compilers: topDown/5 50 Parsing "a + b * c" E T E1 F T1 + T E1 id a * F T1id b  F T1  id c  a+b*c input

51 241-437 Compilers: topDown/5 51 Optimizations It's possible to combine grammar rules and/or parse functions, in order to simplify the compiler. For example, we can combine: – –E and E1 – –T and T1

52 241-437 Compilers: topDown/5 52 Translation Example 2 The previous LL(1) grammar can be expressed using *: E => T ( '+' T )* T => F ( '*' F )* F => id | '(' E ')' same as before

53 241-437 Compilers: topDown/5 53 Generated Parser void E()// E => T ('+' T)* { T(); while (currToken == '+') { match('+'); T(); } } void T()// T => F ('*' F)* { F(); while (currToken == '*') { match('*'); F(); } } rule *-1

54 241-437 Compilers: topDown/5 54 void F()// F => id | '(' E ')' { if (currToken == ID) match(ID); else if (currToken == '(') { match('('); E(); match(')'): } else error(); } same as before

55 241-437 Compilers: topDown/5 55 Parsing "a + b * c" Again E T F + T id a * F id b F id c done inside the E() loop done inside the T() loop

56 241-437 Compilers: topDown/5 56 3. The Expressions Language Parser Is this grammar LL(1)? Stats => ( [ Stat ] \n )* Stat => let ID = Expr | Expr Expr => Term ( (+ | - ) Term )* Term => Fact ( (* | / ) Fact ) * Fact => '(' Expr ')' | Int | ID

57 241-437 Compilers: topDown/5 57 3.1. FIRST and FOLLOW Sets First(Stats) = {let, (, Int, Id, \n,  } First(Stat) = {let, (, Int, Id} First(Expr) = {(, Int, Id} First(Term) = {(, Int, Id} First(Fact) = {(, Int, Id} Follow(Stats) = {$} Follow(Stats) = {$} Follow(Stat) = {\n} Follow(Stat) = {\n} Follow(Expr) = {\n} Follow(Expr) = {\n} Follow(Term) = {+, -, \n} Follow(Term) = {+, -, \n} Follow(Fact) = {*, /, +,-,\n} Follow(Fact) = {*, /, +,-,\n}

58 241-437 Compilers: topDown/5 58 3.2. PREDICT Sets ProductionPredict Disjoint Stats => ( [ Stat ] \n )*{let,(,Int,Id,\n,$}Yes Stat => let ID = Expr{let}Yes Stat => Expr{(,Int,Id} Expr => Term ( (+ | - ) Term )*{(,Int,Id}Yes Term => Fact ( (* | / ) Fact ) *{(,Int,Id}Yes Fact => '(' Expr ')'{(}Yes Fact => Int{Int} Fact => Id{Id}

59 241-437 Compilers: topDown/5 59 3.3. exprParse0.c exprParse0.c is a recursive descent parser generated from the expressions grammar. It reads in an expressions program file. It's output is a print-out of parse function calls.

60 241-437 Compilers: topDown/5 60 An Expressions Program (test1.txt) 5 + 6 let x = 2 3 + ( (x*y)/2) // comments // y let x = 5 let y = x /0 // comments

61 241-437 Compilers: topDown/5 61 Usage > gcc -Wall -o exprParse0 exprParse0.c >./exprParse0 < test1.txt 1: stats< 2: stat >'+' term >>> 3: stat >>> 4: stat >'+' term 5: 6: stat >>> 7: stat '/' fact >>> 8: 9: 10: >'eof'

62 241-437 Compilers: topDown/5 62 exprParse0.c Callgraph lexical parser (like exprTokens.c) generated from the grammar

63 241-437 Compilers: topDown/5 63 Standard Token Functions // globals (first used in exprToken.c) Token currToken; char tokString[MAX_IDLEN]; int tokStrLen = 0; int currTokValue; int lineNum = 1; // no. of lines read in void nextToken(void) { currToken = scanner(); } continued

64 241-437 Compilers: topDown/5 64 void match(Token expected) { if(currToken == expected){ printToken(); // produces the parser's output if(currToken != SCANEOF) currToken = scanner(); } else printf("Expected %s, found %s on line %d\n", tokSyms[expected], tokSyms[currToken],lineNum); } // end of match() continued

65 241-437 Compilers: topDown/5 65 void printToken(void) { if (currToken == ID) printf("%s(%s) ", tokSyms[currToken], tokString); // show token string else if (currToken == INT) printf("%s(%d) ", tokSyms[currToken], currTokValue); // show value else if (currToken == NEWLINE) printf("%s%2d: ", tokSyms[currToken], lineNum); // print newline token else printf("'%s' ", tokSyms[currToken]); // other tokens } // end of printToken()

66 241-437 Compilers: topDown/5 66 Syntax Error Reporting void syntax_error(Token tok) { printf("\nSyntax error at \'%s\' on line %d\n", tokSyms[tok], lineNum); exit(1); }

67 241-437 Compilers: topDown/5 67 main() int main(void) { printf("%2d: ", lineNum); nextToken(); statements(); match(SCANEOF); printf("\n\n"); return 0; } function for start symbol check that program is finished at eof

68 241-437 Compilers: topDown/5 68 Parsing Functions void statements(void) // Stats => ( [ Stat ] '\n' )* { printf("stats<"); while (currToken != SCANEOF) { if (currToken != NEWLINE) statement(); match(NEWLINE); } printf(">"); } // end of statements() rule *-3 rule []-2

69 241-437 Compilers: topDown/5 69 void statement(void) // Stat => ( 'let' ID '=' Expr ) | Expr { printf("stat<"); if (currToken == LET) { match(LET); match(ID); match(ASSIGNOP); expression(); } else if ((currToken == LPAREN) || (currToken == INT) || (currToken == ID)) expression(); else error(); printf(">"); } // end of statement() Complicated, but it can be optimized with some 'tricks'

70 241-437 Compilers: topDown/5 70 void expression(void) // Expr => Term ( ( '+' | '-' ) Term )* { printf("expr<"); term(); while((currToken == PLUSOP) || (currToken == MINUSOP)) { if (currToken == PLUSOP) match(PLUSOP); else if (currToken == MINUSOP) match(MINUSOP); else error(); term(); } printf(">"); } // end of expression() rule *-1 Version 1

71 241-437 Compilers: topDown/5 71 void expression(void) // Expr => Term ( ( '+' | '-' ) Term )* { printf("expr<"); term(); while((currToken == PLUSOP) || (currToken == MINUSOP)) { match(currToken); term(); } printf(">"); } // end of expression() Version 2: simplified | code Shorter, but also harder to understand!

72 241-437 Compilers: topDown/5 72 void term(void) // Term => Fact ( ('*' | '/' ) Fact )* { printf("term<"); factor(); while((currToken == MULTOP) || (currToken == DIVOP)) { if (currToken == MULTOP) match(MULTOP); else if (currToken == DIVOP) match(DIVOP); else error(); factor(); } printf(">"); } // end of term() rule *-1 Version 1

73 241-437 Compilers: topDown/5 73 void term(void) // Term => Fact ( ('*' | '/' ) Fact )* { printf("term<"); factor(); while((currToken == MULTOP) || (currToken == DIVOP)) { match(currToken); factor(); } printf(">"); } // end of term() Version 2: simplified | code Shorter, but also harder to understand!

74 241-437 Compilers: topDown/5 74 void factor(void) // Fact => '(' Expr ')' | INT | ID { printf("fact<"); if(currToken == LPAREN) { match(LPAREN); expression(); match(RPAREN); } else if(currToken == INT) match(INT); else if (currToken == ID) match(ID); else syntax_error(currToken); printf(">"); } // end of factor()

75 241-437 Compilers: topDown/5 75 4. LL(1) Parse Tables The format of a parse table: – –T[non-term][term] A non-terminals b terminals a production A =>  with b  PREDICT(A=>  )

76 241-437 Compilers: topDown/5 76 Other Data Structures Sequence of input tokens (ending with $). A parse stack to hold nonterminals and terminals that are being processed. $ E push pop

77 241-437 Compilers: topDown/5 77 push($); push(start_symbol); currToken = scanner(); do X = pop(stack); if (X is a terminal or $) { if (X == currToken) currToken = scanner(); else error(); } else // X is a non-terminal if (T[X][currToken] == X => Y 1 Y 2...Y m ) push(Y m );... push (Y 1 ); else error(); while (X != $); The Parsing Algorithm like match()

78 241-437 Compilers: topDown/5 78 4.1. Table Parsing Example Use the LL(1) grammar: E => T E1 E1 => '+' T E1 |  T => F T1 T1 => '*' F T1 |  F => id | '(' E ')'

79 241-437 Compilers: topDown/5 79 NT/T+*()ID$ E11 E123 T44 T16566 F87 ProductionPredict 1: E => T E1 {(,id} 2: E1 => + T E1 {+} 3: E1 =>  {$,)} 4: T => F T1 {(,id} 5: T1 => * F T1 {*} 6: T1 =>  {+,$,)} 7: F => id {id} 8: F => ( E ) {(} Parse Table Generation

80 241-437 Compilers: topDown/5 80 Parsing "a + b * c $" StackInputAction $Ea+b*c$ E => T E1 $E1 T " T => F T1 $E1 T1 F " F => id $E1 T1 id "match $E1 T1 +b*c$ T1 =>  $E1" E1 => + T E1 $E1 T+ "match $E1 T b*c$ T => F T1 StackInputAction $E1 T1 F " F => id $E1 T1 id "match $E1 T1 *c$ T1 => * F T1 $E1 T1 F * "match $E1 T1 F c$ F => id $E1 T1 id "match $E1 T1 $ T1 =>  $E1" E1 =>  $" Success!

81 241-437 Compilers: topDown/5 81 5. Making a Grammar LL(1) Not all context free grammars are LL(1). We can tell if a grammar is not LL(1) by looking at its PREDICT sets – –for a LL(1) grammar, the PREDICT sets for a non-terminal will be disjoint

82 241-437 Compilers: topDown/5 82 Example ProductionPredict E => E + T = FIRST(E) = {(,id} E => T = FIRST(T) = {(,id} T => T * F = FIRST(T) = {(,id} T => F = FIRST(F) = {(,id} F => id = {id} F => ( E ) = {(} FIRST(F) = {(,id} FIRST(T) = {(,id} FIRST(E) = {(,id} FOLLOW(E) = {$,),+} FOLLOW(T) = {+,$,),*} FOLLOW(F) = {+,$,),*} E and T are problems since their PREDICT sets are not disjoint.

83 241-437 Compilers: topDown/5 83 Example of Disjoint Problem Input "5 + b" There are two productions to choose from: E => E + T E => T Which should be chosen by looking only at the current token "5"?

84 241-437 Compilers: topDown/5 84 5.1. From non-LL(1) to LL(1) There are two main techniques for converting a non-LL(1) grammar to LL(1). – –but they don't work for every grammar 1. Left Factoring – –e.g. used on A => B a C D | B a C E 2. Transforming left recursion to right recursion – –e.g. used on E => E + T | T

85 241-437 Compilers: topDown/5 85 5.2. Left Factoring S => a B | a C – –to see the problem try choosing a production to parse "a" in "andrew" Change S to: S => a S1 S1 => B | C – –now there is no difficult choice

86 241-437 Compilers: topDown/5 86 In general: A =>  n becomes A =>  A1 A1 =>  n

87 241-437 Compilers: topDown/5 87 5.3. Why is Left Recursion a Problem? Grammar: A => A b A => b The input is "bbbb". Using only the current token, "b", which production should be used?

88 241-437 Compilers: topDown/5 88 Remove Left Recursion A => A  1 | A  2 | … |  1 |  2 | … becomes A =>  1 A1 |  2 A1 | … A1 =>  1 A1 |  2 A1 | … |   he left recursion is changed to right recursion in the new A1 rule.

89 241-437 Compilers: topDown/5 89 Example Translation The left recursive grammar: A => A b | b becomes A => b A1 A1 => b A1 |  Try parsing the input string "bbbb" using only the current token "b".

90 241-437 Compilers: topDown/5 90 Fixing the E Grammar The folowing E grammar is not LL(1): E => E + T | T T => T * F | F F => id | ( E ) Try parsing "5 + b" continued

91 241-437 Compilers: topDown/5 91 Eliminate left recursion in E and T: E => T E1 E1 => + T E1 |  T => F T1 T1 => * F T1 |  F => id | ( E ) This version of the E grammar is LL(1), and we've been using it for most of our examples.

92 241-437 Compilers: topDown/5 92 5.4. Non-Immediate Left Recursion Ex: A 1 => A 2 a | b A 2 => A 1 c | A 2 d Convert to immediate left recursion – –replace A 1 in A 2 productions by A 1 ’s definition: A 1 => A 2 a | b A 2 => A 2 a c | b c | A 2 d Now eliminate left recursion in A 2 : A 1 => A 2 a | b A 2 => b c A 3 A 3 => a c A 3 | d A 3 |  A1A1 A2A2

93 241-437 Compilers: topDown/5 93 Example A => B c | d B => C f | B f C => A e | g Replace C in B's production by C's defn: B => A e f | g f | B f Replace A in B's production by A's defn: B => B c e f | d e f | g f | B f A C B

94 241-437 Compilers: topDown/5 94 Now grammar is: A => B c | d B => B c e f | d e f | g f | B f C => A e | g Get rid of left recursion in B: A => B c | d B => d e f B1 | g f B1 B1 => c e f B1 | f B1 |  C => A e | g If A is the start symbol, then the C production is never called, so can be deleted.

95 241-437 Compilers: topDown/5 95 6. Error Recovery in LL Parsing Simple answer: – –when there's an error, print a message and exit Better error recovery: – –1. insert the expected token and continue this approach can cause non-termination – –2. keep deleting tokens until the parser gets a token in the FOLLOW set for the production that went wrong see example on next slide

96 241-437 Compilers: topDown/5 96 void E() { if (currToken in FIRST(T)) { // error checking T(); E1(); // FIRST(T) == {(,ID} } else { // error reporting and recovery printf("Expecting one of FIRST(T)"); while (currToken not in FOLLOW(E)) // FOLLOW(E) == {),$} currToken = scanner(); // skip input } } // end of E() Example: E→T E1 from slide 29

97 241-437 Compilers: topDown/5 97 void E() { if ((currToken == LPAREN) || (currToken == ID)) { T(); E1(); } else { printf("Expecting ( or id"); while ( (currToken != RPAREN) && (currToken != SCANEOF)) currToken = scanner(); } } // end of E() C Code


Download ppt "241-437 Compilers: topDown/5 1 Compiler Structures Objective – –look at top-down (LL) parsing using recursive descent and tables – –consider a recursive."

Similar presentations


Ads by Google