Sunteți pe pagina 1din 2

5/11/2016

Bison1.25DebuggingYourParser

Gotothefirst,previous,next,lastsection,tableofcontents.

DebuggingYourParser
IfaBisongrammarcompilesproperlybutdoesn'tdowhatyouwantwhenitruns,theyydebugparser
tracefeaturecanhelpyoufigureoutwhy.
Toenablecompilationoftracefacilities,youmustdefinethemacroYYDEBUGwhenyoucompilethe
parser.Youcoulduse`DYYDEBUG=1'asacompileroptionoryoucouldput`#defineYYDEBUG1'intheC
declarationssectionofthegrammarfile(seesectionTheCDeclarationsSection).Alternatively,usethe
`t'optionwhenyourunBison(seesectionInvokingBison).WealwaysdefineYYDEBUGsothat
debuggingisalwayspossible.
Thetracefacilityusesstderr,soyoumustadd#include<stdio.h>totheCdeclarationssectionunlessit
isalreadythere.
Onceyouhavecompiledtheprogramwithtracefacilities,thewaytorequestatraceistostoreanonzero
valueinthevariableyydebug.YoucandothisbymakingtheCcodedoit(inmain,perhaps),oryoucan
alterthevaluewithaCdebugger.
Eachsteptakenbytheparserwhenyydebugisnonzeroproducesalineortwooftraceinformation,
writtenonstderr.Thetracemessagestellyouthesethings:
Eachtimetheparsercallsyylex,whatkindoftokenwasread.
Eachtimeatokenisshifted,thedepthandcompletecontentsofthestatestack(seesectionParser
States).
Eachtimearuleisreduced,whichruleitis,andthecompletecontentsofthestatestackafterward.
Tomakesenseofthisinformation,ithelpstorefertothelistingfileproducedbytheBison`v'option
(seesectionInvokingBison).Thisfileshowsthemeaningofeachstateintermsofpositionsinvarious
rules,andalsowhateachstatewilldowitheachpossibleinputtoken.Asyoureadthesuccessivetrace
messages,youcanseethattheparserisfunctioningaccordingtoitsspecificationinthelistingfile.
Eventuallyyouwillarriveattheplacewheresomethingundesirablehappens,andyouwillseewhich
partsofthegrammararetoblame.
TheparserfileisaCprogramandyoucanuseCdebuggersonit,butit'snoteasytointerpretwhatitis
doing.Theparserfunctionisafinitestatemachineinterpreter,andasidefromtheactionsitexecutesthe
samecodeoverandover.Onlythevaluesofvariablesshowwhereinthegrammaritisworking.
Thedebugginginformationnormallygivesthetokentypeofeachtokenread,butnotitssemanticvalue.
YoucanoptionallydefineamacronamedYYPRINTtoprovideawaytoprintthevalue.Ifyoudefine
YYPRINT,itshouldtakethreearguments.TheparserwillpassastandardI/Ostream,thenumericcodefor
thetokentype,andthetokenvalue(fromyylval).
HereisanexampleofYYPRINTsuitableforthemultifunctioncalculator(seesectionDeclarationsfor
mfcalc):
#defineYYPRINT(file,type,value)yyprint(file,type,value)
staticvoid
http://dinosaur.compilertools.net/bison/bison_11.html#SEC86

1/2

5/11/2016

Bison1.25DebuggingYourParser

yyprint(file,type,value)
FILE*file;
inttype;
YYSTYPEvalue;
{
if(type==VAR)
fprintf(file,"%s",value.tptr>name);
elseif(type==NUM)
fprintf(file,"%d",value.val);
}

Gotothefirst,previous,next,lastsection,tableofcontents.

http://dinosaur.compilertools.net/bison/bison_11.html#SEC86

2/2

S-ar putea să vă placă și