Error Recovery: First Previous Next Last Table of Contents
Error Recovery: First Previous Next Last Table of Contents
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