DESKTOP

Algorithms for Compiler Design: ERROR RECOVERY IN LR PARSING

7/24/2010 8:06:08 PM
9.4 ERROR RECOVERY IN LR PARSING
A systematic method for error recovery in LR parsing is to scan down the stack until a state S with a goto on a particular nonterminal A is found, and then discard zero or more input symbols until a symbol a is found that can legitimately follow A. The parser then shifts the state goto [S, A] on the stack and resumes normal parsing.

There might be more than one choice for the nonterminal A. Normally, these would be nonterminals representing major program pieces, such as statements.

Another method of error recovery that can be implemented is called "phrase level recovery". Each error entry in the LR parsing table is examined, and, based on language usage, an appropriate error-recovery procedure is constructed. For example, to recover from an construct error that starts with an operator, the error-recovery routine will push an imaginary id onto the stack and cover it with the appropriate state. While doing this, the error entries in a particular state that call for a particular reduction on some input symbols are replaced by that reduction. This has the effect of postponing the error detection until one or more reductions are made; but the error will still be caught before a shift.

A phrase level error-recovery implementation for an LR parser is shown below. The parsing table's grammar is:

The SLR parsing table for the above grammar is shown in Table 1.

Table 1: Parsing Table for E E + E | E * E | id

id

+

*

$

E

I0

S2

1

I1

S3

S4

Accept

I2

R3

R3

R3

I3

S2

5

I4

S2

6

I5

S3/R1

S4/R1

R1

I6

S3/R2

S4/R2

R2

The conflict is resolved by giving higher precedence to * and using leftassociativity, as shown in Table 2.

Table 2: Higher Precedent * and Left-Associativity

id

+

*

$

E

I0

S2

1

I1

S3

S4

Accept

I2

R3

R3

R3

I3

S2

5

I4

S2

6

I5

R1

S4

R1

I6

R2

R2

R2

The parsing table with error routines is shown in Table 3, where routine e1 is called from states I0, I3, and I4, which pushes an imaginary id onto the stack and covers it with state I2. The routine e2 is called from state I1, which pushes + onto stack and covers it with state I3.

Table 3: Parsing Table with Error Routines

id

+

*

$

E

I0

S2

e1

e1

e1

1

I1

E2

S3

S4

Accept

I2

R3

R3

R3

R3

I3

S2

e1

E1

E1

5

I4

S2

E1

E1

E1

6

I5

R1

R1

S4

R1

I6

R2

R2

R2

R2

For example, if we trace the behavior of the parser described above for the input id + *id $:

Stack Contents

Unspent Input

Moves

$I0

id+*id$

shift and enter into state 2

$I0idI2

+*id$

reduce by production number 3

$I0EI1

+*id$

shift and enter into state 3

$I0EI1+I3

*id$

call error routine e1

$I0EI1+I3id I2

*id$

reduce by production number 3

(id I2 pushed by e1)

$I0EI1+I3EI5

*id$

shift and enter into state 4

$I0EI1+I3E I5*I4

id$

shift and enter into state 2

$I0EI1+I3E I5*I4idI2

$

reduce by production number 3

$I0EI1+I3E I5*I4EI6

$

reduce by production number 2

$I0EI1+I3EI5

$

reduce by production number 1

$I0EI1

$

accept

Similarly, if we trace the behavior of the parser for the input id id*id $:

Stack Contents

Unspent Input

Moves

$I0

id id*id$

shift and enter into state 2

$I0idI2

id*id$

reduce by production number 3

$I0EI1

id*id$

call error routine e2

$I0EI1+ I3

id*id$

shift and enter into state 2

(I3 pushed by e2)

$I0EI1+I3id I2

*id$

reduce by production number 3

$I0EI1+I3EI5

*id$

shift and enter into state 4

$I0EI1+I3EI5*I4

id$

shift and enter into state 2

$I0EI1+I3EI5*I4idI2

$

reduce by production number 3

$I0EI1+I3EI5*I4EI6

$

reduce by production number 2

$I0EI1+I3EI5

$

reduce by production number 1

$I0EI1

$

accept


