bison.texinfo 203 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463
  1. \input texinfo @c -*-texinfo-*-
  2. @comment %**start of header
  3. @setfilename bison.info
  4. @settitle Bison A2.2
  5. @setchapternewpage odd
  6. @c SMALL BOOK version
  7. @c This edition has been formatted so that you can format and print it in
  8. @c the smallbook format.
  9. @c @smallbook
  10. @c next time, consider using @set for edition number, etc...
  11. @c Set following if you have the new `shorttitlepage' command
  12. @c @clear shorttitlepage-enabled
  13. @c @set shorttitlepage-enabled
  14. @c ISPELL CHECK: done, 14 Jan 1993 --bob
  15. @c Check COPYRIGHT dates. should be updated in the titlepage, ifinfo
  16. @c titlepage; should NOT be changed in the GPL. --mew
  17. @iftex
  18. @syncodeindex fn cp
  19. @syncodeindex vr cp
  20. @syncodeindex tp cp
  21. @end iftex
  22. @ifinfo
  23. @synindex fn cp
  24. @synindex vr cp
  25. @synindex tp cp
  26. @end ifinfo
  27. @comment %**end of header
  28. @ifinfo
  29. This file documents the Bison parser generator.
  30. Copyright (C) 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  31. Modified (1993) from bison-1.22 by
  32. Wilfred J. Hansen (wjh+@@cmu.edu),
  33. Andrew Consortium, Carnegie Mellon University
  34. CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD
  35. TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  36. AND FITNESS. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR
  37. ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  38. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  39. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  40. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  41. Permission is granted to make and distribute verbatim copies of
  42. this manual provided the copyright notice and this permission notice
  43. are preserved on all copies.
  44. @ignore
  45. Permission is granted to process this file through Tex and print the
  46. results, provided the printed document carries copying permission
  47. notice identical to this one except for the removal of this paragraph
  48. (this paragraph not being relevant to the printed manual).
  49. @end ignore
  50. Permission is granted to copy and distribute modified versions of this
  51. manual under the conditions for verbatim copying, provided also that the
  52. sections entitled ``GNU General Public License'' and ``Conditions for
  53. Using Bison'' are included exactly as in the original, and provided that
  54. the entire resulting derived work is distributed under the terms of a
  55. permission notice identical to this one.
  56. Permission is granted to copy and distribute translations of this manual
  57. into another language, under the above conditions for modified versions,
  58. except that the sections entitled ``GNU General Public License'',
  59. ``Conditions for Using Bison'' and this permission notice may be
  60. included in translations approved by the Free Software Foundation
  61. instead of in the original English.
  62. @end ifinfo
  63. @ifset shorttitlepage-enabled
  64. @shorttitlepage Bison
  65. @end ifset
  66. @titlepage
  67. @title Bison
  68. @subtitle The YACC-compatible Parser Generator
  69. @subtitle April 1993, Bison Version A2.2
  70. @author by Charles Donnelly and Richard Stallman
  71. Modified (1993) from bison-1.22 by
  72. Wilfred J. Hansen (wjh+@@cmu.edu),
  73. Andrew Consortium, Carnegie Mellon University
  74. @page
  75. @vskip 0pt plus 1filll
  76. Copyright @copyright{} 1988, 1989, 1990, 1991, 1992 Free Software
  77. Foundation
  78. @sp 2
  79. The version corresponding to Bison 1.22 is @*
  80. Published by the Free Software Foundation @*
  81. 675 Massachusetts Avenue @*
  82. Cambridge, MA 02139 USA @*
  83. Printed copies are available for $15 each.@*
  84. ISBN-1-882114-30-2
  85. Permission is granted to make and distribute verbatim copies of
  86. this manual provided the copyright notice and this permission notice
  87. are preserved on all copies.
  88. @ignore
  89. Permission is granted to process this file through TeX and print the
  90. results, provided the printed document carries copying permission
  91. notice identical to this one except for the removal of this paragraph
  92. (this paragraph not being relevant to the printed manual).
  93. @end ignore
  94. Permission is granted to copy and distribute modified versions of this
  95. manual under the conditions for verbatim copying, provided also that the
  96. sections entitled ``GNU General Public License'' and ``Conditions for
  97. Using Bison'' are included exactly as in the original, and provided that
  98. the entire resulting derived work is distributed under the terms of a
  99. permission notice identical to this one.
  100. Permission is granted to copy and distribute translations of this manual
  101. into another language, under the above conditions for modified versions,
  102. except that the sections entitled ``GNU General Public License'',
  103. ``Conditions for Using Bison'' and this permission notice may be
  104. included in translations approved by the Free Software Foundation
  105. instead of in the original English.
  106. @sp 2
  107. Cover art by Etienne Suvasa.
  108. @end titlepage
  109. @page
  110. @node Top, Introduction, (dir), (dir)
  111. @ifinfo
  112. This manual documents version A2.2 of Bison
  113. @end ifinfo
  114. @menu
  115. * Introduction::
  116. * Conditions::
  117. * Copying:: The GNU General Public License says
  118. how you can copy and share Bison
  119. Tutorial sections:
  120. * Concepts:: Basic concepts for understanding Bison.
  121. * Examples:: Three simple explained examples of using Bison.
  122. Reference sections:
  123. * Grammar File:: Writing Bison declarations and rules.
  124. * Interface:: C-language interface to the parser function @code{yyparse}.
  125. * Algorithm:: How the Bison parser works at run-time.
  126. * Error Recovery:: Writing rules for error recovery.
  127. * Context Dependency:: What to do if your language syntax is too
  128. messy for Bison to handle straightforwardly.
  129. * Debugging:: Debugging Bison parsers that parse wrong.
  130. * Invocation:: How to run Bison (to produce the parser source file).
  131. * Table of Symbols:: All the keywords of the Bison language are explained.
  132. * Parser Symbols:: Symbols used by the parser.
  133. * Glossary:: Basic concepts are explained.
  134. * Index:: Cross-references to the text.
  135. --- The Detailed Node Listing ---
  136. The Concepts of Bison
  137. * Language and Grammar:: Languages and context-free grammars,
  138. as mathematical ideas.
  139. * Grammar in Bison:: How we represent grammars for Bison's sake.
  140. * Semantic Values:: Each token or syntactic grouping can have
  141. a semantic value (the value of an integer,
  142. the name of an identifier, etc.).
  143. * Semantic Actions:: Each rule can have an action containing C code.
  144. * Bison Parser:: What are Bison's input and output,
  145. how is the output used?
  146. * Stages:: Stages in writing and running Bison grammars.
  147. * Grammar Layout:: Overall structure of a Bison grammar file.
  148. Examples
  149. * RPN Calc:: Reverse polish notation calculator;
  150. a first example with no operator precedence.
  151. * Infix Calc:: Infix (algebraic) notation calculator.
  152. Operator precedence is introduced.
  153. * Simple Error Recovery:: Continuing after syntax errors.
  154. * Multi-function Calc:: Calculator with memory and trig functions.
  155. It uses multiple data-types for semantic values.
  156. * Exercises:: Ideas for improving the multi-function calculator.
  157. Reverse Polish Notation Calculator
  158. * Decls: Rpcalc Decls. Bison and C declarations for rpcalc.
  159. * Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation.
  160. * Lexer: Rpcalc Lexer. The lexical analyzer.
  161. * Main: Rpcalc Main. The controlling function.
  162. * Error: Rpcalc Error. The error reporting function.
  163. * Gen: Rpcalc Gen. Running Bison on the grammar file.
  164. * Comp: Rpcalc Compile. Run the C compiler on the output code.
  165. Grammar Rules for @code{rpcalc}
  166. * Rpcalc Input::
  167. * Rpcalc Line::
  168. * Rpcalc Expr::
  169. Multi-Function Calculator: @code{mfcalc}
  170. * Decl: Mfcalc Decl. Bison declarations for multi-function calculator.
  171. * Rules: Mfcalc Rules. Grammar rules for the calculator.
  172. * Symtab: Mfcalc Symtab. Symbol table management subroutines.
  173. Bison Grammar Files
  174. * Grammar Outline:: Overall layout of the grammar file.
  175. * Symbols:: Terminal and nonterminal symbols.
  176. * Rules:: How to write grammar rules.
  177. * Recursion:: Writing recursive rules.
  178. * Semantics:: Semantic values and actions.
  179. * Declarations:: All kinds of Bison declarations are described here.
  180. * Multiple Parsers:: Putting more than one Bison parser in one program.
  181. Outline of a Bison Grammar
  182. * C Declarations:: Syntax and usage of the C declarations section.
  183. * Bison Declarations:: Syntax and usage of the Bison declarations section.
  184. * Grammar Rules:: Syntax and usage of the grammar rules section.
  185. * C Code:: Syntax and usage of the additional C code section.
  186. Defining Language Semantics
  187. * Value Type:: Specifying one data type for all semantic values.
  188. * Multiple Types:: Specifying several alternative data types.
  189. * Actions:: An action is the semantic definition of a grammar rule.
  190. * Action Types:: Specifying data types for actions to operate on.
  191. * Mid-Rule Actions:: Most actions go at the end of a rule.
  192. This says when, why and how to use the exceptional
  193. action in the middle of a rule.
  194. Bison Declarations
  195. * Token Decl:: Declaring terminal symbols.
  196. * Precedence Decl:: Declaring terminals with precedence and associativity.
  197. * Union Decl:: Declaring the set of all semantic value types.
  198. * Type Decl:: Declaring the choice of type for a nonterminal symbol.
  199. * Expect Decl:: Suppressing warnings about shift/reduce conflicts.
  200. * Start Decl:: Specifying the start symbol.
  201. * Pure Decl:: Requesting a reentrant parser.
  202. * Decl Summary:: Table of all Bison declarations.
  203. Parser C-Language Interface
  204. * Parser Function:: How to call @code{yyparse} and what it returns.
  205. * Lexical:: You must supply a function @code{yylex}
  206. which reads tokens.
  207. * Error Reporting:: You must supply a function @code{yyerror}.
  208. * Action Features:: Special features for use in actions.
  209. The Lexical Analyzer Function @code{yylex}
  210. * Calling Convention:: How @code{yyparse} calls @code{yylex}.
  211. * Token Values:: How @code{yylex} must return the semantic value
  212. of the token it has read.
  213. * Token Positions:: How @code{yylex} must return the text position
  214. (line number, etc.) of the token, if the
  215. actions want that.
  216. * Pure Calling:: How the calling convention differs
  217. in a pure parser (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}).
  218. The Bison Parser Algorithm
  219. * Look-Ahead:: Parser looks one token ahead when deciding what to do.
  220. * Shift/Reduce:: Conflicts: when either shifting or reduction is valid.
  221. * Precedence:: Operator precedence works by resolving conflicts.
  222. * Contextual Precedence:: When an operator's precedence depends on context.
  223. * Parser States:: The parser is a finite-state-machine with stack.
  224. * Reduce/Reduce:: When two rules are applicable in the same situation.
  225. * Mystery Conflicts:: Reduce/reduce conflicts that look unjustified.
  226. * Stack Overflow:: What happens when stack gets full. How to avoid it.
  227. Operator Precedence
  228. * Why Precedence:: An example showing why precedence is needed.
  229. * Using Precedence:: How to specify precedence in Bison grammars.
  230. * Precedence Examples:: How these features are used in the previous example.
  231. * How Precedence:: How they work.
  232. Handling Context Dependencies
  233. * Semantic Tokens:: Token parsing can depend on the semantic context.
  234. * Lexical Tie-ins:: Token parsing can depend on the syntactic context.
  235. * Tie-in Recovery:: Lexical tie-ins have implications for how
  236. error recovery rules must be written.
  237. Invoking Bison
  238. * Bison Options:: All the options described in detail,
  239. in alphabetical order by short options.
  240. * Option Cross Key:: Alphabetical list of long options.
  241. * VMS Invocation:: Bison command syntax on VMS.
  242. @end menu
  243. @node Introduction, Conditions, Top, Top
  244. @unnumbered Introduction
  245. @cindex introduction
  246. @dfn{Bison} is a general-purpose parser generator that converts a
  247. grammar description for an LALR(1) context-free grammar into a C
  248. program to parse that grammar. Once you are proficient with Bison,
  249. you may use it to develop a wide range of language parsers, from those
  250. used in simple desk calculators to complex programming languages.
  251. Bison is upward compatible with Yacc: all properly-written Yacc grammars
  252. ought to work with Bison with no change. Anyone familiar with Yacc
  253. should be able to use Bison with little trouble. You need to be fluent in
  254. C programming in order to use Bison or to understand this manual.
  255. We begin with tutorial chapters that explain the basic concepts of using
  256. Bison and show three explained examples, each building on the last. If you
  257. don't know Bison or Yacc, start by reading these chapters. Reference
  258. chapters follow which describe specific aspects of Bison in detail.
  259. Bison was written primarily by Robert Corbett; Richard Stallman made
  260. it Yacc-compatible.
  261. This edition corresponds to Bison version A2.2 as adapted from Bison-1.22
  262. by Wilfred J. Hansen, Andrew Consortium, Carnegie Mellon University. It
  263. differs from earlier versions in three major ways:
  264. @itemize @bullet
  265. @item
  266. Errors in the input grammar are not fatal; all errors are
  267. found in one pass. A complete example and test case is
  268. the file mess.y in the distribution.
  269. @item
  270. Tokens may now be specified as multiple-character strings:
  271. "<=" can appear where formerly would have to be LESSEQ.
  272. See the section @pxref{Symbols, ,Symbols - Terminal and Nonterminal}.
  273. @item
  274. The -n switch produces the parser tables without including
  275. the parser; a project can now use its own parser
  276. and avoid the GNU license for the resulting application.
  277. See the @samp{--no-parser} switch.
  278. @end itemize
  279. @node Conditions, Copying, Introduction, Top
  280. @unnumbered Conditions for Using Bison
  281. Unless you execute Bison with the @samp{-n} or @samp{--no-parser} switch,
  282. Bison grammars can be used only in programs that are free software. This
  283. is in contrast to what happens with the GNU C compiler and the other
  284. GNU programming tools.
  285. The reason Bison is special is that the output of the Bison utility---the
  286. Bison parser file---contains a verbatim copy of a sizable piece of Bison,
  287. which is the code for the @code{yyparse} function. (The actions from your
  288. grammar are inserted into this function at one point, but the rest of the
  289. function is not changed.)
  290. As a result, the Bison parser file is covered by the same copying
  291. conditions that cover Bison itself and the rest of the GNU system: any
  292. program containing it has to be distributed under the standard GNU copying
  293. conditions.
  294. Occasionally people who would like to use Bison to develop proprietary
  295. programs complain about this.
  296. We don't particularly sympathize with their complaints. The purpose of the
  297. GNU project is to promote the right to share software and the practice of
  298. sharing software; it is a means of changing society. The people who
  299. complain are planning to be uncooperative toward the rest of the world; why
  300. should they deserve our help in doing so?
  301. However, it's possible that a change in these conditions might encourage
  302. computer companies to use and distribute the GNU system. If so, then we
  303. might decide to change the terms on @code{yyparse} as a matter of the
  304. strategy of promoting the right to share. Such a change would be
  305. irrevocable. Since we stand by the copying permissions we have announced,
  306. we cannot withdraw them once given.
  307. We mustn't make an irrevocable change hastily. We have to wait until there
  308. is a complete GNU system and there has been time to learn how this issue
  309. affects its reception.
  310. In the meantime, those wishing to utilize bison without utilizing the bison
  311. parser can write their own parser and utilize only the tables generated
  312. from the @samp{-n} or @samp{--no-parser} switch.
  313. @node Copying, Concepts, Conditions, Top
  314. @unnumbered GNU GENERAL PUBLIC LICENSE
  315. @center Version 2, June 1991
  316. @display
  317. Copyright @copyright{} 1989, 1991 Free Software Foundation, Inc.
  318. 675 Mass Ave, Cambridge, MA 02139, USA
  319. Everyone is permitted to copy and distribute verbatim copies
  320. of this license document, but changing it is not allowed.
  321. @end display
  322. @unnumberedsec Preamble
  323. The licenses for most software are designed to take away your
  324. freedom to share and change it. By contrast, the GNU General Public
  325. License is intended to guarantee your freedom to share and change free
  326. software---to make sure the software is free for all its users. This
  327. General Public License applies to most of the Free Software
  328. Foundation's software and to any other program whose authors commit to
  329. using it. (Some other Free Software Foundation software is covered by
  330. the GNU Library General Public License instead.) You can apply it to
  331. your programs, too.
  332. When we speak of free software, we are referring to freedom, not
  333. price. Our General Public Licenses are designed to make sure that you
  334. have the freedom to distribute copies of free software (and charge for
  335. this service if you wish), that you receive source code or can get it
  336. if you want it, that you can change the software or use pieces of it
  337. in new free programs; and that you know you can do these things.
  338. To protect your rights, we need to make restrictions that forbid
  339. anyone to deny you these rights or to ask you to surrender the rights.
  340. These restrictions translate to certain responsibilities for you if you
  341. distribute copies of the software, or if you modify it.
  342. For example, if you distribute copies of such a program, whether
  343. gratis or for a fee, you must give the recipients all the rights that
  344. you have. You must make sure that they, too, receive or can get the
  345. source code. And you must show them these terms so they know their
  346. rights.
  347. We protect your rights with two steps: (1) copyright the software, and
  348. (2) offer you this license which gives you legal permission to copy,
  349. distribute and/or modify the software.
  350. Also, for each author's protection and ours, we want to make certain
  351. that everyone understands that there is no warranty for this free
  352. software. If the software is modified by someone else and passed on, we
  353. want its recipients to know that what they have is not the original, so
  354. that any problems introduced by others will not reflect on the original
  355. authors' reputations.
  356. Finally, any free program is threatened constantly by software
  357. patents. We wish to avoid the danger that redistributors of a free
  358. program will individually obtain patent licenses, in effect making the
  359. program proprietary. To prevent this, we have made it clear that any
  360. patent must be licensed for everyone's free use or not licensed at all.
  361. The precise terms and conditions for copying, distribution and
  362. modification follow.
  363. @iftex
  364. @unnumberedsec TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  365. @end iftex
  366. @ifinfo
  367. @center TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  368. @end ifinfo
  369. @enumerate 0
  370. @item
  371. This License applies to any program or other work which contains
  372. a notice placed by the copyright holder saying it may be distributed
  373. under the terms of this General Public License. The ``Program'', below,
  374. refers to any such program or work, and a ``work based on the Program''
  375. means either the Program or any derivative work under copyright law:
  376. that is to say, a work containing the Program or a portion of it,
  377. either verbatim or with modifications and/or translated into another
  378. language. (Hereinafter, translation is included without limitation in
  379. the term ``modification''.) Each licensee is addressed as ``you''.
  380. Activities other than copying, distribution and modification are not
  381. covered by this License; they are outside its scope. The act of
  382. running the Program is not restricted, and the output from the Program
  383. is covered only if its contents constitute a work based on the
  384. Program (independent of having been made by running the Program).
  385. Whether that is true depends on what the Program does.
  386. @item
  387. You may copy and distribute verbatim copies of the Program's
  388. source code as you receive it, in any medium, provided that you
  389. conspicuously and appropriately publish on each copy an appropriate
  390. copyright notice and disclaimer of warranty; keep intact all the
  391. notices that refer to this License and to the absence of any warranty;
  392. and give any other recipients of the Program a copy of this License
  393. along with the Program.
  394. You may charge a fee for the physical act of transferring a copy, and
  395. you may at your option offer warranty protection in exchange for a fee.
  396. @item
  397. You may modify your copy or copies of the Program or any portion
  398. of it, thus forming a work based on the Program, and copy and
  399. distribute such modifications or work under the terms of Section 1
  400. above, provided that you also meet all of these conditions:
  401. @enumerate a
  402. @item
  403. You must cause the modified files to carry prominent notices
  404. stating that you changed the files and the date of any change.
  405. @item
  406. You must cause any work that you distribute or publish, that in
  407. whole or in part contains or is derived from the Program or any
  408. part thereof, to be licensed as a whole at no charge to all third
  409. parties under the terms of this License.
  410. @item
  411. If the modified program normally reads commands interactively
  412. when run, you must cause it, when started running for such
  413. interactive use in the most ordinary way, to print or display an
  414. announcement including an appropriate copyright notice and a
  415. notice that there is no warranty (or else, saying that you provide
  416. a warranty) and that users may redistribute the program under
  417. these conditions, and telling the user how to view a copy of this
  418. License. (Exception: if the Program itself is interactive but
  419. does not normally print such an announcement, your work based on
  420. the Program is not required to print an announcement.)
  421. @end enumerate
  422. These requirements apply to the modified work as a whole. If
  423. identifiable sections of that work are not derived from the Program,
  424. and can be reasonably considered independent and separate works in
  425. themselves, then this License, and its terms, do not apply to those
  426. sections when you distribute them as separate works. But when you
  427. distribute the same sections as part of a whole which is a work based
  428. on the Program, the distribution of the whole must be on the terms of
  429. this License, whose permissions for other licensees extend to the
  430. entire whole, and thus to each and every part regardless of who wrote it.
  431. Thus, it is not the intent of this section to claim rights or contest
  432. your rights to work written entirely by you; rather, the intent is to
  433. exercise the right to control the distribution of derivative or
  434. collective works based on the Program.
  435. In addition, mere aggregation of another work not based on the Program
  436. with the Program (or with a work based on the Program) on a volume of
  437. a storage or distribution medium does not bring the other work under
  438. the scope of this License.
  439. @item
  440. You may copy and distribute the Program (or a work based on it,
  441. under Section 2) in object code or executable form under the terms of
  442. Sections 1 and 2 above provided that you also do one of the following:
  443. @enumerate a
  444. @item
  445. Accompany it with the complete corresponding machine-readable
  446. source code, which must be distributed under the terms of Sections
  447. 1 and 2 above on a medium customarily used for software interchange; or,
  448. @item
  449. Accompany it with a written offer, valid for at least three
  450. years, to give any third party, for a charge no more than your
  451. cost of physically performing source distribution, a complete
  452. machine-readable copy of the corresponding source code, to be
  453. distributed under the terms of Sections 1 and 2 above on a medium
  454. customarily used for software interchange; or,
  455. @item
  456. Accompany it with the information you received as to the offer
  457. to distribute corresponding source code. (This alternative is
  458. allowed only for noncommercial distribution and only if you
  459. received the program in object code or executable form with such
  460. an offer, in accord with Subsection b above.)
  461. @end enumerate
  462. The source code for a work means the preferred form of the work for
  463. making modifications to it. For an executable work, complete source
  464. code means all the source code for all modules it contains, plus any
  465. associated interface definition files, plus the scripts used to
  466. control compilation and installation of the executable. However, as a
  467. special exception, the source code distributed need not include
  468. anything that is normally distributed (in either source or binary
  469. form) with the major components (compiler, kernel, and so on) of the
  470. operating system on which the executable runs, unless that component
  471. itself accompanies the executable.
  472. If distribution of executable or object code is made by offering
  473. access to copy from a designated place, then offering equivalent
  474. access to copy the source code from the same place counts as
  475. distribution of the source code, even though third parties are not
  476. compelled to copy the source along with the object code.
  477. @item
  478. You may not copy, modify, sublicense, or distribute the Program
  479. except as expressly provided under this License. Any attempt
  480. otherwise to copy, modify, sublicense or distribute the Program is
  481. void, and will automatically terminate your rights under this License.
  482. However, parties who have received copies, or rights, from you under
  483. this License will not have their licenses terminated so long as such
  484. parties remain in full compliance.
  485. @item
  486. You are not required to accept this License, since you have not
  487. signed it. However, nothing else grants you permission to modify or
  488. distribute the Program or its derivative works. These actions are
  489. prohibited by law if you do not accept this License. Therefore, by
  490. modifying or distributing the Program (or any work based on the
  491. Program), you indicate your acceptance of this License to do so, and
  492. all its terms and conditions for copying, distributing or modifying
  493. the Program or works based on it.
  494. @item
  495. Each time you redistribute the Program (or any work based on the
  496. Program), the recipient automatically receives a license from the
  497. original licensor to copy, distribute or modify the Program subject to
  498. these terms and conditions. You may not impose any further
  499. restrictions on the recipients' exercise of the rights granted herein.
  500. You are not responsible for enforcing compliance by third parties to
  501. this License.
  502. @item
  503. If, as a consequence of a court judgment or allegation of patent
  504. infringement or for any other reason (not limited to patent issues),
  505. conditions are imposed on you (whether by court order, agreement or
  506. otherwise) that contradict the conditions of this License, they do not
  507. excuse you from the conditions of this License. If you cannot
  508. distribute so as to satisfy simultaneously your obligations under this
  509. License and any other pertinent obligations, then as a consequence you
  510. may not distribute the Program at all. For example, if a patent
  511. license would not permit royalty-free redistribution of the Program by
  512. all those who receive copies directly or indirectly through you, then
  513. the only way you could satisfy both it and this License would be to
  514. refrain entirely from distribution of the Program.
  515. If any portion of this section is held invalid or unenforceable under
  516. any particular circumstance, the balance of the section is intended to
  517. apply and the section as a whole is intended to apply in other
  518. circumstances.
  519. It is not the purpose of this section to induce you to infringe any
  520. patents or other property right claims or to contest validity of any
  521. such claims; this section has the sole purpose of protecting the
  522. integrity of the free software distribution system, which is
  523. implemented by public license practices. Many people have made
  524. generous contributions to the wide range of software distributed
  525. through that system in reliance on consistent application of that
  526. system; it is up to the author/donor to decide if he or she is willing
  527. to distribute software through any other system and a licensee cannot
  528. impose that choice.
  529. This section is intended to make thoroughly clear what is believed to
  530. be a consequence of the rest of this License.
  531. @item
  532. If the distribution and/or use of the Program is restricted in
  533. certain countries either by patents or by copyrighted interfaces, the
  534. original copyright holder who places the Program under this License
  535. may add an explicit geographical distribution limitation excluding
  536. those countries, so that distribution is permitted only in or among
  537. countries not thus excluded. In such case, this License incorporates
  538. the limitation as if written in the body of this License.
  539. @item
  540. The Free Software Foundation may publish revised and/or new versions
  541. of the General Public License from time to time. Such new versions will
  542. be similar in spirit to the present version, but may differ in detail to
  543. address new problems or concerns.
  544. Each version is given a distinguishing version number. If the Program
  545. specifies a version number of this License which applies to it and ``any
  546. later version'', you have the option of following the terms and conditions
  547. either of that version or of any later version published by the Free
  548. Software Foundation. If the Program does not specify a version number of
  549. this License, you may choose any version ever published by the Free Software
  550. Foundation.
  551. @item
  552. If you wish to incorporate parts of the Program into other free
  553. programs whose distribution conditions are different, write to the author
  554. to ask for permission. For software which is copyrighted by the Free
  555. Software Foundation, write to the Free Software Foundation; we sometimes
  556. make exceptions for this. Our decision will be guided by the two goals
  557. of preserving the free status of all derivatives of our free software and
  558. of promoting the sharing and reuse of software generally.
  559. @iftex
  560. @heading NO WARRANTY
  561. @end iftex
  562. @ifinfo
  563. @center NO WARRANTY
  564. @end ifinfo
  565. @item
  566. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
  567. FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
  568. OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
  569. PROVIDE THE PROGRAM ``AS IS'' WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
  570. OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  571. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
  572. TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
  573. PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
  574. REPAIR OR CORRECTION.
  575. @item
  576. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  577. WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
  578. REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
  579. INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
  580. OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
  581. TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
  582. YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
  583. PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
  584. POSSIBILITY OF SUCH DAMAGES.
  585. @end enumerate
  586. @iftex
  587. @heading END OF TERMS AND CONDITIONS
  588. @end iftex
  589. @ifinfo
  590. @center END OF TERMS AND CONDITIONS
  591. @end ifinfo
  592. @page
  593. @unnumberedsec How to Apply These Terms to Your New Programs
  594. If you develop a new program, and you want it to be of the greatest
  595. possible use to the public, the best way to achieve this is to make it
  596. free software which everyone can redistribute and change under these terms.
  597. To do so, attach the following notices to the program. It is safest
  598. to attach them to the start of each source file to most effectively
  599. convey the exclusion of warranty; and each file should have at least
  600. the ``copyright'' line and a pointer to where the full notice is found.
  601. @smallexample
  602. @var{one line to give the program's name and a brief idea of what it does.}
  603. Copyright (C) 19@var{yy} @var{name of author}
  604. This program is free software; you can redistribute it and/or modify
  605. it under the terms of the GNU General Public License as published by
  606. the Free Software Foundation; either version 2 of the License, or
  607. (at your option) any later version.
  608. This program is distributed in the hope that it will be useful,
  609. but WITHOUT ANY WARRANTY; without even the implied warranty of
  610. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  611. GNU General Public License for more details.
  612. You should have received a copy of the GNU General Public License
  613. along with this program; if not, write to the Free Software
  614. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  615. @end smallexample
  616. Also add information on how to contact you by electronic and paper mail.
  617. If the program is interactive, make it output a short notice like this
  618. when it starts in an interactive mode:
  619. @smallexample
  620. Gnomovision version 69, Copyright (C) 19@var{yy} @var{name of author}
  621. Gnomovision comes with ABSOLUTELY NO WARRANTY; for details
  622. type `show w'.
  623. This is free software, and you are welcome to redistribute it
  624. under certain conditions; type `show c' for details.
  625. @end smallexample
  626. The hypothetical commands @samp{show w} and @samp{show c} should show
  627. the appropriate parts of the General Public License. Of course, the
  628. commands you use may be called something other than @samp{show w} and
  629. @samp{show c}; they could even be mouse-clicks or menu items---whatever
  630. suits your program.
  631. You should also get your employer (if you work as a programmer) or your
  632. school, if any, to sign a ``copyright disclaimer'' for the program, if
  633. necessary. Here is a sample; alter the names:
  634. @smallexample
  635. Yoyodyne, Inc., hereby disclaims all copyright interest in the program
  636. `Gnomovision' (which makes passes at compilers) written by James Hacker.
  637. @var{signature of Ty Coon}, 1 April 1989
  638. Ty Coon, President of Vice
  639. @end smallexample
  640. This General Public License does not permit incorporating your program into
  641. proprietary programs. If your program is a subroutine library, you may
  642. consider it more useful to permit linking proprietary applications with the
  643. library. If this is what you want to do, use the GNU Library General
  644. Public License instead of this License.
  645. @node Concepts, Examples, Copying, Top
  646. @chapter The Concepts of Bison
  647. This chapter introduces many of the basic concepts without which the
  648. details of Bison will not make sense. If you do not already know how to
  649. use Bison or Yacc, we suggest you start by reading this chapter carefully.
  650. @menu
  651. * Language and Grammar:: Languages and context-free grammars,
  652. as mathematical ideas.
  653. * Grammar in Bison:: How we represent grammars for Bison's sake.
  654. * Semantic Values:: Each token or syntactic grouping can have
  655. a semantic value (the value of an integer,
  656. the name of an identifier, etc.).
  657. * Semantic Actions:: Each rule can have an action containing C code.
  658. * Bison Parser:: What are Bison's input and output,
  659. how is the output used?
  660. * Stages:: Stages in writing and running Bison grammars.
  661. * Grammar Layout:: Overall structure of a Bison grammar file.
  662. @end menu
  663. @node Language and Grammar, Grammar in Bison, , Concepts
  664. @section Languages and Context-Free Grammars
  665. @c !!! ``An expression can be an integer'' is not a valid Bison
  666. @c expression---Bison cannot read English! --rjc 6 Feb 1992
  667. @cindex context-free grammar
  668. @cindex grammar, context-free
  669. In order for Bison to parse a language, it must be described by a
  670. @dfn{context-free grammar}. This means that you specify one or more
  671. @dfn{syntactic groupings} and give rules for constructing them from their
  672. parts. For example, in the C language, one kind of grouping is called an
  673. `expression'. One rule for making an expression might be, ``An expression
  674. can be made of a minus sign and another expression''. Another would be,
  675. ``An expression can be an integer''. As you can see, rules are often
  676. recursive, but there must be at least one rule which leads out of the
  677. recursion.
  678. @cindex BNF
  679. @cindex Backus-Naur form
  680. The most common formal system for presenting such rules for humans to read
  681. is @dfn{Backus-Naur Form} or ``BNF'', which was developed in order to
  682. specify the language Algol 60. Any grammar expressed in BNF is a
  683. context-free grammar. The input to Bison is essentially machine-readable
  684. BNF.
  685. Not all context-free languages can be handled by Bison, only those
  686. that are LALR(1). In brief, this means that it must be possible to
  687. tell how to parse any portion of an input string with just a single
  688. token of look-ahead. Strictly speaking, that is a description of an
  689. LR(1) grammar, and LALR(1) involves additional restrictions that are
  690. hard to explain simply; but it is rare in actual practice to find an
  691. LR(1) grammar that fails to be LALR(1). @xref{Mystery Conflicts, ,
  692. Mysterious Reduce/Reduce Conflicts}, for more information on this.
  693. @cindex symbols (abstract)
  694. @cindex token
  695. @cindex syntactic grouping
  696. @cindex grouping, syntactic
  697. In the formal grammatical rules for a language, each kind of syntactic unit
  698. or grouping is named by a @dfn{symbol}. Those which are built by grouping
  699. smaller constructs according to grammatical rules are called
  700. @dfn{nonterminal symbols}; those which can't be subdivided are called
  701. @dfn{terminal symbols} or @dfn{token types}. We call a piece of input
  702. corresponding to a single terminal symbol a @dfn{token}, and a piece
  703. corresponding to a single nonterminal symbol a @dfn{grouping}.@refill
  704. We can use the C language as an example of what symbols, terminal and
  705. nonterminal, mean. The tokens of C are identifiers, constants (numeric and
  706. string), and the various keywords, arithmetic operators and punctuation
  707. marks. So the terminal symbols of a grammar for C include `identifier',
  708. `number', `string', plus one symbol for each keyword, operator or
  709. punctuation mark: `if', `return', `const', `static', `int', `char',
  710. `plus-sign', `open-brace', `close-brace', `comma' and many more. (These
  711. tokens can be subdivided into characters, but that is a matter of
  712. lexicography, not grammar.)
  713. Here is a simple C function subdivided into tokens:
  714. @example
  715. int /* @r{keyword `int'} */
  716. square (x) /* @r{identifier, open-paren,} */
  717. /* @r{identifier, close-paren} */
  718. int x; /* @r{keyword `int', identifier, semicolon} */
  719. @{ /* @r{open-brace} */
  720. return x * x; /* @r{keyword `return', identifier,} */
  721. /* @r{asterisk, identifier, semicolon} */
  722. @} /* @r{close-brace} */
  723. @end example
  724. The syntactic groupings of C include the expression, the statement, the
  725. declaration, and the function definition. These are represented in the
  726. grammar of C by nonterminal symbols `expression', `statement',
  727. `declaration' and `function definition'. The full grammar uses dozens of
  728. additional language constructs, each with its own nonterminal symbol, in
  729. order to express the meanings of these four. The example above is a
  730. function definition; it contains one declaration, and one statement. In
  731. the statement, each @samp{x} is an expression and so is @samp{x * x}.
  732. Each nonterminal symbol must have grammatical rules showing how it is made
  733. out of simpler constructs. For example, one kind of C statement is the
  734. @code{return} statement; this would be described with a grammar rule which
  735. reads informally as follows:
  736. @quotation
  737. A `statement' can be made of a `return' keyword, an `expression' and a
  738. `semicolon'.
  739. @end quotation
  740. @noindent
  741. There would be many other rules for `statement', one for each kind of
  742. statement in C.
  743. @cindex start symbol
  744. One nonterminal symbol must be distinguished as the special one which
  745. defines a complete utterance in the language. It is called the @dfn{start
  746. symbol}. In a compiler, this means a complete input program. In the C
  747. language, the nonterminal symbol `sequence of definitions and declarations'
  748. plays this role.
  749. For example, @samp{1 + 2} is a valid C expression---a valid part of a C
  750. program---but it is not valid as an @emph{entire} C program. In the
  751. context-free grammar of C, this follows from the fact that `expression' is
  752. not the start symbol.
  753. The Bison parser reads a sequence of tokens as its input, and groups the
  754. tokens using the grammar rules. If the input is valid, the end result is
  755. that the entire token sequence reduces to a single grouping whose symbol is
  756. the grammar's start symbol. If we use a grammar for C, the entire input
  757. must be a `sequence of definitions and declarations'. If not, the parser
  758. reports a syntax error.
  759. @node Grammar in Bison, Semantic Values, Language and Grammar, Concepts
  760. @section From Formal Rules to Bison Input
  761. @cindex Bison grammar
  762. @cindex grammar, Bison
  763. @cindex formal grammar
  764. A formal grammar is a mathematical construct. To define the language
  765. for Bison, you must write a file expressing the grammar in Bison syntax:
  766. a @dfn{Bison grammar} file. @xref{Grammar File, ,Bison Grammar Files}.
  767. A nonterminal symbol in the formal grammar is represented in Bison input
  768. as an identifier, like an identifier in C. By convention, it should be
  769. in lower case, such as @code{expr}, @code{stmt} or @code{declaration}.
  770. The Bison representation for a terminal symbol is also called a @dfn{token
  771. type}. Token types as well can be represented as C-like identifiers. By
  772. convention, these identifiers should be upper case to distinguish them from
  773. nonterminals: for example, @code{INTEGER}, @code{IDENTIFIER}, @code{IF} or
  774. @code{RETURN}. A terminal symbol that stands for a particular keyword in
  775. the language should be named after that keyword converted to upper case.
  776. The terminal symbol @code{error} is reserved for error recovery.
  777. @xref{Symbols, ,Symbols - Terminal and Nonterminal}.@refill
  778. A terminal symbol can also be represented as a character literal, just like
  779. a C character constant. You should do this whenever a token is just a
  780. single character (parenthesis, plus-sign, etc.): use that same character in
  781. a literal as the terminal symbol for that token.
  782. (See @pxref{Symbols, ,Symbols - Terminal and Nonterminal} for
  783. multiple character literal symbols, also called string tokens.)
  784. The grammar rules also have an expression in Bison syntax. For example,
  785. here is the Bison rule for a C @code{return} statement. The semicolon in
  786. quotes is a literal character token, representing part of the C syntax for
  787. the statement; the naked semicolon, and the colon, are Bison punctuation
  788. used in every rule.
  789. @example
  790. stmt: RETURN expr ';'
  791. ;
  792. @end example
  793. @noindent
  794. @xref{Rules, ,Syntax of Grammar Rules}.
  795. @node Semantic Values, Semantic Actions, Grammar in Bison, Concepts
  796. @section Semantic Values
  797. @cindex semantic value
  798. @cindex value, semantic
  799. A formal grammar selects tokens only by their classifications: for example,
  800. if a rule mentions the terminal symbol `integer constant', it means that
  801. @emph{any} integer constant is grammatically valid in that position. The
  802. precise value of the constant is irrelevant to how to parse the input: if
  803. @samp{x+4} is grammatical then @samp{x+1} or @samp{x+3989} is equally
  804. grammatical.@refill
  805. But the precise value is very important for what the input means once it is
  806. parsed. A compiler is useless if it fails to distinguish between 4, 1 and
  807. 3989 as constants in the program! Therefore, each token in a Bison grammar
  808. has both a token type and a @dfn{semantic value}. @xref{Semantics, ,Defining Language Semantics},
  809. for details.
  810. The token type is a terminal symbol defined in the grammar, such as
  811. @code{INTEGER}, @code{IDENTIFIER} or @code{','}. It tells everything
  812. you need to know to decide where the token may validly appear and how to
  813. group it with other tokens. The grammar rules know nothing about tokens
  814. except their types.@refill
  815. The semantic value has all the rest of the information about the
  816. meaning of the token, such as the value of an integer, or the name of an
  817. identifier. (A token such as @code{','} which is just punctuation doesn't
  818. need to have any semantic value.)
  819. For example, an input token might be classified as token type
  820. @code{INTEGER} and have the semantic value 4. Another input token might
  821. have the same token type @code{INTEGER} but value 3989. When a grammar
  822. rule says that @code{INTEGER} is allowed, either of these tokens is
  823. acceptable because each is an @code{INTEGER}. When the parser accepts the
  824. token, it keeps track of the token's semantic value.
  825. Each grouping can also have a semantic value as well as its nonterminal
  826. symbol. For example, in a calculator, an expression typically has a
  827. semantic value that is a number. In a compiler for a programming
  828. language, an expression typically has a semantic value that is a tree
  829. structure describing the meaning of the expression.
  830. @node Semantic Actions, Bison Parser, Semantic Values, Concepts
  831. @section Semantic Actions
  832. @cindex semantic actions
  833. @cindex actions, semantic
  834. In order to be useful, a program must do more than parse input; it must
  835. also produce some output based on the input. In a Bison grammar, a grammar
  836. rule can have an @dfn{action} made up of C statements. Each time the
  837. parser recognizes a match for that rule, the action is executed.
  838. @xref{Actions}.
  839. Most of the time, the purpose of an action is to compute the semantic value
  840. of the whole construct from the semantic values of its parts. For example,
  841. suppose we have a rule which says an expression can be the sum of two
  842. expressions. When the parser recognizes such a sum, each of the
  843. subexpressions has a semantic value which describes how it was built up.
  844. The action for this rule should create a similar sort of value for the
  845. newly recognized larger expression.
  846. For example, here is a rule that says an expression can be the sum of
  847. two subexpressions:
  848. @example
  849. expr: expr '+' expr @{ $$ = $1 + $3; @}
  850. ;
  851. @end example
  852. @noindent
  853. The action says how to produce the semantic value of the sum expression
  854. from the values of the two subexpressions.
  855. @node Bison Parser, Stages, Semantic Actions, Concepts
  856. @section Bison Output: the Parser File
  857. @cindex Bison parser
  858. @cindex Bison utility
  859. @cindex lexical analyzer, purpose
  860. @cindex parser
  861. When you run Bison, you give it a Bison grammar file as input. The output
  862. is a C source file that parses the language described by the grammar.
  863. This file is called a @dfn{Bison parser}. Keep in mind that the Bison
  864. utility and the Bison parser are two distinct programs: the Bison utility
  865. is a program whose output is the Bison parser that becomes part of your
  866. program.
  867. The job of the Bison parser is to group tokens into groupings according to
  868. the grammar rules---for example, to build identifiers and operators into
  869. expressions. As it does this, it runs the actions for the grammar rules it
  870. uses.
  871. The tokens come from a function called the @dfn{lexical analyzer} that you
  872. must supply in some fashion (such as by writing it in C). The Bison parser
  873. calls the lexical analyzer each time it wants a new token. It doesn't know
  874. what is ``inside'' the tokens (though their semantic values may reflect
  875. this). Typically the lexical analyzer makes the tokens by parsing
  876. characters of text, but Bison does not depend on this. @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}.
  877. The Bison parser file is C code which defines a function named
  878. @code{yyparse} which implements that grammar. This function does not make
  879. a complete C program: you must supply some additional functions. One is
  880. the lexical analyzer. Another is an error-reporting function which the
  881. parser calls to report an error. In addition, a complete C program must
  882. start with a function called @code{main}; you have to provide this, and
  883. arrange for it to call @code{yyparse} or the parser will never run.
  884. @xref{Interface, ,Parser C-Language Interface}.
  885. Aside from the token type names and the symbols in the actions you
  886. write, all variable and function names used in the Bison parser file
  887. begin with @samp{yy} or @samp{YY}. This includes interface functions
  888. such as the lexical analyzer function @code{yylex}, the error reporting
  889. function @code{yyerror} and the parser function @code{yyparse} itself.
  890. This also includes numerous identifiers used for internal purposes.
  891. Therefore, you should avoid using C identifiers starting with @samp{yy}
  892. or @samp{YY} in the Bison grammar file except for the ones defined in
  893. this manual.
  894. @node Stages, Grammar Layout, Bison Parser, Concepts
  895. @section Stages in Using Bison
  896. @cindex stages in using Bison
  897. @cindex using Bison
  898. The actual language-design process using Bison, from grammar specification
  899. to a working compiler or interpreter, has these parts:
  900. @enumerate
  901. @item
  902. Formally specify the grammar in a form recognized by Bison
  903. (@pxref{Grammar File, ,Bison Grammar Files}). For each grammatical rule in the language,
  904. describe the action that is to be taken when an instance of that rule
  905. is recognized. The action is described by a sequence of C statements.
  906. @item
  907. Write a lexical analyzer to process input and pass tokens to the
  908. parser. The lexical analyzer may be written by hand in C
  909. (@pxref{Lexical, ,The Lexical Analyzer Function @code{yylex}}). It could also be produced using Lex, but the use
  910. of Lex is not discussed in this manual.
  911. @item
  912. Write a controlling function that calls the Bison-produced parser.
  913. @item
  914. Write error-reporting routines.
  915. @end enumerate
  916. To turn this source code as written into a runnable program, you
  917. must follow these steps:
  918. @enumerate
  919. @item
  920. Run Bison on the grammar to produce the parser.
  921. @item
  922. Compile the code output by Bison, as well as any other source files.
  923. @item
  924. Link the object files to produce the finished product.
  925. @end enumerate
  926. @node Grammar Layout, , Stages, Concepts
  927. @section The Overall Layout of a Bison Grammar
  928. @cindex grammar file
  929. @cindex file format
  930. @cindex format of grammar file
  931. @cindex layout of Bison grammar
  932. The input file for the Bison utility is a @dfn{Bison grammar file}. The
  933. general form of a Bison grammar file is as follows:
  934. @example
  935. %@{
  936. @var{C declarations}
  937. %@}
  938. @var{Bison declarations}
  939. %%
  940. @var{Grammar rules}
  941. %%
  942. @var{Additional C code}
  943. @end example
  944. @noindent
  945. The @samp{%%}, @samp{%@{} and @samp{%@}} are punctuation that appears
  946. in every Bison grammar file to separate the sections.
  947. The C declarations may define types and variables used in the actions.
  948. You can also use preprocessor commands to define macros used there, and use
  949. @code{#include} to include header files that do any of these things.
  950. The Bison declarations declare the names of the terminal and nonterminal
  951. symbols, and may also describe operator precedence and the data types of
  952. semantic values of various symbols.
  953. The grammar rules define how to construct each nonterminal symbol from its
  954. parts.
  955. The additional C code can contain any C code you want to use. Often the
  956. definition of the lexical analyzer @code{yylex} goes here, plus subroutines
  957. called by the actions in the grammar rules. In a simple program, all the
  958. rest of the program can go here.
  959. @node Examples, Grammar File, Concepts, Top
  960. @chapter Examples
  961. @cindex simple examples
  962. @cindex examples, simple
  963. Now we show and explain three sample programs written using Bison: a
  964. reverse polish notation calculator, an algebraic (infix) notation
  965. calculator, and a multi-function calculator. All three have been tested
  966. under BSD Unix 4.3; each produces a usable, though limited, interactive
  967. desk-top calculator.
  968. These examples are simple, but Bison grammars for real programming
  969. languages are written the same way.
  970. @ifinfo
  971. You can copy these examples out of the Info file and into a source file
  972. to try them.
  973. @end ifinfo
  974. @menu
  975. * RPN Calc:: Reverse polish notation calculator;
  976. a first example with no operator precedence.
  977. * Infix Calc:: Infix (algebraic) notation calculator.
  978. Operator precedence is introduced.
  979. * Simple Error Recovery:: Continuing after syntax errors.
  980. * Multi-function Calc:: Calculator with memory and trig functions.
  981. It uses multiple data-types for semantic values.
  982. * Exercises:: Ideas for improving the multi-function calculator.
  983. @end menu
  984. @node RPN Calc, Infix Calc, , Examples
  985. @section Reverse Polish Notation Calculator
  986. @cindex reverse polish notation
  987. @cindex polish notation calculator
  988. @cindex @code{rpcalc}
  989. @cindex calculator, simple
  990. The first example is that of a simple double-precision @dfn{reverse polish
  991. notation} calculator (a calculator using postfix operators). This example
  992. provides a good starting point, since operator precedence is not an issue.
  993. The second example will illustrate how operator precedence is handled.
  994. The source code for this calculator is named @file{rpcalc.y}. The
  995. @samp{.y} extension is a convention used for Bison input files.
  996. @menu
  997. * Decls: Rpcalc Decls. Bison and C declarations for rpcalc.
  998. * Rules: Rpcalc Rules. Grammar Rules for rpcalc, with explanation.
  999. * Lexer: Rpcalc Lexer. The lexical analyzer.
  1000. * Main: Rpcalc Main. The controlling function.
  1001. * Error: Rpcalc Error. The error reporting function.
  1002. * Gen: Rpcalc Gen. Running Bison on the grammar file.
  1003. * Comp: Rpcalc Compile. Run the C compiler on the output code.
  1004. @end menu
  1005. @node Rpcalc Decls, Rpcalc Rules, , RPN Calc
  1006. @subsection Declarations for @code{rpcalc}
  1007. Here are the C and Bison declarations for the reverse polish notation
  1008. calculator. As in C, comments are placed between @samp{/*@dots{}*/}.
  1009. @example
  1010. /* Reverse polish notation calculator. */
  1011. %@{
  1012. #define YYSTYPE double
  1013. #include <math.h>
  1014. %@}
  1015. %token NUM
  1016. %% /* Grammar rules and actions follow */
  1017. @end example
  1018. The C declarations section (@pxref{C Declarations, ,The C Declarations Section}) contains two
  1019. preprocessor directives.
  1020. The @code{#define} directive defines the macro @code{YYSTYPE}, thus
  1021. specifying the C data type for semantic values of both tokens and groupings
  1022. (@pxref{Value Type, ,Data Types of Semantic Values}). The Bison parser will use whatever type
  1023. @code{YYSTYPE} is defined as; if you don't define it, @code{int} is the
  1024. default. Because we specify @code{double}, each token and each expression
  1025. has an associated value, which is a floating point number.
  1026. The @code{#include} directive is used to declare the exponentiation
  1027. function @code{pow}.
  1028. The second section, Bison declarations, provides information to Bison about
  1029. the token types (@pxref{Bison Declarations, ,The Bison Declarations Section}). Each terminal symbol that is
  1030. not a single-character literal must be declared here. (Single-character
  1031. literals normally don't need to be declared.) In this example, all the
  1032. arithmetic operators are designated by single-character literals, so the
  1033. only terminal symbol that needs to be declared is @code{NUM}, the token
  1034. type for numeric constants.
  1035. @node Rpcalc Rules, Rpcalc Lexer, Rpcalc Decls, RPN Calc
  1036. @subsection Grammar Rules for @code{rpcalc}
  1037. Here are the grammar rules for the reverse polish notation calculator.
  1038. @example
  1039. input: /* empty */
  1040. | input line
  1041. ;
  1042. line: '\n'
  1043. | exp '\n' @{ printf ("\t%.10g\n", $1); @}
  1044. ;
  1045. exp: NUM @{ $$ = $1; @}
  1046. | exp exp '+' @{ $$ = $1 + $2; @}
  1047. | exp exp '-' @{ $$ = $1 - $2; @}
  1048. | exp exp '*' @{ $$ = $1 * $2; @}
  1049. | exp exp '/' @{ $$ = $1 / $2; @}
  1050. /* Exponentiation */
  1051. | exp exp '^' @{ $$ = pow ($1, $2); @}
  1052. /* Unary minus */
  1053. | exp 'n' @{ $$ = -$1; @}
  1054. ;
  1055. %%
  1056. @end example
  1057. The groupings of the rpcalc ``language'' defined here are the expression
  1058. (given the name @code{exp}), the line of input (@code{line}), and the
  1059. complete input transcript (@code{input}). Each of these nonterminal
  1060. symbols has several alternate rules, joined by the @samp{|} punctuator
  1061. which is read as ``or''. The following sections explain what these rules
  1062. mean.
  1063. The semantics of the language is determined by the actions taken when a
  1064. grouping is recognized. The actions are the C code that appears inside
  1065. braces. @xref{Actions}.
  1066. You must specify these actions in C, but Bison provides the means for
  1067. passing semantic values between the rules. In each action, the
  1068. pseudo-variable @code{$$} stands for the semantic value for the grouping
  1069. that the rule is going to construct. Assigning a value to @code{$$} is the
  1070. main job of most actions. The semantic values of the components of the
  1071. rule are referred to as @code{$1}, @code{$2}, and so on.
  1072. @menu
  1073. * Rpcalc Input::
  1074. * Rpcalc Line::
  1075. * Rpcalc Expr::
  1076. @end menu
  1077. @node Rpcalc Input, Rpcalc Line, , Rpcalc Rules
  1078. @subsubsection Explanation of @code{input}
  1079. Consider the definition of @code{input}:
  1080. @example
  1081. input: /* empty */
  1082. | input line
  1083. ;
  1084. @end example
  1085. This definition reads as follows: ``A complete input is either an empty
  1086. string, or a complete input followed by an input line''. Notice that
  1087. ``complete input'' is defined in terms of itself. This definition is said
  1088. to be @dfn{left recursive} since @code{input} appears always as the
  1089. leftmost symbol in the sequence. @xref{Recursion, ,Recursive Rules}.
  1090. The first alternative is empty because there are no symbols between the
  1091. colon and the first @samp{|}; this means that @code{input} can match an
  1092. empty string of input (no tokens). We write the rules this way because it
  1093. is legitimate to type @kbd{Ctrl-d} right after you start the calculator.
  1094. It's conventional to put an empty alternative first and write the comment
  1095. @samp{/* empty */} in it.
  1096. The second alternate rule (@code{input line}) handles all nontrivial input.
  1097. It means, ``After reading any number of lines, read one more line if
  1098. possible.'' The left recursion makes this rule into a loop. Since the
  1099. first alternative matches empty input, the loop can be executed zero or
  1100. more times.
  1101. The parser function @code{yyparse} continues to process input until a
  1102. grammatical error is seen or the lexical analyzer says there are no more
  1103. input tokens; we will arrange for the latter to happen at end of file.
  1104. @node Rpcalc Line, Rpcalc Expr, Rpcalc Input, Rpcalc Rules
  1105. @subsubsection Explanation of @code{line}
  1106. Now consider the definition of @code{line}:
  1107. @example
  1108. line: '\n'
  1109. | exp '\n' @{ printf ("\t%.10g\n", $1); @}
  1110. ;
  1111. @end example
  1112. The first alternative is a token which is a newline character; this means
  1113. that rpcalc accepts a blank line (and ignores it, since there is no
  1114. action). The second alternative is an expression followed by a newline.
  1115. This is the alternative that makes rpcalc useful. The semantic value of
  1116. the @code{exp} grouping is the value of @code{$1} because the @code{exp} in
  1117. question is the first symbol in the alternative. The action prints this
  1118. value, which is the result of the computation the user asked for.
  1119. This action is unusual because it does not assign a value to @code{$$}. As
  1120. a consequence, the semantic value associated with the @code{line} is
  1121. uninitialized (its value will be unpredictable). This would be a bug if
  1122. that value were ever used, but we don't use it: once rpcalc has printed the
  1123. value of the user's input line, that value is no longer needed.
  1124. @node Rpcalc Expr, , Rpcalc Line, Rpcalc Rules
  1125. @subsubsection Explanation of @code{expr}
  1126. The @code{exp} grouping has several rules, one for each kind of expression.
  1127. The first rule handles the simplest expressions: those that are just numbers.
  1128. The second handles an addition-expression, which looks like two expressions
  1129. followed by a plus-sign. The third handles subtraction, and so on.
  1130. @example
  1131. exp: NUM
  1132. | exp exp '+' @{ $$ = $1 + $2; @}
  1133. | exp exp '-' @{ $$ = $1 - $2; @}
  1134. @dots{}
  1135. ;
  1136. @end example
  1137. We have used @samp{|} to join all the rules for @code{exp}, but we could
  1138. equally well have written them separately:
  1139. @example
  1140. exp: NUM ;
  1141. exp: exp exp '+' @{ $$ = $1 + $2; @} ;
  1142. exp: exp exp '-' @{ $$ = $1 - $2; @} ;
  1143. @dots{}
  1144. @end example
  1145. Most of the rules have actions that compute the value of the expression in
  1146. terms of the value of its parts. For example, in the rule for addition,
  1147. @code{$1} refers to the first component @code{exp} and @code{$2} refers to
  1148. the second one. The third component, @code{'+'}, has no meaningful
  1149. associated semantic value, but if it had one you could refer to it as
  1150. @code{$3}. When @code{yyparse} recognizes a sum expression using this
  1151. rule, the sum of the two subexpressions' values is produced as the value of
  1152. the entire expression. @xref{Actions}.
  1153. You don't have to give an action for every rule. When a rule has no
  1154. action, Bison by default copies the value of @code{$1} into @code{$$}.
  1155. This is what happens in the first rule (the one that uses @code{NUM}).
  1156. The formatting shown here is the recommended convention, but Bison does
  1157. not require it. You can add or change whitespace as much as you wish.
  1158. For example, this:
  1159. @example
  1160. exp : NUM | exp exp '+' @{$$ = $1 + $2; @} | @dots{}
  1161. @end example
  1162. @noindent
  1163. means the same thing as this:
  1164. @example
  1165. exp: NUM
  1166. | exp exp '+' @{ $$ = $1 + $2; @}
  1167. | @dots{}
  1168. @end example
  1169. @noindent
  1170. The latter, however, is much more readable.
  1171. @node Rpcalc Lexer, Rpcalc Main, Rpcalc Rules, RPN Calc
  1172. @subsection The @code{rpcalc} Lexical Analyzer
  1173. @cindex writing a lexical analyzer
  1174. @cindex lexical analyzer, writing
  1175. The lexical analyzer's job is low-level parsing: converting characters or
  1176. sequences of characters into tokens. The Bison parser gets its tokens by
  1177. calling the lexical analyzer. @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}.
  1178. Only a simple lexical analyzer is needed for the RPN calculator. This
  1179. lexical analyzer skips blanks and tabs, then reads in numbers as
  1180. @code{double} and returns them as @code{NUM} tokens. Any other character
  1181. that isn't part of a number is a separate token. Note that the token-code
  1182. for such a single-character token is the character itself.
  1183. The return value of the lexical analyzer function is a numeric code which
  1184. represents a token type. The same text used in Bison rules to stand for
  1185. this token type is also a C expression for the numeric code for the type.
  1186. This works in two ways. If the token type is a character literal, then its
  1187. numeric code is the ASCII code for that character; you can use the same
  1188. character literal in the lexical analyzer to express the number. If the
  1189. token type is an identifier, that identifier is defined by Bison as a C
  1190. macro whose definition is the appropriate number. In this example,
  1191. therefore, @code{NUM} becomes a macro for @code{yylex} to use.
  1192. The semantic value of the token (if it has one) is stored into the global
  1193. variable @code{yylval}, which is where the Bison parser will look for it.
  1194. (The C data type of @code{yylval} is @code{YYSTYPE}, which was defined
  1195. at the beginning of the grammar; @pxref{Rpcalc Decls, ,Declarations for @code{rpcalc}}.)
  1196. A token type code of zero is returned if the end-of-file is encountered.
  1197. (Bison recognizes any nonpositive value as indicating the end of the
  1198. input.)
  1199. Here is the code for the lexical analyzer:
  1200. @example
  1201. @group
  1202. /* Lexical analyzer returns a double floating point
  1203. number on the stack and the token NUM, or the ASCII
  1204. character read if not a number. Skips all blanks
  1205. and tabs, returns 0 for EOF. */
  1206. #include <ctype.h>
  1207. @end group
  1208. @group
  1209. yylex ()
  1210. @{
  1211. int c;
  1212. /* skip white space */
  1213. while ((c = getchar ()) == ' ' || c == '\t')
  1214. ;
  1215. @end group
  1216. @group
  1217. /* process numbers */
  1218. if (c == '.' || isdigit (c))
  1219. @{
  1220. ungetc (c, stdin);
  1221. scanf ("%lf", &yylval);
  1222. return NUM;
  1223. @}
  1224. @end group
  1225. @group
  1226. /* return end-of-file */
  1227. if (c == EOF)
  1228. return 0;
  1229. /* return single chars */
  1230. return c;
  1231. @}
  1232. @end group
  1233. @end example
  1234. @node Rpcalc Main, Rpcalc Error, Rpcalc Lexer, RPN Calc
  1235. @subsection The Controlling Function
  1236. @cindex controlling function
  1237. @cindex main function in simple example
  1238. In keeping with the spirit of this example, the controlling function is
  1239. kept to the bare minimum. The only requirement is that it call
  1240. @code{yyparse} to start the process of parsing.
  1241. @example
  1242. @group
  1243. main ()
  1244. @{
  1245. yyparse ();
  1246. @}
  1247. @end group
  1248. @end example
  1249. @node Rpcalc Error, Rpcalc Gen, Rpcalc Main, RPN Calc
  1250. @subsection The Error Reporting Routine
  1251. @cindex error reporting routine
  1252. When @code{yyparse} detects a syntax error, it calls the error reporting
  1253. function @code{yyerror} to print an error message (usually but not always
  1254. @code{"parse error"}). It is up to the programmer to supply @code{yyerror}
  1255. (@pxref{Interface, ,Parser C-Language Interface}), so here is the definition we will use:
  1256. @example
  1257. @group
  1258. #include <stdio.h>
  1259. yyerror (s) /* Called by yyparse on error */
  1260. char *s;
  1261. @{
  1262. printf ("%s\n", s);
  1263. @}
  1264. @end group
  1265. @end example
  1266. After @code{yyerror} returns, the Bison parser may recover from the error
  1267. and continue parsing if the grammar contains a suitable error rule
  1268. (@pxref{Error Recovery}). Otherwise, @code{yyparse} returns nonzero. We
  1269. have not written any error rules in this example, so any invalid input will
  1270. cause the calculator program to exit. This is not clean behavior for a
  1271. real calculator, but it is adequate in the first example.
  1272. @node Rpcalc Gen, Rpcalc Compile, Rpcalc Error, RPN Calc
  1273. @subsection Running Bison to Make the Parser
  1274. @cindex running Bison (introduction)
  1275. Before running Bison to produce a parser, we need to decide how to arrange
  1276. all the source code in one or more source files. For such a simple example,
  1277. the easiest thing is to put everything in one file. The definitions of
  1278. @code{yylex}, @code{yyerror} and @code{main} go at the end, in the
  1279. ``additional C code'' section of the file (@pxref{Grammar Layout, ,The Overall Layout of a Bison Grammar}).
  1280. For a large project, you would probably have several source files, and use
  1281. @code{make} to arrange to recompile them.
  1282. With all the source in a single file, you use the following command to
  1283. convert it into a parser file:
  1284. @example
  1285. bison @var{file_name}.y
  1286. @end example
  1287. @noindent
  1288. In this example the file was called @file{rpcalc.y} (for ``Reverse Polish
  1289. CALCulator''). Bison produces a file named @file{@var{file_name}.tab.c},
  1290. removing the @samp{.y} from the original file name. The file output by
  1291. Bison contains the source code for @code{yyparse}. The additional
  1292. functions in the input file (@code{yylex}, @code{yyerror} and @code{main})
  1293. are copied verbatim to the output.
  1294. @node Rpcalc Compile, , Rpcalc Gen, RPN Calc
  1295. @subsection Compiling the Parser File
  1296. @cindex compiling the parser
  1297. Here is how to compile and run the parser file:
  1298. @example
  1299. @group
  1300. # @r{List files in current directory.}
  1301. % ls
  1302. rpcalc.tab.c rpcalc.y
  1303. @end group
  1304. @group
  1305. # @r{Compile the Bison parser.}
  1306. # @r{@samp{-lm} tells compiler to search math library for @code{pow}.}
  1307. % cc rpcalc.tab.c -lm -o rpcalc
  1308. @end group
  1309. @group
  1310. # @r{List files again.}
  1311. % ls
  1312. rpcalc rpcalc.tab.c rpcalc.y
  1313. @end group
  1314. @end example
  1315. The file @file{rpcalc} now contains the executable code. Here is an
  1316. example session using @code{rpcalc}.
  1317. @example
  1318. % rpcalc
  1319. 4 9 +
  1320. 13
  1321. 3 7 + 3 4 5 *+-
  1322. -13
  1323. 3 7 + 3 4 5 * + - n @r{Note the unary minus, @samp{n}}
  1324. 13
  1325. 5 6 / 4 n +
  1326. -3.166666667
  1327. 3 4 ^ @r{Exponentiation}
  1328. 81
  1329. ^D @r{End-of-file indicator}
  1330. %
  1331. @end example
  1332. @node Infix Calc, Simple Error Recovery, RPN Calc, Examples
  1333. @section Infix Notation Calculator: @code{calc}
  1334. @cindex infix notation calculator
  1335. @cindex @code{calc}
  1336. @cindex calculator, infix notation
  1337. We now modify rpcalc to handle infix operators instead of postfix. Infix
  1338. notation involves the concept of operator precedence and the need for
  1339. parentheses nested to arbitrary depth. Here is the Bison code for
  1340. @file{calc.y}, an infix desk-top calculator.
  1341. @example
  1342. /* Infix notation calculator--calc */
  1343. %@{
  1344. #define YYSTYPE double
  1345. #include <math.h>
  1346. %@}
  1347. /* BISON Declarations */
  1348. %token NUM
  1349. %left '-' '+'
  1350. %left '*' '/'
  1351. %left NEG /* negation--unary minus */
  1352. %right '^' /* exponentiation */
  1353. /* Grammar follows */
  1354. %%
  1355. input: /* empty string */
  1356. | input line
  1357. ;
  1358. line: '\n'
  1359. | exp '\n' @{ printf ("\t%.10g\n", $1); @}
  1360. ;
  1361. exp: NUM @{ $$ = $1; @}
  1362. | exp '+' exp @{ $$ = $1 + $3; @}
  1363. | exp '-' exp @{ $$ = $1 - $3; @}
  1364. | exp '*' exp @{ $$ = $1 * $3; @}
  1365. | exp '/' exp @{ $$ = $1 / $3; @}
  1366. | '-' exp %prec NEG @{ $$ = -$2; @}
  1367. | exp '^' exp @{ $$ = pow ($1, $3); @}
  1368. | '(' exp ')' @{ $$ = $2; @}
  1369. ;
  1370. %%
  1371. @end example
  1372. @noindent
  1373. The functions @code{yylex}, @code{yyerror} and @code{main} can be the same
  1374. as before.
  1375. There are two important new features shown in this code.
  1376. In the second section (Bison declarations), @code{%left} declares token
  1377. types and says they are left-associative operators. The declarations
  1378. @code{%left} and @code{%right} (right associativity) take the place of
  1379. @code{%token} which is used to declare a token type name without
  1380. associativity. (These tokens are single-character literals, which
  1381. ordinarily don't need to be declared. We declare them here to specify
  1382. the associativity.)
  1383. Operator precedence is determined by the line ordering of the
  1384. declarations; the higher the line number of the declaration (lower on
  1385. the page or screen), the higher the precedence. Hence, exponentiation
  1386. has the highest precedence, unary minus (@code{NEG}) is next, followed
  1387. by @samp{*} and @samp{/}, and so on. @xref{Precedence, ,Operator Precedence}.
  1388. The other important new feature is the @code{%prec} in the grammar section
  1389. for the unary minus operator. The @code{%prec} simply instructs Bison that
  1390. the rule @samp{| '-' exp} has the same precedence as @code{NEG}---in this
  1391. case the next-to-highest. @xref{Contextual Precedence, ,Context-Dependent Precedence}.
  1392. Here is a sample run of @file{calc.y}:
  1393. @need 500
  1394. @example
  1395. % calc
  1396. 4 + 4.5 - (34/(8*3+-3))
  1397. 6.880952381
  1398. -56 + 2
  1399. -54
  1400. 3 ^ 2
  1401. 9
  1402. @end example
  1403. @node Simple Error Recovery, Multi-function Calc, Infix Calc, Examples
  1404. @section Simple Error Recovery
  1405. @cindex error recovery, simple
  1406. Up to this point, this manual has not addressed the issue of @dfn{error
  1407. recovery}---how to continue parsing after the parser detects a syntax
  1408. error. All we have handled is error reporting with @code{yyerror}. Recall
  1409. that by default @code{yyparse} returns after calling @code{yyerror}. This
  1410. means that an erroneous input line causes the calculator program to exit.
  1411. Now we show how to rectify this deficiency.
  1412. The Bison language itself includes the reserved word @code{error}, which
  1413. may be included in the grammar rules. In the example below it has
  1414. been added to one of the alternatives for @code{line}:
  1415. @example
  1416. @group
  1417. line: '\n'
  1418. | exp '\n' @{ printf ("\t%.10g\n", $1); @}
  1419. | error '\n' @{ yyerrok; @}
  1420. ;
  1421. @end group
  1422. @end example
  1423. This addition to the grammar allows for simple error recovery in the event
  1424. of a parse error. If an expression that cannot be evaluated is read, the
  1425. error will be recognized by the third rule for @code{line}, and parsing
  1426. will continue. (The @code{yyerror} function is still called upon to print
  1427. its message as well.) The action executes the statement @code{yyerrok}, a
  1428. macro defined automatically by Bison; its meaning is that error recovery is
  1429. complete (@pxref{Error Recovery}). Note the difference between
  1430. @code{yyerrok} and @code{yyerror}; neither one is a misprint.@refill
  1431. This form of error recovery deals with syntax errors. There are other
  1432. kinds of errors; for example, division by zero, which raises an exception
  1433. signal that is normally fatal. A real calculator program must handle this
  1434. signal and use @code{longjmp} to return to @code{main} and resume parsing
  1435. input lines; it would also have to discard the rest of the current line of
  1436. input. We won't discuss this issue further because it is not specific to
  1437. Bison programs.
  1438. @node Multi-function Calc, Exercises, Simple Error Recovery, Examples
  1439. @section Multi-Function Calculator: @code{mfcalc}
  1440. @cindex multi-function calculator
  1441. @cindex @code{mfcalc}
  1442. @cindex calculator, multi-function
  1443. Now that the basics of Bison have been discussed, it is time to move on to
  1444. a more advanced problem. The above calculators provided only five
  1445. functions, @samp{+}, @samp{-}, @samp{*}, @samp{/} and @samp{^}. It would
  1446. be nice to have a calculator that provides other mathematical functions such
  1447. as @code{sin}, @code{cos}, etc.
  1448. It is easy to add new operators to the infix calculator as long as they are
  1449. only single-character literals. The lexical analyzer @code{yylex} passes
  1450. back all non-number characters as tokens, so new grammar rules suffice for
  1451. adding a new operator. But we want something more flexible: built-in
  1452. functions whose syntax has this form:
  1453. @example
  1454. @var{function_name} (@var{argument})
  1455. @end example
  1456. @noindent
  1457. At the same time, we will add memory to the calculator, by allowing you
  1458. to create named variables, store values in them, and use them later.
  1459. Here is a sample session with the multi-function calculator:
  1460. @example
  1461. % acalc
  1462. pi = 3.141592653589
  1463. 3.1415926536
  1464. sin(pi)
  1465. 0.0000000000
  1466. alpha = beta1 = 2.3
  1467. 2.3000000000
  1468. alpha
  1469. 2.3000000000
  1470. ln(alpha)
  1471. 0.8329091229
  1472. exp(ln(beta1))
  1473. 2.3000000000
  1474. %
  1475. @end example
  1476. Note that multiple assignment and nested function calls are permitted.
  1477. @menu
  1478. * Decl: Mfcalc Decl. Bison declarations for multi-function calculator.
  1479. * Rules: Mfcalc Rules. Grammar rules for the calculator.
  1480. * Symtab: Mfcalc Symtab. Symbol table management subroutines.
  1481. @end menu
  1482. @node Mfcalc Decl, Mfcalc Rules, , Multi-function Calc
  1483. @subsection Declarations for @code{mfcalc}
  1484. Here are the C and Bison declarations for the multi-function calculator.
  1485. @smallexample
  1486. %@{
  1487. #include <math.h> /* For math functions, cos(), sin(), etc. */
  1488. #include "calc.h" /* Contains definition of `symrec' */
  1489. %@}
  1490. %union @{
  1491. double val; /* For returning numbers. */
  1492. symrec *tptr; /* For returning symbol-table pointers */
  1493. @}
  1494. %token <val> NUM /* Simple double precision number */
  1495. %token <tptr> VAR FNCT /* Variable and Function */
  1496. %type <val> exp
  1497. %right '='
  1498. %left '-' '+'
  1499. %left '*' '/'
  1500. %left NEG /* Negation--unary minus */
  1501. %right '^' /* Exponentiation */
  1502. /* Grammar follows */
  1503. %%
  1504. @end smallexample
  1505. The above grammar introduces only two new features of the Bison language.
  1506. These features allow semantic values to have various data types
  1507. (@pxref{Multiple Types, ,More Than One Value Type}).
  1508. The @code{%union} declaration specifies the entire list of possible types;
  1509. this is instead of defining @code{YYSTYPE}. The allowable types are now
  1510. double-floats (for @code{exp} and @code{NUM}) and pointers to entries in
  1511. the symbol table. @xref{Union Decl, ,The Collection of Value Types}.
  1512. Since values can now have various types, it is necessary to associate a
  1513. type with each grammar symbol whose semantic value is used. These symbols
  1514. are @code{NUM}, @code{VAR}, @code{FNCT}, and @code{exp}. Their
  1515. declarations are augmented with information about their data type (placed
  1516. between angle brackets).
  1517. The Bison construct @code{%type} is used for declaring nonterminal symbols,
  1518. just as @code{%token} is used for declaring token types. We have not used
  1519. @code{%type} before because nonterminal symbols are normally declared
  1520. implicitly by the rules that define them. But @code{exp} must be declared
  1521. explicitly so we can specify its value type. @xref{Type Decl, ,Nonterminal Symbols}.
  1522. @node Mfcalc Rules, Mfcalc Symtab, Mfcalc Decl, Multi-function Calc
  1523. @subsection Grammar Rules for @code{mfcalc}
  1524. Here are the grammar rules for the multi-function calculator.
  1525. Most of them are copied directly from @code{calc}; three rules,
  1526. those which mention @code{VAR} or @code{FNCT}, are new.
  1527. @smallexample
  1528. input: /* empty */
  1529. | input line
  1530. ;
  1531. line:
  1532. '\n'
  1533. | exp '\n' @{ printf ("\t%.10g\n", $1); @}
  1534. | error '\n' @{ yyerrok; @}
  1535. ;
  1536. exp: NUM @{ $$ = $1; @}
  1537. | VAR @{ $$ = $1->value.var; @}
  1538. | VAR '=' exp @{ $$ = $3; $1->value.var = $3; @}
  1539. | FNCT '(' exp ')' @{ $$ = (*($1->value.fnctptr))($3); @}
  1540. | exp '+' exp @{ $$ = $1 + $3; @}
  1541. | exp '-' exp @{ $$ = $1 - $3; @}
  1542. | exp '*' exp @{ $$ = $1 * $3; @}
  1543. | exp '/' exp @{ $$ = $1 / $3; @}
  1544. | '-' exp %prec NEG @{ $$ = -$2; @}
  1545. | exp '^' exp @{ $$ = pow ($1, $3); @}
  1546. | '(' exp ')' @{ $$ = $2; @}
  1547. ;
  1548. /* End of grammar */
  1549. %%
  1550. @end smallexample
  1551. @node Mfcalc Symtab, , Mfcalc Rules, Multi-function Calc
  1552. @subsection The @code{mfcalc} Symbol Table
  1553. @cindex symbol table example
  1554. The multi-function calculator requires a symbol table to keep track of the
  1555. names and meanings of variables and functions. This doesn't affect the
  1556. grammar rules (except for the actions) or the Bison declarations, but it
  1557. requires some additional C functions for support.
  1558. The symbol table itself consists of a linked list of records. Its
  1559. definition, which is kept in the header @file{calc.h}, is as follows. It
  1560. provides for either functions or variables to be placed in the table.
  1561. @smallexample
  1562. @group
  1563. /* Data type for links in the chain of symbols. */
  1564. struct symrec
  1565. @{
  1566. char *name; /* name of symbol */
  1567. int type; /* type of symbol: either VAR or FNCT */
  1568. union @{
  1569. double var; /* value of a VAR */
  1570. double (*fnctptr)(); /* value of a FNCT */
  1571. @} value;
  1572. struct symrec *next; /* link field */
  1573. @};
  1574. @end group
  1575. @group
  1576. typedef struct symrec symrec;
  1577. /* The symbol table: a chain of `struct symrec'. */
  1578. extern symrec *sym_table;
  1579. symrec *putsym ();
  1580. symrec *getsym ();
  1581. @end group
  1582. @end smallexample
  1583. The new version of @code{main} includes a call to @code{init_table}, a
  1584. function that initializes the symbol table. Here it is, and
  1585. @code{init_table} as well:
  1586. @smallexample
  1587. @group
  1588. #include <stdio.h>
  1589. main ()
  1590. @{
  1591. init_table ();
  1592. yyparse ();
  1593. @}
  1594. @end group
  1595. @group
  1596. yyerror (s) /* Called by yyparse on error */
  1597. char *s;
  1598. @{
  1599. printf ("%s\n", s);
  1600. @}
  1601. struct init
  1602. @{
  1603. char *fname;
  1604. double (*fnct)();
  1605. @};
  1606. @end group
  1607. @group
  1608. struct init arith_fncts[]
  1609. = @{
  1610. "sin", sin,
  1611. "cos", cos,
  1612. "atan", atan,
  1613. "ln", log,
  1614. "exp", exp,
  1615. "sqrt", sqrt,
  1616. 0, 0
  1617. @};
  1618. /* The symbol table: a chain of `struct symrec'. */
  1619. symrec *sym_table = (symrec *)0;
  1620. @end group
  1621. @group
  1622. init_table () /* puts arithmetic functions in table. */
  1623. @{
  1624. int i;
  1625. symrec *ptr;
  1626. for (i = 0; arith_fncts[i].fname != 0; i++)
  1627. @{
  1628. ptr = putsym (arith_fncts[i].fname, FNCT);
  1629. ptr->value.fnctptr = arith_fncts[i].fnct;
  1630. @}
  1631. @}
  1632. @end group
  1633. @end smallexample
  1634. By simply editing the initialization list and adding the necessary include
  1635. files, you can add additional functions to the calculator.
  1636. Two important functions allow look-up and installation of symbols in the
  1637. symbol table. The function @code{putsym} is passed a name and the type
  1638. (@code{VAR} or @code{FNCT}) of the object to be installed. The object is
  1639. linked to the front of the list, and a pointer to the object is returned.
  1640. The function @code{getsym} is passed the name of the symbol to look up. If
  1641. found, a pointer to that symbol is returned; otherwise zero is returned.
  1642. @smallexample
  1643. symrec *
  1644. putsym (sym_name,sym_type)
  1645. char *sym_name;
  1646. int sym_type;
  1647. @{
  1648. symrec *ptr;
  1649. ptr = (symrec *) malloc (sizeof (symrec));
  1650. ptr->name = (char *) malloc (strlen (sym_name) + 1);
  1651. strcpy (ptr->name,sym_name);
  1652. ptr->type = sym_type;
  1653. ptr->value.var = 0; /* set value to 0 even if fctn. */
  1654. ptr->next = (struct symrec *)sym_table;
  1655. sym_table = ptr;
  1656. return ptr;
  1657. @}
  1658. symrec *
  1659. getsym (sym_name)
  1660. char *sym_name;
  1661. @{
  1662. symrec *ptr;
  1663. for (ptr = sym_table; ptr != (symrec *) 0;
  1664. ptr = (symrec *)ptr->next)
  1665. if (strcmp (ptr->name,sym_name) == 0)
  1666. return ptr;
  1667. return 0;
  1668. @}
  1669. @end smallexample
  1670. The function @code{yylex} must now recognize variables, numeric values, and
  1671. the single-character arithmetic operators. Strings of alphanumeric
  1672. characters with a leading nondigit are recognized as either variables or
  1673. functions depending on what the symbol table says about them.
  1674. The string is passed to @code{getsym} for look up in the symbol table. If
  1675. the name appears in the table, a pointer to its location and its type
  1676. (@code{VAR} or @code{FNCT}) is returned to @code{yyparse}. If it is not
  1677. already in the table, then it is installed as a @code{VAR} using
  1678. @code{putsym}. Again, a pointer and its type (which must be @code{VAR}) is
  1679. returned to @code{yyparse}.@refill
  1680. No change is needed in the handling of numeric values and arithmetic
  1681. operators in @code{yylex}.
  1682. @smallexample
  1683. @group
  1684. #include <ctype.h>
  1685. yylex ()
  1686. @{
  1687. int c;
  1688. /* Ignore whitespace, get first nonwhite character. */
  1689. while ((c = getchar ()) == ' ' || c == '\t');
  1690. if (c == EOF)
  1691. return 0;
  1692. @end group
  1693. @group
  1694. /* Char starts a number => parse the number. */
  1695. if (c == '.' || isdigit (c))
  1696. @{
  1697. ungetc (c, stdin);
  1698. scanf ("%lf", &yylval.val);
  1699. return NUM;
  1700. @}
  1701. @end group
  1702. @group
  1703. /* Char starts an identifier => read the name. */
  1704. if (isalpha (c))
  1705. @{
  1706. symrec *s;
  1707. static char *symbuf = 0;
  1708. static int length = 0;
  1709. int i;
  1710. @end group
  1711. @group
  1712. /* Initially make the buffer long enough
  1713. for a 40-character symbol name. */
  1714. if (length == 0)
  1715. length = 40, symbuf = (char *)malloc (length + 1);
  1716. i = 0;
  1717. do
  1718. @end group
  1719. @group
  1720. @{
  1721. /* If buffer is full, make it bigger. */
  1722. if (i == length)
  1723. @{
  1724. length *= 2;
  1725. symbuf = (char *)realloc (symbuf, length + 1);
  1726. @}
  1727. /* Add this character to the buffer. */
  1728. symbuf[i++] = c;
  1729. /* Get another character. */
  1730. c = getchar ();
  1731. @}
  1732. @end group
  1733. @group
  1734. while (c != EOF && isalnum (c));
  1735. ungetc (c, stdin);
  1736. symbuf[i] = '\0';
  1737. @end group
  1738. @group
  1739. s = getsym (symbuf);
  1740. if (s == 0)
  1741. s = putsym (symbuf, VAR);
  1742. yylval.tptr = s;
  1743. return s->type;
  1744. @}
  1745. /* Any other character is a token by itself. */
  1746. return c;
  1747. @}
  1748. @end group
  1749. @end smallexample
  1750. This program is both powerful and flexible. You may easily add new
  1751. functions, and it is a simple job to modify this code to install predefined
  1752. variables such as @code{pi} or @code{e} as well.
  1753. @node Exercises, , Multi-function Calc, Examples
  1754. @section Exercises
  1755. @cindex exercises
  1756. @enumerate
  1757. @item
  1758. Add some new functions from @file{math.h} to the initialization list.
  1759. @item
  1760. Add another array that contains constants and their values. Then
  1761. modify @code{init_table} to add these constants to the symbol table.
  1762. It will be easiest to give the constants type @code{VAR}.
  1763. @item
  1764. Make the program report an error if the user refers to an
  1765. uninitialized variable in any way except to store a value in it.
  1766. @end enumerate
  1767. @node Grammar File, Interface, Examples, Top
  1768. @chapter Bison Grammar Files
  1769. Bison takes as input a context-free grammar specification and produces a
  1770. C-language function that recognizes correct instances of the grammar.
  1771. The Bison grammar input file conventionally has a name ending in @samp{.y}.
  1772. @menu
  1773. * Grammar Outline:: Overall layout of the grammar file.
  1774. * Symbols:: Terminal and nonterminal symbols.
  1775. * Rules:: How to write grammar rules.
  1776. * Recursion:: Writing recursive rules.
  1777. * Semantics:: Semantic values and actions.
  1778. * Declarations:: All kinds of Bison declarations are described here.
  1779. * Multiple Parsers:: Putting more than one Bison parser in one program.
  1780. @end menu
  1781. @node Grammar Outline, Symbols, , Grammar File
  1782. @section Outline of a Bison Grammar
  1783. A Bison grammar file has four main sections, shown here with the
  1784. appropriate delimiters:
  1785. @example
  1786. %@{
  1787. @var{C declarations}
  1788. %@}
  1789. @var{Bison declarations}
  1790. %%
  1791. @var{Grammar rules}
  1792. %%
  1793. @var{Additional C code}
  1794. @end example
  1795. Comments enclosed in @samp{/* @dots{} */} may appear in any of the sections.
  1796. @menu
  1797. * C Declarations:: Syntax and usage of the C declarations section.
  1798. * Bison Declarations:: Syntax and usage of the Bison declarations section.
  1799. * Grammar Rules:: Syntax and usage of the grammar rules section.
  1800. * C Code:: Syntax and usage of the additional C code section.
  1801. @end menu
  1802. @node C Declarations, Bison Declarations, , Grammar Outline
  1803. @subsection The C Declarations Section
  1804. @cindex C declarations section
  1805. @cindex declarations, C
  1806. The @var{C declarations} section contains macro definitions and
  1807. declarations of functions and variables that are used in the actions in the
  1808. grammar rules. These are copied to the beginning of the parser file so
  1809. that they precede the definition of @code{yyparse}. You can use
  1810. @samp{#include} to get the declarations from a header file. If you don't
  1811. need any C declarations, you may omit the @samp{%@{} and @samp{%@}}
  1812. delimiters that bracket this section.
  1813. @node Bison Declarations, Grammar Rules, C Declarations, Grammar Outline
  1814. @subsection The Bison Declarations Section
  1815. @cindex Bison declarations (introduction)
  1816. @cindex declarations, Bison (introduction)
  1817. The @var{Bison declarations} section contains declarations that define
  1818. terminal and nonterminal symbols, specify precedence, and so on.
  1819. In some simple grammars you may not need any declarations.
  1820. @xref{Declarations, ,Bison Declarations}.
  1821. @node Grammar Rules, C Code, Bison Declarations, Grammar Outline
  1822. @subsection The Grammar Rules Section
  1823. @cindex grammar rules section
  1824. @cindex rules section for grammar
  1825. The @dfn{grammar rules} section contains one or more Bison grammar
  1826. rules, and nothing else. @xref{Rules, ,Syntax of Grammar Rules}.
  1827. There must always be at least one grammar rule, and the first
  1828. @samp{%%} (which precedes the grammar rules) may never be omitted even
  1829. if it is the first thing in the file.
  1830. @node C Code, , Grammar Rules, Grammar Outline
  1831. @subsection The Additional C Code Section
  1832. @cindex additional C code section
  1833. @cindex C code, section for additional
  1834. The @var{additional C code} section is copied verbatim to the end of
  1835. the parser file, just as the @var{C declarations} section is copied to
  1836. the beginning. This is the most convenient place to put anything
  1837. that you want to have in the parser file but which need not come before
  1838. the definition of @code{yyparse}. For example, the definitions of
  1839. @code{yylex} and @code{yyerror} often go here. @xref{Interface, ,Parser C-Language Interface}.
  1840. If the last section is empty, you may omit the @samp{%%} that separates it
  1841. from the grammar rules.
  1842. The Bison parser itself contains many static variables whose names start
  1843. with @samp{yy} and many macros whose names start with @samp{YY}. It is a
  1844. good idea to avoid using any such names (except those documented in this
  1845. manual) in the additional C code section of the grammar file.
  1846. @node Symbols, Rules, Grammar Outline, Grammar File
  1847. @section Symbols, Terminal and Nonterminal
  1848. @cindex nonterminal symbol
  1849. @cindex terminal symbol
  1850. @cindex token type
  1851. @cindex symbol
  1852. @dfn{Symbols} in Bison grammars represent the grammatical classifications
  1853. of the language.
  1854. A @dfn{terminal symbol} (also known as a @dfn{token type}) represents a
  1855. class of syntactically equivalent tokens. You use the symbol in grammar
  1856. rules to mean that a token in that class is allowed. The symbol is
  1857. represented in the Bison parser by a numeric code, and the @code{yylex}
  1858. function returns a token type code to indicate what kind of token has been
  1859. read. You don't need to know what the code value is; you can use the
  1860. symbol to stand for it.
  1861. A @dfn{nonterminal symbol} stands for a class of syntactically equivalent
  1862. groupings. The symbol name is used in writing grammar rules. By convention,
  1863. it should be all lower case.
  1864. Symbol names can contain letters, digits (not at the beginning),
  1865. underscores and periods. Periods make sense only in nonterminals.
  1866. There are three ways of writing terminal symbols in the grammar:
  1867. @itemize @bullet
  1868. @item
  1869. A @dfn{named token type} is written with an identifier, like an
  1870. identifier in C. By convention, it should be all upper case. Each
  1871. such name must be defined with a Bison declaration such as
  1872. @code{%token}. @xref{Token Decl, ,Token Type Names}.
  1873. @item
  1874. @cindex character token
  1875. @cindex literal token
  1876. @cindex single-character literal
  1877. A @dfn{character token type} (or @dfn{literal token}) is written in
  1878. the grammar using the same syntax used in C for character constants;
  1879. for example, @code{'+'} is a character token type. A character token
  1880. type doesn't need to be declared unless you need to specify its
  1881. semantic value data type (@pxref{Value Type, ,Data Types of Semantic Values}), associativity, or
  1882. precedence (@pxref{Precedence, ,Operator Precedence}).
  1883. By convention, a character token type is used only to represent a
  1884. token that consists of that particular character. Thus, the token
  1885. type @code{'+'} is used to represent the character @samp{+} as a
  1886. token. Nothing enforces this convention, but if you depart from it,
  1887. your program will confuse other readers.
  1888. All the usual escape sequences used in character literals in C can be
  1889. used in Bison as well, but you must not use the null character as a
  1890. character literal because its ASCII code, zero, is the code
  1891. @code{yylex} returns for end-of-input (@pxref{Calling Convention, ,Calling Convention for @code{yylex}}).
  1892. @item
  1893. @cindex string token
  1894. @cindex literal string token
  1895. @cindex multiple-character literal
  1896. WARNING: Use of string tokens such as @code{"<="} is incompatible with yacc.
  1897. A @dfn{literal string token} is written in
  1898. the grammar using the same syntax used in C for string constants;
  1899. for example, @code{"<="} is a string literal. A string literal
  1900. doesn't need to be declared unless you need to specify its
  1901. semantic value data type (@pxref{Value Type}), associativity,
  1902. precedence (@pxref{Precedence}). To associate a symbolic name with the
  1903. string literal, use the @code{%thong} declaration (@pxref{Thong Decl, ,Thong Declarations});
  1904. otherwise the lexical analyzer can retrieve the token number from the
  1905. tables generated with the @samp{-k} or @samp{--token-table} switch.
  1906. By convention, a string literal token is used only to represent a
  1907. token that consists of that particular string. Thus, the token
  1908. type @code{"<="} is used to represent the string @samp{<=} as a
  1909. token. Nothing enforces this convention, but if you depart from it,
  1910. your program will confuse other readers.
  1911. All the usual escape sequences used in string literals in C can be
  1912. used in Bison as well. The contents of the string must be two or more
  1913. characters; use a character token for a single character and @code{/*EMPTY*/}
  1914. for the empty token.
  1915. @end itemize
  1916. How you choose to write a terminal symbol has no effect on its
  1917. grammatical meaning. That depends only on where it appears in rules and
  1918. on when the parser function returns that symbol.
  1919. The value returned by @code{yylex} is always one of the terminal symbols
  1920. (or 0 for end-of-input). Whichever way you write the token type in the
  1921. grammar rules, you write it the same way in the definition of @code{yylex}.
  1922. The numeric code for a character token type is simply the ASCII code for
  1923. the character, so @code{yylex} can use the identical character constant to
  1924. generate the requisite code. Each named token type becomes a C macro in
  1925. the parser file, so @code{yylex} can use the name to stand for the code.
  1926. (This is why periods don't make sense in terminal symbols.)
  1927. @xref{Calling Convention, ,Calling Convention for @code{yylex}}.
  1928. If @code{yylex} is defined in a separate file, you need to arrange for the
  1929. token-type macro definitions to be available there. Use the @samp{-d}
  1930. option when you run Bison, so that it will write these macro definitions
  1931. into a separate header file @file{@var{name}.tab.h} which you can include
  1932. in the other source files that need it. @xref{Invocation, ,Invoking Bison}.
  1933. The symbol @code{error} is a terminal symbol reserved for error recovery
  1934. (@pxref{Error Recovery}); you shouldn't use it for any other purpose.
  1935. In particular, @code{yylex} should never return this value.
  1936. @node Rules, Recursion, Symbols, Grammar File
  1937. @section Syntax of Grammar Rules
  1938. @cindex rule syntax
  1939. @cindex grammar rule syntax
  1940. @cindex syntax of grammar rules
  1941. A Bison grammar rule has the following general form:
  1942. @example
  1943. @var{result}: @var{components}@dots{}
  1944. ;
  1945. @end example
  1946. @noindent
  1947. where @var{result} is the nonterminal symbol that this rule describes
  1948. and @var{components} are various terminal and nonterminal symbols that
  1949. are put together by this rule (@pxref{Symbols, ,Symbols - Terminal and Nonterminal}).
  1950. For example,
  1951. @example
  1952. @group
  1953. exp: exp '+' exp
  1954. ;
  1955. @end group
  1956. @end example
  1957. @noindent
  1958. says that two groupings of type @code{exp}, with a @samp{+} token in between,
  1959. can be combined into a larger grouping of type @code{exp}.
  1960. Whitespace in rules is significant only to separate symbols. You can add
  1961. extra whitespace as you wish.
  1962. Scattered among the components can be @var{actions} that determine
  1963. the semantics of the rule. An action looks like this:
  1964. @example
  1965. @{@var{C statements}@}
  1966. @end example
  1967. @noindent
  1968. Usually there is only one action and it follows the components.
  1969. @xref{Actions}.
  1970. @findex |
  1971. Multiple rules for the same @var{result} can be written separately or can
  1972. be joined with the vertical-bar character @samp{|} as follows:
  1973. @ifinfo
  1974. @example
  1975. @var{result}: @var{rule1-components}@dots{}
  1976. | @var{rule2-components}@dots{}
  1977. @dots{}
  1978. ;
  1979. @end example
  1980. @end ifinfo
  1981. @iftex
  1982. @example
  1983. @group
  1984. @var{result}: @var{rule1-components}@dots{}
  1985. | @var{rule2-components}@dots{}
  1986. @dots{}
  1987. ;
  1988. @end group
  1989. @end example
  1990. @end iftex
  1991. @noindent
  1992. They are still considered distinct rules even when joined in this way.
  1993. If @var{components} in a rule is empty, it means that @var{result} can
  1994. match the empty string. For example, here is how to define a
  1995. comma-separated sequence of zero or more @code{exp} groupings:
  1996. @example
  1997. @group
  1998. expseq: /* empty */
  1999. | expseq1
  2000. ;
  2001. @end group
  2002. @group
  2003. expseq1: exp
  2004. | expseq1 ',' exp
  2005. ;
  2006. @end group
  2007. @end example
  2008. @noindent
  2009. It is customary to write a comment @samp{/* empty */} in each rule
  2010. with no components.
  2011. @node Recursion, Semantics, Rules, Grammar File
  2012. @section Recursive Rules
  2013. @cindex recursive rule
  2014. A rule is called @dfn{recursive} when its @var{result} nonterminal appears
  2015. also on its right hand side. Nearly all Bison grammars need to use
  2016. recursion, because that is the only way to define a sequence of any number
  2017. of somethings. Consider this recursive definition of a comma-separated
  2018. sequence of one or more expressions:
  2019. @example
  2020. @group
  2021. expseq1: exp
  2022. | expseq1 ',' exp
  2023. ;
  2024. @end group
  2025. @end example
  2026. @cindex left recursion
  2027. @cindex right recursion
  2028. @noindent
  2029. Since the recursive use of @code{expseq1} is the leftmost symbol in the
  2030. right hand side, we call this @dfn{left recursion}. By contrast, here
  2031. the same construct is defined using @dfn{right recursion}:
  2032. @example
  2033. @group
  2034. expseq1: exp
  2035. | exp ',' expseq1
  2036. ;
  2037. @end group
  2038. @end example
  2039. @noindent
  2040. Any kind of sequence can be defined using either left recursion or
  2041. right recursion, but you should always use left recursion, because it
  2042. can parse a sequence of any number of elements with bounded stack
  2043. space. Right recursion uses up space on the Bison stack in proportion
  2044. to the number of elements in the sequence, because all the elements
  2045. must be shifted onto the stack before the rule can be applied even
  2046. once. @xref{Algorithm, ,The Bison Parser Algorithm }, for
  2047. further explanation of this.
  2048. @cindex mutual recursion
  2049. @dfn{Indirect} or @dfn{mutual} recursion occurs when the result of the
  2050. rule does not appear directly on its right hand side, but does appear
  2051. in rules for other nonterminals which do appear on its right hand
  2052. side.
  2053. For example:
  2054. @example
  2055. @group
  2056. expr: primary
  2057. | primary '+' primary
  2058. ;
  2059. @end group
  2060. @group
  2061. primary: constant
  2062. | '(' expr ')'
  2063. ;
  2064. @end group
  2065. @end example
  2066. @noindent
  2067. defines two mutually-recursive nonterminals, since each refers to the
  2068. other.
  2069. @node Semantics, Declarations, Recursion, Grammar File
  2070. @section Defining Language Semantics
  2071. @cindex defining language semantics
  2072. @cindex language semantics, defining
  2073. The grammar rules for a language determine only the syntax. The semantics
  2074. are determined by the semantic values associated with various tokens and
  2075. groupings, and by the actions taken when various groupings are recognized.
  2076. For example, the calculator calculates properly because the value
  2077. associated with each expression is the proper number; it adds properly
  2078. because the action for the grouping @w{@samp{@var{x} + @var{y}}} is to add
  2079. the numbers associated with @var{x} and @var{y}.
  2080. @menu
  2081. * Value Type:: Specifying one data type for all semantic values.
  2082. * Multiple Types:: Specifying several alternative data types.
  2083. * Actions:: An action is the semantic definition of a grammar rule.
  2084. * Action Types:: Specifying data types for actions to operate on.
  2085. * Mid-Rule Actions:: Most actions go at the end of a rule.
  2086. This says when, why and how to use the exceptional
  2087. action in the middle of a rule.
  2088. @end menu
  2089. @node Value Type, Multiple Types, , Semantics
  2090. @subsection Data Types of Semantic Values
  2091. @cindex semantic value type
  2092. @cindex value type, semantic
  2093. @cindex data types of semantic values
  2094. @cindex default data type
  2095. In a simple program it may be sufficient to use the same data type for
  2096. the semantic values of all language constructs. This was true in the
  2097. RPN and infix calculator examples (@pxref{RPN Calc, ,Reverse Polish Notation Calculator}).
  2098. Bison's default is to use type @code{int} for all semantic values. To
  2099. specify some other type, define @code{YYSTYPE} as a macro, like this:
  2100. @example
  2101. #define YYSTYPE double
  2102. @end example
  2103. @noindent
  2104. This macro definition must go in the C declarations section of the grammar
  2105. file (@pxref{Grammar Outline, ,Outline of a Bison Grammar}).
  2106. @node Multiple Types, Actions, Value Type, Semantics
  2107. @subsection More Than One Value Type
  2108. In most programs, you will need different data types for different kinds
  2109. of tokens and groupings. For example, a numeric constant may need type
  2110. @code{int} or @code{long}, while a string constant needs type @code{char *},
  2111. and an identifier might need a pointer to an entry in the symbol table.
  2112. To use more than one data type for semantic values in one parser, Bison
  2113. requires you to do two things:
  2114. @itemize @bullet
  2115. @item
  2116. Specify the entire collection of possible data types, with the
  2117. @code{%union} Bison declaration (@pxref{Union Decl, ,The Collection of Value Types}).
  2118. @item
  2119. Choose one of those types for each symbol (terminal or nonterminal)
  2120. for which semantic values are used. This is done for tokens with the
  2121. @code{%token} Bison declaration (@pxref{Token Decl, ,Token Type Names}) and for groupings
  2122. with the @code{%type} Bison declaration (@pxref{Type Decl, ,Nonterminal Symbols}).
  2123. @end itemize
  2124. @node Actions, Action Types, Multiple Types, Semantics
  2125. @subsection Actions
  2126. @cindex action
  2127. @vindex $$
  2128. @vindex $@var{n}
  2129. An action accompanies a syntactic rule and contains C code to be executed
  2130. each time an instance of that rule is recognized. The task of most actions
  2131. is to compute a semantic value for the grouping built by the rule from the
  2132. semantic values associated with tokens or smaller groupings.
  2133. An action consists of C statements surrounded by braces, much like a
  2134. compound statement in C. It can be placed at any position in the rule; it
  2135. is executed at that position. Most rules have just one action at the end
  2136. of the rule, following all the components. Actions in the middle of a rule
  2137. are tricky and used only for special purposes (@pxref{Mid-Rule Actions, ,Actions in Mid-Rule}).
  2138. The C code in an action can refer to the semantic values of the components
  2139. matched by the rule with the construct @code{$@var{n}}, which stands for
  2140. the value of the @var{n}th component. The semantic value for the grouping
  2141. being constructed is @code{$$}. (Bison translates both of these constructs
  2142. into array element references when it copies the actions into the parser
  2143. file.)
  2144. Here is a typical example:
  2145. @example
  2146. @group
  2147. exp: @dots{}
  2148. | exp '+' exp
  2149. @{ $$ = $1 + $3; @}
  2150. @end group
  2151. @end example
  2152. @noindent
  2153. This rule constructs an @code{exp} from two smaller @code{exp} groupings
  2154. connected by a plus-sign token. In the action, @code{$1} and @code{$3}
  2155. refer to the semantic values of the two component @code{exp} groupings,
  2156. which are the first and third symbols on the right hand side of the rule.
  2157. The sum is stored into @code{$$} so that it becomes the semantic value of
  2158. the addition-expression just recognized by the rule. If there were a
  2159. useful semantic value associated with the @samp{+} token, it could be
  2160. referred to as @code{$2}.@refill
  2161. @cindex default action
  2162. If you don't specify an action for a rule, Bison supplies a default:
  2163. @w{@code{$$ = $1}.} Thus, the value of the first symbol in the rule becomes
  2164. the value of the whole rule. Of course, the default rule is valid only
  2165. if the two data types match. There is no meaningful default action for
  2166. an empty rule; every empty rule must have an explicit action unless the
  2167. rule's value does not matter.
  2168. @code{$@var{n}} with @var{n} zero or negative is allowed for reference
  2169. to tokens and groupings on the stack @emph{before} those that match the
  2170. current rule. This is a very risky practice, and to use it reliably
  2171. you must be certain of the context in which the rule is applied. Here
  2172. is a case in which you can use this reliably:
  2173. @example
  2174. @group
  2175. foo: expr bar '+' expr @{ @dots{} @}
  2176. | expr bar '-' expr @{ @dots{} @}
  2177. ;
  2178. @end group
  2179. @group
  2180. bar: /* empty */
  2181. @{ previous_expr = $0; @}
  2182. ;
  2183. @end group
  2184. @end example
  2185. As long as @code{bar} is used only in the fashion shown here, @code{$0}
  2186. always refers to the @code{expr} which precedes @code{bar} in the
  2187. definition of @code{foo}.
  2188. @node Action Types, Mid-Rule Actions, Actions, Semantics
  2189. @subsection Data Types of Values in Actions
  2190. @cindex action data types
  2191. @cindex data types in actions
  2192. If you have chosen a single data type for semantic values, the @code{$$}
  2193. and @code{$@var{n}} constructs always have that data type.
  2194. If you have used @code{%union} to specify a variety of data types, then you
  2195. must declare a choice among these types for each terminal or nonterminal
  2196. symbol that can have a semantic value. Then each time you use @code{$$} or
  2197. @code{$@var{n}}, its data type is determined by which symbol it refers to
  2198. in the rule. In this example,@refill
  2199. @example
  2200. @group
  2201. exp: @dots{}
  2202. | exp '+' exp
  2203. @{ $$ = $1 + $3; @}
  2204. @end group
  2205. @end example
  2206. @noindent
  2207. @code{$1} and @code{$3} refer to instances of @code{exp}, so they all
  2208. have the data type declared for the nonterminal symbol @code{exp}. If
  2209. @code{$2} were used, it would have the data type declared for the
  2210. terminal symbol @code{'+'}, whatever that might be.@refill
  2211. Alternatively, you can specify the data type when you refer to the value,
  2212. by inserting @samp{<@var{type}>} after the @samp{$} at the beginning of the
  2213. reference. For example, if you have defined types as shown here:
  2214. @example
  2215. @group
  2216. %union @{
  2217. int itype;
  2218. double dtype;
  2219. @}
  2220. @end group
  2221. @end example
  2222. @noindent
  2223. then you can write @code{$<itype>1} to refer to the first subunit of the
  2224. rule as an integer, or @code{$<dtype>1} to refer to it as a double.
  2225. @node Mid-Rule Actions, , Action Types, Semantics
  2226. @subsection Actions in Mid-Rule
  2227. @cindex actions in mid-rule
  2228. @cindex mid-rule actions
  2229. Occasionally it is useful to put an action in the middle of a rule.
  2230. These actions are written just like usual end-of-rule actions, but they
  2231. are executed before the parser even recognizes the following components.
  2232. A mid-rule action may refer to the components preceding it using
  2233. @code{$@var{n}}, but it may not refer to subsequent components because
  2234. it is run before they are parsed.
  2235. The mid-rule action itself counts as one of the components of the rule.
  2236. This makes a difference when there is another action later in the same rule
  2237. (and usually there is another at the end): you have to count the actions
  2238. along with the symbols when working out which number @var{n} to use in
  2239. @code{$@var{n}}.
  2240. The mid-rule action can also have a semantic value. The action can set
  2241. its value with an assignment to @code{$$}, and actions later in the rule
  2242. can refer to the value using @code{$@var{n}}. Since there is no symbol
  2243. to name the action, there is no way to declare a data type for the value
  2244. in advance, so you must use the @samp{$<@dots{}>} construct to specify a
  2245. data type each time you refer to this value.
  2246. There is no way to set the value of the entire rule with a mid-rule
  2247. action, because assignments to @code{$$} do not have that effect. The
  2248. only way to set the value for the entire rule is with an ordinary action
  2249. at the end of the rule.
  2250. Here is an example from a hypothetical compiler, handling a @code{let}
  2251. statement that looks like @samp{let (@var{variable}) @var{statement}} and
  2252. serves to create a variable named @var{variable} temporarily for the
  2253. duration of @var{statement}. To parse this construct, we must put
  2254. @var{variable} into the symbol table while @var{statement} is parsed, then
  2255. remove it afterward. Here is how it is done:
  2256. @example
  2257. @group
  2258. stmt: LET '(' var ')'
  2259. @{ $<context>$ = push_context ();
  2260. declare_variable ($3); @}
  2261. stmt @{ $$ = $6;
  2262. pop_context ($<context>5); @}
  2263. @end group
  2264. @end example
  2265. @noindent
  2266. As soon as @samp{let (@var{variable})} has been recognized, the first
  2267. action is run. It saves a copy of the current semantic context (the
  2268. list of accessible variables) as its semantic value, using alternative
  2269. @code{context} in the data-type union. Then it calls
  2270. @code{declare_variable} to add the new variable to that list. Once the
  2271. first action is finished, the embedded statement @code{stmt} can be
  2272. parsed. Note that the mid-rule action is component number 5, so the
  2273. @samp{stmt} is component number 6.
  2274. After the embedded statement is parsed, its semantic value becomes the
  2275. value of the entire @code{let}-statement. Then the semantic value from the
  2276. earlier action is used to restore the prior list of variables. This
  2277. removes the temporary @code{let}-variable from the list so that it won't
  2278. appear to exist while the rest of the program is parsed.
  2279. Taking action before a rule is completely recognized often leads to
  2280. conflicts since the parser must commit to a parse in order to execute the
  2281. action. For example, the following two rules, without mid-rule actions,
  2282. can coexist in a working parser because the parser can shift the open-brace
  2283. token and look at what follows before deciding whether there is a
  2284. declaration or not:
  2285. @example
  2286. @group
  2287. compound: '@{' declarations statements '@}'
  2288. | '@{' statements '@}'
  2289. ;
  2290. @end group
  2291. @end example
  2292. @noindent
  2293. But when we add a mid-rule action as follows, the rules become nonfunctional:
  2294. @example
  2295. @group
  2296. compound: @{ prepare_for_local_variables (); @}
  2297. '@{' declarations statements '@}'
  2298. @end group
  2299. @group
  2300. | '@{' statements '@}'
  2301. ;
  2302. @end group
  2303. @end example
  2304. @noindent
  2305. Now the parser is forced to decide whether to run the mid-rule action
  2306. when it has read no farther than the open-brace. In other words, it
  2307. must commit to using one rule or the other, without sufficient
  2308. information to do it correctly. (The open-brace token is what is called
  2309. the @dfn{look-ahead} token at this time, since the parser is still
  2310. deciding what to do about it. @xref{Look-Ahead, ,Look-Ahead Tokens}.)
  2311. You might think that you could correct the problem by putting identical
  2312. actions into the two rules, like this:
  2313. @example
  2314. @group
  2315. compound: @{ prepare_for_local_variables (); @}
  2316. '@{' declarations statements '@}'
  2317. | @{ prepare_for_local_variables (); @}
  2318. '@{' statements '@}'
  2319. ;
  2320. @end group
  2321. @end example
  2322. @noindent
  2323. But this does not help, because Bison does not realize that the two actions
  2324. are identical. (Bison never tries to understand the C code in an action.)
  2325. If the grammar is such that a declaration can be distinguished from a
  2326. statement by the first token (which is true in C), then one solution which
  2327. does work is to put the action after the open-brace, like this:
  2328. @example
  2329. @group
  2330. compound: '@{' @{ prepare_for_local_variables (); @}
  2331. declarations statements '@}'
  2332. | '@{' statements '@}'
  2333. ;
  2334. @end group
  2335. @end example
  2336. @noindent
  2337. Now the first token of the following declaration or statement,
  2338. which would in any case tell Bison which rule to use, can still do so.
  2339. Another solution is to bury the action inside a nonterminal symbol which
  2340. serves as a subroutine:
  2341. @example
  2342. @group
  2343. subroutine: /* empty */
  2344. @{ prepare_for_local_variables (); @}
  2345. ;
  2346. @end group
  2347. @group
  2348. compound: subroutine
  2349. '@{' declarations statements '@}'
  2350. | subroutine
  2351. '@{' statements '@}'
  2352. ;
  2353. @end group
  2354. @end example
  2355. @noindent
  2356. Now Bison can execute the action in the rule for @code{subroutine} without
  2357. deciding which rule for @code{compound} it will eventually use. Note that
  2358. the action is now at the end of its rule. Any mid-rule action can be
  2359. converted to an end-of-rule action in this way, and this is what Bison
  2360. actually does to implement mid-rule actions.
  2361. @node Declarations, Multiple Parsers, Semantics, Grammar File
  2362. @section Bison Declarations
  2363. @cindex declarations, Bison
  2364. @cindex Bison declarations
  2365. The @dfn{Bison declarations} section of a Bison grammar defines the symbols
  2366. used in formulating the grammar and the data types of semantic values.
  2367. @xref{Symbols, ,Symbols - Terminal and Nonterminal}.
  2368. All token type names (but not single-character literal tokens such as
  2369. @code{'+'} and @code{'*'}) must be declared. Nonterminal symbols must be
  2370. declared if you need to specify which data type to use for the semantic
  2371. value (@pxref{Multiple Types, ,More Than One Value Type}).
  2372. The first rule in the file also specifies the start symbol, by default.
  2373. If you want some other symbol to be the start symbol, you must declare
  2374. it explicitly (@pxref{Language and Grammar, ,Languages and Context-Free Grammars}).
  2375. @menu
  2376. * Token Decl:: Declaring terminal symbols.
  2377. * Thong Decl:: Declaring terminals for multi-character tokens.
  2378. * Precedence Decl:: Declaring terminals with precedence and associativity.
  2379. * Union Decl:: Declaring the set of all semantic value types.
  2380. * Type Decl:: Declaring the choice of type for a nonterminal symbol.
  2381. * Expect Decl:: Suppressing warnings about shift/reduce conflicts.
  2382. * Start Decl:: Specifying the start symbol.
  2383. * Pure Decl:: Requesting a reentrant parser.
  2384. * Decl Summary:: Table of all Bison declarations.
  2385. @end menu
  2386. @node Token Decl, Thong Decl, , Declarations
  2387. @subsection Token Type Names
  2388. @cindex declaring token type names
  2389. @cindex token type names, declaring
  2390. @findex %token
  2391. The basic way to declare a token type name (terminal symbol) is as follows:
  2392. @example
  2393. %token @var{name}
  2394. @end example
  2395. Bison will convert this into a @code{#define} directive in
  2396. the parser, so that the function @code{yylex} (if it is in this file)
  2397. can use the name @var{name} to stand for this token type's code.
  2398. Alternatively, you can use @code{%left}, @code{%right}, or @code{%nonassoc}
  2399. instead of @code{%token}, if you wish to specify precedence.
  2400. @xref{Precedence Decl, ,Operator Precedence}.
  2401. You can explicitly specify the numeric code for a token type by appending
  2402. an integer value in the field immediately following the token name:
  2403. @example
  2404. %token NUM 300
  2405. @end example
  2406. @noindent
  2407. It is generally best, however, to let Bison choose the numeric codes for
  2408. all token types. Bison will automatically select codes that don't conflict
  2409. with each other or with ASCII characters.
  2410. In the event that the stack type is a union, you must augment the
  2411. @code{%token} or other token declaration to include the data type
  2412. alternative delimited by angle-brackets (@pxref{Multiple Types, ,More Than One Value Type}).
  2413. For example:
  2414. @example
  2415. @group
  2416. %union @{ /* define stack type */
  2417. double val;
  2418. symrec *tptr;
  2419. @}
  2420. %token <val> NUM /* define token NUM and its type */
  2421. @end group
  2422. @end example
  2423. @node Thong Decl, Precedence Decl, Token Decl, Declarations
  2424. @subsection Thong Declarations
  2425. @cindex thong declarations
  2426. @cindex multi-character terminal symbols
  2427. @cindex declaring thongs
  2428. @cindex declaring string literal tokens
  2429. WARNING: If you use thong declarations,
  2430. your grammar will NOT be compatible with Yacc.
  2431. The @code{%thong} declaration associates a symbol with a multiple-character
  2432. string literal token. The symbol will be entered in the #defines
  2433. generated by the @samp{-d} switch and may then be used to report the
  2434. token by the lexical analyzer.
  2435. The syntax of a thong declaration has four elements:
  2436. the keyword, the type (optional), the token name, and a quoted string
  2437. giving the contents of the thong. (Backslashes may be used as the
  2438. usual escapes in the string.)
  2439. For example, a C parser might specify
  2440. @example
  2441. %thong <operator> OR "||"
  2442. %thong <operator> LE "<="
  2443. %left OR "<="
  2444. @end example
  2445. @noindent
  2446. Either the symbolic or the string literal form may be used in
  2447. further declarations or the grammar rules.
  2448. Whether or not there is a @code{%thong} declaration, the symbolic name
  2449. does not appear in the generated @code{yytname} table; only the string
  2450. literal appears. Its corresponding token number is available as the index
  2451. of its entry in the @code{yytname} table generated in response
  2452. to the @samp{-k} or @samp{--token-table} switch.
  2453. @node Precedence Decl, Union Decl, Thong Decl, Declarations
  2454. @subsection Operator Precedence
  2455. @cindex precedence declarations
  2456. @cindex declaring operator precedence
  2457. @cindex operator precedence, declaring
  2458. Use the @code{%left}, @code{%right} or @code{%nonassoc} declaration to
  2459. declare a token and specify its precedence and associativity, all at
  2460. once. These are called @dfn{precedence declarations}.
  2461. @xref{Precedence, ,Operator Precedence}, for general information on operator precedence.
  2462. The syntax of a precedence declaration is the same as that of
  2463. @code{%token}: either
  2464. @example
  2465. %left @var{symbols}@dots{}
  2466. @end example
  2467. @noindent
  2468. or
  2469. @example
  2470. %left <@var{type}> @var{symbols}@dots{}
  2471. @end example
  2472. And indeed any of these declarations serves the purposes of @code{%token}.
  2473. But in addition, they specify the associativity and relative precedence for
  2474. all the @var{symbols}:
  2475. @itemize @bullet
  2476. @item
  2477. The associativity of an operator @var{op} determines how repeated uses
  2478. of the operator nest: whether @samp{@var{x} @var{op} @var{y} @var{op}
  2479. @var{z}} is parsed by grouping @var{x} with @var{y} first or by
  2480. grouping @var{y} with @var{z} first. @code{%left} specifies
  2481. left-associativity (grouping @var{x} with @var{y} first) and
  2482. @code{%right} specifies right-associativity (grouping @var{y} with
  2483. @var{z} first). @code{%nonassoc} specifies no associativity, which
  2484. means that @samp{@var{x} @var{op} @var{y} @var{op} @var{z}} is
  2485. considered a syntax error.
  2486. @item
  2487. The precedence of an operator determines how it nests with other operators.
  2488. All the tokens declared in a single precedence declaration have equal
  2489. precedence and nest together according to their associativity.
  2490. When two tokens declared in different precedence declarations associate,
  2491. the one declared later has the higher precedence and is grouped first.
  2492. @end itemize
  2493. @node Union Decl, Type Decl, Precedence Decl, Declarations
  2494. @subsection The Collection of Value Types
  2495. @cindex declaring value types
  2496. @cindex value types, declaring
  2497. @findex %union
  2498. The @code{%union} declaration specifies the entire collection of possible
  2499. data types for semantic values. The keyword @code{%union} is followed by a
  2500. pair of braces containing the same thing that goes inside a @code{union} in
  2501. C.
  2502. For example:
  2503. @example
  2504. @group
  2505. %union @{
  2506. double val;
  2507. symrec *tptr;
  2508. @}
  2509. @end group
  2510. @end example
  2511. @noindent
  2512. This says that the two alternative types are @code{double} and @code{symrec
  2513. *}. They are given names @code{val} and @code{tptr}; these names are used
  2514. in the @code{%token} and @code{%type} declarations to pick one of the types
  2515. for a terminal or nonterminal symbol (@pxref{Type Decl, ,Nonterminal Symbols}).
  2516. Note that, unlike making a @code{union} declaration in C, you do not write
  2517. a semicolon after the closing brace.
  2518. @node Type Decl, Expect Decl, Union Decl, Declarations
  2519. @subsection Nonterminal Symbols
  2520. @cindex declaring value types, nonterminals
  2521. @cindex value types, nonterminals, declaring
  2522. @findex %type
  2523. @noindent
  2524. When you use @code{%union} to specify multiple value types, you must
  2525. declare the value type of each nonterminal symbol for which values are
  2526. used. This is done with a @code{%type} declaration, like this:
  2527. @example
  2528. %type <@var{type}> @var{nonterminal}@dots{}
  2529. @end example
  2530. @noindent
  2531. Here @var{nonterminal} is the name of a nonterminal symbol, and @var{type}
  2532. is the name given in the @code{%union} to the alternative that you want
  2533. (@pxref{Union Decl, ,The Collection of Value Types}). You can give any number of nonterminal symbols in
  2534. the same @code{%type} declaration, if they have the same value type. Use
  2535. spaces to separate the symbol names.
  2536. The value types of symbols may be declared by putting
  2537. @code{<}@var{type}@code{>} after any of these declarators:
  2538. @code{%type}, @code{%token}, @code{%term}, @code{%nterm}, @code{%equiv},
  2539. @code{%assoc}, @code{%left}, @code{%right}, @code{%nonassoc}, @code{%binary}.
  2540. @node Expect Decl, Start Decl, Type Decl, Declarations
  2541. @subsection Suppressing Conflict Warnings
  2542. @cindex suppressing conflict warnings
  2543. @cindex preventing warnings about conflicts
  2544. @cindex warnings, preventing
  2545. @cindex conflicts, suppressing warnings of
  2546. @findex %expect
  2547. Bison normally warns if there are any conflicts in the grammar
  2548. (@pxref{Shift/Reduce, ,Shift/Reduce Conflicts}), but most real grammars have harmless shift/reduce
  2549. conflicts which are resolved in a predictable way and would be difficult to
  2550. eliminate. It is desirable to suppress the warning about these conflicts
  2551. unless the number of conflicts changes. You can do this with the
  2552. @code{%expect} declaration.
  2553. The declaration looks like this:
  2554. @example
  2555. %expect @var{n}
  2556. @end example
  2557. Here @var{n} is a decimal integer. The declaration says there should be no
  2558. warning if there are @var{n} shift/reduce conflicts and no reduce/reduce
  2559. conflicts. The usual warning is given if there are either more or fewer
  2560. conflicts, or if there are any reduce/reduce conflicts.
  2561. In general, using @code{%expect} involves these steps:
  2562. @itemize @bullet
  2563. @item
  2564. Compile your grammar without @code{%expect}. Use the @samp{-v} option
  2565. to get a verbose list of where the conflicts occur. Bison will also
  2566. print the number of conflicts.
  2567. @item
  2568. Check each of the conflicts to make sure that Bison's default
  2569. resolution is what you really want. If not, rewrite the grammar and
  2570. go back to the beginning.
  2571. @item
  2572. Add an @code{%expect} declaration, copying the number @var{n} from the
  2573. number which Bison printed.
  2574. @end itemize
  2575. Now Bison will stop annoying you about the conflicts you have checked, but
  2576. it will warn you again if changes in the grammar result in additional
  2577. conflicts.
  2578. @node Start Decl, Pure Decl, Expect Decl, Declarations
  2579. @subsection The Start-Symbol
  2580. @cindex declaring the start symbol
  2581. @cindex start symbol, declaring
  2582. @cindex default start symbol
  2583. @findex %start
  2584. Bison assumes by default that the start symbol for the grammar is the first
  2585. nonterminal specified in the grammar specification section. The programmer
  2586. may override this restriction with the @code{%start} declaration as follows:
  2587. @example
  2588. %start @var{symbol}
  2589. @end example
  2590. @node Pure Decl, Decl Summary, Start Decl, Declarations
  2591. @subsection A Pure (Reentrant) Parser
  2592. @cindex reentrant parser
  2593. @cindex pure parser
  2594. @findex %pure_parser
  2595. A @dfn{reentrant} program is one which does not alter in the course of
  2596. execution; in other words, it consists entirely of @dfn{pure} (read-only)
  2597. code. Reentrancy is important whenever asynchronous execution is possible;
  2598. for example, a nonreentrant program may not be safe to call from a signal
  2599. handler. In systems with multiple threads of control, a nonreentrant
  2600. program must be called only within interlocks.
  2601. The Bison parser is not normally a reentrant program, because it uses
  2602. statically allocated variables for communication with @code{yylex}. These
  2603. variables include @code{yylval} and @code{yylloc}.
  2604. The Bison declaration @code{%pure_parser} says that you want the parser
  2605. to be reentrant. It looks like this:
  2606. @example
  2607. %pure_parser
  2608. @end example
  2609. The effect is that the two communication variables become local
  2610. variables in @code{yyparse}, and a different calling convention is used for
  2611. the lexical analyzer function @code{yylex}. @xref{Pure Calling, ,Calling for Pure Parsers}, for the
  2612. details of this. The variable @code{yynerrs} also becomes local in
  2613. @code{yyparse} (@pxref{Error Reporting, ,The Error Reporting Function @code{yyerror}}). The convention for calling
  2614. @code{yyparse} itself is unchanged.
  2615. @node Decl Summary, , Pure Decl, Declarations
  2616. @subsection Bison Declaration Summary
  2617. @cindex Bison declaration summary
  2618. @cindex declaration summary
  2619. @cindex summary, Bison declaration
  2620. Here is a summary of all Bison declarations:
  2621. @table @code
  2622. @item %union
  2623. @item %union
  2624. Declare the collection of data types that semantic values may have
  2625. (@pxref{Union Decl, ,The Collection of Value Types}).
  2626. @item %token
  2627. Declare a terminal symbol (token type name) with no precedence
  2628. or associativity specified (@pxref{Token Decl, ,Token Type Names}).
  2629. @item %thong
  2630. Declare a token name to refer to a multiple-character string literal
  2631. token. (@pxref{Thong Decl, ,Thong Declarations}.)
  2632. @item %right
  2633. Declare a terminal symbol (token type name) that is right-associative
  2634. (@pxref{Precedence Decl, ,Operator Precedence}).
  2635. @item %left
  2636. Declare a terminal symbol (token type name) that is left-associative
  2637. (@pxref{Precedence Decl, ,Operator Precedence}).
  2638. @item %nonassoc
  2639. Declare a terminal symbol (token type name) that is nonassociative
  2640. (using it in a way that would be associative is a syntax error)
  2641. (@pxref{Precedence Decl, ,Operator Precedence}).
  2642. @item %type
  2643. Declare the type of semantic values for a nonterminal symbol
  2644. (@pxref{Type Decl, ,Nonterminal Symbols}).
  2645. @item %start
  2646. Specify the grammar's start symbol (@pxref{Start Decl, ,The Start-Symbol}).
  2647. @item %expect
  2648. Declare the expected number of shift-reduce conflicts
  2649. (@pxref{Expect Decl, ,Suppressing Conflict Warnings}).
  2650. @item %pure_parser
  2651. Request a pure (reentrant) parser program (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}).
  2652. @item %semantic_parser
  2653. This causes Bison to utilize a more sophisticated parsing machine.
  2654. It is only appropriate if you have specified @code{%guard@{...@}}
  2655. segments after one or more rules of the grammar.
  2656. @end table
  2657. @node Multiple Parsers,, Declarations, Grammar File
  2658. @section Multiple Parsers in the Same Program
  2659. Most programs that use Bison parse only one language and therefore contain
  2660. only one Bison parser. But what if you want to parse more than one
  2661. language with the same program? Then you need to avoid a name conflict
  2662. between different definitions of @code{yyparse}, @code{yylval}, and so on.
  2663. The easy way to do this is to use the option @samp{-p @var{prefix}}
  2664. (@pxref{Invocation, ,Invoking Bison}). This renames the interface functions and
  2665. variables of the Bison parser to start with @var{prefix} instead of
  2666. @samp{yy}. You can use this to give each parser distinct names that do
  2667. not conflict.
  2668. The precise list of symbols renamed is @code{yyparse}, @code{yylex},
  2669. @code{yyerror}, @code{yylval}, @code{yychar} and @code{yydebug}. For
  2670. example, if you use @samp{-p c}, the names become @code{cparse},
  2671. @code{clex}, and so on.
  2672. @strong{All the other variables and macros associated with Bison are not
  2673. renamed.} These others are not global; there is no conflict if the same
  2674. name is used in different parsers. For example, @code{YYSTYPE} is not
  2675. renamed, but defining this in different ways in different parsers causes
  2676. no trouble (@pxref{Value Type, ,Data Types of Semantic Values}).
  2677. The @samp{-p} option works by adding macro definitions to the beginning
  2678. of the parser source file, defining @code{yyparse} as
  2679. @code{@var{prefix}parse}, and so on. This effectively substitutes one
  2680. name for the other in the entire parser file.
  2681. @node Interface, Algorithm, Grammar File, Top
  2682. @chapter Parser C-Language Interface
  2683. @cindex C-language interface
  2684. @cindex interface
  2685. The Bison parser is actually a C function named @code{yyparse}. Here we
  2686. describe the interface conventions of @code{yyparse} and the other
  2687. functions that it needs to use.
  2688. Keep in mind that the parser uses many C identifiers starting with
  2689. @samp{yy} and @samp{YY} for internal purposes. If you use such an
  2690. identifier (aside from those in this manual) in an action or in additional
  2691. C code in the grammar file, you are likely to run into trouble.
  2692. @menu
  2693. * Parser Function:: How to call @code{yyparse} and what it returns.
  2694. * Lexical:: You must supply a function @code{yylex}
  2695. which reads tokens.
  2696. * Error Reporting:: You must supply a function @code{yyerror}.
  2697. * Action Features:: Special features for use in actions.
  2698. @end menu
  2699. @node Parser Function, Lexical, , Interface
  2700. @section The Parser Function @code{yyparse}
  2701. @findex yyparse
  2702. You call the function @code{yyparse} to cause parsing to occur. This
  2703. function reads tokens, executes actions, and ultimately returns when it
  2704. encounters end-of-input or an unrecoverable syntax error. You can also
  2705. write an action which directs @code{yyparse} to return immediately without
  2706. reading further.
  2707. The value returned by @code{yyparse} is 0 if parsing was successful (return
  2708. is due to end-of-input).
  2709. The value is 1 if parsing failed (return is due to a syntax error).
  2710. In an action, you can cause immediate return from @code{yyparse} by using
  2711. these macros:
  2712. @table @code
  2713. @item YYACCEPT
  2714. @findex YYACCEPT
  2715. Return immediately with value 0 (to report success).
  2716. @item YYABORT
  2717. @findex YYABORT
  2718. Return immediately with value 1 (to report failure).
  2719. @end table
  2720. @node Lexical, Error Reporting, Parser Function, Interface
  2721. @section The Lexical Analyzer Function @code{yylex}
  2722. @findex yylex
  2723. @cindex lexical analyzer
  2724. The @dfn{lexical analyzer} function, @code{yylex}, recognizes tokens from
  2725. the input stream and returns them to the parser. Bison does not create
  2726. this function automatically; you must write it so that @code{yyparse} can
  2727. call it. The function is sometimes referred to as a lexical scanner.
  2728. In simple programs, @code{yylex} is often defined at the end of the Bison
  2729. grammar file. If @code{yylex} is defined in a separate source file, you
  2730. need to arrange for the token-type macro definitions to be available there.
  2731. To do this, use the @samp{-d} option when you run Bison, so that it will
  2732. write these macro definitions into a separate header file
  2733. @file{@var{name}.tab.h} which you can include in the other source files
  2734. that need it. @xref{Invocation, ,Invoking Bison}.@refill
  2735. @menu
  2736. * Calling Convention:: How @code{yyparse} calls @code{yylex}.
  2737. * Token Values:: How @code{yylex} must return the semantic value
  2738. of the token it has read.
  2739. * Token Positions:: How @code{yylex} must return the text position
  2740. (line number, etc.) of the token, if the
  2741. actions want that.
  2742. * Pure Calling:: How the calling convention differs
  2743. in a pure parser (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}).
  2744. @end menu
  2745. @node Calling Convention, Token Values, , Lexical
  2746. @subsection Calling Convention for @code{yylex}
  2747. The value that @code{yylex} returns must be the numeric code for the type
  2748. of token it has just found, or 0 for end-of-input.
  2749. When a token is referred to in the grammar rules by a name, that name
  2750. in the parser file becomes a C macro whose definition is the proper
  2751. numeric code for that token type. So @code{yylex} can use the name
  2752. to indicate that type. @xref{Symbols, ,Symbols - Terminal and Nonterminal}.
  2753. When a token is referred to in the grammar rules by a character literal,
  2754. the numeric code for that character is also the code for the token type.
  2755. So @code{yylex} can simply return that character code. The null character
  2756. must not be used this way, because its code is zero and that is what
  2757. signifies end-of-input.
  2758. Here is an example showing these things:
  2759. @example
  2760. yylex ()
  2761. @{
  2762. @dots{}
  2763. if (c == EOF) /* Detect end of file. */
  2764. return 0;
  2765. @dots{}
  2766. if (c == '+' || c == '-')
  2767. return c; /* Assume token type for `+' is '+'. */
  2768. @dots{}
  2769. return INT; /* Return the type of the token. */
  2770. @dots{}
  2771. @}
  2772. @end example
  2773. @noindent
  2774. This interface has been designed so that the output from the @code{lex}
  2775. utility can be used without change as the definition of @code{yylex}.
  2776. @node Token Values, Token Positions, Calling Convention, Lexical
  2777. @subsection Semantic Values of Tokens
  2778. @vindex yylval
  2779. In an ordinary (nonreentrant) parser, the semantic value of the token must
  2780. be stored into the global variable @code{yylval}. When you are using
  2781. just one data type for semantic values, @code{yylval} has that type.
  2782. Thus, if the type is @code{int} (the default), you might write this in
  2783. @code{yylex}:
  2784. @example
  2785. @group
  2786. @dots{}
  2787. yylval = value; /* Put value onto Bison stack. */
  2788. return INT; /* Return the type of the token. */
  2789. @dots{}
  2790. @end group
  2791. @end example
  2792. When you are using multiple data types, @code{yylval}'s type is a union
  2793. made from the @code{%union} declaration (@pxref{Union Decl, ,The Collection of Value Types}). So when
  2794. you store a token's value, you must use the proper member of the union.
  2795. If the @code{%union} declaration looks like this:
  2796. @example
  2797. @group
  2798. %union @{
  2799. int intval;
  2800. double val;
  2801. symrec *tptr;
  2802. @}
  2803. @end group
  2804. @end example
  2805. @noindent
  2806. then the code in @code{yylex} might look like this:
  2807. @example
  2808. @group
  2809. @dots{}
  2810. yylval.intval = value; /* Put value onto Bison stack. */
  2811. return INT; /* Return the type of the token. */
  2812. @dots{}
  2813. @end group
  2814. @end example
  2815. @node Token Positions, Pure Calling, Token Values, Lexical
  2816. @subsection Textual Positions of Tokens
  2817. @vindex yylloc
  2818. If you are using the @samp{@@@var{n}}-feature (@pxref{Action Features, ,Special Features for Use in Actions}) in
  2819. actions to keep track of the textual locations of tokens and groupings,
  2820. then you must provide this information in @code{yylex}. The function
  2821. @code{yyparse} expects to find the textual location of a token just parsed
  2822. in the global variable @code{yylloc}. So @code{yylex} must store the
  2823. proper data in that variable. The value of @code{yylloc} is a structure
  2824. and you need only initialize the members that are going to be used by the
  2825. actions. The four members are called @code{first_line},
  2826. @code{first_column}, @code{last_line} and @code{last_column}. Note that
  2827. the use of this feature makes the parser noticeably slower.
  2828. @tindex YYLTYPE
  2829. The data type of @code{yylloc} has the name @code{YYLTYPE}.
  2830. @node Pure Calling, , Token Positions, Lexical
  2831. @subsection Calling for Pure Parsers
  2832. When you use the Bison declaration @code{%pure_parser} to request a pure,
  2833. reentrant parser, the global communication variables @code{yylval} and
  2834. @code{yylloc} cannot be used. (@xref{Pure Decl, ,A Pure (Reentrant) Parser}.) In such parsers the
  2835. two global variables are replaced by pointers passed as arguments to
  2836. @code{yylex}. You must declare them as shown here, and pass the
  2837. information back by storing it through those pointers.
  2838. @example
  2839. yylex (lvalp, llocp)
  2840. YYSTYPE *lvalp;
  2841. YYLTYPE *llocp;
  2842. @{
  2843. @dots{}
  2844. *lvalp = value; /* Put value onto Bison stack. */
  2845. return INT; /* Return the type of the token. */
  2846. @dots{}
  2847. @}
  2848. @end example
  2849. If the grammar file does not use the @samp{@@} constructs to refer to
  2850. textual positions, then the type @code{YYLTYPE} will not be defined. In
  2851. this case, omit the second argument; @code{yylex} will be called with
  2852. only one argument.
  2853. @node Error Reporting, Action Features, Lexical, Interface
  2854. @section The Error Reporting Function @code{yyerror}
  2855. @cindex error reporting function
  2856. @findex yyerror
  2857. @cindex parse error
  2858. @cindex syntax error
  2859. The Bison parser detects a @dfn{parse error} or @dfn{syntax error}
  2860. whenever it reads a token which cannot satisfy any syntax rule. A
  2861. action in the grammar can also explicitly proclaim an error, using the
  2862. macro @code{YYERROR} (@pxref{Action Features, ,Special Features for Use in Actions}).
  2863. The Bison parser expects to report the error by calling an error
  2864. reporting function named @code{yyerror}, which you must supply. It is
  2865. called by @code{yyparse} whenever a syntax error is found, and it
  2866. receives one argument. For a parse error, the string is normally
  2867. @w{@code{"parse error"}}.
  2868. @findex YYERROR_VERBOSE
  2869. If you define the macro @code{YYERROR_VERBOSE} in the Bison declarations
  2870. section (@pxref{Bison Declarations, ,The Bison Declarations Section}), then Bison provides a more verbose
  2871. and specific error message string instead of just plain @w{@code{"parse
  2872. error"}}. It doesn't matter what definition you use for
  2873. @code{YYERROR_VERBOSE}, just whether you define it.
  2874. The parser can detect one other kind of error: stack overflow. This
  2875. happens when the input contains constructions that are very deeply
  2876. nested. It isn't likely you will encounter this, since the Bison
  2877. parser extends its stack automatically up to a very large limit. But
  2878. if overflow happens, @code{yyparse} calls @code{yyerror} in the usual
  2879. fashion, except that the argument string is @w{@code{"parser stack
  2880. overflow"}}.
  2881. The following definition suffices in simple programs:
  2882. @example
  2883. @group
  2884. yyerror (s)
  2885. char *s;
  2886. @{
  2887. @end group
  2888. @group
  2889. fprintf (stderr, "%s\n", s);
  2890. @}
  2891. @end group
  2892. @end example
  2893. After @code{yyerror} returns to @code{yyparse}, the latter will attempt
  2894. error recovery if you have written suitable error recovery grammar rules
  2895. (@pxref{Error Recovery}). If recovery is impossible, @code{yyparse} will
  2896. immediately return 1.
  2897. @vindex yynerrs
  2898. The variable @code{yynerrs} contains the number of syntax errors
  2899. encountered so far. Normally this variable is global; but if you
  2900. request a pure parser (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}) then it is a local variable
  2901. which only the actions can access.
  2902. @node Action Features, , Error Reporting, Interface
  2903. @section Special Features for Use in Actions
  2904. @cindex summary, action features
  2905. @cindex action features summary
  2906. Here is a table of Bison constructs, variables and macros that
  2907. are useful in actions.
  2908. @table @samp
  2909. @item $$
  2910. Acts like a variable that contains the semantic value for the
  2911. grouping made by the current rule. @xref{Actions}.
  2912. @item $@var{n}
  2913. Acts like a variable that contains the semantic value for the
  2914. @var{n}th component of the current rule. @xref{Actions}.
  2915. @item $<@var{typealt}>$
  2916. Like @code{$$} but specifies alternative @var{typealt} in the union
  2917. specified by the @code{%union} declaration. @xref{Action Types, ,Data Types of Values in Actions}.
  2918. @item $<@var{typealt}>@var{n}
  2919. Like @code{$@var{n}} but specifies alternative @var{typealt} in the
  2920. union specified by the @code{%union} declaration.
  2921. @xref{Action Types, ,Data Types of Values in Actions}.@refill
  2922. @item YYABORT;
  2923. Return immediately from @code{yyparse}, indicating failure.
  2924. @xref{Parser Function, ,The Parser Function @code{yyparse}}.
  2925. @item YYACCEPT;
  2926. Return immediately from @code{yyparse}, indicating success.
  2927. @xref{Parser Function, ,The Parser Function @code{yyparse}}.
  2928. @item YYBACKUP (@var{token}, @var{value});
  2929. @findex YYBACKUP
  2930. Unshift a token. This macro is allowed only for rules that reduce
  2931. a single value, and only when there is no look-ahead token.
  2932. It installs a look-ahead token with token type @var{token} and
  2933. semantic value @var{value}; then it discards the value that was
  2934. going to be reduced by this rule.
  2935. If the macro is used when it is not valid, such as when there is
  2936. a look-ahead token already, then it reports a syntax error with
  2937. a message @samp{cannot back up} and performs ordinary error
  2938. recovery.
  2939. In either case, the rest of the action is not executed.
  2940. @item YYEMPTY
  2941. @vindex YYEMPTY
  2942. Value stored in @code{yychar} when there is no look-ahead token.
  2943. @item YYERROR;
  2944. @findex YYERROR
  2945. Cause an immediate syntax error. This statement initiates error
  2946. recovery just as if the parser itself had detected an error; however, it
  2947. does not call @code{yyerror}, and does not print any message. If you
  2948. want to print an error message, call @code{yyerror} explicitly before
  2949. the @samp{YYERROR;} statement. @xref{Error Recovery}.
  2950. @item YYRECOVERING
  2951. This macro stands for an expression that has the value 1 when the parser
  2952. is recovering from a syntax error, and 0 the rest of the time.
  2953. @xref{Error Recovery}.
  2954. @item yychar
  2955. Variable containing the current look-ahead token. (In a pure parser,
  2956. this is actually a local variable within @code{yyparse}.) When there is
  2957. no look-ahead token, the value @code{YYEMPTY} is stored in the variable.
  2958. @xref{Look-Ahead, ,Look-Ahead Tokens}.
  2959. @item yyclearin;
  2960. Discard the current look-ahead token. This is useful primarily in
  2961. error rules. @xref{Error Recovery}.
  2962. @item yyerrok;
  2963. Resume generating error messages immediately for subsequent syntax
  2964. errors. This is useful primarily in error rules.
  2965. @xref{Error Recovery}.
  2966. @item @@@var{n}
  2967. @findex @@@var{n}
  2968. Acts like a structure variable containing information on the line
  2969. numbers and column numbers of the @var{n}th component of the current
  2970. rule. The structure has four members, like this:
  2971. @example
  2972. struct @{
  2973. int first_line, last_line;
  2974. int first_column, last_column;
  2975. @};
  2976. @end example
  2977. Thus, to get the starting line number of the third component, use
  2978. @samp{@@3.first_line}.
  2979. In order for the members of this structure to contain valid information,
  2980. you must make @code{yylex} supply this information about each token.
  2981. If you need only certain members, then @code{yylex} need only fill in
  2982. those members.
  2983. The use of this feature makes the parser noticeably slower.
  2984. @end table
  2985. @node Algorithm, Error Recovery, Interface, Top
  2986. @chapter The Bison Parser Algorithm
  2987. @cindex Bison parser algorithm
  2988. @cindex algorithm of parser
  2989. @cindex shifting
  2990. @cindex reduction
  2991. @cindex parser stack
  2992. @cindex stack, parser
  2993. As Bison reads tokens, it pushes them onto a stack along with their
  2994. semantic values. The stack is called the @dfn{parser stack}. Pushing a
  2995. token is traditionally called @dfn{shifting}.
  2996. For example, suppose the infix calculator has read @samp{1 + 5 *}, with a
  2997. @samp{3} to come. The stack will have four elements, one for each token
  2998. that was shifted.
  2999. But the stack does not always have an element for each token read. When
  3000. the last @var{n} tokens and groupings shifted match the components of a
  3001. grammar rule, they can be combined according to that rule. This is called
  3002. @dfn{reduction}. Those tokens and groupings are replaced on the stack by a
  3003. single grouping whose symbol is the result (left hand side) of that rule.
  3004. Running the rule's action is part of the process of reduction, because this
  3005. is what computes the semantic value of the resulting grouping.
  3006. For example, if the infix calculator's parser stack contains this:
  3007. @example
  3008. 1 + 5 * 3
  3009. @end example
  3010. @noindent
  3011. and the next input token is a newline character, then the last three
  3012. elements can be reduced to 15 via the rule:
  3013. @example
  3014. expr: expr '*' expr;
  3015. @end example
  3016. @noindent
  3017. Then the stack contains just these three elements:
  3018. @example
  3019. 1 + 15
  3020. @end example
  3021. @noindent
  3022. At this point, another reduction can be made, resulting in the single value
  3023. 16. Then the newline token can be shifted.
  3024. The parser tries, by shifts and reductions, to reduce the entire input down
  3025. to a single grouping whose symbol is the grammar's start-symbol
  3026. (@pxref{Language and Grammar, ,Languages and Context-Free Grammars}).
  3027. This kind of parser is known in the literature as a bottom-up parser.
  3028. @menu
  3029. * Look-Ahead:: Parser looks one token ahead when deciding what to do.
  3030. * Shift/Reduce:: Conflicts: when either shifting or reduction is valid.
  3031. * Precedence:: Operator precedence works by resolving conflicts.
  3032. * Contextual Precedence:: When an operator's precedence depends on context.
  3033. * Parser States:: The parser is a finite-state-machine with stack.
  3034. * Reduce/Reduce:: When two rules are applicable in the same situation.
  3035. * Mystery Conflicts:: Reduce/reduce conflicts that look unjustified.
  3036. * Stack Overflow:: What happens when stack gets full. How to avoid it.
  3037. @end menu
  3038. @node Look-Ahead, Shift/Reduce, , Algorithm
  3039. @section Look-Ahead Tokens
  3040. @cindex look-ahead token
  3041. The Bison parser does @emph{not} always reduce immediately as soon as the
  3042. last @var{n} tokens and groupings match a rule. This is because such a
  3043. simple strategy is inadequate to handle most languages. Instead, when a
  3044. reduction is possible, the parser sometimes ``looks ahead'' at the next
  3045. token in order to decide what to do.
  3046. When a token is read, it is not immediately shifted; first it becomes the
  3047. @dfn{look-ahead token}, which is not on the stack. Now the parser can
  3048. perform one or more reductions of tokens and groupings on the stack, while
  3049. the look-ahead token remains off to the side. When no more reductions
  3050. should take place, the look-ahead token is shifted onto the stack. This
  3051. does not mean that all possible reductions have been done; depending on the
  3052. token type of the look-ahead token, some rules may choose to delay their
  3053. application.
  3054. Here is a simple case where look-ahead is needed. These three rules define
  3055. expressions which contain binary addition operators and postfix unary
  3056. factorial operators (@samp{!}), and allow parentheses for grouping.
  3057. @example
  3058. @group
  3059. expr: term '+' expr
  3060. | term
  3061. ;
  3062. @end group
  3063. @group
  3064. term: '(' expr ')'
  3065. | term '!'
  3066. | NUMBER
  3067. ;
  3068. @end group
  3069. @end example
  3070. Suppose that the tokens @w{@samp{1 + 2}} have been read and shifted; what
  3071. should be done? If the following token is @samp{)}, then the first three
  3072. tokens must be reduced to form an @code{expr}. This is the only valid
  3073. course, because shifting the @samp{)} would produce a sequence of symbols
  3074. @w{@code{term ')'}}, and no rule allows this.
  3075. If the following token is @samp{!}, then it must be shifted immediately so
  3076. that @w{@samp{2 !}} can be reduced to make a @code{term}. If instead the
  3077. parser were to reduce before shifting, @w{@samp{1 + 2}} would become an
  3078. @code{expr}. It would then be impossible to shift the @samp{!} because
  3079. doing so would produce on the stack the sequence of symbols @code{expr
  3080. '!'}. No rule allows that sequence.
  3081. @vindex yychar
  3082. The current look-ahead token is stored in the variable @code{yychar}.
  3083. @xref{Action Features, ,Special Features for Use in Actions}.
  3084. @node Shift/Reduce, Precedence, Look-Ahead, Algorithm
  3085. @section Shift/Reduce Conflicts
  3086. @cindex conflicts
  3087. @cindex shift/reduce conflicts
  3088. @cindex dangling @code{else}
  3089. @cindex @code{else}, dangling
  3090. Suppose we are parsing a language which has if-then and if-then-else
  3091. statements, with a pair of rules like this:
  3092. @example
  3093. @group
  3094. if_stmt:
  3095. IF expr THEN stmt
  3096. | IF expr THEN stmt ELSE stmt
  3097. ;
  3098. @end group
  3099. @end example
  3100. @noindent
  3101. Here we assume that @code{IF}, @code{THEN} and @code{ELSE} are
  3102. terminal symbols for specific keyword tokens.
  3103. When the @code{ELSE} token is read and becomes the look-ahead token, the
  3104. contents of the stack (assuming the input is valid) are just right for
  3105. reduction by the first rule. But it is also legitimate to shift the
  3106. @code{ELSE}, because that would lead to eventual reduction by the second
  3107. rule.
  3108. This situation, where either a shift or a reduction would be valid, is
  3109. called a @dfn{shift/reduce conflict}. Bison is designed to resolve
  3110. these conflicts by choosing to shift, unless otherwise directed by
  3111. operator precedence declarations. To see the reason for this, let's
  3112. contrast it with the other alternative.
  3113. Since the parser prefers to shift the @code{ELSE}, the result is to attach
  3114. the else-clause to the innermost if-statement, making these two inputs
  3115. equivalent:
  3116. @example
  3117. if x then if y then win (); else lose;
  3118. if x then do; if y then win (); else lose; end;
  3119. @end example
  3120. But if the parser chose to reduce when possible rather than shift, the
  3121. result would be to attach the else-clause to the outermost if-statement,
  3122. making these two inputs equivalent:
  3123. @example
  3124. if x then if y then win (); else lose;
  3125. if x then do; if y then win (); end; else lose;
  3126. @end example
  3127. The conflict exists because the grammar as written is ambiguous: either
  3128. parsing of the simple nested if-statement is legitimate. The established
  3129. convention is that these ambiguities are resolved by attaching the
  3130. else-clause to the innermost if-statement; this is what Bison accomplishes
  3131. by choosing to shift rather than reduce. (It would ideally be cleaner to
  3132. write an unambiguous grammar, but that is very hard to do in this case.)
  3133. This particular ambiguity was first encountered in the specifications of
  3134. Algol 60 and is called the ``dangling @code{else}'' ambiguity.
  3135. To avoid warnings from Bison about predictable, legitimate shift/reduce
  3136. conflicts, use the @code{%expect @var{n}} declaration. There will be no
  3137. warning as long as the number of shift/reduce conflicts is exactly @var{n}.
  3138. @xref{Expect Decl, ,Suppressing Conflict Warnings}.
  3139. The definition of @code{if_stmt} above is solely to blame for the
  3140. conflict, but the conflict does not actually appear without additional
  3141. rules. Here is a complete Bison input file that actually manifests the
  3142. conflict:
  3143. @example
  3144. @group
  3145. %token IF THEN ELSE variable
  3146. %%
  3147. @end group
  3148. @group
  3149. stmt: expr
  3150. | if_stmt
  3151. ;
  3152. @end group
  3153. @group
  3154. if_stmt:
  3155. IF expr THEN stmt
  3156. | IF expr THEN stmt ELSE stmt
  3157. ;
  3158. @end group
  3159. expr: variable
  3160. ;
  3161. @end example
  3162. @node Precedence, Contextual Precedence, Shift/Reduce, Algorithm
  3163. @section Operator Precedence
  3164. @cindex operator precedence
  3165. @cindex precedence of operators
  3166. Another situation where shift/reduce conflicts appear is in arithmetic
  3167. expressions. Here shifting is not always the preferred resolution; the
  3168. Bison declarations for operator precedence allow you to specify when to
  3169. shift and when to reduce.
  3170. @menu
  3171. * Why Precedence:: An example showing why precedence is needed.
  3172. * Using Precedence:: How to specify precedence in Bison grammars.
  3173. * Precedence Examples:: How these features are used in the previous example.
  3174. * How Precedence:: How they work.
  3175. @end menu
  3176. @node Why Precedence, Using Precedence, , Precedence
  3177. @subsection When Precedence is Needed
  3178. Consider the following ambiguous grammar fragment (ambiguous because the
  3179. input @w{@samp{1 - 2 * 3}} can be parsed in two different ways):
  3180. @example
  3181. @group
  3182. expr: expr '-' expr
  3183. | expr '*' expr
  3184. | expr '<' expr
  3185. | '(' expr ')'
  3186. @dots{}
  3187. ;
  3188. @end group
  3189. @end example
  3190. @noindent
  3191. Suppose the parser has seen the tokens @samp{1}, @samp{-} and @samp{2};
  3192. should it reduce them via the rule for the addition operator? It depends
  3193. on the next token. Of course, if the next token is @samp{)}, we must
  3194. reduce; shifting is invalid because no single rule can reduce the token
  3195. sequence @w{@samp{- 2 )}} or anything starting with that. But if the next
  3196. token is @samp{*} or @samp{<}, we have a choice: either shifting or
  3197. reduction would allow the parse to complete, but with different
  3198. results.
  3199. To decide which one Bison should do, we must consider the
  3200. results. If the next operator token @var{op} is shifted, then it
  3201. must be reduced first in order to permit another opportunity to
  3202. reduce the sum. The result is (in effect) @w{@samp{1 - (2
  3203. @var{op} 3)}}. On the other hand, if the subtraction is reduced
  3204. before shifting @var{op}, the result is @w{@samp{(1 - 2) @var{op}
  3205. 3}}. Clearly, then, the choice of shift or reduce should depend
  3206. on the relative precedence of the operators @samp{-} and
  3207. @var{op}: @samp{*} should be shifted first, but not @samp{<}.
  3208. @cindex associativity
  3209. What about input such as @w{@samp{1 - 2 - 5}}; should this be
  3210. @w{@samp{(1 - 2) - 5}} or should it be @w{@samp{1 - (2 - 5)}}? For
  3211. most operators we prefer the former, which is called @dfn{left
  3212. association}. The latter alternative, @dfn{right association}, is
  3213. desirable for assignment operators. The choice of left or right
  3214. association is a matter of whether the parser chooses to shift or
  3215. reduce when the stack contains @w{@samp{1 - 2}} and the look-ahead
  3216. token is @samp{-}: shifting makes right-associativity.
  3217. @node Using Precedence, Precedence Examples, Why Precedence, Precedence
  3218. @subsection Specifying Operator Precedence
  3219. @findex %left
  3220. @findex %right
  3221. @findex %nonassoc
  3222. Bison allows you to specify these choices with the operator precedence
  3223. declarations @code{%left} and @code{%right}. Each such declaration
  3224. contains a list of tokens, which are operators whose precedence and
  3225. associativity is being declared. The @code{%left} declaration makes all
  3226. those operators left-associative and the @code{%right} declaration makes
  3227. them right-associative. A third alternative is @code{%nonassoc}, which
  3228. declares that it is a syntax error to find the same operator twice ``in a
  3229. row''.
  3230. The relative precedence of different operators is controlled by the
  3231. order in which they are declared. The first @code{%left} or
  3232. @code{%right} declaration in the file declares the operators whose
  3233. precedence is lowest, the next such declaration declares the operators
  3234. whose precedence is a little higher, and so on.
  3235. @node Precedence Examples, How Precedence, Using Precedence, Precedence
  3236. @subsection Precedence Examples
  3237. In our example, we would want the following declarations:
  3238. @example
  3239. %left '<'
  3240. %left '-'
  3241. %left '*'
  3242. @end example
  3243. In a more complete example, which supports other operators as well, we
  3244. would declare them in groups of equal precedence. For example, @code{'+'} is
  3245. declared with @code{'-'}:
  3246. @example
  3247. %left '<' '>' '=' NE LE GE
  3248. %left '+' '-'
  3249. %left '*' '/'
  3250. @end example
  3251. @noindent
  3252. (Here @code{NE} and so on stand for the operators for ``not equal''
  3253. and so on. We assume that these tokens are more than one character long
  3254. and therefore are represented by names, not character literals.)
  3255. @node How Precedence, , Precedence Examples, Precedence
  3256. @subsection How Precedence Works
  3257. The first effect of the precedence declarations is to assign precedence
  3258. levels to the terminal symbols declared. The second effect is to assign
  3259. precedence levels to certain rules: each rule gets its precedence from the
  3260. last terminal symbol mentioned in the components. (You can also specify
  3261. explicitly the precedence of a rule. @xref{Contextual Precedence, ,Context-Dependent Precedence}.)
  3262. Finally, the resolution of conflicts works by comparing the
  3263. precedence of the rule being considered with that of the
  3264. look-ahead token. If the token's precedence is higher, the
  3265. choice is to shift. If the rule's precedence is higher, the
  3266. choice is to reduce. If they have equal precedence, the choice
  3267. is made based on the associativity of that precedence level. The
  3268. verbose output file made by @samp{-v} (@pxref{Invocation, ,Invoking Bison}) says
  3269. how each conflict was resolved.
  3270. Not all rules and not all tokens have precedence. If either the rule or
  3271. the look-ahead token has no precedence, then the default is to shift.
  3272. @node Contextual Precedence, Parser States, Precedence, Algorithm
  3273. @section Context-Dependent Precedence
  3274. @cindex context-dependent precedence
  3275. @cindex unary operator precedence
  3276. @cindex precedence, context-dependent
  3277. @cindex precedence, unary operator
  3278. @findex %prec
  3279. Often the precedence of an operator depends on the context. This sounds
  3280. outlandish at first, but it is really very common. For example, a minus
  3281. sign typically has a very high precedence as a unary operator, and a
  3282. somewhat lower precedence (lower than multiplication) as a binary operator.
  3283. The Bison precedence declarations, @code{%left}, @code{%right} and
  3284. @code{%nonassoc}, can only be used once for a given token; so a token has
  3285. only one precedence declared in this way. For context-dependent
  3286. precedence, you need to use an additional mechanism: the @code{%prec}
  3287. modifier for rules.@refill
  3288. The @code{%prec} modifier declares the precedence of a particular rule by
  3289. specifying a terminal symbol whose precedence should be used for that rule.
  3290. It's not necessary for that symbol to appear otherwise in the rule. The
  3291. modifier's syntax is:
  3292. @example
  3293. %prec @var{terminal-symbol}
  3294. @end example
  3295. @noindent
  3296. and it is written after the components of the rule. Its effect is to
  3297. assign the rule the precedence of @var{terminal-symbol}, overriding
  3298. the precedence that would be deduced for it in the ordinary way. The
  3299. altered rule precedence then affects how conflicts involving that rule
  3300. are resolved (@pxref{Precedence, ,Operator Precedence}).
  3301. Here is how @code{%prec} solves the problem of unary minus. First, declare
  3302. a precedence for a fictitious terminal symbol named @code{UMINUS}. There
  3303. are no tokens of this type, but the symbol serves to stand for its
  3304. precedence:
  3305. @example
  3306. @dots{}
  3307. %left '+' '-'
  3308. %left '*'
  3309. %left UMINUS
  3310. @end example
  3311. Now the precedence of @code{UMINUS} can be used in specific rules:
  3312. @example
  3313. @group
  3314. exp: @dots{}
  3315. | exp '-' exp
  3316. @dots{}
  3317. | '-' exp %prec UMINUS
  3318. @end group
  3319. @end example
  3320. @node Parser States, Reduce/Reduce, Contextual Precedence, Algorithm
  3321. @section Parser States
  3322. @cindex finite-state machine
  3323. @cindex parser state
  3324. @cindex state (of parser)
  3325. The function @code{yyparse} is implemented using a finite-state machine.
  3326. The values pushed on the parser stack are not simply token type codes; they
  3327. represent the entire sequence of terminal and nonterminal symbols at or
  3328. near the top of the stack. The current state collects all the information
  3329. about previous input which is relevant to deciding what to do next.
  3330. Each time a look-ahead token is read, the current parser state together
  3331. with the type of look-ahead token are looked up in a table. This table
  3332. entry can say, ``Shift the look-ahead token.'' In this case, it also
  3333. specifies the new parser state, which is pushed onto the top of the
  3334. parser stack. Or it can say, ``Reduce using rule number @var{n}.''
  3335. This means that a certain number of tokens or groupings are taken off
  3336. the top of the stack, and replaced by one grouping. In other words,
  3337. that number of states are popped from the stack, and one new state is
  3338. pushed.
  3339. There is one other alternative: the table can say that the look-ahead token
  3340. is erroneous in the current state. This causes error processing to begin
  3341. (@pxref{Error Recovery}).
  3342. @node Reduce/Reduce, Mystery Conflicts, Parser States, Algorithm
  3343. @section Reduce/Reduce Conflicts
  3344. @cindex reduce/reduce conflict
  3345. @cindex conflicts, reduce/reduce
  3346. A reduce/reduce conflict occurs if there are two or more rules that apply
  3347. to the same sequence of input. This usually indicates a serious error
  3348. in the grammar.
  3349. For example, here is an erroneous attempt to define a sequence
  3350. of zero or more @code{word} groupings.
  3351. @example
  3352. sequence: /* empty */
  3353. @{ printf ("empty sequence\n"); @}
  3354. | maybeword
  3355. | sequence word
  3356. @{ printf ("added word %s\n", $2); @}
  3357. ;
  3358. maybeword: /* empty */
  3359. @{ printf ("empty maybeword\n"); @}
  3360. | word
  3361. @{ printf ("single word %s\n", $1); @}
  3362. ;
  3363. @end example
  3364. @noindent
  3365. The error is an ambiguity: there is more than one way to parse a single
  3366. @code{word} into a @code{sequence}. It could be reduced to a
  3367. @code{maybeword} and then into a @code{sequence} via the second rule.
  3368. Alternatively, nothing-at-all could be reduced into a @code{sequence}
  3369. via the first rule, and this could be combined with the @code{word}
  3370. using the third rule for @code{sequence}.
  3371. There is also more than one way to reduce nothing-at-all into a
  3372. @code{sequence}. This can be done directly via the first rule,
  3373. or indirectly via @code{maybeword} and then the second rule.
  3374. You might think that this is a distinction without a difference, because it
  3375. does not change whether any particular input is valid or not. But it does
  3376. affect which actions are run. One parsing order runs the second rule's
  3377. action; the other runs the first rule's action and the third rule's action.
  3378. In this example, the output of the program changes.
  3379. Bison resolves a reduce/reduce conflict by choosing to use the rule that
  3380. appears first in the grammar, but it is very risky to rely on this. Every
  3381. reduce/reduce conflict must be studied and usually eliminated. Here is the
  3382. proper way to define @code{sequence}:
  3383. @example
  3384. sequence: /* empty */
  3385. @{ printf ("empty sequence\n"); @}
  3386. | sequence word
  3387. @{ printf ("added word %s\n", $2); @}
  3388. ;
  3389. @end example
  3390. Here is another common error that yields a reduce/reduce conflict:
  3391. @example
  3392. sequence: /* empty */
  3393. | sequence words
  3394. | sequence redirects
  3395. ;
  3396. words: /* empty */
  3397. | words word
  3398. ;
  3399. redirects:/* empty */
  3400. | redirects redirect
  3401. ;
  3402. @end example
  3403. @noindent
  3404. The intention here is to define a sequence which can contain either
  3405. @code{word} or @code{redirect} groupings. The individual definitions of
  3406. @code{sequence}, @code{words} and @code{redirects} are error-free, but the
  3407. three together make a subtle ambiguity: even an empty input can be parsed
  3408. in infinitely many ways!
  3409. Consider: nothing-at-all could be a @code{words}. Or it could be two
  3410. @code{words} in a row, or three, or any number. It could equally well be a
  3411. @code{redirects}, or two, or any number. Or it could be a @code{words}
  3412. followed by three @code{redirects} and another @code{words}. And so on.
  3413. Here are two ways to correct these rules. First, to make it a single level
  3414. of sequence:
  3415. @example
  3416. sequence: /* empty */
  3417. | sequence word
  3418. | sequence redirect
  3419. ;
  3420. @end example
  3421. Second, to prevent either a @code{words} or a @code{redirects}
  3422. from being empty:
  3423. @example
  3424. sequence: /* empty */
  3425. | sequence words
  3426. | sequence redirects
  3427. ;
  3428. words: word
  3429. | words word
  3430. ;
  3431. redirects:redirect
  3432. | redirects redirect
  3433. ;
  3434. @end example
  3435. @node Mystery Conflicts, Stack Overflow, Reduce/Reduce, Algorithm
  3436. @section Mysterious Reduce/Reduce Conflicts
  3437. Sometimes reduce/reduce conflicts can occur that don't look warranted.
  3438. Here is an example:
  3439. @example
  3440. @group
  3441. %token ID
  3442. %%
  3443. def: param_spec return_spec ','
  3444. ;
  3445. param_spec:
  3446. type
  3447. | name_list ':' type
  3448. ;
  3449. @end group
  3450. @group
  3451. return_spec:
  3452. type
  3453. | name ':' type
  3454. ;
  3455. @end group
  3456. @group
  3457. type: ID
  3458. ;
  3459. @end group
  3460. @group
  3461. name: ID
  3462. ;
  3463. name_list:
  3464. name
  3465. | name ',' name_list
  3466. ;
  3467. @end group
  3468. @end example
  3469. It would seem that this grammar can be parsed with only a single token
  3470. of look-ahead: when a @code{param_spec} is being read, an @code{ID} is
  3471. a @code{name} if a comma or colon follows, or a @code{type} if another
  3472. @code{ID} follows. In other words, this grammar is LR(1).
  3473. @cindex LR(1)
  3474. @cindex LALR(1)
  3475. However, Bison, like most parser generators, cannot actually handle all
  3476. LR(1) grammars. In this grammar, two contexts, that after an @code{ID}
  3477. at the beginning of a @code{param_spec} and likewise at the beginning of
  3478. a @code{return_spec}, are similar enough that Bison assumes they are the
  3479. same. They appear similar because the same set of rules would be
  3480. active---the rule for reducing to a @code{name} and that for reducing to
  3481. a @code{type}. Bison is unable to determine at that stage of processing
  3482. that the rules would require different look-ahead tokens in the two
  3483. contexts, so it makes a single parser state for them both. Combining
  3484. the two contexts causes a conflict later. In parser terminology, this
  3485. occurrence means that the grammar is not LALR(1).
  3486. In general, it is better to fix deficiencies than to document them. But
  3487. this particular deficiency is intrinsically hard to fix; parser
  3488. generators that can handle LR(1) grammars are hard to write and tend to
  3489. produce parsers that are very large. In practice, Bison is more useful
  3490. as it is now.
  3491. When the problem arises, you can often fix it by identifying the two
  3492. parser states that are being confused, and adding something to make them
  3493. look distinct. In the above example, adding one rule to
  3494. @code{return_spec} as follows makes the problem go away:
  3495. @example
  3496. @group
  3497. %token BOGUS
  3498. @dots{}
  3499. %%
  3500. @dots{}
  3501. return_spec:
  3502. type
  3503. | name ':' type
  3504. /* This rule is never used. */
  3505. | ID BOGUS
  3506. ;
  3507. @end group
  3508. @end example
  3509. This corrects the problem because it introduces the possibility of an
  3510. additional active rule in the context after the @code{ID} at the beginning of
  3511. @code{return_spec}. This rule is not active in the corresponding context
  3512. in a @code{param_spec}, so the two contexts receive distinct parser states.
  3513. As long as the token @code{BOGUS} is never generated by @code{yylex},
  3514. the added rule cannot alter the way actual input is parsed.
  3515. In this particular example, there is another way to solve the problem:
  3516. rewrite the rule for @code{return_spec} to use @code{ID} directly
  3517. instead of via @code{name}. This also causes the two confusing
  3518. contexts to have different sets of active rules, because the one for
  3519. @code{return_spec} activates the altered rule for @code{return_spec}
  3520. rather than the one for @code{name}.
  3521. @example
  3522. param_spec:
  3523. type
  3524. | name_list ':' type
  3525. ;
  3526. return_spec:
  3527. type
  3528. | ID ':' type
  3529. ;
  3530. @end example
  3531. @node Stack Overflow, , Mystery Conflicts, Algorithm
  3532. @section Stack Overflow, and How to Avoid It
  3533. @cindex stack overflow
  3534. @cindex parser stack overflow
  3535. @cindex overflow of parser stack
  3536. The Bison parser stack can overflow if too many tokens are shifted and
  3537. not reduced. When this happens, the parser function @code{yyparse}
  3538. returns a nonzero value, pausing only to call @code{yyerror} to report
  3539. the overflow.
  3540. @vindex YYMAXDEPTH
  3541. By defining the macro @code{YYMAXDEPTH}, you can control how deep the
  3542. parser stack can become before a stack overflow occurs. Define the
  3543. macro with a value that is an integer. This value is the maximum number
  3544. of tokens that can be shifted (and not reduced) before overflow.
  3545. It must be a constant expression whose value is known at compile time.
  3546. The stack space allowed is not necessarily allocated. If you specify a
  3547. large value for @code{YYMAXDEPTH}, the parser actually allocates a small
  3548. stack at first, and then makes it bigger by stages as needed. This
  3549. increasing allocation happens automatically and silently. Therefore,
  3550. you do not need to make @code{YYMAXDEPTH} painfully small merely to save
  3551. space for ordinary inputs that do not need much stack.
  3552. @cindex default stack limit
  3553. The default value of @code{YYMAXDEPTH}, if you do not define it, is
  3554. 10000.
  3555. @vindex YYINITDEPTH
  3556. You can control how much stack is allocated initially by defining the
  3557. macro @code{YYINITDEPTH}. This value too must be a compile-time
  3558. constant integer. The default is 200.
  3559. @node Error Recovery, Context Dependency, Algorithm, Top
  3560. @chapter Error Recovery
  3561. @cindex error recovery
  3562. @cindex recovery from errors
  3563. It is not usually acceptable to have a program terminate on a parse
  3564. error. For example, a compiler should recover sufficiently to parse the
  3565. rest of the input file and check it for errors; a calculator should accept
  3566. another expression.
  3567. In a simple interactive command parser where each input is one line, it may
  3568. be sufficient to allow @code{yyparse} to return 1 on error and have the
  3569. caller ignore the rest of the input line when that happens (and then call
  3570. @code{yyparse} again). But this is inadequate for a compiler, because it
  3571. forgets all the syntactic context leading up to the error. A syntax error
  3572. deep within a function in the compiler input should not cause the compiler
  3573. to treat the following line like the beginning of a source file.
  3574. @findex error
  3575. You can define how to recover from a syntax error by writing rules to
  3576. recognize the special token @code{error}. This is a terminal symbol that
  3577. is always defined (you need not declare it) and reserved for error
  3578. handling. The Bison parser generates an @code{error} token whenever a
  3579. syntax error happens; if you have provided a rule to recognize this token
  3580. in the current context, the parse can continue.
  3581. For example:
  3582. @example
  3583. stmnts: /* empty string */
  3584. | stmnts '\n'
  3585. | stmnts exp '\n'
  3586. | stmnts error '\n'
  3587. @end example
  3588. The fourth rule in this example says that an error followed by a newline
  3589. makes a valid addition to any @code{stmnts}.
  3590. What happens if a syntax error occurs in the middle of an @code{exp}? The
  3591. error recovery rule, interpreted strictly, applies to the precise sequence
  3592. of a @code{stmnts}, an @code{error} and a newline. If an error occurs in
  3593. the middle of an @code{exp}, there will probably be some additional tokens
  3594. and subexpressions on the stack after the last @code{stmnts}, and there
  3595. will be tokens to read before the next newline. So the rule is not
  3596. applicable in the ordinary way.
  3597. But Bison can force the situation to fit the rule, by discarding part of
  3598. the semantic context and part of the input. First it discards states and
  3599. objects from the stack until it gets back to a state in which the
  3600. @code{error} token is acceptable. (This means that the subexpressions
  3601. already parsed are discarded, back to the last complete @code{stmnts}.) At
  3602. this point the @code{error} token can be shifted. Then, if the old
  3603. look-ahead token is not acceptable to be shifted next, the parser reads
  3604. tokens and discards them until it finds a token which is acceptable. In
  3605. this example, Bison reads and discards input until the next newline
  3606. so that the fourth rule can apply.
  3607. The choice of error rules in the grammar is a choice of strategies for
  3608. error recovery. A simple and useful strategy is simply to skip the rest of
  3609. the current input line or current statement if an error is detected:
  3610. @example
  3611. stmnt: error ';' /* on error, skip until ';' is read */
  3612. @end example
  3613. It is also useful to recover to the matching close-delimiter of an
  3614. opening-delimiter that has already been parsed. Otherwise the
  3615. close-delimiter will probably appear to be unmatched, and generate another,
  3616. spurious error message:
  3617. @example
  3618. primary: '(' expr ')'
  3619. | '(' error ')'
  3620. @dots{}
  3621. ;
  3622. @end example
  3623. Error recovery strategies are necessarily guesses. When they guess wrong,
  3624. one syntax error often leads to another. In the above example, the error
  3625. recovery rule guesses that an error is due to bad input within one
  3626. @code{stmnt}. Suppose that instead a spurious semicolon is inserted in the
  3627. middle of a valid @code{stmnt}. After the error recovery rule recovers
  3628. from the first error, another syntax error will be found straightaway,
  3629. since the text following the spurious semicolon is also an invalid
  3630. @code{stmnt}.
  3631. To prevent an outpouring of error messages, the parser will output no error
  3632. message for another syntax error that happens shortly after the first; only
  3633. after three consecutive input tokens have been successfully shifted will
  3634. error messages resume.
  3635. Note that rules which accept the @code{error} token may have actions, just
  3636. as any other rules can.
  3637. @findex yyerrok
  3638. You can make error messages resume immediately by using the macro
  3639. @code{yyerrok} in an action. If you do this in the error rule's action, no
  3640. error messages will be suppressed. This macro requires no arguments;
  3641. @samp{yyerrok;} is a valid C statement.
  3642. @findex yyclearin
  3643. The previous look-ahead token is reanalyzed immediately after an error. If
  3644. this is unacceptable, then the macro @code{yyclearin} may be used to clear
  3645. this token. Write the statement @samp{yyclearin;} in the error rule's
  3646. action.
  3647. For example, suppose that on a parse error, an error handling routine is
  3648. called that advances the input stream to some point where parsing should
  3649. once again commence. The next symbol returned by the lexical scanner is
  3650. probably correct. The previous look-ahead token ought to be discarded
  3651. with @samp{yyclearin;}.
  3652. @vindex YYRECOVERING
  3653. The macro @code{YYRECOVERING} stands for an expression that has the
  3654. value 1 when the parser is recovering from a syntax error, and 0 the
  3655. rest of the time. A value of 1 indicates that error messages are
  3656. currently suppressed for new syntax errors.
  3657. @node Context Dependency, Debugging, Error Recovery, Top
  3658. @chapter Handling Context Dependencies
  3659. The Bison paradigm is to parse tokens first, then group them into larger
  3660. syntactic units. In many languages, the meaning of a token is affected by
  3661. its context. Although this violates the Bison paradigm, certain techniques
  3662. (known as @dfn{kludges}) may enable you to write Bison parsers for such
  3663. languages.
  3664. @menu
  3665. * Semantic Tokens:: Token parsing can depend on the semantic context.
  3666. * Lexical Tie-ins:: Token parsing can depend on the syntactic context.
  3667. * Tie-in Recovery:: Lexical tie-ins have implications for how
  3668. error recovery rules must be written.
  3669. @end menu
  3670. (Actually, ``kludge'' means any technique that gets its job done but is
  3671. neither clean nor robust.)
  3672. @node Semantic Tokens, Lexical Tie-ins, , Context Dependency
  3673. @section Semantic Info in Token Types
  3674. The C language has a context dependency: the way an identifier is used
  3675. depends on what its current meaning is. For example, consider this:
  3676. @example
  3677. foo (x);
  3678. @end example
  3679. This looks like a function call statement, but if @code{foo} is a typedef
  3680. name, then this is actually a declaration of @code{x}. How can a Bison
  3681. parser for C decide how to parse this input?
  3682. The method used in GNU C is to have two different token types,
  3683. @code{IDENTIFIER} and @code{TYPENAME}. When @code{yylex} finds an
  3684. identifier, it looks up the current declaration of the identifier in order
  3685. to decide which token type to return: @code{TYPENAME} if the identifier is
  3686. declared as a typedef, @code{IDENTIFIER} otherwise.
  3687. The grammar rules can then express the context dependency by the choice of
  3688. token type to recognize. @code{IDENTIFIER} is accepted as an expression,
  3689. but @code{TYPENAME} is not. @code{TYPENAME} can start a declaration, but
  3690. @code{IDENTIFIER} cannot. In contexts where the meaning of the identifier
  3691. is @emph{not} significant, such as in declarations that can shadow a
  3692. typedef name, either @code{TYPENAME} or @code{IDENTIFIER} is
  3693. accepted---there is one rule for each of the two token types.
  3694. This technique is simple to use if the decision of which kinds of
  3695. identifiers to allow is made at a place close to where the identifier is
  3696. parsed. But in C this is not always so: C allows a declaration to
  3697. redeclare a typedef name provided an explicit type has been specified
  3698. earlier:
  3699. @example
  3700. typedef int foo, bar, lose;
  3701. static foo (bar); /* @r{redeclare @code{bar} as static variable} */
  3702. static int foo (lose); /* @r{redeclare @code{foo} as function} */
  3703. @end example
  3704. Unfortunately, the name being declared is separated from the declaration
  3705. construct itself by a complicated syntactic structure---the ``declarator''.
  3706. As a result, the part of Bison parser for C needs to be duplicated, with
  3707. all the nonterminal names changed: once for parsing a declaration in which
  3708. a typedef name can be redefined, and once for parsing a declaration in
  3709. which that can't be done. Here is a part of the duplication, with actions
  3710. omitted for brevity:
  3711. @example
  3712. initdcl:
  3713. declarator maybeasm '='
  3714. init
  3715. | declarator maybeasm
  3716. ;
  3717. notype_initdcl:
  3718. notype_declarator maybeasm '='
  3719. init
  3720. | notype_declarator maybeasm
  3721. ;
  3722. @end example
  3723. @noindent
  3724. Here @code{initdcl} can redeclare a typedef name, but @code{notype_initdcl}
  3725. cannot. The distinction between @code{declarator} and
  3726. @code{notype_declarator} is the same sort of thing.
  3727. There is some similarity between this technique and a lexical tie-in
  3728. (described next), in that information which alters the lexical analysis is
  3729. changed during parsing by other parts of the program. The difference is
  3730. here the information is global, and is used for other purposes in the
  3731. program. A true lexical tie-in has a special-purpose flag controlled by
  3732. the syntactic context.
  3733. @node Lexical Tie-ins, Tie-in Recovery, Semantic Tokens, Context Dependency
  3734. @section Lexical Tie-ins
  3735. @cindex lexical tie-in
  3736. One way to handle context-dependency is the @dfn{lexical tie-in}: a flag
  3737. which is set by Bison actions, whose purpose is to alter the way tokens are
  3738. parsed.
  3739. For example, suppose we have a language vaguely like C, but with a special
  3740. construct @samp{hex (@var{hex-expr})}. After the keyword @code{hex} comes
  3741. an expression in parentheses in which all integers are hexadecimal. In
  3742. particular, the token @samp{a1b} must be treated as an integer rather than
  3743. as an identifier if it appears in that context. Here is how you can do it:
  3744. @example
  3745. @group
  3746. %@{
  3747. int hexflag;
  3748. %@}
  3749. %%
  3750. @dots{}
  3751. @end group
  3752. @group
  3753. expr: IDENTIFIER
  3754. | constant
  3755. | HEX '('
  3756. @{ hexflag = 1; @}
  3757. expr ')'
  3758. @{ hexflag = 0;
  3759. $$ = $4; @}
  3760. | expr '+' expr
  3761. @{ $$ = make_sum ($1, $3); @}
  3762. @dots{}
  3763. ;
  3764. @end group
  3765. @group
  3766. constant:
  3767. INTEGER
  3768. | STRING
  3769. ;
  3770. @end group
  3771. @end example
  3772. @noindent
  3773. Here we assume that @code{yylex} looks at the value of @code{hexflag}; when
  3774. it is nonzero, all integers are parsed in hexadecimal, and tokens starting
  3775. with letters are parsed as integers if possible.
  3776. The declaration of @code{hexflag} shown in the C declarations section of
  3777. the parser file is needed to make it accessible to the actions
  3778. (@pxref{C Declarations, ,The C Declarations Section}). You must also write the code in @code{yylex}
  3779. to obey the flag.
  3780. @node Tie-in Recovery, , Lexical Tie-ins, Context Dependency
  3781. @section Lexical Tie-ins and Error Recovery
  3782. Lexical tie-ins make strict demands on any error recovery rules you have.
  3783. @xref{Error Recovery}.
  3784. The reason for this is that the purpose of an error recovery rule is to
  3785. abort the parsing of one construct and resume in some larger construct.
  3786. For example, in C-like languages, a typical error recovery rule is to skip
  3787. tokens until the next semicolon, and then start a new statement, like this:
  3788. @example
  3789. stmt: expr ';'
  3790. | IF '(' expr ')' stmt @{ @dots{} @}
  3791. @dots{}
  3792. error ';'
  3793. @{ hexflag = 0; @}
  3794. ;
  3795. @end example
  3796. If there is a syntax error in the middle of a @samp{hex (@var{expr})}
  3797. construct, this error rule will apply, and then the action for the
  3798. completed @samp{hex (@var{expr})} will never run. So @code{hexflag} would
  3799. remain set for the entire rest of the input, or until the next @code{hex}
  3800. keyword, causing identifiers to be misinterpreted as integers.
  3801. To avoid this problem the error recovery rule itself clears @code{hexflag}.
  3802. There may also be an error recovery rule that works within expressions.
  3803. For example, there could be a rule which applies within parentheses
  3804. and skips to the close-parenthesis:
  3805. @example
  3806. @group
  3807. expr: @dots{}
  3808. | '(' expr ')'
  3809. @{ $$ = $2; @}
  3810. | '(' error ')'
  3811. @dots{}
  3812. @end group
  3813. @end example
  3814. If this rule acts within the @code{hex} construct, it is not going to abort
  3815. that construct (since it applies to an inner level of parentheses within
  3816. the construct). Therefore, it should not clear the flag: the rest of
  3817. the @code{hex} construct should be parsed with the flag still in effect.
  3818. What if there is an error recovery rule which might abort out of the
  3819. @code{hex} construct or might not, depending on circumstances? There is no
  3820. way you can write the action to determine whether a @code{hex} construct is
  3821. being aborted or not. So if you are using a lexical tie-in, you had better
  3822. make sure your error recovery rules are not of this kind. Each rule must
  3823. be such that you can be sure that it always will, or always won't, have to
  3824. clear the flag.
  3825. @node Debugging, Invocation, Context Dependency, Top
  3826. @chapter Debugging Your Parser
  3827. @findex YYDEBUG
  3828. @findex yydebug
  3829. @cindex debugging
  3830. @cindex tracing the parser
  3831. If a Bison grammar compiles properly but doesn't do what you want when it
  3832. runs, the @code{yydebug} parser-trace feature can help you figure out why.
  3833. To enable compilation of trace facilities, you must define the macro
  3834. @code{YYDEBUG} when you compile the parser. You could use
  3835. @samp{-DYYDEBUG=1} as a compiler option or you could put @samp{#define
  3836. YYDEBUG 1} in the C declarations section of the grammar file
  3837. (@pxref{C Declarations, ,The C Declarations Section}). Alternatively, use the @samp{-t} option when
  3838. you run Bison (@pxref{Invocation, ,Invoking Bison}). We always define @code{YYDEBUG} so that
  3839. debugging is always possible.
  3840. The trace facility uses @code{stderr}, so you must add @w{@code{#include
  3841. <stdio.h>}} to the C declarations section unless it is already there.
  3842. Once you have compiled the program with trace facilities, the way to
  3843. request a trace is to store a nonzero value in the variable @code{yydebug}.
  3844. You can do this by making the C code do it (in @code{main}, perhaps), or
  3845. you can alter the value with a C debugger.
  3846. Each step taken by the parser when @code{yydebug} is nonzero produces a
  3847. line or two of trace information, written on @code{stderr}. The trace
  3848. messages tell you these things:
  3849. @itemize @bullet
  3850. @item
  3851. Each time the parser calls @code{yylex}, what kind of token was read.
  3852. @item
  3853. Each time a token is shifted, the depth and complete contents of the
  3854. state stack (@pxref{Parser States}).
  3855. @item
  3856. Each time a rule is reduced, which rule it is, and the complete contents
  3857. of the state stack afterward.
  3858. @end itemize
  3859. To make sense of this information, it helps to refer to the listing file
  3860. produced by the Bison @samp{-v} option (@pxref{Invocation, ,Invoking Bison}). This file
  3861. shows the meaning of each state in terms of positions in various rules, and
  3862. also what each state will do with each possible input token. As you read
  3863. the successive trace messages, you can see that the parser is functioning
  3864. according to its specification in the listing file. Eventually you will
  3865. arrive at the place where something undesirable happens, and you will see
  3866. which parts of the grammar are to blame.
  3867. The parser file is a C program and you can use C debuggers on it, but it's
  3868. not easy to interpret what it is doing. The parser function is a
  3869. finite-state machine interpreter, and aside from the actions it executes
  3870. the same code over and over. Only the values of variables show where in
  3871. the grammar it is working.
  3872. @findex YYPRINT
  3873. The debugging information normally gives the token type of each token
  3874. read, but not its semantic value. You can optionally define a macro
  3875. named @code{YYPRINT} to provide a way to print the value. If you define
  3876. @code{YYPRINT}, it should take three arguments. The parser will pass a
  3877. standard I/O stream, the numeric code for the token type, and the token
  3878. value (from @code{yylval}).
  3879. Here is an example of @code{YYPRINT} suitable for the multi-function
  3880. calculator (@pxref{Mfcalc Decl, ,Declarations for @code{mfcalc}}):
  3881. @smallexample
  3882. #define YYPRINT(file, type, value) yyprint (file, type, value)
  3883. static void
  3884. yyprint (file, type, value)
  3885. FILE *file;
  3886. int type;
  3887. YYSTYPE value;
  3888. @{
  3889. if (type == VAR)
  3890. fprintf (file, " %s", value.tptr->name);
  3891. else if (type == NUM)
  3892. fprintf (file, " %d", value.val);
  3893. @}
  3894. @end smallexample
  3895. @node Invocation, Table of Symbols, Debugging, Top
  3896. @chapter Invoking Bison
  3897. @cindex invoking Bison
  3898. @cindex Bison invocation
  3899. @cindex options for invoking Bison
  3900. The usual way to invoke Bison is as follows:
  3901. @example
  3902. bison @var{infile}
  3903. @end example
  3904. Here @var{infile} is the grammar file name, which usually ends in
  3905. @samp{.y}. The parser file's name is made by replacing the @samp{.y}
  3906. with @samp{.tab.c}. Thus, the @samp{bison foo.y} filename yields
  3907. @file{foo.tab.c}, and the @samp{bison hack/foo.y} filename yields
  3908. @file{hack/foo.tab.c}.@refill
  3909. @menu
  3910. * Bison Options:: All the options described in detail,
  3911. in alphabetical order by short options.
  3912. * Option Cross Key:: Alphabetical list of long options.
  3913. * VMS Invocation:: Bison command syntax on VMS.
  3914. @end menu
  3915. @node Bison Options, Option Cross Key, , Invocation
  3916. @section Bison Options
  3917. Bison supports both traditional single-letter options and mnemonic long
  3918. option names. Long option names are indicated with @samp{--} instead of
  3919. @samp{-}. Abbreviations for option names are allowed as long as they
  3920. are unique. When a long option takes an argument, like
  3921. @samp{--file-prefix}, connect the option name and the argument with
  3922. @samp{=}.
  3923. Here is a list of options that can be used with Bison, alphabetized by
  3924. short option. It is followed by a cross key alphabetized by long
  3925. option.
  3926. @table @samp
  3927. @item -b @var{file-prefix}
  3928. @itemx --file-prefix=@var{prefix}
  3929. Specify a prefix to use for all Bison output file names. The names are
  3930. chosen as if the input file were named @file{@var{prefix}.c}.
  3931. @item -d
  3932. @itemx --defines
  3933. Write an extra output file containing macro definitions for the token
  3934. type names defined in the grammar and the semantic value type
  3935. @code{YYSTYPE}, as well as a few @code{extern} variable declarations.
  3936. If the parser output file is named @file{@var{name}.c} then this file
  3937. is named @file{@var{name}.h}.@refill
  3938. This output file is essential if you wish to put the definition of
  3939. @code{yylex} in a separate source file, because @code{yylex} needs to
  3940. be able to refer to token type codes and the variable
  3941. @code{yylval}. @xref{Token Values, ,Semantic Values of Tokens}.@refill
  3942. @item -k
  3943. @itemx --token-table
  3944. This switch causes the .tab.c output to include a list of token names in
  3945. order by their token numbers; this is defined in the array @code{yytname}.
  3946. The first three elements are @code{"$"}, @code{"error"}, and
  3947. @code{"$illegal"}; entries for single- and multiple-character symbols
  3948. include their quotes: @code{"\'+\'"} and @code{"\"<=\""}. Also generated
  3949. are #defines for @code{YYNTOKENS, YYNNTS, YYNRULES}, and @code{YYNSTATES}
  3950. giving, respectively, one more than the highest token number, the number
  3951. of non-terminal symbols, the number of grammar rules, and the number of states.
  3952. @item -l
  3953. @itemx --no-lines
  3954. Don't put any @code{#line} preprocessor commands in the parser file.
  3955. Ordinarily Bison puts them in the parser file so that the C compiler
  3956. and debuggers will associate errors with your source file, the
  3957. grammar file. This option causes them to associate errors with the
  3958. parser file, treating it an independent source file in its own right.
  3959. @item -n
  3960. @itemx --no-parser
  3961. Do not generate the parser code into the output; generate only
  3962. declarations. The generated @samp{y.tab.c} file will have only
  3963. constant declarations. In addition, a @var{filename}.act file is
  3964. generated containing a switch statement body containing all the
  3965. translated actions. (The @var{filename} is taken from the input file or
  3966. set in accordance with the -o switch.) The declarations in the
  3967. @var{filename}@samp{.tab.c} file will all be static or #defined.
  3968. Some symbols only appear if appropriate options are selected.
  3969. These are #defined: YYLTYPE, YYFINAL, YYFLAG, YYNTBASE,
  3970. YYTRANSLATE, YYLAST, YYNTOKENS, YYNNTS, YYNRULES, YYNSTATES, YYMAXUTOK.
  3971. These are declared and given values: yyltype, yytranslate, yyprhs, yyrhs,
  3972. yystos, yyrline, yytname, yytoknum, yyr1, yyr2, yydefact, yydefgoto,
  3973. yypact, yypgoto, yytable, and yycheck. See the source file output.c
  3974. for definitions of these variables.
  3975. @item -o @var{outfile}
  3976. @itemx --output-file=@var{outfile}
  3977. Specify the name @var{outfile} for the parser file.
  3978. The other output files' names are constructed from @var{outfile}
  3979. as described under the @samp{-v} and @samp{-d} options.
  3980. @item -p @var{prefix}
  3981. @itemx --name-prefix=@var{prefix}
  3982. Rename the external symbols used in the parser so that they start with
  3983. @var{prefix} instead of @samp{yy}. The precise list of symbols renamed
  3984. is @code{yyparse}, @code{yylex}, @code{yyerror}, @code{yylval},
  3985. @code{yychar} and @code{yydebug}.
  3986. For example, if you use @samp{-p c}, the names become @code{cparse},
  3987. @code{clex}, and so on.
  3988. @xref{Multiple Parsers, ,Multiple Parsers in the Same Program}.
  3989. @item -r
  3990. @itemx --raw
  3991. In the output to @file{@var{name}.h} the tokens are usually defined with
  3992. Yacc compatible token numbers. If this switch is specified, the Bison
  3993. assigned numbers are output instead. (Yacc numbers start at 257 except
  3994. for single character tokens; Bison assigns token numbers sequentially
  3995. for all tokens starting at 3.)
  3996. @item -t
  3997. @itemx --debug
  3998. Output a definition of the macro @code{YYDEBUG} into the parser file,
  3999. so that the debugging facilities are compiled. @xref{Debugging, ,Debugging Your Parser}.
  4000. @item -v
  4001. @itemx --verbose
  4002. Write an extra output file containing verbose descriptions of the
  4003. parser states and what is done for each type of look-ahead token in
  4004. that state.
  4005. This file also describes all the conflicts, both those resolved by
  4006. operator precedence and the unresolved ones.
  4007. The file's name is made by removing @samp{.tab.c} or @samp{.c} from
  4008. the parser output file name, and adding @samp{.output} instead.@refill
  4009. Therefore, if the input file is @file{foo.y}, then the parser file is
  4010. called @file{foo.tab.c} by default. As a consequence, the verbose
  4011. output file is called @file{foo.output}.@refill
  4012. @item -V
  4013. @itemx --version
  4014. Print the version number of Bison and exit.
  4015. @item -h
  4016. @itemx --help
  4017. Print a summary of the command-line options to Bison and exit.
  4018. @need 1750
  4019. @item -y
  4020. @itemx --yacc
  4021. @itemx --fixed-output-files
  4022. Equivalent to @samp{-o y.tab.c}; the parser output file is called
  4023. @file{y.tab.c}, and the other outputs are called @file{y.output} and
  4024. @file{y.tab.h}. The purpose of this switch is to imitate Yacc's output
  4025. file name conventions. Thus, the following shell script can substitute
  4026. for Yacc:@refill
  4027. @example
  4028. bison -y $*
  4029. @end example
  4030. @end table
  4031. @node Option Cross Key, VMS Invocation, Bison Options, Invocation
  4032. @section Option Cross Key
  4033. Here is a list of options, alphabetized by long option, to help you find
  4034. the corresponding short option.
  4035. @tex
  4036. \def\leaderfill{\leaders\hbox to 1em{\hss.\hss}\hfill}
  4037. {\tt
  4038. \line{ --debug \leaderfill -t}
  4039. \line{ --defines \leaderfill -d}
  4040. \line{ --file-prefix \leaderfill -b}
  4041. \line{ --fixed-output-files \leaderfill -y}
  4042. \line{ --help \leaderfill -h}
  4043. \line{ --name-prefix \leaderfill -p}
  4044. \line{ --no-lines \leaderfill -l}
  4045. \line{ --no-parser \leaderfill -n}
  4046. \line{ --output-file \leaderfill -o}
  4047. \line{ --raw \leaderfill -r}
  4048. \line{ --token-table \leaderfill -k}
  4049. \line{ --verbose \leaderfill -v}
  4050. \line{ --version \leaderfill -V}
  4051. \line{ --yacc \leaderfill -y}
  4052. }
  4053. @end tex
  4054. @ifinfo
  4055. @example
  4056. --debug -t
  4057. --defines -d
  4058. --file-prefix=@var{prefix} -b @var{file-prefix}
  4059. --fixed-output-files --yacc -y
  4060. --help -h
  4061. --name-prefix -p
  4062. --no-lines -l
  4063. --no-parser -n
  4064. --output-file=@var{outfile} -o @var{outfile}
  4065. --raw -r
  4066. --token-table -k
  4067. --verbose -v
  4068. --version -V
  4069. @end example
  4070. @end ifinfo
  4071. @node VMS Invocation, , Option Cross Key, Invocation
  4072. @section Invoking Bison under VMS
  4073. @cindex invoking Bison under VMS
  4074. @cindex VMS
  4075. The command line syntax for Bison on VMS is a variant of the usual
  4076. Bison command syntax---adapted to fit VMS conventions.
  4077. To find the VMS equivalent for any Bison option, start with the long
  4078. option, and substitute a @samp{/} for the leading @samp{--}, and
  4079. substitute a @samp{_} for each @samp{-} in the name of the long option.
  4080. For example, the following invocation under VMS:
  4081. @example
  4082. bison /debug/name_prefix=bar foo.y
  4083. @end example
  4084. @noindent
  4085. is equivalent to the following command under POSIX.
  4086. @example
  4087. bison --debug --name-prefix=bar foo.y
  4088. @end example
  4089. The VMS file system does not permit filenames such as
  4090. @file{foo.tab.c}. In the above example, the output file
  4091. would instead be named @file{foo_tab.c}.
  4092. @node Table of Symbols, Parser Symbols, Invocation, Top
  4093. @appendix Bison Symbols
  4094. @cindex Bison symbols, table of
  4095. @cindex symbols in Bison, table of
  4096. @table @code
  4097. @item error
  4098. A token name reserved for error recovery. This token may be used in
  4099. grammar rules so as to allow the Bison parser to recognize an error in
  4100. the grammar without halting the process. In effect, a sentence
  4101. containing an error may be recognized as valid. On a parse error, the
  4102. token @code{error} becomes the current look-ahead token. Actions
  4103. corresponding to @code{error} are then executed, and the look-ahead
  4104. token is reset to the token that originally caused the violation.
  4105. @xref{Error Recovery}.
  4106. @item YYABORT
  4107. Macro to pretend that an unrecoverable syntax error has occurred, by
  4108. making @code{yyparse} return 1 immediately. The error reporting
  4109. function @code{yyerror} is not called. @xref{Parser Function, ,The Parser Function @code{yyparse}}.
  4110. @item YYACCEPT
  4111. Macro to pretend that a complete utterance of the language has been
  4112. read, by making @code{yyparse} return 0 immediately.
  4113. @xref{Parser Function, ,The Parser Function @code{yyparse}}.
  4114. @item YYBACKUP
  4115. Macro to discard a value from the parser stack and fake a look-ahead
  4116. token. @xref{Action Features, ,Special Features for Use in Actions}.
  4117. @item YYERROR
  4118. Macro to pretend that a syntax error has just been detected: call
  4119. @code{yyerror} and then perform normal error recovery if possible
  4120. (@pxref{Error Recovery}), or (if recovery is impossible) make
  4121. @code{yyparse} return 1. @xref{Error Recovery}.
  4122. @item YYERROR_VERBOSE
  4123. Macro that you define with @code{#define} in the Bison declarations
  4124. section to request verbose, specific error message strings when
  4125. @code{yyerror} is called.
  4126. @item YYINITDEPTH
  4127. Macro for specifying the initial size of the parser stack.
  4128. @xref{Stack Overflow}.
  4129. @item YYLTYPE
  4130. Macro for the data type of @code{yylloc}; a structure with four
  4131. members. @xref{Token Positions, ,Textual Positions of Tokens}.
  4132. @item yyltype
  4133. Default value for YYLTYPE.
  4134. @item YYMAXDEPTH
  4135. Macro for specifying the maximum size of the parser stack.
  4136. @xref{Stack Overflow}.
  4137. @item YYRECOVERING
  4138. Macro whose value indicates whether the parser is recovering from a
  4139. syntax error. @xref{Action Features, ,Special Features for Use in Actions}.
  4140. @item YYSTYPE
  4141. Macro for the data type of semantic values; @code{int} by default.
  4142. @xref{Value Type, ,Data Types of Semantic Values}.
  4143. @item yychar
  4144. External integer variable that contains the integer value of the
  4145. current look-ahead token. (In a pure parser, it is a local variable
  4146. within @code{yyparse}.) Error-recovery rule actions may examine this
  4147. variable. @xref{Action Features, ,Special Features for Use in Actions}.
  4148. @item yyclearin
  4149. Macro used in error-recovery rule actions. It clears the previous
  4150. look-ahead token. @xref{Error Recovery}.
  4151. @item yydebug
  4152. External integer variable set to zero by default. If @code{yydebug}
  4153. is given a nonzero value, the parser will output information on input
  4154. symbols and parser action. @xref{Debugging, ,Debugging Your Parser}.
  4155. @item yyerrok
  4156. Macro to cause parser to recover immediately to its normal mode
  4157. after a parse error. @xref{Error Recovery}.
  4158. @item yyerror
  4159. User-supplied function to be called by @code{yyparse} on error. The
  4160. function receives one argument, a pointer to a character string
  4161. containing an error message. @xref{Error Reporting, ,The Error Reporting Function @code{yyerror}}.
  4162. @item yylex
  4163. User-supplied lexical analyzer function, called with no arguments
  4164. to get the next token. @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}.
  4165. @item yylval
  4166. External variable in which @code{yylex} should place the semantic
  4167. value associated with a token. (In a pure parser, it is a local
  4168. variable within @code{yyparse}, and its address is passed to
  4169. @code{yylex}.) @xref{Token Values, ,Semantic Values of Tokens}.
  4170. @item yylloc
  4171. External variable in which @code{yylex} should place the line and
  4172. column numbers associated with a token. (In a pure parser, it is a
  4173. local variable within @code{yyparse}, and its address is passed to
  4174. @code{yylex}.) You can ignore this variable if you don't use the
  4175. @samp{@@} feature in the grammar actions. @xref{Token Positions, ,Textual Positions of Tokens}.
  4176. @item yynerrs
  4177. Global variable which Bison increments each time there is a parse
  4178. error. (In a pure parser, it is a local variable within
  4179. @code{yyparse}.) @xref{Error Reporting, ,The Error Reporting Function @code{yyerror}}.
  4180. @item yyparse
  4181. The parser function produced by Bison; call this function to start
  4182. parsing. @xref{Parser Function, ,The Parser Function @code{yyparse}}.
  4183. @item %left
  4184. Bison declaration to assign left associativity to token(s).
  4185. @xref{Precedence Decl, ,Operator Precedence}.
  4186. @item %nonassoc
  4187. Bison declaration to assign nonassociativity to token(s).
  4188. @xref{Precedence Decl, ,Operator Precedence}.
  4189. @item %prec
  4190. Bison declaration to assign a precedence to a specific rule.
  4191. @xref{Contextual Precedence, ,Context-Dependent Precedence}.
  4192. @item %pure_parser
  4193. Bison declaration to request a pure (reentrant) parser.
  4194. @xref{Pure Decl, ,A Pure (Reentrant) Parser}.
  4195. @item %right
  4196. Bison declaration to assign right associativity to token(s).
  4197. @xref{Precedence Decl, ,Operator Precedence}.
  4198. @item %start
  4199. Bison declaration to specify the start symbol. @xref{Start Decl, ,The Start-Symbol}.
  4200. @item %token
  4201. Bison declaration to declare token(s) without specifying precedence.
  4202. @xref{Token Decl, ,Token Type Names}.
  4203. @item %type
  4204. Bison declaration to declare nonterminals. @xref{Type Decl, ,Nonterminal Symbols}.
  4205. @item %union
  4206. Bison declaration to specify several possible data types for semantic
  4207. values. @xref{Union Decl, ,The Collection of Value Types}.
  4208. @end table
  4209. These are the punctuation and delimiters used in Bison input:
  4210. @table @samp
  4211. @item %%
  4212. Delimiter used to separate the grammar rule section from the
  4213. Bison declarations section or the additional C code section.
  4214. @xref{Grammar Layout, ,The Overall Layout of a Bison Grammar}.
  4215. @item %@{ %@}
  4216. All code listed between @samp{%@{} and @samp{%@}} is copied directly
  4217. to the output file uninterpreted. Such code forms the ``C
  4218. declarations'' section of the input file. @xref{Grammar Outline, ,Outline of a Bison Grammar}.
  4219. @item /*@dots{}*/
  4220. Comment delimiters, as in C.
  4221. @item :
  4222. Separates a rule's result from its components. @xref{Rules, ,Syntax of Grammar Rules}.
  4223. @item ;
  4224. Terminates a rule. @xref{Rules, ,Syntax of Grammar Rules}.
  4225. @item |
  4226. Separates alternate rules for the same result nonterminal.
  4227. @xref{Rules, ,Syntax of Grammar Rules}.
  4228. @end table
  4229. @node Parser Symbols, Glossary, Table of Symbols, Top
  4230. @section Parser Symbols
  4231. @cindex symbols, parser
  4232. Each symbol (either token or variable) receives a symbol number.
  4233. Numbers 0 to ntokens-1 are for tokens, and ntokens to nsyms-1 are for
  4234. variables. Symbol number zero is the end-of-input token. This token
  4235. is counted in ntokens.
  4236. The rules receive rule numbers 1 to nrules in the order they are written.
  4237. Actions and guards are accessed via the rule number.
  4238. The rules themselves are described by three arrays: rrhs, rlhs and
  4239. ritem. rlhs[R] is the symbol number of the left hand side of rule R.
  4240. The right hand side is stored as symbol numbers in a portion of
  4241. ritem. rrhs[R] contains the index in ritem of the beginning of the
  4242. portion for rule R.
  4243. If rlhs[R] is -1, the rule has been thrown out by reduce.c
  4244. and should be ignored.
  4245. The length of the portion is one greater
  4246. than the number of symbols in the rule's right hand side.
  4247. The last element in the portion contains minus R, which
  4248. identifies it as the end of a portion and says which rule it is for.
  4249. The portions of ritem come in order of increasing rule number and are
  4250. followed by an element which is zero to mark the end.
  4251. These symbols are #defined:
  4252. YYFINAL = the state number of the termination state.
  4253. YYFLAG = most negative short int. Used to flag ??
  4254. YYNTBASE = ntokens
  4255. YYTRANSLATE = macro to translate token number from yacc to bison
  4256. YYLAST = index of highest entry in yytable and yycheck
  4257. YYNTOKENS = number of terminal symbols (same as YYNTBASE)
  4258. YYNNTS = number of nonterminals
  4259. YYNRULES = number of rules in the grammar
  4260. YYNSTATES = number of states in parser
  4261. YYMAXUTOK = highest user token number
  4262. YYTRANSLATE(y) == if y <= YYMAXUTOK then yytranslate[y] else YYNTOKENS+YYNNTS
  4263. The parser tables consist of the following tables.
  4264. Starred ones needed only for the semantic parser.
  4265. Double starred are output only if switches are set.
  4266. yytranslate = vector mapping yylex's token numbers into bison's token numbers.
  4267. The token numbers differ because (a) yacc/yylex utilize the first
  4268. 256 token numbers to refer to individual characters and
  4269. (b) the yacc grammar allows explicit assignment of token numbers.
  4270. Bison token numbers are assigned sequentially from 0 to YYNTOKENS-1.
  4271. ** yytname = vector of string-names indexed by bison token number
  4272. (range 0...YYNTOKENS+YYNNTS, the last entry is "")
  4273. user token names are in (3...YYNTOKENS-1) and may be
  4274. identifier - all letters
  4275. an identifier represents either
  4276. a reserved word or a token class
  4277. character literal - 'x' (the apostrophes appear in the string)
  4278. string literal - "xxx" (the quotes appear in the string)
  4279. non-terminal symbol names are in (YYNTOKENS...YYNTOKENS+YYNNTS-1)
  4280. ** yytoknum = vector of yacc/yylex token numbers corresponding to entries
  4281. in yytname (range 0...YYNTOKENS+YYNNTS, the last entry is 0)
  4282. yyrline = vector of line-numbers of all rules. For yydebug printouts.
  4283. (range 0...YYNRULES, the first entry is 0)
  4284. ** yyrhs = vector of items of all rules.
  4285. This is exactly what ritems contains. For yydebug and for semantic
  4286. parser.
  4287. ** yyprhs[r] = index in yyrhs of first item for rule r.
  4288. (range 0...YYNRULES, the first entry is 0)
  4289. yyr1[r] = symbol number of symbol that rule r derives.
  4290. (range 0...YYNRULES, the first entry is 0)
  4291. yyr2[r] = number of symbols composing right hand side of rule r.
  4292. (range 0...YYNRULES, the first entry is 0)
  4293. * yystos[s] = the symbol number of the symbol that leads to state s.
  4294. (range 0...NSTATES-1)
  4295. yydefact[s] = default rule to reduce with in state s,
  4296. when yytable doesn't specify something else to do.
  4297. Zero means the default is an error.
  4298. (range 0...NSTATES-1)
  4299. yydefgoto[i] = default state to go to after a reduction of a rule that
  4300. generates variable ntokens + i, except when yytable
  4301. specifies something else to do.
  4302. yypact[s] = index in yytable of the portion describing state s.
  4303. (range 0...YYNSTATES-1).
  4304. The lookahead token's type is used to index that portion
  4305. to find out what to do.
  4306. If the value in yytable is positive,
  4307. we shift the token and go to that state.
  4308. If the value is negative, it is minus a rule number to reduce by.
  4309. If the value is zero, the default action from yydefact[s] is used.
  4310. yypgoto[i] = the index in yytable of the portion describing
  4311. what to do after reducing a rule that derives variable i + ntokens.
  4312. This portion is indexed by the parser state number
  4313. as of before the text for this nonterminal was read.
  4314. The value from yytable is the state to go to.
  4315. yytable = a vector filled with portions for different uses,
  4316. found via yypact and yypgoto. (range 0...YYLAST)
  4317. yycheck = a vector indexed in parallel with yytable.
  4318. It indicates, in a roundabout way, the bounds of the
  4319. portion you are trying to examine.
  4320. Suppose that the portion of yytable starts at index p
  4321. and the index to be examined within the portion is i.
  4322. Then if yycheck[p+i] != i, i is outside the bounds
  4323. of what is actually allocated, and the default
  4324. (from yydefact or yydefgoto) should be used.
  4325. Otherwise, yytable[p+i] should be used. (range 0...YYLAST)
  4326. @node Glossary, Index, Parser Symbols, Top
  4327. @appendix Glossary
  4328. @cindex glossary
  4329. @table @asis
  4330. @item Backus-Naur Form (BNF)
  4331. Formal method of specifying context-free grammars. BNF was first used
  4332. in the @cite{ALGOL-60} report, 1963. @xref{Language and Grammar, ,Languages and Context-Free Grammars}.
  4333. @item Context-free grammars
  4334. Grammars specified as rules that can be applied regardless of context.
  4335. Thus, if there is a rule which says that an integer can be used as an
  4336. expression, integers are allowed @emph{anywhere} an expression is
  4337. permitted. @xref{Language and Grammar, ,Languages and Context-Free Grammars}.
  4338. @item Dynamic allocation
  4339. Allocation of memory that occurs during execution, rather than at
  4340. compile time or on entry to a function.
  4341. @item Empty string
  4342. Analogous to the empty set in set theory, the empty string is a
  4343. character string of length zero.
  4344. @item Finite-state stack machine
  4345. A ``machine'' that has discrete states in which it is said to exist at
  4346. each instant in time. As input to the machine is processed, the
  4347. machine moves from state to state as specified by the logic of the
  4348. machine. In the case of the parser, the input is the language being
  4349. parsed, and the states correspond to various stages in the grammar
  4350. rules. @xref{Algorithm, ,The Bison Parser Algorithm }.
  4351. @item Grouping
  4352. A language construct that is (in general) grammatically divisible;
  4353. for example, `expression' or `declaration' in C.
  4354. @xref{Language and Grammar, ,Languages and Context-Free Grammars}.
  4355. @item Infix operator
  4356. An arithmetic operator that is placed between the operands on which it
  4357. performs some operation.
  4358. @item Input stream
  4359. A continuous flow of data between devices or programs.
  4360. @item Language construct
  4361. One of the typical usage schemas of the language. For example, one of
  4362. the constructs of the C language is the @code{if} statement.
  4363. @xref{Language and Grammar, ,Languages and Context-Free Grammars}.
  4364. @item Left associativity
  4365. Operators having left associativity are analyzed from left to right:
  4366. @samp{a+b+c} first computes @samp{a+b} and then combines with
  4367. @samp{c}. @xref{Precedence, ,Operator Precedence}.
  4368. @item Left recursion
  4369. A rule whose result symbol is also its first component symbol;
  4370. for example, @samp{expseq1 : expseq1 ',' exp;}. @xref{Recursion, ,Recursive Rules}.
  4371. @item Left-to-right parsing
  4372. Parsing a sentence of a language by analyzing it token by token from
  4373. left to right. @xref{Algorithm, ,The Bison Parser Algorithm }.
  4374. @item Lexical analyzer (scanner)
  4375. A function that reads an input stream and returns tokens one by one.
  4376. @xref{Lexical, ,The Lexical Analyzer Function @code{yylex}}.
  4377. @item Lexical tie-in
  4378. A flag, set by actions in the grammar rules, which alters the way
  4379. tokens are parsed. @xref{Lexical Tie-ins}.
  4380. @item Look-ahead token
  4381. A token already read but not yet shifted. @xref{Look-Ahead, ,Look-Ahead Tokens}.
  4382. @item LALR(1)
  4383. The class of context-free grammars that Bison (like most other parser
  4384. generators) can handle; a subset of LR(1). @xref{Mystery Conflicts, ,
  4385. Mysterious Reduce/Reduce Conflicts}.
  4386. @item LR(1)
  4387. The class of context-free grammars in which at most one token of
  4388. look-ahead is needed to disambiguate the parsing of any piece of input.
  4389. @item Nonterminal symbol
  4390. A grammar symbol standing for a grammatical construct that can
  4391. be expressed through rules in terms of smaller constructs; in other
  4392. words, a construct that is not a token. @xref{Symbols, ,Symbols - Terminal and Nonterminal}.
  4393. @item Parse error
  4394. An error encountered during parsing of an input stream due to invalid
  4395. syntax. @xref{Error Recovery}.
  4396. @item Parser
  4397. A function that recognizes valid sentences of a language by analyzing
  4398. the syntax structure of a set of tokens passed to it from a lexical
  4399. analyzer.
  4400. @item Postfix operator
  4401. An arithmetic operator that is placed after the operands upon which it
  4402. performs some operation.
  4403. @item Reduction
  4404. Replacing a string of nonterminals and/or terminals with a single
  4405. nonterminal, according to a grammar rule. @xref{Algorithm, ,The Bison Parser Algorithm }.
  4406. @item Reentrant
  4407. A reentrant subprogram is a subprogram which can be in invoked any
  4408. number of times in parallel, without interference between the various
  4409. invocations. @xref{Pure Decl, ,A Pure (Reentrant) Parser}.
  4410. @item Reverse polish notation
  4411. A language in which all operators are postfix operators.
  4412. @item Right recursion
  4413. A rule whose result symbol is also its last component symbol;
  4414. for example, @samp{expseq1: exp ',' expseq1;}. @xref{Recursion, ,Recursive Rules}.
  4415. @item Semantics
  4416. In computer languages, the semantics are specified by the actions
  4417. taken for each instance of the language, i.e., the meaning of
  4418. each statement. @xref{Semantics, ,Defining Language Semantics}.
  4419. @item Shift
  4420. A parser is said to shift when it makes the choice of analyzing
  4421. further input from the stream rather than reducing immediately some
  4422. already-recognized rule. @xref{Algorithm, ,The Bison Parser Algorithm }.
  4423. @item Single-character literal
  4424. A single character that is recognized and interpreted as is.
  4425. @xref{Grammar in Bison, ,From Formal Rules to Bison Input}.
  4426. @item Start symbol
  4427. The nonterminal symbol that stands for a complete valid utterance in
  4428. the language being parsed. The start symbol is usually listed as the
  4429. first nonterminal symbol in a language specification.
  4430. @xref{Start Decl, ,The Start-Symbol}.
  4431. @item Symbol table
  4432. A data structure where symbol names and associated data are stored
  4433. during parsing to allow for recognition and use of existing
  4434. information in repeated uses of a symbol. @xref{Multi-function Calc}.
  4435. @item Token
  4436. A basic, grammatically indivisible unit of a language. The symbol
  4437. that describes a token in the grammar is a terminal symbol.
  4438. The input of the Bison parser is a stream of tokens which comes from
  4439. the lexical analyzer. @xref{Symbols, ,Symbols - Terminal and Nonterminal}.
  4440. @item Terminal symbol
  4441. A grammar symbol that has no rules in the grammar and therefore
  4442. is grammatically indivisible. The piece of text it represents
  4443. is a token. @xref{Language and Grammar, ,Languages and Context-Free Grammars}.
  4444. @end table
  4445. @node Index, , Glossary, Top
  4446. @unnumbered Index
  4447. @printindex cp
  4448. @contents
  4449. @bye
  4450. @c old menu
  4451. * Introduction::
  4452. * Conditions::
  4453. * Copying:: The GNU General Public License says
  4454. how you can copy and share Bison
  4455. Tutorial sections:
  4456. * Concepts:: Basic concepts for understanding Bison.
  4457. * Examples:: Three simple explained examples of using Bison.
  4458. Reference sections:
  4459. * Grammar File:: Writing Bison declarations and rules.
  4460. * Interface:: C-language interface to the parser function @code{yyparse}.
  4461. * Algorithm:: How the Bison parser works at run-time.
  4462. * Error Recovery:: Writing rules for error recovery.
  4463. * Context Dependency::What to do if your language syntax is too
  4464. messy for Bison to handle straightforwardly.
  4465. * Debugging:: Debugging Bison parsers that parse wrong.
  4466. * Invocation:: How to run Bison (to produce the parser source file).
  4467. * Table of Symbols:: All the keywords of the Bison language are explained.
  4468. * Glossary:: Basic concepts are explained.
  4469. * Index:: Cross-references to the text.