bison.html 198 KB

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