• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Compiler » Error Handling and Error Recovery In Syntax Analyzer
Next →
← Prev

Error Handling and Error Recovery In Syntax Analyzer

By Dinesh Thakur

• An efficient program should not terminate on an parse error.

• It must recover to parse the rest of the input and check for subsequent errors.

• For one line input, the routine yyparse() can be made to return 1 on error and then calls yyparse() again.

YACC program error handling

• Parser must be capable of detecting the error as soon as it encounters, i.e., when an input stream does not match the rules in grammar.

• If there is an error-handling subroutine in the grammar file, the parser can allow for entering the data again, ignoring the bad data or initiating a cleanup and recovery action.

• When the parser finds an error, it may need to reclaim parse tree storage, delete or alter symbol table entries and set switches to avoid generating further output.

• Error handling routines are used to restart the parser to continue its process even after the occurrence of error.

• Tokens following the error get discarded to restart the parser.

• The YACC command uses a special token name error, for error handling. The token is placed at places where error might occur so that it provides a recovery subroutine.

• To prevent subsequent occurrence of errors, the parser remains in error state until it processes three tokens following an error.

• The input is discarded and no message is produced, if an error occurred while the

parser remains in error state.

(eg.) stat : error ‘;’

o The above rule tells the parser that when there is an error, it should ignore the token and all following tokens until it finds the next semicolon.

o It discards all the tokens after the error and before the next semicolon.

o Once semicolon is found, the rule is reduced by parser and cleanup action associated with that rule will be performed.

Providing for error correction

The input errors can be corrected by entering a line in the data stream again.

input : error ‘\n’

{

printf (“Reenter last line:”);

}

input

{

$$ = $4;

} ;

The YACC statement, yyerrok is used to indicate that error recovery is complete.

This statement leaves the error state and begins processing normally.

input : error ‘\n’

yyerrok;

printf (“Reenter last line:”);

}

input

{

$$ = $4;

};

Clearing the Lookahead token

 

• When an error occurs, the lookahead token becomes the token at which the error was detected.

• The lookahead token must be changed if the error recovery action includes code to find the correct place to start processing again.

• To clear the lookahead token, the error-recovery action issues the following statement:

yyclearin;

To assist in error handling, macros can be placed in YACC actions.

                                            Macros for error handling

 

YYERROR

Causes the parser to initiate error handling.

YYABORT

Causes the parser to return with a value of 1.

YYACCEPT

Causes the parser to return with a value of 0.

YYRECOVERING()

Returns a value of 1 if a syntax error has

been detected and the parser has not yet fully

recovered.

You’ll also like:

  1. What is Parser (Syntax analysis)? Error Handling and Recovery in Syntax Analyzer
  2. Error-Handling functions in C
  3. What is compile error, time error, logical error,, when does it occur, and what might it cause it
  4. Syntax Directed Definition (SDD) and Types of Syntax Directed Definitions
  5. How Does a Single Bit Error Differs From Burst Error.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Compiler Design

Compiler Design

  • CD - Translators
  • CD - Compiler Phases
  • CD - Input Buffering
  • CD - Lexical Analysis
  • CD - Regular Expression
  • CD - LR Parsers
  • CD - Parse Tree
  • CD - Parsing Type
  • CD - Lex
  • CD - Syntax Directed Definition
  • CD - Compiler Construction tools
  • CD - Regular Expression
  • CD - Context Free Grammars
  • CD - Syntax Analysis
  • CD - Grouping of Phases
  • CD - Language Processing System
  • CD - Derivations
  • CD - Error Handling

Other Links

  • Compiler Design - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW