[go: up one dir, main page]

0% found this document useful (0 votes)
69 views13 pages

Error Recovery in Parsing

This document discusses error recovery in parsing. It explains that when an error is detected, the parser has two options: stop immediately or try to continue parsing after recording the error. Continuing allows avoiding recompiling from scratch but risks multiple error messages. The document then describes how parsers can do error recovery by adjusting the input stream through deletion, insertion or substitution of tokens. It outlines local and global recovery strategies and provides an example of local bottom-up recovery where the parser shifts an "error" token, discards input until it can resume parsing, and continues.

Uploaded by

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

Error Recovery in Parsing

This document discusses error recovery in parsing. It explains that when an error is detected, the parser has two options: stop immediately or try to continue parsing after recording the error. Continuing allows avoiding recompiling from scratch but risks multiple error messages. The document then describes how parsers can do error recovery by adjusting the input stream through deletion, insertion or substitution of tokens. It outlines local and global recovery strategies and provides an example of local bottom-up recovery where the parser shifts an "error" token, discards input until it can resume parsing, and continues.

Uploaded by

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

Parsing & Error Recovery

vaibhav kumar
03711502716
Error Recovery
• What should happen when your parser finds an
error in the user’s input?
– stop immediately and signal an error
– record the error but try to continue
• In the first case, the user must recompile from
scratch after possibly a trivial fix
• In the second case, the user might be
overwhelmed by a whole series of error
messages, all caused by essentially the same
problem
• We will talk about how to do error recovery in a
principled way
Error Recovery
• Error recovery:
– process of adjusting input stream so that the parser can continue
after unexpected input
• Possible adjustments:
– delete tokens
– insert tokens
– substitute tokens
• Classes of recovery:
– local recovery: adjust input at the point where error was
detected (and also possibly immediately after)
– global recovery: adjust input before point where error was
detected.
• Error recovery is possible in both top-down and
bottom-up parsers
Local Bottom-up Error Recovery
exp : NUM () exps : exp ()
| exp PLUS exp () | exps ; exp ()
| LPAR exp RPAR ()

• general strategy for both bottom-up and top-down:


• look for a synchronizing token
Local Bottom-up Error Recovery
exp : NUM () exps : exp ()
| exp PLUS exp () | exps ; exp ()
| LPAR exp RPAR ()

• general strategy for both bottom-up and top-down:


• look for a synchronizing token
• accomplished in bottom-up parsers by adding error rules to grammar:
exp : LPAR error RPAR ()

exps : exp ()
| error ; exp ()
Local Bottom-up Error Recovery
exp : NUM () exps : exp ()
| exp PLUS exp () | exps ; exp ()
| LPAR exp RPAR ()

• general strategy for both bottom-up and top-down:


• look for a synchronizing token
• accomplished in bottom-up parsers by adding error rules to grammar:
exp : LPAR error RPAR ()

exps : exp ()
| error ; exp ()
• in general, follow error with a synchronizing token. Recovery steps:
• Pop stack (if necessary) until a state is reached in which the
action for the error token is shift
• Shift the error token
• Discard input symbols (if necessary) until a state is reached that has
a non-error action
• Resume normal parsing
Local Bottom-up Error Recovery
exp : NUM () exps : exp ()
| exp PLUS exp () | exps ; exp ()
| ( exp ) ()

exp : ( error ) ()

exps : exp ()
| error ; exp ()

yet to read

input: NUM PLUS ( NUM PLUS @#$ PLUS NUM ) PLUS NUM

stack: exp PLUS ( exp PLUS

@#$ is an unexpected token!


Local Bottom-up Error Recovery
exp : NUM () exps : exp ()
| exp PLUS exp () | exps ; exp ()
| ( exp ) ()

exp : ( error ) ()

exps : exp ()
| error ; exp ()

yet to read

input: NUM PLUS ( NUM PLUS @#$ PLUS NUM ) PLUS NUM

stack: exp PLUS (

pop stack until shifting “error” can result in correct parse


Local Bottom-up Error Recovery
exp : NUM () exps : exp ()
| exp PLUS exp () | exps ; exp ()
| ( exp ) ()

exp : ( error ) ()

exps : exp ()
| error ; exp ()

yet to read

input: NUM PLUS ( NUM PLUS @#$ PLUS NUM ) PLUS NUM

stack: exp PLUS ( error

shift “error”
Local Bottom-up Error Recovery
exp : NUM () exps : exp ()
| exp PLUS exp () | exps ; exp ()
| ( exp ) ()

exp : ( error ) ()

exps : exp ()
| error ; exp ()

yet to read

input: NUM PLUS ( NUM PLUS @#$ PLUS NUM ) PLUS NUM

stack: exp PLUS ( error

discard input until we can legally


shift or reduce
Local Bottom-up Error Recovery
exp : NUM () exps : exp ()
| exp PLUS exp () | exps ; exp ()
| ( exp ) ()

exp : ( error ) ()

exps : exp ()
| error ; exp ()

yet to read

input: NUM PLUS ( NUM PLUS @#$ PLUS NUM ) PLUS NUM

stack: exp PLUS ( error )

SHIFT )
Local Bottom-up Error Recovery
exp : NUM () exps : exp ()
| exp PLUS exp () | exps ; exp ()
| ( exp ) ()

exp : ( error ) ()

exps : exp ()
| error ; exp ()

yet to read

input: NUM PLUS ( NUM PLUS @#$ PLUS NUM ) PLUS NUM

stack: exp PLUS exp

REDUCE using exp ::= ( error )


Local Bottom-up Error Recovery
exp : NUM () exps : exp ()
| exp PLUS exp () | exps ; exp ()
| ( exp ) ()

exp : ( error ) ()

exps : exp ()
| error ; exp ()

yet to read

input: NUM PLUS ( NUM PLUS @#$ PLUS NUM ) PLUS NUM

stack: exp PLUS exp

continue parsing...

You might also like