CS 540 George Mason University

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

Top-Down Parsing.
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
Parsing III (Eliminating left recursion, recursive descent parsing)
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Professor Yihjia Tsai Tamkang University
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Top-Down Parsing.
COP4020 Programming Languages Computing LL(1) parsing table Prof. Xin Yuan.
Compilers: topDown/5 1 Compiler Structures Objective – –look at top-down (LL) parsing using recursive descent and tables – –consider a recursive.
Top-Down Parsing - recursive descent - predictive parsing
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
Chapter 5 Top-Down Parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Parsing Top-Down.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Lecture 3: Parsing CS 540 George Mason University.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lecture 5: LR Parsing CS 540 George Mason University.
Parsing COMP 3002 School of Computer Science. 2 The Structure of a Compiler syntactic analyzer code generator program text interm. rep. machine code tokenizer.
Parsing #1 Leonidas Fegaras.
Parsing — Part II (Top-down parsing, left-recursion removal)
Programming Languages Translator
Compiler design Bottom-up parsing Concepts
Lexical and Syntax Analysis
Lecture #12 Parsing Types.
Compilers Welcome to a journey to CS419 Lecture15: Syntax Analysis:
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
Introduction to Top Down Parser
Top-down parsing cannot be performed on left recursive grammars.
Syntax Analysis Chapter 4.
Parsing with Context Free Grammars
Top-Down Parsing.
CS 363 Comparative Programming Languages
4 (c) parsing.
Parsing Techniques.
3.2 Language and Grammar Left Factoring Unclear productions
Top-Down Parsing CS 671 January 29, 2008.
Compiler Design 7. Top-Down Table-Driven Parsing
Top-Down Parsing Identify a leftmost derivation for an input string
Top-Down Parsing The parse tree is created top to bottom.
Chapter 4 Top Down Parser.
Lecture 8: Top-Down Parsing
Ambiguity, Precedence, Associativity & Top-Down Parsing
Bottom Up Parsing.
Computing Follow(A) : All Non-Terminals
Syntax Analysis - Parsing
Compiler Structures 5. Top-down Parsing Objectives
Recursive descent parsing
Nonrecursive Predictive Parsing
Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Recursive descent parsing
Presentation transcript:

CS 540 George Mason University Lecture 4: LL Parsing CS 540 George Mason University

Parsing Symbol Table Syntax described formally Syntatic/semantic structure tokens Syntatic structure Scanner (lexical analysis) Parser (syntax analysis) Semantic Analysis (IC generator) Code Generator Source language Target language Code Optimizer Syntax described formally Tokens organized into syntax tree that describes structure Error checking Symbol Table CS 540 Spring 2010 GMU

Top Down (LL) Parsing P begin simplestmt ; simplestmt ; end P begin SS end SS  S ; SS SS  e S  simplestmt S  begin SS end begin simplestmt ; simplestmt ; end CS 540 Spring 2010 GMU

Top Down (LL) Parsing P SS begin simplestmt ; simplestmt ; end P begin SS end SS  S ; SS SS  e S  simplestmt S  begin SS end SS begin simplestmt ; simplestmt ; end CS 540 Spring 2010 GMU

Top Down (LL) Parsing P SS SS S begin simplestmt ; simplestmt ; end P begin SS end SS  S ; SS SS  e S  simplestmt S  begin SS end SS SS S begin simplestmt ; simplestmt ; end CS 540 Spring 2010 GMU

Top Down (LL) Parsing P SS SS S begin simplestmt ; simplestmt ; end P begin SS end SS  S ; SS SS  e S  simplestmt S  begin SS end SS SS S begin simplestmt ; simplestmt ; end CS 540 Spring 2010 GMU

Top Down (LL) Parsing P SS SS SS S S P begin SS end SS  S ; SS SS  e S  simplestmt S  begin SS end SS SS S SS S begin simplestmt ; simplestmt ; end CS 540 Spring 2010 GMU

