0% found this document useful (0 votes)
13 views3 pages

Introduction To Lalr Parser, Error Handler and Recovery in Syntax Analyzer

The document provides an overview of the LALR parser, highlighting its efficiency in generating smaller tables compared to canonical LR parsers. It details the algorithm for constructing LALR parsing tables and discusses error recovery strategies in LR parsing, including panic-mode and phrase-level recovery. Additionally, it outlines various error handling methods such as error productions and global correction for syntax analyzers.

Uploaded by

Subramanian R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Introduction To Lalr Parser, Error Handler and Recovery in Syntax Analyzer

The document provides an overview of the LALR parser, highlighting its efficiency in generating smaller tables compared to canonical LR parsers. It details the algorithm for constructing LALR parsing tables and discusses error recovery strategies in LR parsing, including panic-mode and phrase-level recovery. Additionally, it outlines various error handling methods such as error productions and global correction for syntax analyzers.

Uploaded by

Subramanian R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

R.V.

S COLLEGE OF ENGINEERING, DINDIGUL

INTRODUCTION TO LALR PARSER


 The last parser method LALR (LookAhead-LR) technique is often used in practice because the tables
obtained by it are considerably smaller than the canonical LR tables.
 The SLR and LALR tables for a grammar always have the same number of states whereas CLR would
typically have several number of states.
 For example, for a language like Pascal SLR and LALR would have hundreds of states but CLR
would have several thousands of states.
Algorithm: LALR table construction.
INPUT: An augmented grammar G'.
OUTPUT: The LALR parsing-table functions ACTION and GOT0 for G'.
METHOD:
1. Construct C = (I0 I1, . . , In ), the collection of sets of LR(1) items.
2. For each core present among the set of LR(1) items, find all sets having that core, and replace these sets by
their union.
3. Let C' = {J0J1, . . . ,Jm) be the resulting sets of LR(1) items. If there is a parsing action conflict, the
algorithm fails to produce a parser, and the grammar is said not to be LALR(1).
4. The GOTO table is constructed as follows. If J is the union of one or more sets of LR(1) items, that is, J =
I1 U I2 U . . .U Ik , then the cores of GOTO(I1, X) , GOTO(I2, X) , . . . , GOTO(Ik, X) are the same, since
I1,I2,
. . ,Ik all have the same core. Let K be the union of all sets of items having the same core as GOTO(I1, X).
Then GOTO(J,X) = K.
Let us consider grammar
S->CC
C->cC
C->d whose sets of LR(1) items are

CS3501 – COMPILER DESIGN


R.V.S COLLEGE OF ENGINEERING, DINDIGUL

Error recovery in LR parsing:


 An LR parser will detect an error when it consults the parsing action table and finds an error entry. An
LR parser will announce an error as soon as there is no valid continuation for the portion of the input
thus far scanned.
 A canonical LR parser will not make even a single reduction before announcing an error. SLR and
LALR parsers may make several reductions before announcing an error, but they will never shift an
erroneous input symbol onto the stack.
Panic-mode error recovery:
 We scan down the stack until a state s with a goto on a particular nonterminal A is found. Zero or
more input symbols are then discarded until a symbol a is found that can legitimately follow A.
 The parser then stacks the state GOTO (s, A) and resumes normal parsing.
Phrase-level recovery:
 It is implemented by examining each error entry in the LR parsing table and deciding on the basis of
language usage the most likely programmer error that would give rise to that error.
 An appropriate recovery procedure can then be constructed; presumably the top of the stack and/or
first input symbols would be modified in a way deemed appropriate for each error entry.
ERROR HANDLING AND RECOVERY IN SYNTAX ANALYZER
The different strategies that a parse uses to recover from a syntactic error are:
1. Panic mode
2. Phrase level
3. Error productions
4. Global correction

CS3501 – COMPILER DESIGN


R.V.S COLLEGE OF ENGINEERING, DINDIGUL

Panic mode recovery:


 On discovering an error, the parser discards input symbols one at a time until a synchronizing token is
found. The synchronizing tokens are usually delimiters, such as semicolon or end.
 It has the advantage of simplicity and does not go into an infinite loop. When multiple errors in the
same statement are rare, this method is quite useful.
Phrase level recovery:
 On discovering an error, the parser performs local correction on the remaining input that allows it to
continue.
 Example: Insert a missing semicolon or delete an extraneous semicolon etc.
Error productions:
 The parser is constructed using augmented grammar with error productions. If an error production is
used by the parser, appropriate error diagnostics can be generated to indicate the erroneous constructs
recognized by the input.
Global correction:
 Given an incorrect input string x and grammar G, certain algorithms can be used to find a parse tree
for a string y, such that the number of insertions, deletions and changes of tokens is as small as
possible. However, these methods are in general too costly in terms of time and space.

CS3501 – COMPILER DESIGN

You might also like