Other  
  •  Algorithms for Compiler Design: PREDICTIVE PARSING ERROR RECOVERY
  •  Algorithms for Compiler Design: LOOP OPTIMIZATION
  •  Algorithms for Compiler Design: ELIMINATING INDUCTION VARIABLES
  •  Algorithms for Compiler Design: ELIMINATING LOCAL COMMON SUBEXPRESSIONS
  •  Algorithms for Compiler Design:
  •  Algorithms for Compiler Design
  •  Algorithms for Compiler Design: THE MACHINE MODEL
  •  Algorithms for Compiler Design: STRAIGHTFORWARD CODE GENERATION
  •  Algorithms for Compiler Design: USING DAG FOR CODE GENERATION
  •  Algorithms for Compiler Design: USING ALGEBRAIC PROPERTIES TO REDUCE THE REGISTER REQUIREMENT
  •  Algorithms for Compiler Design: PEEPHOLE OPTIMIZATION
  •  Algorithms for Compiler Design: IMPLEMENTATION OF THE TRANSLATIONS SPECIFIED BY SYNTAX-DIRECTED DEFINITIONS
  •  Algorithms for Compiler Design: L-ATTRIBUTED DEFINITIONS
  •  Algorithms for Compiler Design: SYNTAX-DIRECTED TRANSLATION SCHEMES
  •  Algorithms for Compiler Design: INTERMEDIATE CODE GENERATION
  •  Algorithms for Compiler Design: REPRESENTING THREE-ADDRESS STATEMENTS
  •  Algorithms for Compiler Design: SYNTAX-DIRECTED TRANSLATION SCHEMES TO SPECIFY THE TRANSLATION OF VARIOUS PROGRAMMING LANGUAGE CONSTRUCTS
  •  Algorithms for Compiler Design: THE ARRAY REFERENCE
  •  Algorithms for Compiler Design: SWITCH/CASE
  •  EXAMPLES for Syntax-Directed Definitions and Translations
  •  
    Top 10
    Motorola RAZR MAXX - “Being The World’s Lightest”
    Nokia 808 Pureview
    Nokia Lumia 610 - Shines Out Of Low Segment
    Nokia Lumia 900 - The Brightest Star In Lumia Series
    One More Thing: Two New Ipods
    Sony Xperia P Review (Part 2)
    Sony Xperia P Review (Part 1)
    Onyx Calypso 9.7 Tablet
    Aesthetix Calypso Light Preamplifier Review
    Worthy Audio Block C-100 For Mid-Class CD Player
    Most View
    Game Changer - iPhone Revolution (Part 2)
    Gaming Mouse Tt ESPOTRS Theron And Mouse Pad Ladon
    Algorithms for Compiler Design: ELIMINATING LOCAL COMMON SUBEXPRESSIONS
    SQL Injection : Platform-Level Defenses - Using Runtime Protection (part 2) - Intercepting Filters
    Samsung NX Series - Smart Shooters With Fanciful New Features
    Microsoft SQL Server 2005 : Report Definition and Design (part 1) - Data Sources
    Using SQL Server 2005 Integration Services : Programming Integration Services (part 2)
    Searching for Google’s future (Part4) - Smarter search
    The Three-Step Approach to Security
    Exchange Server 2010 : Administering Mailbox Content - Monitor and Restrict Communication (part 1) - Perform Basic Message Policy Configuration
    The Future Of Apple: Chip Off The Block (Part 7)
    Laptop For All Budgets (Part 2) - Notebooks, Ultrabooks
    A brief history of transforming robots (Part 1)
    Corsair Nova 2
    Becoming an Excel Programmer : View Results
    Buying Guide: CPU Cooling Equipment (Part 2) - Antec KUHLER H2O 920, Corsair Hydro Series H80, Corsair Hydro Series H100, Noctua NH-D14
    Windows Vista : Performance - Hard Disk (part 3) - Transfer Windows to Another Hard Disk
    SQL Server 2008 : Working with Multiple-Source Queries - OpenQuery, OpenRowSet, and OpenDataSource Explained
    iPhone Application Development : Implementing a Custom Picker View (part 4) - Tweaking the Picker UI
    Motorola Xoom 2 Media Edition