Top Down (LL) Parsing P SS SS SS S S P begin SS end SS  S ; SS SS  e S  simplestmt S  begin SS end SS SS S SS S begin simplestmt ; simplestmt ; end CS 540 Spring 2010 GMU

Top Down (LL) Parsing 1 2 4 3 6 5 P SS SS SS S S P  begin SS end  begin S ; SS end  begin simplestmt ; SS end  begin simplestmt ; S ; SS end simplestmt ; SS end simplestmt ; end P begin SS end SS  S ; SS SS  e S  simplestmt S  begin SS end 1 P 2 SS 4 SS S 3 SS 6 5 S e begin simplestmt ; simplestmt ; end CS 540 Spring 2010 GMU

Grammar S S b C a B c c b b C c c S  a B | b C B  b b C C  c c Two strings in the language: abbcc and bcc Can choose between them based on the first character of the input. b C a B c c b b C c c CS 540 Spring 2010 GMU

LL(k) parsing also known as the lookahead Process input k symbols at a time. Initially, ‘current’ non-terminal is start symbol. Algorithm Loop until no more input Given next k input tokens and ‘current’ non-terminal T, choose a rule R (T  …) For each element X in rule R from left to right, if X is a non-terminal, we will need to ‘expand’ X else if symbol X is a terminal, see if next input symbol matches X; if so, update from the input Typically, we consider LL(1) CS 540 Spring 2010 GMU

Two Approaches Recursive Descent parsing Code tailored to the grammar Table Driven – predictive parsing Table tailored to the grammar General Algorithm Both algorithms driven by the tokens coming from the lexer. CS 540 Spring 2010 GMU

Writing a Recursive Descent Parser Generate a procedure for each non-terminal. Use next token from yylex() (lookahead) to choose (PREDICT) which production to ‘mimic’. for non-terminal X, call procedure X() for terminals X, call ‘match(X)’ Ex: B  b C D B() { if (lookahead == ‘b’) { match(‘b’); C(); D(); } else … } CS 540 Spring 2010 GMU

