0% found this document useful (0 votes)
44 views2 pages

Error Recovery: First Previous Next Last Table of Contents

This document discusses error recovery in Bison parsers. It explains that parsers need to be able to recover from syntax errors rather than terminating. It describes using special error rules and tokens to allow the parser to continue after an error. These rules discard state and input to get the parser back on track. Strategies like skipping to the next semicolon or closing delimiter are useful for recovery. Error messages may be suppressed during recovery to avoid cascading errors.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
44 views2 pages

Error Recovery: First Previous Next Last Table of Contents

This document discusses error recovery in Bison parsers. It explains that parsers need to be able to recover from syntax errors rather than terminating. It describes using special error rules and tokens to allow the parser to continue after an error. These rules discard state and input to get the parser back on track. Strategies like skipping to the next semicolon or closing delimiter are useful for recovery. Error messages may be suppressed during recovery to avoid cascading errors.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

5/11/2016

Bison1.25ErrorRecovery

Gotothefirst,previous,next,lastsection,tableofcontents.

ErrorRecovery
Itisnotusuallyacceptabletohaveaprogramterminateonaparseerror.Forexample,acompilershould
recoversufficientlytoparsetherestoftheinputfileandcheckitforerrorsacalculatorshouldaccept
anotherexpression.
Inasimpleinteractivecommandparserwhereeachinputisoneline,itmaybesufficienttoallow
yyparsetoreturn1onerrorandhavethecallerignoretherestoftheinputlinewhenthathappens(and
thencallyyparseagain).Butthisisinadequateforacompiler,becauseitforgetsallthesyntacticcontext
leadinguptotheerror.Asyntaxerrordeepwithinafunctioninthecompilerinputshouldnotcausethe
compilertotreatthefollowinglinelikethebeginningofasourcefile.
Youcandefinehowtorecoverfromasyntaxerrorbywritingrulestorecognizethespecialtokenerror.
Thisisaterminalsymbolthatisalwaysdefined(youneednotdeclareit)andreservedforerrorhandling.
TheBisonparsergeneratesanerrortokenwheneverasyntaxerrorhappensifyouhaveprovidedarule
torecognizethistokeninthecurrentcontext,theparsecancontinue.
Forexample:
stmnts:/*emptystring*/
|stmnts'\n'
|stmntsexp'\n'
|stmntserror'\n'

Thefourthruleinthisexamplesaysthatanerrorfollowedbyanewlinemakesavalidadditiontoany
stmnts.
Whathappensifasyntaxerroroccursinthemiddleofanexp?Theerrorrecoveryrule,interpreted
strictly,appliestotheprecisesequenceofastmnts,anerrorandanewline.Ifanerroroccursinthe
middleofanexp,therewillprobablybesomeadditionaltokensandsubexpressionsonthestackafterthe
laststmnts,andtherewillbetokenstoreadbeforethenextnewline.Sotheruleisnotapplicableinthe
ordinaryway.
ButBisoncanforcethesituationtofittherule,bydiscardingpartofthesemanticcontextandpartofthe
input.Firstitdiscardsstatesandobjectsfromthestackuntilitgetsbacktoastateinwhichtheerror
tokenisacceptable.(Thismeansthatthesubexpressionsalreadyparsedarediscarded,backtothelast
completestmnts.)Atthispointtheerrortokencanbeshifted.Then,iftheoldlookaheadtokenisnot
acceptabletobeshiftednext,theparserreadstokensanddiscardsthemuntilitfindsatokenwhichis
acceptable.Inthisexample,Bisonreadsanddiscardsinputuntilthenextnewlinesothatthefourthrule
canapply.
Thechoiceoferrorrulesinthegrammarisachoiceofstrategiesforerrorrecovery.Asimpleanduseful
strategyissimplytoskiptherestofthecurrentinputlineorcurrentstatementifanerrorisdetected:
stmnt:error';'/*onerror,skipuntil';'isread*/

Itisalsousefultorecovertothematchingclosedelimiterofanopeningdelimiterthathasalreadybeen
parsed.Otherwisetheclosedelimiterwillprobablyappeartobeunmatched,andgenerateanother,
http://dinosaur.compilertools.net/bison/bison_9.html#SEC81

1/2

5/11/2016

Bison1.25ErrorRecovery

spuriouserrormessage:
primary:'('expr')'
|'('error')'
...
;

Errorrecoverystrategiesarenecessarilyguesses.Whentheyguesswrong,onesyntaxerroroftenleadsto
another.Intheaboveexample,theerrorrecoveryruleguessesthatanerrorisduetobadinputwithinone
stmnt.Supposethatinsteadaspurioussemicolonisinsertedinthemiddleofavalidstmnt.Aftertheerror
recoveryrulerecoversfromthefirsterror,anothersyntaxerrorwillbefoundstraightaway,sincethetext
followingthespurioussemicolonisalsoaninvalidstmnt.
Topreventanoutpouringoferrormessages,theparserwilloutputnoerrormessageforanothersyntax
errorthathappensshortlyafterthefirstonlyafterthreeconsecutiveinputtokenshavebeensuccessfully
shiftedwillerrormessagesresume.
Notethatruleswhichaccepttheerrortokenmayhaveactions,justasanyotherrulescan.
Youcanmakeerrormessagesresumeimmediatelybyusingthemacroyyerrokinanaction.Ifyoudo
thisintheerrorrule'saction,noerrormessageswillbesuppressed.Thismacrorequiresnoarguments
`yyerrok;'isavalidCstatement.
Thepreviouslookaheadtokenisreanalyzedimmediatelyafteranerror.Ifthisisunacceptable,thenthe
macroyyclearinmaybeusedtoclearthistoken.Writethestatement`yyclearin;'intheerrorrule's
action.
Forexample,supposethatonaparseerror,anerrorhandlingroutineiscalledthatadvancestheinput
streamtosomepointwhereparsingshouldonceagaincommence.Thenextsymbolreturnedbythe
lexicalscannerisprobablycorrect.Thepreviouslookaheadtokenoughttobediscardedwith
`yyclearin;'.
ThemacroYYRECOVERINGstandsforanexpressionthathasthevalue1whentheparserisrecoveringfrom
asyntaxerror,and0therestofthetime.Avalueof1indicatesthaterrormessagesarecurrently
suppressedfornewsyntaxerrors.
Gotothefirst,previous,next,lastsection,tableofcontents.

http://dinosaur.compilertools.net/bison/bison_9.html#SEC81

2/2

You might also like