Writing a Recursive Descent Parser Also need the following: match(symbol) { if (symbol == lookahead) lookahead = yylex() else error() } main() { lookahead = yylex(); S(); /* S is the start symbol */ if (lookahead == EOF) then accept else reject } error() { … CS 540 Spring 2010 GMU

Back to grammar S() { if (lookahead == a ) { match(a);B(); } S  a B else if (lookahead == b) { match(b); C(); } S  b C else error(“expecting a or b”); } B() { if (lookahead == b) {match(b); match(b); C();} B  b b C else error(); C() { if (lookahead == c) { match(c) ; match(c) ;} C  c c CS 540 Spring 2010 GMU

Parsing abbcc abbcc Remaining input: S Call S() from main() S() { if (lookahead == a ) { match(a);B(); } S  a B else if (lookahead == b) { match(b); C(); } S  b C else error(“expecting a or b”); } CS 540 Spring 2010 GMU

Parsing abbcc bbcc Remaining input: S a B Call B() from A(): B() { if (lookahead == b) {match(b); match(b); C();} B  b b C else error(); } CS 540 Spring 2010 GMU

Parsing abbcc cc Remaining input: S a B b b C Call C() from B(): C() { if (lookahead == c) { match(c) ; match(c) ;} C  c c else error(); } b b C CS 540 Spring 2010 GMU

Parsing abbcc S Remaining input: a B b b C c c CS 540 Spring 2010 GMU

How do we find the lookaheads? Can compute PREDICT sets from FIRST and FOLLOW for LL(1) parsing: PREDICT(A  a) = (FIRST(a) – {e})  FOLLOW(A) if e in FIRST(a) = FIRST(a) if e not in FIRST(a) NOTE: e never in PREDICT sets For LL(k) grammars, the PREDICT sets for the productions associated with a given non-terminal must be disjoint. CS 540 Spring 2010 GMU

Example Assume E is the start symbol Production Predict E  T E’ = FIRST(T) = {(,id} E’  + T E’ {+} E’  e = FOLLOW(E’) = {$,)} T  F T’ = FIRST(F) = {(,id} T’  * F T’ {*} T’  e = FOLLOW(T’) = {+,$,)} F  id {id} F  ( E ) {(} FIRST(F) = {(,id} FIRST(T) = {(,id} FIRST(E) = {(,id} FIRST(T’) = {*,e} FIRST(E’) = {+,e} FOLLOW(E) = {$,)} FOLLOW(E’) = {$,)} FOLLOW(T) = {+$,)} FOLLOW(T’) = {+,$,)} FOLLOW(F) = {*,+,$,)} Assume E is the start symbol CS 540 Spring 2010 GMU

if (lookahead in {(,id } ) { T(); E_prime(); } E  T E’ else error(“E expecting ( or identifier”); } E_prime() { if (lookahead in {+}) {match(+); T(); E_prime();} E’  + T E’ else if (lookahead in {),end_of_file}) return; E’  e else error(“E_prime expecting +, ) or end of file”); T() { if (lookahead in {(,id}) { F(); T_prime(); } T  F T’ else error(“T expecting ( or identifier”); CS 540 Spring 2010 GMU

if (lookahead in {*}) {match(*); F(); T_prime();} T’  * F T’ else if (lookahead in {+,),end_of_file}) return; T’  e else error(“T_prime expecting *, ) or end of file”); } F() { if (lookahead in {id}) match(id); F  id else if (lookahead in {(} ) { match( ( ); E(); match ( ) ); } F  ( E ) else error(“F expecting ( or identifier”);} CS 540 Spring 2010 GMU

Parsing a + b * c E Remaining input: a+b*c CS 540 Spring 2010 GMU

Parsing a + b * c Remaining input: a+b*c E T E’ E() { if (lookahead in {(,id } ) { T(); E_prime(); } else error(“E expecting ( or identifier”); } CS 540 Spring 2010 GMU

Parsing a + b * c Remaining input: a+b*c E T E’ F T’ T() { if (lookahead in {(,id } ) { F(); T_prime(); } else error(“T expecting ( or identifier”); } CS 540 Spring 2010 GMU

Parsing a + b * c Remaining input: +b*c E T E’ F T’ id a F() { if (lookahead in {id } ) match(id) else if (lookahead in { ( } { match( ( ); E(); match( ) ); } else error(“F expecting ( or identifier”); } id a CS 540 Spring 2010 GMU

Parsing a + b * c Remaining input: +b*c E T E’ F T’ id e a T_prime() { if (lookahead in {*}) {match(*); F(); T_prime();}’ else if (lookahead in {+,),end_of_file}) return; else error(“T_prime expecting *, ) or end of file”); } } id a e CS 540 Spring 2010 GMU

Parsing a + b * c Remaining input: b*c E T E’ F T’ + T E’ id e a E_prime() { if (lookahead in {+}) {match(+); T(); E_prime();}’ else if (lookahead in {),end_of_file}) return; else error(“E_prime expecting *, ) or end of file”); } } id a e CS 540 Spring 2010 GMU

Parsing a + b * c Remaining input: b*c E T E’ F T’ + T E’ id a F T’ e if (lookahead in {(,id } ) { F(); T_prime(); } else error(“T expecting ( or identifier”); } id a F T’ e CS 540 Spring 2010 GMU

Parsing a + b * c Remaining input: *c E T E’ F T’ + T E’ id a F T’ e if (lookahead in {id } ) match(id) else if (lookahead in { ( } { match( ( ); E(); match( ) ); } else error(“F expecting ( or identifier”); } id a F T’ e id b CS 540 Spring 2010 GMU

Parsing a + b * c Remaining input: c E T E’ F T’ + T E’ id a F T’ e id T_prime() { if (lookahead in {*}) {match(*); F(); T_prime();}’ else if (lookahead in {+,),end_of_file}) return; else error(“T_prime expecting *, ) or end of file”); } } id a F T’ e id b * F T’ CS 540 Spring 2010 GMU

Parsing a + b * c Remaining input: E T E’ F T’ + T E’ id a F T’ e id b if (lookahead in {id } ) match(id) else if (lookahead in { ( } { match( ( ); E(); match( ) ); } else error(“F expecting ( or identifier”); } id a F T’ e id b * F T’ id c CS 540 Spring 2010 GMU

Parsing a + b * c Remaining input: E T E’ F T’ + T E’ id a F T’ e id b T_prime() { if (lookahead in {*}) {match(*); F(); T_prime();}’ else if (lookahead in {+,),end_of_file}) return; else error(“T_prime expecting *, ) or end of file”); } } id a F T’ e id b * F T’ e id c CS 540 Spring 2010 GMU

Parsing a + b * c Remaining input: E T E’ F T’ + T E’ id a F T’ e e id E_prime() { if (lookahead in {+}) {match(+); T(); E_prime();}’ else if (lookahead in {),end_of_file}) return; else error(“E_prime expecting *, ) or end of file”); } } id a F T’ e e id b * F T’ e id c CS 540 Spring 2010 GMU

Stacks in Recursive Descent Parsing Runtime stack Procedure activations correspond to a path in parse tree from root to some interior node E’ T F id b CS 540 Spring 2010 GMU

Two Approaches Recursive Descent parsing Code tailored to the grammar Table Driven – predictive parsing Table tailored to the grammar General Algorithm Both algorithms driven by the tokens coming from the lexer. CS 540 Spring 2010 GMU

LL(1) Predictive Parse Tables An LL(1) Parse table is a mapping T: Vn x Vt  production P or error For all productions A  a do For each terminal t in Predict(A a), T[A][t] = A  a Every undefined table entry is an error. CS 540 Spring 2010 GMU

Using LL(1) Parse Tables ALGORITHM INPUT: token sequence to be parsed, followed by ‘$’ (end of file) DATA STRUCTURES: Parse stack: Initialized by pushing ‘$’ and then pushing the start symbol Parse table T CS 540 Spring 2010 GMU

Algorithm: Predictive Parsing push($); push(start_symbol); lookahead = yylex() repeat X = pop(stack) if X is a terminal symbol or $ then if X = lookahead then else error() else /* X is non-terminal */ if T[X][lookahead] = X  Y1 Y2 …Ym push(Ym) … push (Y1) until X = $ token similar to ‘match’ similar to ‘mimic’ CS 540 Spring 2010 GMU

Example Production Predict NT/T + * ( ) ID $ E E’ T T’ F 1: E  T E’ {+} 3: E’  e {$,)} 4: T  F T’ 5: T’  * F T’ {*} 6: T’  e {+,$,)} 7: F  id {id} 8: F  ( E ) {(} NT/T + * ( ) ID $ E E’ T T’ F CS 540 Spring 2010 GMU

Production Predict NT/T + * ( ) ID $ E E’ T T’ F 1: E  T E’ {(,id} {+} 3: E’  e {$,)} 4: T  F T’ 5: T’  * F T’ {*} 6: T’  e {+,$,)} 7: F  id {id} 8: F  ( E ) {(} NT/T + * ( ) ID $ E 1 E’ 2 3 T 4 T’ 6 5 F 8 7 CS 540 Spring 2010 GMU

Assume E is the start symbol Stack Input Action $E a+b*c$ E  T E’ NT/T + * ( ) ID $ E 1 E’ 2 3 T 4 T’ 6 5 F 8 7 Assume E is the start symbol CS 540 Spring 2010 GMU

Stack Input Action $E $E’T NT/T + * ( ) ID $ E E’ T T’ F a+b*c$ E  T E’ $E’T T  F T’ NT/T + * ( ) ID $ E 1 E’ 2 3 T 4 T’ 6 5 F 8 7 CS 540 Spring 2010 GMU

Stack Input Action $E $E’T $E’T’F NT/T + * ( ) ID $ E E’ T T’ F a+b*c$ T  F T’ $E’T’F F  id NT/T + * ( ) ID $ E 1 E’ 2 3 T 4 T’ 6 5 F 8 7 CS 540 Spring 2010 GMU

Stack Input Action $E $E’T $E’T’F $E’T’id NT/T + * ( ) ID $ E E’ T T’ a+b*c$ E  T E’ $E’T T  F T’ $E’T’F F  id $E’T’id match NT/T + * ( ) ID $ E 1 E’ 2 3 T 4 T’ 6 5 F 8 7 CS 540 Spring 2010 GMU

Stack Input Action $E $E’T $E’T’F $E’T’id $E’T’ NT/T + * ( ) ID $ E E’ a+b*c$ E  T E’ $E’T T  F T’ $E’T’F F  id $E’T’id match $E’T’ +b*c$ T’  e NT/T + * ( ) ID $ E 1 E’ 2 3 T 4 T’ 6 5 F 8 7 CS 540 Spring 2010 GMU

Stack Input Action $E $E’T $E’T’F $E’T’id $E’T’ $E’ NT/T + * ( ) ID $ a+b*c$ E  T E’ $E’T T  F T’ $E’T’F F  id $E’T’id match $E’T’ +b*c$ T’  e $E’ E’  + T E’ NT/T + * ( ) ID $ E 1 E’ 2 3 T 4 T’ 6 5 F 8 7 CS 540 Spring 2010 GMU

Stack Input Action $E $E’T $E’T’F $E’T’id $E’T’ $E’ $E’ T + NT/T + * ( a+b*c$ E  T E’ $E’T T  F T’ $E’T’F F  id $E’T’id match $E’T’ +b*c$ T’  e $E’ E’  + T E’ $E’ T + NT/T + * ( ) ID $ E 1 E’ 2 3 T 4 T’ 6 5 F 8 7 CS 540 Spring 2010 GMU

Stack Input Action $E $E’T $E’T’F $E’T’id $E’T’ $E’ $E’ T + $E’ T NT/T a+b*c$ E  T E’ $E’T T  F T’ $E’T’F F  id $E’T’id match $E’T’ +b*c$ T’  e $E’ E’  + T E’ $E’ T + $E’ T b*c$ NT/T + * ( ) ID $ E 1 E’ 2 3 T 4 T’ 6 5 F 8 7 CS 540 Spring 2010 GMU

Stack Input Action $E $E’T $E’T’F $E’T’id $E’T’ $E’ $E’ T + $E’ T NT/T a+b*c$ E  T E’ $E’T T  F T’ $E’T’F F  id $E’T’id match $E’T’ +b*c$ T’  e $E’ E’  + T E’ $E’ T + $E’ T b*c$ NT/T + * ( ) ID $ E 1 E’ 2 3 T 4 T’ 6 5 F 8 7 CS 540 Spring 2010 GMU

Stack Input Action $E $E’T $E’T’F $E’T’id $E’T’ $E’ $E’ T + $E’ T a+b*c$ E  T E’ $E’T T  F T’ $E’T’F F  id $E’T’id match $E’T’ +b*c$ T’  e $E’ E’  + T E’ $E’ T + $E’ T b*c$ $E’T id NT/T + * ( ) ID $ E 1 E’ 2 3 T 4 T’ 6 5 F 8 7 CS 540 Spring 2010 GMU

Parsing a + b * c Stack Input Action Stack Input Action $E a+b*c$ E  T E’ $E’T T  F T’ $E’T’F F  id $E’T’id match $E’T’ +b*c$ T’  e $E’ E’  + T E’ $E’T+ b*c$ Stack Input Action $E’T’F F  id $E’T’id match $E’T’ *c$ T’  * F T’ $E’T’F* c$ $ T’  e $E’ E’  e accept CS 540 Spring 2010 GMU

Stacks in Predictive Parsing Algorithm data structure Hold terminals and non-terminals from the grammar terminals – still need to be matched from the input non-terminals – still need to be expanded CS 540 Spring 2010 GMU

Making a grammar LL(1) Not all context free languages have LL(1) grammars Can show a grammar is not LL(1) by looking at the predict sets For LL(1) grammars, the PREDICT sets for a given non-terminal will be disjoint. CS 540 Spring 2010 GMU

Example Two problems: E and T Production Predict E  E + T = FIRST(E) = {(,id} E  T = FIRST(T) = {(,id} T  T * F T  F = FIRST(F) = {(,id} F  id = {id} F  ( E ) = {(} FIRST(F) = {(,id} FIRST(T) = {(,id} FIRST(E) = {(,id} FIRST(T’) = {*,e} FIRST(E’) = {+,e} FOLLOW(E) = {$,)} FOLLOW(E’) = {$,)} FOLLOW(T) = {+$,)} FOLLOW(T’) = {+,$,)} FOLLOW(F) = {*,+,$,)} Two problems: E and T CS 540 Spring 2010 GMU

Making a non-LL(1) grammar LL(1) Eliminate common prefixes Ex: A  B a C D | B a C E Transform left recursion to right recursion Ex: E  E + T | T CS 540 Spring 2010 GMU

Eliminate Common Prefixes A  a b | a d Can become: A  a A’ A’  b | d Doesn’t always remove the problem. Why? CS 540 Spring 2010 GMU

Why is left recursion a problem? A a A a A a CS 540 Spring 2010 GMU

Remove Left Recursion A  A a1 | A a2 | … | b1 | b2 | … becomes A  b1 A’| b2 A’| … A’  a1 A’ | a2 A’ | … | e The left recursion becomes right recursion CS 540 Spring 2010 GMU

A  A a | b becomes A  b B, B  a B | e CS 540 Spring 2010 GMU

Expression Grammar E  E + T | T T  T * F | F F  id | ( E ) NOT LL(1) Eliminate left recursion: E  T E’, E’  + T E’ | e T  F T’, T’  * F T’ | e F  id | ( E ) CS 540 Spring 2010 GMU

E  E + T | T becomes E  T E’, E’  + T E’ | e CS 540 Spring 2010 GMU

Non-Immediate Left Recursion Ex: A1  A2 a | b A2  A1 c | A2 d Convert to immediate left recursion Substitute A1 in second set of productions by A1’s definition: A1  A2 a | b A2  A2 a c | b c | A2 d Eliminate recursion: A2  b c A3 A3  a c A3 | d A3 | e A1 A2 CS 540 Spring 2010 GMU

Example A B C A  B c | d B  C f | B f C  A e | g Rewrite: replace C in B B  A e f | g f | B f Rewrite: replace A in B B  B c e f | d e f | g f | B f A B C CS 540 Spring 2010 GMU

Get rid of left recursion (and C if A is start) B  d e f B’ | g f B’ 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 (and C if A is start) B  d e f B’ | g f B’ B’  c e f B’ | f B’ | e CS 540 Spring 2009 GMU

Error Recovery in LL parsing Simple option: When see an error, print a message and halt “Real” error recovery Insert “expected” token and continue – can have a problem with termination Deleting tokens – for an error for non-terminal F, keep deleting tokens until see a token in follow(F). CS 540 Spring 2010 GMU

if (lookahead in {(,id} ) { T(); E_prime(); } E  T E’ For example: E() { if (lookahead in {(,id} ) { T(); E_prime(); } E  T E’ else { printf(“E expecting ( or identifier”); Follow(E) = $ ) while (lookahead != ) or $) lookahead = yylex(); } CS 540 Spring 2010 GMU

Real-World Compilers http://cs.gmu.edu/~white/CS540/parser.cpp // CParser::ParseSourceModule is the “main” CS 540 Spring 2010 GMU