lemon.c 182 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057
  1. /*
  2. ** This file contains all sources (including headers) to the LEMON
  3. ** LALR(1) parser generator. The sources have been combined into a
  4. ** single file to make it easy to include LEMON in the source tree
  5. ** and Makefile of another program.
  6. **
  7. ** The author of this program disclaims copyright.
  8. */
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <stdlib.h>
  14. #include <assert.h>
  15. #define ISSPACE(X) isspace((unsigned char)(X))
  16. #define ISDIGIT(X) isdigit((unsigned char)(X))
  17. #define ISALNUM(X) isalnum((unsigned char)(X))
  18. #define ISALPHA(X) isalpha((unsigned char)(X))
  19. #define ISUPPER(X) isupper((unsigned char)(X))
  20. #define ISLOWER(X) islower((unsigned char)(X))
  21. #ifndef __WIN32__
  22. # if defined(_WIN32) || defined(WIN32)
  23. # define __WIN32__
  24. # endif
  25. #endif
  26. #ifdef __WIN32__
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. extern int access(const char *path, int mode);
  31. #ifdef __cplusplus
  32. }
  33. #endif
  34. #else
  35. #include <unistd.h>
  36. #endif
  37. /* #define PRIVATE static */
  38. #define PRIVATE
  39. #ifdef TEST
  40. #define MAXRHS 5 /* Set low to exercise exception code */
  41. #else
  42. #define MAXRHS 1000
  43. #endif
  44. extern void memory_error();
  45. static int showPrecedenceConflict = 0;
  46. static char *msort(char*,char**,int(*)(const char*,const char*));
  47. /*
  48. ** Compilers are getting increasingly pedantic about type conversions
  49. ** as C evolves ever closer to Ada.... To work around the latest problems
  50. ** we have to define the following variant of strlen().
  51. */
  52. #define lemonStrlen(X) ((int)strlen(X))
  53. /*
  54. ** Header on the linked list of memory allocations.
  55. */
  56. typedef struct MemChunk MemChunk;
  57. struct MemChunk {
  58. MemChunk *pNext;
  59. size_t sz;
  60. /* Actually memory follows */
  61. };
  62. /*
  63. ** Global linked list of all memory allocations.
  64. */
  65. static MemChunk *memChunkList = 0;
  66. /*
  67. ** Wrappers around malloc(), calloc(), realloc() and free().
  68. **
  69. ** All memory allocations are kept on a doubly-linked list. The
  70. ** lemon_free_all() function can be called prior to exit to clean
  71. ** up any memory leaks.
  72. **
  73. ** This is not necessary. But compilers and getting increasingly
  74. ** fussy about memory leaks, even in command-line programs like Lemon
  75. ** where they do not matter. So this code is provided to hush the
  76. ** warnings.
  77. */
  78. static void *lemon_malloc(size_t nByte){
  79. MemChunk *p;
  80. if( nByte<0 ) return 0;
  81. p = malloc( nByte + sizeof(MemChunk) );
  82. if( p==0 ){
  83. fprintf(stderr, "Out of memory. Failed to allocate %lld bytes.\n",
  84. (long long int)nByte);
  85. exit(1);
  86. }
  87. p->pNext = memChunkList;
  88. p->sz = nByte;
  89. memChunkList = p;
  90. return (void*)&p[1];
  91. }
  92. static void *lemon_calloc(size_t nElem, size_t sz){
  93. void *p = lemon_malloc(nElem*sz);
  94. memset(p, 0, nElem*sz);
  95. return p;
  96. }
  97. static void lemon_free(void *pOld){
  98. if( pOld ){
  99. MemChunk *p = (MemChunk*)pOld;
  100. p--;
  101. memset(pOld, 0, p->sz);
  102. }
  103. }
  104. static void *lemon_realloc(void *pOld, size_t nNew){
  105. void *pNew;
  106. MemChunk *p;
  107. if( pOld==0 ) return lemon_malloc(nNew);
  108. p = (MemChunk*)pOld;
  109. p--;
  110. if( p->sz>=nNew ) return pOld;
  111. pNew = lemon_malloc( nNew );
  112. memcpy(pNew, pOld, p->sz);
  113. return pNew;
  114. }
  115. /* Free all outstanding memory allocations.
  116. ** Do this right before exiting.
  117. */
  118. static void lemon_free_all(void){
  119. while( memChunkList ){
  120. MemChunk *pNext = memChunkList->pNext;
  121. free( memChunkList );
  122. memChunkList = pNext;
  123. }
  124. }
  125. /*
  126. ** Compilers are starting to complain about the use of sprintf() and strcpy(),
  127. ** saying they are unsafe. So we define our own versions of those routines too.
  128. **
  129. ** There are three routines here: lemon_sprintf(), lemon_vsprintf(), and
  130. ** lemon_addtext(). The first two are replacements for sprintf() and vsprintf().
  131. ** The third is a helper routine for vsnprintf() that adds texts to the end of a
  132. ** buffer, making sure the buffer is always zero-terminated.
  133. **
  134. ** The string formatter is a minimal subset of stdlib sprintf() supporting only
  135. ** a few simply conversions:
  136. **
  137. ** %d
  138. ** %s
  139. ** %.*s
  140. **
  141. */
  142. static void lemon_addtext(
  143. char *zBuf, /* The buffer to which text is added */
  144. int *pnUsed, /* Slots of the buffer used so far */
  145. const char *zIn, /* Text to add */
  146. int nIn, /* Bytes of text to add. -1 to use strlen() */
  147. int iWidth /* Field width. Negative to left justify */
  148. ){
  149. if( nIn<0 ) for(nIn=0; zIn[nIn]; nIn++){}
  150. while( iWidth>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth--; }
  151. if( nIn==0 ) return;
  152. memcpy(&zBuf[*pnUsed], zIn, nIn);
  153. *pnUsed += nIn;
  154. while( (-iWidth)>nIn ){ zBuf[(*pnUsed)++] = ' '; iWidth++; }
  155. zBuf[*pnUsed] = 0;
  156. }
  157. static int lemon_vsprintf(char *str, const char *zFormat, va_list ap){
  158. int i, j, k, c;
  159. int nUsed = 0;
  160. const char *z;
  161. char zTemp[50];
  162. str[0] = 0;
  163. for(i=j=0; (c = zFormat[i])!=0; i++){
  164. if( c=='%' ){
  165. int iWidth = 0;
  166. lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0);
  167. c = zFormat[++i];
  168. if( ISDIGIT(c) || (c=='-' && ISDIGIT(zFormat[i+1])) ){
  169. if( c=='-' ) i++;
  170. while( ISDIGIT(zFormat[i]) ) iWidth = iWidth*10 + zFormat[i++] - '0';
  171. if( c=='-' ) iWidth = -iWidth;
  172. c = zFormat[i];
  173. }
  174. if( c=='d' ){
  175. int v = va_arg(ap, int);
  176. if( v<0 ){
  177. lemon_addtext(str, &nUsed, "-", 1, iWidth);
  178. v = -v;
  179. }else if( v==0 ){
  180. lemon_addtext(str, &nUsed, "0", 1, iWidth);
  181. }
  182. k = 0;
  183. while( v>0 ){
  184. k++;
  185. zTemp[sizeof(zTemp)-k] = (v%10) + '0';
  186. v /= 10;
  187. }
  188. lemon_addtext(str, &nUsed, &zTemp[sizeof(zTemp)-k], k, iWidth);
  189. }else if( c=='s' ){
  190. z = va_arg(ap, const char*);
  191. lemon_addtext(str, &nUsed, z, -1, iWidth);
  192. }else if( c=='.' && memcmp(&zFormat[i], ".*s", 3)==0 ){
  193. i += 2;
  194. k = va_arg(ap, int);
  195. z = va_arg(ap, const char*);
  196. lemon_addtext(str, &nUsed, z, k, iWidth);
  197. }else if( c=='%' ){
  198. lemon_addtext(str, &nUsed, "%", 1, 0);
  199. }else{
  200. fprintf(stderr, "illegal format\n");
  201. exit(1);
  202. }
  203. j = i+1;
  204. }
  205. }
  206. lemon_addtext(str, &nUsed, &zFormat[j], i-j, 0);
  207. return nUsed;
  208. }
  209. static int lemon_sprintf(char *str, const char *format, ...){
  210. va_list ap;
  211. int rc;
  212. va_start(ap, format);
  213. rc = lemon_vsprintf(str, format, ap);
  214. va_end(ap);
  215. return rc;
  216. }
  217. static void lemon_strcpy(char *dest, const char *src){
  218. while( (*(dest++) = *(src++))!=0 ){}
  219. }
  220. static void lemon_strcat(char *dest, const char *src){
  221. while( *dest ) dest++;
  222. lemon_strcpy(dest, src);
  223. }
  224. /* a few forward declarations... */
  225. struct rule;
  226. struct lemon;
  227. struct action;
  228. static struct action *Action_new(void);
  229. static struct action *Action_sort(struct action *);
  230. /********** From the file "build.h" ************************************/
  231. void FindRulePrecedences(struct lemon*);
  232. void FindFirstSets(struct lemon*);
  233. void FindStates(struct lemon*);
  234. void FindLinks(struct lemon*);
  235. void FindFollowSets(struct lemon*);
  236. void FindActions(struct lemon*);
  237. /********* From the file "configlist.h" *********************************/
  238. void Configlist_init(void);
  239. struct config *Configlist_add(struct rule *, int);
  240. struct config *Configlist_addbasis(struct rule *, int);
  241. void Configlist_closure(struct lemon *);
  242. void Configlist_sort(void);
  243. void Configlist_sortbasis(void);
  244. struct config *Configlist_return(void);
  245. struct config *Configlist_basis(void);
  246. void Configlist_eat(struct config *);
  247. void Configlist_reset(void);
  248. /********* From the file "error.h" ***************************************/
  249. void ErrorMsg(const char *, int,const char *, ...);
  250. /****** From the file "option.h" ******************************************/
  251. enum option_type { OPT_FLAG=1, OPT_INT, OPT_DBL, OPT_STR,
  252. OPT_FFLAG, OPT_FINT, OPT_FDBL, OPT_FSTR};
  253. struct s_options {
  254. enum option_type type;
  255. const char *label;
  256. char *arg;
  257. const char *message;
  258. };
  259. int OptInit(char**,struct s_options*,FILE*);
  260. int OptNArgs(void);
  261. char *OptArg(int);
  262. void OptErr(int);
  263. void OptPrint(void);
  264. /******** From the file "parse.h" *****************************************/
  265. void Parse(struct lemon *lemp);
  266. /********* From the file "plink.h" ***************************************/
  267. struct plink *Plink_new(void);
  268. void Plink_add(struct plink **, struct config *);
  269. void Plink_copy(struct plink **, struct plink *);
  270. void Plink_delete(struct plink *);
  271. /********** From the file "report.h" *************************************/
  272. void Reprint(struct lemon *);
  273. void ReportOutput(struct lemon *);
  274. void ReportTable(struct lemon *, int, int);
  275. void ReportHeader(struct lemon *);
  276. void CompressTables(struct lemon *);
  277. void ResortStates(struct lemon *);
  278. /********** From the file "set.h" ****************************************/
  279. void SetSize(int); /* All sets will be of size N */
  280. char *SetNew(void); /* A new set for element 0..N */
  281. void SetFree(char*); /* Deallocate a set */
  282. int SetAdd(char*,int); /* Add element to a set */
  283. int SetUnion(char *,char *); /* A <- A U B, thru element N */
  284. #define SetFind(X,Y) (X[Y]) /* True if Y is in set X */
  285. /********** From the file "struct.h" *************************************/
  286. /*
  287. ** Principal data structures for the LEMON parser generator.
  288. */
  289. typedef enum {LEMON_FALSE=0, LEMON_TRUE} Boolean;
  290. /* Symbols (terminals and nonterminals) of the grammar are stored
  291. ** in the following: */
  292. enum symbol_type {
  293. TERMINAL,
  294. NONTERMINAL,
  295. MULTITERMINAL
  296. };
  297. enum e_assoc {
  298. LEFT,
  299. RIGHT,
  300. NONE,
  301. UNK
  302. };
  303. struct symbol {
  304. const char *name; /* Name of the symbol */
  305. int index; /* Index number for this symbol */
  306. enum symbol_type type; /* Symbols are all either TERMINALS or NTs */
  307. struct rule *rule; /* Linked list of rules of this (if an NT) */
  308. struct symbol *fallback; /* fallback token in case this token doesn't parse */
  309. int prec; /* Precedence if defined (-1 otherwise) */
  310. enum e_assoc assoc; /* Associativity if precedence is defined */
  311. char *firstset; /* First-set for all rules of this symbol */
  312. Boolean lambda; /* True if NT and can generate an empty string */
  313. int useCnt; /* Number of times used */
  314. char *destructor; /* Code which executes whenever this symbol is
  315. ** popped from the stack during error processing */
  316. int destLineno; /* Line number for start of destructor. Set to
  317. ** -1 for duplicate destructors. */
  318. char *datatype; /* The data type of information held by this
  319. ** object. Only used if type==NONTERMINAL */
  320. int dtnum; /* The data type number. In the parser, the value
  321. ** stack is a union. The .yy%d element of this
  322. ** union is the correct data type for this object */
  323. int bContent; /* True if this symbol ever carries content - if
  324. ** it is ever more than just syntax */
  325. /* The following fields are used by MULTITERMINALs only */
  326. int nsubsym; /* Number of constituent symbols in the MULTI */
  327. struct symbol **subsym; /* Array of constituent symbols */
  328. };
  329. /* Each production rule in the grammar is stored in the following
  330. ** structure. */
  331. struct rule {
  332. struct symbol *lhs; /* Left-hand side of the rule */
  333. const char *lhsalias; /* Alias for the LHS (NULL if none) */
  334. int lhsStart; /* True if left-hand side is the start symbol */
  335. int ruleline; /* Line number for the rule */
  336. int nrhs; /* Number of RHS symbols */
  337. struct symbol **rhs; /* The RHS symbols */
  338. const char **rhsalias; /* An alias for each RHS symbol (NULL if none) */
  339. int line; /* Line number at which code begins */
  340. const char *code; /* The code executed when this rule is reduced */
  341. const char *codePrefix; /* Setup code before code[] above */
  342. const char *codeSuffix; /* Breakdown code after code[] above */
  343. struct symbol *precsym; /* Precedence symbol for this rule */
  344. int index; /* An index number for this rule */
  345. int iRule; /* Rule number as used in the generated tables */
  346. Boolean noCode; /* True if this rule has no associated C code */
  347. Boolean codeEmitted; /* True if the code has been emitted already */
  348. Boolean canReduce; /* True if this rule is ever reduced */
  349. Boolean doesReduce; /* Reduce actions occur after optimization */
  350. Boolean neverReduce; /* Reduce is theoretically possible, but prevented
  351. ** by actions or other outside implementation */
  352. struct rule *nextlhs; /* Next rule with the same LHS */
  353. struct rule *next; /* Next rule in the global list */
  354. };
  355. /* A configuration is a production rule of the grammar together with
  356. ** a mark (dot) showing how much of that rule has been processed so far.
  357. ** Configurations also contain a follow-set which is a list of terminal
  358. ** symbols which are allowed to immediately follow the end of the rule.
  359. ** Every configuration is recorded as an instance of the following: */
  360. enum cfgstatus {
  361. COMPLETE,
  362. INCOMPLETE
  363. };
  364. struct config {
  365. struct rule *rp; /* The rule upon which the configuration is based */
  366. int dot; /* The parse point */
  367. char *fws; /* Follow-set for this configuration only */
  368. struct plink *fplp; /* Follow-set forward propagation links */
  369. struct plink *bplp; /* Follow-set backwards propagation links */
  370. struct state *stp; /* Pointer to state which contains this */
  371. enum cfgstatus status; /* used during followset and shift computations */
  372. struct config *next; /* Next configuration in the state */
  373. struct config *bp; /* The next basis configuration */
  374. };
  375. enum e_action {
  376. SHIFT,
  377. ACCEPT,
  378. REDUCE,
  379. ERROR,
  380. SSCONFLICT, /* A shift/shift conflict */
  381. SRCONFLICT, /* Was a reduce, but part of a conflict */
  382. RRCONFLICT, /* Was a reduce, but part of a conflict */
  383. SH_RESOLVED, /* Was a shift. Precedence resolved conflict */
  384. RD_RESOLVED, /* Was reduce. Precedence resolved conflict */
  385. NOT_USED, /* Deleted by compression */
  386. SHIFTREDUCE /* Shift first, then reduce */
  387. };
  388. /* Every shift or reduce operation is stored as one of the following */
  389. struct action {
  390. struct symbol *sp; /* The look-ahead symbol */
  391. enum e_action type;
  392. union {
  393. struct state *stp; /* The new state, if a shift */
  394. struct rule *rp; /* The rule, if a reduce */
  395. } x;
  396. struct symbol *spOpt; /* SHIFTREDUCE optimization to this symbol */
  397. struct action *next; /* Next action for this state */
  398. struct action *collide; /* Next action with the same hash */
  399. };
  400. /* Each state of the generated parser's finite state machine
  401. ** is encoded as an instance of the following structure. */
  402. struct state {
  403. struct config *bp; /* The basis configurations for this state */
  404. struct config *cfp; /* All configurations in this set */
  405. int statenum; /* Sequential number for this state */
  406. struct action *ap; /* List of actions for this state */
  407. int nTknAct, nNtAct; /* Number of actions on terminals and nonterminals */
  408. int iTknOfst, iNtOfst; /* yy_action[] offset for terminals and nonterms */
  409. int iDfltReduce; /* Default action is to REDUCE by this rule */
  410. struct rule *pDfltReduce;/* The default REDUCE rule. */
  411. int autoReduce; /* True if this is an auto-reduce state */
  412. };
  413. #define NO_OFFSET (-2147483647)
  414. /* A followset propagation link indicates that the contents of one
  415. ** configuration followset should be propagated to another whenever
  416. ** the first changes. */
  417. struct plink {
  418. struct config *cfp; /* The configuration to which linked */
  419. struct plink *next; /* The next propagate link */
  420. };
  421. /* The state vector for the entire parser generator is recorded as
  422. ** follows. (LEMON uses no global variables and makes little use of
  423. ** static variables. Fields in the following structure can be thought
  424. ** of as begin global variables in the program.) */
  425. struct lemon {
  426. struct state **sorted; /* Table of states sorted by state number */
  427. struct rule *rule; /* List of all rules */
  428. struct rule *startRule; /* First rule */
  429. int nstate; /* Number of states */
  430. int nxstate; /* nstate with tail degenerate states removed */
  431. int nrule; /* Number of rules */
  432. int nruleWithAction; /* Number of rules with actions */
  433. int nsymbol; /* Number of terminal and nonterminal symbols */
  434. int nterminal; /* Number of terminal symbols */
  435. int minShiftReduce; /* Minimum shift-reduce action value */
  436. int errAction; /* Error action value */
  437. int accAction; /* Accept action value */
  438. int noAction; /* No-op action value */
  439. int minReduce; /* Minimum reduce action */
  440. int maxAction; /* Maximum action value of any kind */
  441. struct symbol **symbols; /* Sorted array of pointers to symbols */
  442. int errorcnt; /* Number of errors */
  443. struct symbol *errsym; /* The error symbol */
  444. struct symbol *wildcard; /* Token that matches anything */
  445. char *name; /* Name of the generated parser */
  446. char *arg; /* Declaration of the 3rd argument to parser */
  447. char *ctx; /* Declaration of 2nd argument to constructor */
  448. char *tokentype; /* Type of terminal symbols in the parser stack */
  449. char *vartype; /* The default type of non-terminal symbols */
  450. char *start; /* Name of the start symbol for the grammar */
  451. char *stacksize; /* Size of the parser stack */
  452. char *include; /* Code to put at the start of the C file */
  453. char *error; /* Code to execute when an error is seen */
  454. char *overflow; /* Code to execute on a stack overflow */
  455. char *failure; /* Code to execute on parser failure */
  456. char *accept; /* Code to execute when the parser excepts */
  457. char *extracode; /* Code appended to the generated file */
  458. char *tokendest; /* Code to execute to destroy token data */
  459. char *vardest; /* Code for the default non-terminal destructor */
  460. char *filename; /* Name of the input file */
  461. char *outname; /* Name of the current output file */
  462. char *tokenprefix; /* A prefix added to token names in the .h file */
  463. char *reallocFunc; /* Function to use to allocate stack space */
  464. char *freeFunc; /* Function to use to free stack space */
  465. int nconflict; /* Number of parsing conflicts */
  466. int nactiontab; /* Number of entries in the yy_action[] table */
  467. int nlookaheadtab; /* Number of entries in yy_lookahead[] */
  468. int tablesize; /* Total table size of all tables in bytes */
  469. int basisflag; /* Print only basis configurations */
  470. int printPreprocessed; /* Show preprocessor output on stdout */
  471. int has_fallback; /* True if any %fallback is seen in the grammar */
  472. int nolinenosflag; /* True if #line statements should not be printed */
  473. int argc; /* Number of command-line arguments */
  474. char **argv; /* Command-line arguments */
  475. };
  476. #define MemoryCheck(X) if((X)==0){ \
  477. extern void memory_error(); \
  478. memory_error(); \
  479. }
  480. /**************** From the file "table.h" *********************************/
  481. /*
  482. ** All code in this file has been automatically generated
  483. ** from a specification in the file
  484. ** "table.q"
  485. ** by the associative array code building program "aagen".
  486. ** Do not edit this file! Instead, edit the specification
  487. ** file, then rerun aagen.
  488. */
  489. /*
  490. ** Code for processing tables in the LEMON parser generator.
  491. */
  492. /* Routines for handling a strings */
  493. const char *Strsafe(const char *);
  494. void Strsafe_init(void);
  495. int Strsafe_insert(const char *);
  496. const char *Strsafe_find(const char *);
  497. /* Routines for handling symbols of the grammar */
  498. struct symbol *Symbol_new(const char *);
  499. int Symbolcmpp(const void *, const void *);
  500. void Symbol_init(void);
  501. int Symbol_insert(struct symbol *, const char *);
  502. struct symbol *Symbol_find(const char *);
  503. struct symbol *Symbol_Nth(int);
  504. int Symbol_count(void);
  505. struct symbol **Symbol_arrayof(void);
  506. /* Routines to manage the state table */
  507. int Configcmp(const char *, const char *);
  508. struct state *State_new(void);
  509. void State_init(void);
  510. int State_insert(struct state *, struct config *);
  511. struct state *State_find(struct config *);
  512. struct state **State_arrayof(void);
  513. /* Routines used for efficiency in Configlist_add */
  514. void Configtable_init(void);
  515. int Configtable_insert(struct config *);
  516. struct config *Configtable_find(struct config *);
  517. void Configtable_clear(int(*)(struct config *));
  518. /****************** From the file "action.c" *******************************/
  519. /*
  520. ** Routines processing parser actions in the LEMON parser generator.
  521. */
  522. /* Allocate a new parser action */
  523. static struct action *Action_new(void){
  524. static struct action *actionfreelist = 0;
  525. struct action *newaction;
  526. if( actionfreelist==0 ){
  527. int i;
  528. int amt = 100;
  529. actionfreelist = (struct action *)lemon_calloc(amt, sizeof(struct action));
  530. if( actionfreelist==0 ){
  531. fprintf(stderr,"Unable to allocate memory for a new parser action.");
  532. exit(1);
  533. }
  534. for(i=0; i<amt-1; i++) actionfreelist[i].next = &actionfreelist[i+1];
  535. actionfreelist[amt-1].next = 0;
  536. }
  537. newaction = actionfreelist;
  538. actionfreelist = actionfreelist->next;
  539. return newaction;
  540. }
  541. /* Compare two actions for sorting purposes. Return negative, zero, or
  542. ** positive if the first action is less than, equal to, or greater than
  543. ** the first
  544. */
  545. static int actioncmp(
  546. struct action *ap1,
  547. struct action *ap2
  548. ){
  549. int rc;
  550. rc = ap1->sp->index - ap2->sp->index;
  551. if( rc==0 ){
  552. rc = (int)ap1->type - (int)ap2->type;
  553. }
  554. if( rc==0 && (ap1->type==REDUCE || ap1->type==SHIFTREDUCE) ){
  555. rc = ap1->x.rp->index - ap2->x.rp->index;
  556. }
  557. if( rc==0 ){
  558. rc = (int) (ap2 - ap1);
  559. }
  560. return rc;
  561. }
  562. /* Sort parser actions */
  563. static struct action *Action_sort(
  564. struct action *ap
  565. ){
  566. ap = (struct action *)msort((char *)ap,(char **)&ap->next,
  567. (int(*)(const char*,const char*))actioncmp);
  568. return ap;
  569. }
  570. void Action_add(
  571. struct action **app,
  572. enum e_action type,
  573. struct symbol *sp,
  574. char *arg
  575. ){
  576. struct action *newaction;
  577. newaction = Action_new();
  578. newaction->next = *app;
  579. *app = newaction;
  580. newaction->type = type;
  581. newaction->sp = sp;
  582. newaction->spOpt = 0;
  583. if( type==SHIFT ){
  584. newaction->x.stp = (struct state *)arg;
  585. }else{
  586. newaction->x.rp = (struct rule *)arg;
  587. }
  588. }
  589. /********************** New code to implement the "acttab" module ***********/
  590. /*
  591. ** This module implements routines use to construct the yy_action[] table.
  592. */
  593. /*
  594. ** The state of the yy_action table under construction is an instance of
  595. ** the following structure.
  596. **
  597. ** The yy_action table maps the pair (state_number, lookahead) into an
  598. ** action_number. The table is an array of integers pairs. The state_number
  599. ** determines an initial offset into the yy_action array. The lookahead
  600. ** value is then added to this initial offset to get an index X into the
  601. ** yy_action array. If the aAction[X].lookahead equals the value of the
  602. ** of the lookahead input, then the value of the action_number output is
  603. ** aAction[X].action. If the lookaheads do not match then the
  604. ** default action for the state_number is returned.
  605. **
  606. ** All actions associated with a single state_number are first entered
  607. ** into aLookahead[] using multiple calls to acttab_action(). Then the
  608. ** actions for that single state_number are placed into the aAction[]
  609. ** array with a single call to acttab_insert(). The acttab_insert() call
  610. ** also resets the aLookahead[] array in preparation for the next
  611. ** state number.
  612. */
  613. struct lookahead_action {
  614. int lookahead; /* Value of the lookahead token */
  615. int action; /* Action to take on the given lookahead */
  616. };
  617. typedef struct acttab acttab;
  618. struct acttab {
  619. int nAction; /* Number of used slots in aAction[] */
  620. int nActionAlloc; /* Slots allocated for aAction[] */
  621. struct lookahead_action
  622. *aAction, /* The yy_action[] table under construction */
  623. *aLookahead; /* A single new transaction set */
  624. int mnLookahead; /* Minimum aLookahead[].lookahead */
  625. int mnAction; /* Action associated with mnLookahead */
  626. int mxLookahead; /* Maximum aLookahead[].lookahead */
  627. int nLookahead; /* Used slots in aLookahead[] */
  628. int nLookaheadAlloc; /* Slots allocated in aLookahead[] */
  629. int nterminal; /* Number of terminal symbols */
  630. int nsymbol; /* total number of symbols */
  631. };
  632. /* Return the number of entries in the yy_action table */
  633. #define acttab_lookahead_size(X) ((X)->nAction)
  634. /* The value for the N-th entry in yy_action */
  635. #define acttab_yyaction(X,N) ((X)->aAction[N].action)
  636. /* The value for the N-th entry in yy_lookahead */
  637. #define acttab_yylookahead(X,N) ((X)->aAction[N].lookahead)
  638. /* Free all memory associated with the given acttab */
  639. void acttab_free(acttab *p){
  640. lemon_free( p->aAction );
  641. lemon_free( p->aLookahead );
  642. lemon_free( p );
  643. }
  644. /* Allocate a new acttab structure */
  645. acttab *acttab_alloc(int nsymbol, int nterminal){
  646. acttab *p = (acttab *) lemon_calloc( 1, sizeof(*p) );
  647. if( p==0 ){
  648. fprintf(stderr,"Unable to allocate memory for a new acttab.");
  649. exit(1);
  650. }
  651. memset(p, 0, sizeof(*p));
  652. p->nsymbol = nsymbol;
  653. p->nterminal = nterminal;
  654. return p;
  655. }
  656. /* Add a new action to the current transaction set.
  657. **
  658. ** This routine is called once for each lookahead for a particular
  659. ** state.
  660. */
  661. void acttab_action(acttab *p, int lookahead, int action){
  662. if( p->nLookahead>=p->nLookaheadAlloc ){
  663. p->nLookaheadAlloc += 25;
  664. p->aLookahead = (struct lookahead_action *) lemon_realloc( p->aLookahead,
  665. sizeof(p->aLookahead[0])*p->nLookaheadAlloc );
  666. if( p->aLookahead==0 ){
  667. fprintf(stderr,"malloc failed\n");
  668. exit(1);
  669. }
  670. }
  671. if( p->nLookahead==0 ){
  672. p->mxLookahead = lookahead;
  673. p->mnLookahead = lookahead;
  674. p->mnAction = action;
  675. }else{
  676. if( p->mxLookahead<lookahead ) p->mxLookahead = lookahead;
  677. if( p->mnLookahead>lookahead ){
  678. p->mnLookahead = lookahead;
  679. p->mnAction = action;
  680. }
  681. }
  682. p->aLookahead[p->nLookahead].lookahead = lookahead;
  683. p->aLookahead[p->nLookahead].action = action;
  684. p->nLookahead++;
  685. }
  686. /*
  687. ** Add the transaction set built up with prior calls to acttab_action()
  688. ** into the current action table. Then reset the transaction set back
  689. ** to an empty set in preparation for a new round of acttab_action() calls.
  690. **
  691. ** Return the offset into the action table of the new transaction.
  692. **
  693. ** If the makeItSafe parameter is true, then the offset is chosen so that
  694. ** it is impossible to overread the yy_lookaside[] table regardless of
  695. ** the lookaside token. This is done for the terminal symbols, as they
  696. ** come from external inputs and can contain syntax errors. When makeItSafe
  697. ** is false, there is more flexibility in selecting offsets, resulting in
  698. ** a smaller table. For non-terminal symbols, which are never syntax errors,
  699. ** makeItSafe can be false.
  700. */
  701. int acttab_insert(acttab *p, int makeItSafe){
  702. int i, j, k, n, end;
  703. assert( p->nLookahead>0 );
  704. /* Make sure we have enough space to hold the expanded action table
  705. ** in the worst case. The worst case occurs if the transaction set
  706. ** must be appended to the current action table
  707. */
  708. n = p->nsymbol + 1;
  709. if( p->nAction + n >= p->nActionAlloc ){
  710. int oldAlloc = p->nActionAlloc;
  711. p->nActionAlloc = p->nAction + n + p->nActionAlloc + 20;
  712. p->aAction = (struct lookahead_action *) lemon_realloc( p->aAction,
  713. sizeof(p->aAction[0])*p->nActionAlloc);
  714. if( p->aAction==0 ){
  715. fprintf(stderr,"malloc failed\n");
  716. exit(1);
  717. }
  718. for(i=oldAlloc; i<p->nActionAlloc; i++){
  719. p->aAction[i].lookahead = -1;
  720. p->aAction[i].action = -1;
  721. }
  722. }
  723. /* Scan the existing action table looking for an offset that is a
  724. ** duplicate of the current transaction set. Fall out of the loop
  725. ** if and when the duplicate is found.
  726. **
  727. ** i is the index in p->aAction[] where p->mnLookahead is inserted.
  728. */
  729. end = makeItSafe ? p->mnLookahead : 0;
  730. for(i=p->nAction-1; i>=end; i--){
  731. if( p->aAction[i].lookahead==p->mnLookahead ){
  732. /* All lookaheads and actions in the aLookahead[] transaction
  733. ** must match against the candidate aAction[i] entry. */
  734. if( p->aAction[i].action!=p->mnAction ) continue;
  735. for(j=0; j<p->nLookahead; j++){
  736. k = p->aLookahead[j].lookahead - p->mnLookahead + i;
  737. if( k<0 || k>=p->nAction ) break;
  738. if( p->aLookahead[j].lookahead!=p->aAction[k].lookahead ) break;
  739. if( p->aLookahead[j].action!=p->aAction[k].action ) break;
  740. }
  741. if( j<p->nLookahead ) continue;
  742. /* No possible lookahead value that is not in the aLookahead[]
  743. ** transaction is allowed to match aAction[i] */
  744. n = 0;
  745. for(j=0; j<p->nAction; j++){
  746. if( p->aAction[j].lookahead<0 ) continue;
  747. if( p->aAction[j].lookahead==j+p->mnLookahead-i ) n++;
  748. }
  749. if( n==p->nLookahead ){
  750. break; /* An exact match is found at offset i */
  751. }
  752. }
  753. }
  754. /* If no existing offsets exactly match the current transaction, find an
  755. ** an empty offset in the aAction[] table in which we can add the
  756. ** aLookahead[] transaction.
  757. */
  758. if( i<end ){
  759. /* Look for holes in the aAction[] table that fit the current
  760. ** aLookahead[] transaction. Leave i set to the offset of the hole.
  761. ** If no holes are found, i is left at p->nAction, which means the
  762. ** transaction will be appended. */
  763. i = makeItSafe ? p->mnLookahead : 0;
  764. for(; i<p->nActionAlloc - p->mxLookahead; i++){
  765. if( p->aAction[i].lookahead<0 ){
  766. for(j=0; j<p->nLookahead; j++){
  767. k = p->aLookahead[j].lookahead - p->mnLookahead + i;
  768. if( k<0 ) break;
  769. if( p->aAction[k].lookahead>=0 ) break;
  770. }
  771. if( j<p->nLookahead ) continue;
  772. for(j=0; j<p->nAction; j++){
  773. if( p->aAction[j].lookahead==j+p->mnLookahead-i ) break;
  774. }
  775. if( j==p->nAction ){
  776. break; /* Fits in empty slots */
  777. }
  778. }
  779. }
  780. }
  781. /* Insert transaction set at index i. */
  782. #if 0
  783. printf("Acttab:");
  784. for(j=0; j<p->nLookahead; j++){
  785. printf(" %d", p->aLookahead[j].lookahead);
  786. }
  787. printf(" inserted at %d\n", i);
  788. #endif
  789. for(j=0; j<p->nLookahead; j++){
  790. k = p->aLookahead[j].lookahead - p->mnLookahead + i;
  791. p->aAction[k] = p->aLookahead[j];
  792. if( k>=p->nAction ) p->nAction = k+1;
  793. }
  794. if( makeItSafe && i+p->nterminal>=p->nAction ) p->nAction = i+p->nterminal+1;
  795. p->nLookahead = 0;
  796. /* Return the offset that is added to the lookahead in order to get the
  797. ** index into yy_action of the action */
  798. return i - p->mnLookahead;
  799. }
  800. /*
  801. ** Return the size of the action table without the trailing syntax error
  802. ** entries.
  803. */
  804. int acttab_action_size(acttab *p){
  805. int n = p->nAction;
  806. while( n>0 && p->aAction[n-1].lookahead<0 ){ n--; }
  807. return n;
  808. }
  809. /********************** From the file "build.c" *****************************/
  810. /*
  811. ** Routines to construction the finite state machine for the LEMON
  812. ** parser generator.
  813. */
  814. /* Find a precedence symbol of every rule in the grammar.
  815. **
  816. ** Those rules which have a precedence symbol coded in the input
  817. ** grammar using the "[symbol]" construct will already have the
  818. ** rp->precsym field filled. Other rules take as their precedence
  819. ** symbol the first RHS symbol with a defined precedence. If there
  820. ** are not RHS symbols with a defined precedence, the precedence
  821. ** symbol field is left blank.
  822. */
  823. void FindRulePrecedences(struct lemon *xp)
  824. {
  825. struct rule *rp;
  826. for(rp=xp->rule; rp; rp=rp->next){
  827. if( rp->precsym==0 ){
  828. int i, j;
  829. for(i=0; i<rp->nrhs && rp->precsym==0; i++){
  830. struct symbol *sp = rp->rhs[i];
  831. if( sp->type==MULTITERMINAL ){
  832. for(j=0; j<sp->nsubsym; j++){
  833. if( sp->subsym[j]->prec>=0 ){
  834. rp->precsym = sp->subsym[j];
  835. break;
  836. }
  837. }
  838. }else if( sp->prec>=0 ){
  839. rp->precsym = rp->rhs[i];
  840. }
  841. }
  842. }
  843. }
  844. return;
  845. }
  846. /* Find all nonterminals which will generate the empty string.
  847. ** Then go back and compute the first sets of every nonterminal.
  848. ** The first set is the set of all terminal symbols which can begin
  849. ** a string generated by that nonterminal.
  850. */
  851. void FindFirstSets(struct lemon *lemp)
  852. {
  853. int i, j;
  854. struct rule *rp;
  855. int progress;
  856. for(i=0; i<lemp->nsymbol; i++){
  857. lemp->symbols[i]->lambda = LEMON_FALSE;
  858. }
  859. for(i=lemp->nterminal; i<lemp->nsymbol; i++){
  860. lemp->symbols[i]->firstset = SetNew();
  861. }
  862. /* First compute all lambdas */
  863. do{
  864. progress = 0;
  865. for(rp=lemp->rule; rp; rp=rp->next){
  866. if( rp->lhs->lambda ) continue;
  867. for(i=0; i<rp->nrhs; i++){
  868. struct symbol *sp = rp->rhs[i];
  869. assert( sp->type==NONTERMINAL || sp->lambda==LEMON_FALSE );
  870. if( sp->lambda==LEMON_FALSE ) break;
  871. }
  872. if( i==rp->nrhs ){
  873. rp->lhs->lambda = LEMON_TRUE;
  874. progress = 1;
  875. }
  876. }
  877. }while( progress );
  878. /* Now compute all first sets */
  879. do{
  880. struct symbol *s1, *s2;
  881. progress = 0;
  882. for(rp=lemp->rule; rp; rp=rp->next){
  883. s1 = rp->lhs;
  884. for(i=0; i<rp->nrhs; i++){
  885. s2 = rp->rhs[i];
  886. if( s2->type==TERMINAL ){
  887. progress += SetAdd(s1->firstset,s2->index);
  888. break;
  889. }else if( s2->type==MULTITERMINAL ){
  890. for(j=0; j<s2->nsubsym; j++){
  891. progress += SetAdd(s1->firstset,s2->subsym[j]->index);
  892. }
  893. break;
  894. }else if( s1==s2 ){
  895. if( s1->lambda==LEMON_FALSE ) break;
  896. }else{
  897. progress += SetUnion(s1->firstset,s2->firstset);
  898. if( s2->lambda==LEMON_FALSE ) break;
  899. }
  900. }
  901. }
  902. }while( progress );
  903. return;
  904. }
  905. /* Compute all LR(0) states for the grammar. Links
  906. ** are added to between some states so that the LR(1) follow sets
  907. ** can be computed later.
  908. */
  909. PRIVATE struct state *getstate(struct lemon *); /* forward reference */
  910. void FindStates(struct lemon *lemp)
  911. {
  912. struct symbol *sp;
  913. struct rule *rp;
  914. Configlist_init();
  915. /* Find the start symbol */
  916. if( lemp->start ){
  917. sp = Symbol_find(lemp->start);
  918. if( sp==0 ){
  919. ErrorMsg(lemp->filename,0,
  920. "The specified start symbol \"%s\" is not "
  921. "in a nonterminal of the grammar. \"%s\" will be used as the start "
  922. "symbol instead.",lemp->start,lemp->startRule->lhs->name);
  923. lemp->errorcnt++;
  924. sp = lemp->startRule->lhs;
  925. }
  926. }else if( lemp->startRule ){
  927. sp = lemp->startRule->lhs;
  928. }else{
  929. ErrorMsg(lemp->filename,0,"Internal error - no start rule\n");
  930. exit(1);
  931. }
  932. /* Make sure the start symbol doesn't occur on the right-hand side of
  933. ** any rule. Report an error if it does. (YACC would generate a new
  934. ** start symbol in this case.) */
  935. for(rp=lemp->rule; rp; rp=rp->next){
  936. int i;
  937. for(i=0; i<rp->nrhs; i++){
  938. if( rp->rhs[i]==sp ){ /* FIX ME: Deal with multiterminals */
  939. ErrorMsg(lemp->filename,0,
  940. "The start symbol \"%s\" occurs on the "
  941. "right-hand side of a rule. This will result in a parser which "
  942. "does not work properly.",sp->name);
  943. lemp->errorcnt++;
  944. }
  945. }
  946. }
  947. /* The basis configuration set for the first state
  948. ** is all rules which have the start symbol as their
  949. ** left-hand side */
  950. for(rp=sp->rule; rp; rp=rp->nextlhs){
  951. struct config *newcfp;
  952. rp->lhsStart = 1;
  953. newcfp = Configlist_addbasis(rp,0);
  954. SetAdd(newcfp->fws,0);
  955. }
  956. /* Compute the first state. All other states will be
  957. ** computed automatically during the computation of the first one.
  958. ** The returned pointer to the first state is not used. */
  959. (void)getstate(lemp);
  960. return;
  961. }
  962. /* Return a pointer to a state which is described by the configuration
  963. ** list which has been built from calls to Configlist_add.
  964. */
  965. PRIVATE void buildshifts(struct lemon *, struct state *); /* Forwd ref */
  966. PRIVATE struct state *getstate(struct lemon *lemp)
  967. {
  968. struct config *cfp, *bp;
  969. struct state *stp;
  970. /* Extract the sorted basis of the new state. The basis was constructed
  971. ** by prior calls to "Configlist_addbasis()". */
  972. Configlist_sortbasis();
  973. bp = Configlist_basis();
  974. /* Get a state with the same basis */
  975. stp = State_find(bp);
  976. if( stp ){
  977. /* A state with the same basis already exists! Copy all the follow-set
  978. ** propagation links from the state under construction into the
  979. ** preexisting state, then return a pointer to the preexisting state */
  980. struct config *x, *y;
  981. for(x=bp, y=stp->bp; x && y; x=x->bp, y=y->bp){
  982. Plink_copy(&y->bplp,x->bplp);
  983. Plink_delete(x->fplp);
  984. x->fplp = x->bplp = 0;
  985. }
  986. cfp = Configlist_return();
  987. Configlist_eat(cfp);
  988. }else{
  989. /* This really is a new state. Construct all the details */
  990. Configlist_closure(lemp); /* Compute the configuration closure */
  991. Configlist_sort(); /* Sort the configuration closure */
  992. cfp = Configlist_return(); /* Get a pointer to the config list */
  993. stp = State_new(); /* A new state structure */
  994. MemoryCheck(stp);
  995. stp->bp = bp; /* Remember the configuration basis */
  996. stp->cfp = cfp; /* Remember the configuration closure */
  997. stp->statenum = lemp->nstate++; /* Every state gets a sequence number */
  998. stp->ap = 0; /* No actions, yet. */
  999. State_insert(stp,stp->bp); /* Add to the state table */
  1000. buildshifts(lemp,stp); /* Recursively compute successor states */
  1001. }
  1002. return stp;
  1003. }
  1004. /*
  1005. ** Return true if two symbols are the same.
  1006. */
  1007. int same_symbol(struct symbol *a, struct symbol *b)
  1008. {
  1009. int i;
  1010. if( a==b ) return 1;
  1011. if( a->type!=MULTITERMINAL ) return 0;
  1012. if( b->type!=MULTITERMINAL ) return 0;
  1013. if( a->nsubsym!=b->nsubsym ) return 0;
  1014. for(i=0; i<a->nsubsym; i++){
  1015. if( a->subsym[i]!=b->subsym[i] ) return 0;
  1016. }
  1017. return 1;
  1018. }
  1019. /* Construct all successor states to the given state. A "successor"
  1020. ** state is any state which can be reached by a shift action.
  1021. */
  1022. PRIVATE void buildshifts(struct lemon *lemp, struct state *stp)
  1023. {
  1024. struct config *cfp; /* For looping thru the config closure of "stp" */
  1025. struct config *bcfp; /* For the inner loop on config closure of "stp" */
  1026. struct config *newcfg; /* */
  1027. struct symbol *sp; /* Symbol following the dot in configuration "cfp" */
  1028. struct symbol *bsp; /* Symbol following the dot in configuration "bcfp" */
  1029. struct state *newstp; /* A pointer to a successor state */
  1030. /* Each configuration becomes complete after it contributes to a successor
  1031. ** state. Initially, all configurations are incomplete */
  1032. for(cfp=stp->cfp; cfp; cfp=cfp->next) cfp->status = INCOMPLETE;
  1033. /* Loop through all configurations of the state "stp" */
  1034. for(cfp=stp->cfp; cfp; cfp=cfp->next){
  1035. if( cfp->status==COMPLETE ) continue; /* Already used by inner loop */
  1036. if( cfp->dot>=cfp->rp->nrhs ) continue; /* Can't shift this config */
  1037. Configlist_reset(); /* Reset the new config set */
  1038. sp = cfp->rp->rhs[cfp->dot]; /* Symbol after the dot */
  1039. /* For every configuration in the state "stp" which has the symbol "sp"
  1040. ** following its dot, add the same configuration to the basis set under
  1041. ** construction but with the dot shifted one symbol to the right. */
  1042. for(bcfp=cfp; bcfp; bcfp=bcfp->next){
  1043. if( bcfp->status==COMPLETE ) continue; /* Already used */
  1044. if( bcfp->dot>=bcfp->rp->nrhs ) continue; /* Can't shift this one */
  1045. bsp = bcfp->rp->rhs[bcfp->dot]; /* Get symbol after dot */
  1046. if( !same_symbol(bsp,sp) ) continue; /* Must be same as for "cfp" */
  1047. bcfp->status = COMPLETE; /* Mark this config as used */
  1048. newcfg = Configlist_addbasis(bcfp->rp,bcfp->dot+1);
  1049. Plink_add(&newcfg->bplp,bcfp);
  1050. }
  1051. /* Get a pointer to the state described by the basis configuration set
  1052. ** constructed in the preceding loop */
  1053. newstp = getstate(lemp);
  1054. /* The state "newstp" is reached from the state "stp" by a shift action
  1055. ** on the symbol "sp" */
  1056. if( sp->type==MULTITERMINAL ){
  1057. int i;
  1058. for(i=0; i<sp->nsubsym; i++){
  1059. Action_add(&stp->ap,SHIFT,sp->subsym[i],(char*)newstp);
  1060. }
  1061. }else{
  1062. Action_add(&stp->ap,SHIFT,sp,(char *)newstp);
  1063. }
  1064. }
  1065. }
  1066. /*
  1067. ** Construct the propagation links
  1068. */
  1069. void FindLinks(struct lemon *lemp)
  1070. {
  1071. int i;
  1072. struct config *cfp, *other;
  1073. struct state *stp;
  1074. struct plink *plp;
  1075. /* Housekeeping detail:
  1076. ** Add to every propagate link a pointer back to the state to
  1077. ** which the link is attached. */
  1078. for(i=0; i<lemp->nstate; i++){
  1079. stp = lemp->sorted[i];
  1080. for(cfp=stp?stp->cfp:0; cfp; cfp=cfp->next){
  1081. cfp->stp = stp;
  1082. }
  1083. }
  1084. /* Convert all backlinks into forward links. Only the forward
  1085. ** links are used in the follow-set computation. */
  1086. for(i=0; i<lemp->nstate; i++){
  1087. stp = lemp->sorted[i];
  1088. for(cfp=stp?stp->cfp:0; cfp; cfp=cfp->next){
  1089. for(plp=cfp->bplp; plp; plp=plp->next){
  1090. other = plp->cfp;
  1091. Plink_add(&other->fplp,cfp);
  1092. }
  1093. }
  1094. }
  1095. }
  1096. /* Compute all followsets.
  1097. **
  1098. ** A followset is the set of all symbols which can come immediately
  1099. ** after a configuration.
  1100. */
  1101. void FindFollowSets(struct lemon *lemp)
  1102. {
  1103. int i;
  1104. struct config *cfp;
  1105. struct plink *plp;
  1106. int progress;
  1107. int change;
  1108. for(i=0; i<lemp->nstate; i++){
  1109. assert( lemp->sorted[i]!=0 );
  1110. for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
  1111. cfp->status = INCOMPLETE;
  1112. }
  1113. }
  1114. do{
  1115. progress = 0;
  1116. for(i=0; i<lemp->nstate; i++){
  1117. assert( lemp->sorted[i]!=0 );
  1118. for(cfp=lemp->sorted[i]->cfp; cfp; cfp=cfp->next){
  1119. if( cfp->status==COMPLETE ) continue;
  1120. for(plp=cfp->fplp; plp; plp=plp->next){
  1121. change = SetUnion(plp->cfp->fws,cfp->fws);
  1122. if( change ){
  1123. plp->cfp->status = INCOMPLETE;
  1124. progress = 1;
  1125. }
  1126. }
  1127. cfp->status = COMPLETE;
  1128. }
  1129. }
  1130. }while( progress );
  1131. }
  1132. static int resolve_conflict(struct action *,struct action *);
  1133. /* Compute the reduce actions, and resolve conflicts.
  1134. */
  1135. void FindActions(struct lemon *lemp)
  1136. {
  1137. int i,j;
  1138. struct config *cfp;
  1139. struct state *stp;
  1140. struct symbol *sp;
  1141. struct rule *rp;
  1142. /* Add all of the reduce actions
  1143. ** A reduce action is added for each element of the followset of
  1144. ** a configuration which has its dot at the extreme right.
  1145. */
  1146. for(i=0; i<lemp->nstate; i++){ /* Loop over all states */
  1147. stp = lemp->sorted[i];
  1148. for(cfp=stp->cfp; cfp; cfp=cfp->next){ /* Loop over all configurations */
  1149. if( cfp->rp->nrhs==cfp->dot ){ /* Is dot at extreme right? */
  1150. for(j=0; j<lemp->nterminal; j++){
  1151. if( SetFind(cfp->fws,j) ){
  1152. /* Add a reduce action to the state "stp" which will reduce by the
  1153. ** rule "cfp->rp" if the lookahead symbol is "lemp->symbols[j]" */
  1154. Action_add(&stp->ap,REDUCE,lemp->symbols[j],(char *)cfp->rp);
  1155. }
  1156. }
  1157. }
  1158. }
  1159. }
  1160. /* Add the accepting token */
  1161. if( lemp->start ){
  1162. sp = Symbol_find(lemp->start);
  1163. if( sp==0 ){
  1164. if( lemp->startRule==0 ){
  1165. fprintf(stderr, "internal error on source line %d: no start rule\n",
  1166. __LINE__);
  1167. exit(1);
  1168. }
  1169. sp = lemp->startRule->lhs;
  1170. }
  1171. }else{
  1172. sp = lemp->startRule->lhs;
  1173. }
  1174. /* Add to the first state (which is always the starting state of the
  1175. ** finite state machine) an action to ACCEPT if the lookahead is the
  1176. ** start nonterminal. */
  1177. Action_add(&lemp->sorted[0]->ap,ACCEPT,sp,0);
  1178. /* Resolve conflicts */
  1179. for(i=0; i<lemp->nstate; i++){
  1180. struct action *ap, *nap;
  1181. stp = lemp->sorted[i];
  1182. /* assert( stp->ap ); */
  1183. stp->ap = Action_sort(stp->ap);
  1184. for(ap=stp->ap; ap && ap->next; ap=ap->next){
  1185. for(nap=ap->next; nap && nap->sp==ap->sp; nap=nap->next){
  1186. /* The two actions "ap" and "nap" have the same lookahead.
  1187. ** Figure out which one should be used */
  1188. lemp->nconflict += resolve_conflict(ap,nap);
  1189. }
  1190. }
  1191. }
  1192. /* Report an error for each rule that can never be reduced. */
  1193. for(rp=lemp->rule; rp; rp=rp->next) rp->canReduce = LEMON_FALSE;
  1194. for(i=0; i<lemp->nstate; i++){
  1195. struct action *ap;
  1196. for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
  1197. if( ap->type==REDUCE ) ap->x.rp->canReduce = LEMON_TRUE;
  1198. }
  1199. }
  1200. for(rp=lemp->rule; rp; rp=rp->next){
  1201. if( rp->canReduce ) continue;
  1202. ErrorMsg(lemp->filename,rp->ruleline,"This rule can not be reduced.\n");
  1203. lemp->errorcnt++;
  1204. }
  1205. }
  1206. /* Resolve a conflict between the two given actions. If the
  1207. ** conflict can't be resolved, return non-zero.
  1208. **
  1209. ** NO LONGER TRUE:
  1210. ** To resolve a conflict, first look to see if either action
  1211. ** is on an error rule. In that case, take the action which
  1212. ** is not associated with the error rule. If neither or both
  1213. ** actions are associated with an error rule, then try to
  1214. ** use precedence to resolve the conflict.
  1215. **
  1216. ** If either action is a SHIFT, then it must be apx. This
  1217. ** function won't work if apx->type==REDUCE and apy->type==SHIFT.
  1218. */
  1219. static int resolve_conflict(
  1220. struct action *apx,
  1221. struct action *apy
  1222. ){
  1223. struct symbol *spx, *spy;
  1224. int errcnt = 0;
  1225. assert( apx->sp==apy->sp ); /* Otherwise there would be no conflict */
  1226. if( apx->type==SHIFT && apy->type==SHIFT ){
  1227. apy->type = SSCONFLICT;
  1228. errcnt++;
  1229. }
  1230. if( apx->type==SHIFT && apy->type==REDUCE ){
  1231. spx = apx->sp;
  1232. spy = apy->x.rp->precsym;
  1233. if( spy==0 || spx->prec<0 || spy->prec<0 ){
  1234. /* Not enough precedence information. */
  1235. apy->type = SRCONFLICT;
  1236. errcnt++;
  1237. }else if( spx->prec>spy->prec ){ /* higher precedence wins */
  1238. apy->type = RD_RESOLVED;
  1239. }else if( spx->prec<spy->prec ){
  1240. apx->type = SH_RESOLVED;
  1241. }else if( spx->prec==spy->prec && spx->assoc==RIGHT ){ /* Use operator */
  1242. apy->type = RD_RESOLVED; /* associativity */
  1243. }else if( spx->prec==spy->prec && spx->assoc==LEFT ){ /* to break tie */
  1244. apx->type = SH_RESOLVED;
  1245. }else{
  1246. assert( spx->prec==spy->prec && spx->assoc==NONE );
  1247. apx->type = ERROR;
  1248. }
  1249. }else if( apx->type==REDUCE && apy->type==REDUCE ){
  1250. spx = apx->x.rp->precsym;
  1251. spy = apy->x.rp->precsym;
  1252. if( spx==0 || spy==0 || spx->prec<0 ||
  1253. spy->prec<0 || spx->prec==spy->prec ){
  1254. apy->type = RRCONFLICT;
  1255. errcnt++;
  1256. }else if( spx->prec>spy->prec ){
  1257. apy->type = RD_RESOLVED;
  1258. }else if( spx->prec<spy->prec ){
  1259. apx->type = RD_RESOLVED;
  1260. }
  1261. }else{
  1262. assert(
  1263. apx->type==SH_RESOLVED ||
  1264. apx->type==RD_RESOLVED ||
  1265. apx->type==SSCONFLICT ||
  1266. apx->type==SRCONFLICT ||
  1267. apx->type==RRCONFLICT ||
  1268. apy->type==SH_RESOLVED ||
  1269. apy->type==RD_RESOLVED ||
  1270. apy->type==SSCONFLICT ||
  1271. apy->type==SRCONFLICT ||
  1272. apy->type==RRCONFLICT
  1273. );
  1274. /* The REDUCE/SHIFT case cannot happen because SHIFTs come before
  1275. ** REDUCEs on the list. If we reach this point it must be because
  1276. ** the parser conflict had already been resolved. */
  1277. }
  1278. return errcnt;
  1279. }
  1280. /********************* From the file "configlist.c" *************************/
  1281. /*
  1282. ** Routines to processing a configuration list and building a state
  1283. ** in the LEMON parser generator.
  1284. */
  1285. static struct config *freelist = 0; /* List of free configurations */
  1286. static struct config *current = 0; /* Top of list of configurations */
  1287. static struct config **currentend = 0; /* Last on list of configs */
  1288. static struct config *basis = 0; /* Top of list of basis configs */
  1289. static struct config **basisend = 0; /* End of list of basis configs */
  1290. /* Return a pointer to a new configuration */
  1291. PRIVATE struct config *newconfig(void){
  1292. return (struct config*)lemon_calloc(1, sizeof(struct config));
  1293. }
  1294. /* The configuration "old" is no longer used */
  1295. PRIVATE void deleteconfig(struct config *old)
  1296. {
  1297. old->next = freelist;
  1298. freelist = old;
  1299. }
  1300. /* Initialized the configuration list builder */
  1301. void Configlist_init(void){
  1302. current = 0;
  1303. currentend = &current;
  1304. basis = 0;
  1305. basisend = &basis;
  1306. Configtable_init();
  1307. return;
  1308. }
  1309. /* Initialized the configuration list builder */
  1310. void Configlist_reset(void){
  1311. current = 0;
  1312. currentend = &current;
  1313. basis = 0;
  1314. basisend = &basis;
  1315. Configtable_clear(0);
  1316. return;
  1317. }
  1318. /* Add another configuration to the configuration list */
  1319. struct config *Configlist_add(
  1320. struct rule *rp, /* The rule */
  1321. int dot /* Index into the RHS of the rule where the dot goes */
  1322. ){
  1323. struct config *cfp, model;
  1324. assert( currentend!=0 );
  1325. model.rp = rp;
  1326. model.dot = dot;
  1327. cfp = Configtable_find(&model);
  1328. if( cfp==0 ){
  1329. cfp = newconfig();
  1330. cfp->rp = rp;
  1331. cfp->dot = dot;
  1332. cfp->fws = SetNew();
  1333. cfp->stp = 0;
  1334. cfp->fplp = cfp->bplp = 0;
  1335. cfp->next = 0;
  1336. cfp->bp = 0;
  1337. *currentend = cfp;
  1338. currentend = &cfp->next;
  1339. Configtable_insert(cfp);
  1340. }
  1341. return cfp;
  1342. }
  1343. /* Add a basis configuration to the configuration list */
  1344. struct config *Configlist_addbasis(struct rule *rp, int dot)
  1345. {
  1346. struct config *cfp, model;
  1347. assert( basisend!=0 );
  1348. assert( currentend!=0 );
  1349. model.rp = rp;
  1350. model.dot = dot;
  1351. cfp = Configtable_find(&model);
  1352. if( cfp==0 ){
  1353. cfp = newconfig();
  1354. cfp->rp = rp;
  1355. cfp->dot = dot;
  1356. cfp->fws = SetNew();
  1357. cfp->stp = 0;
  1358. cfp->fplp = cfp->bplp = 0;
  1359. cfp->next = 0;
  1360. cfp->bp = 0;
  1361. *currentend = cfp;
  1362. currentend = &cfp->next;
  1363. *basisend = cfp;
  1364. basisend = &cfp->bp;
  1365. Configtable_insert(cfp);
  1366. }
  1367. return cfp;
  1368. }
  1369. /* Compute the closure of the configuration list */
  1370. void Configlist_closure(struct lemon *lemp)
  1371. {
  1372. struct config *cfp, *newcfp;
  1373. struct rule *rp, *newrp;
  1374. struct symbol *sp, *xsp;
  1375. int i, dot;
  1376. assert( currentend!=0 );
  1377. for(cfp=current; cfp; cfp=cfp->next){
  1378. rp = cfp->rp;
  1379. dot = cfp->dot;
  1380. if( dot>=rp->nrhs ) continue;
  1381. sp = rp->rhs[dot];
  1382. if( sp->type==NONTERMINAL ){
  1383. if( sp->rule==0 && sp!=lemp->errsym ){
  1384. ErrorMsg(lemp->filename,rp->line,"Nonterminal \"%s\" has no rules.",
  1385. sp->name);
  1386. lemp->errorcnt++;
  1387. }
  1388. for(newrp=sp->rule; newrp; newrp=newrp->nextlhs){
  1389. newcfp = Configlist_add(newrp,0);
  1390. for(i=dot+1; i<rp->nrhs; i++){
  1391. xsp = rp->rhs[i];
  1392. if( xsp->type==TERMINAL ){
  1393. SetAdd(newcfp->fws,xsp->index);
  1394. break;
  1395. }else if( xsp->type==MULTITERMINAL ){
  1396. int k;
  1397. for(k=0; k<xsp->nsubsym; k++){
  1398. SetAdd(newcfp->fws, xsp->subsym[k]->index);
  1399. }
  1400. break;
  1401. }else{
  1402. SetUnion(newcfp->fws,xsp->firstset);
  1403. if( xsp->lambda==LEMON_FALSE ) break;
  1404. }
  1405. }
  1406. if( i==rp->nrhs ) Plink_add(&cfp->fplp,newcfp);
  1407. }
  1408. }
  1409. }
  1410. return;
  1411. }
  1412. /* Sort the configuration list */
  1413. void Configlist_sort(void){
  1414. current = (struct config*)msort((char*)current,(char**)&(current->next),
  1415. Configcmp);
  1416. currentend = 0;
  1417. return;
  1418. }
  1419. /* Sort the basis configuration list */
  1420. void Configlist_sortbasis(void){
  1421. basis = (struct config*)msort((char*)current,(char**)&(current->bp),
  1422. Configcmp);
  1423. basisend = 0;
  1424. return;
  1425. }
  1426. /* Return a pointer to the head of the configuration list and
  1427. ** reset the list */
  1428. struct config *Configlist_return(void){
  1429. struct config *old;
  1430. old = current;
  1431. current = 0;
  1432. currentend = 0;
  1433. return old;
  1434. }
  1435. /* Return a pointer to the head of the configuration list and
  1436. ** reset the list */
  1437. struct config *Configlist_basis(void){
  1438. struct config *old;
  1439. old = basis;
  1440. basis = 0;
  1441. basisend = 0;
  1442. return old;
  1443. }
  1444. /* Free all elements of the given configuration list */
  1445. void Configlist_eat(struct config *cfp)
  1446. {
  1447. struct config *nextcfp;
  1448. for(; cfp; cfp=nextcfp){
  1449. nextcfp = cfp->next;
  1450. assert( cfp->fplp==0 );
  1451. assert( cfp->bplp==0 );
  1452. if( cfp->fws ) SetFree(cfp->fws);
  1453. deleteconfig(cfp);
  1454. }
  1455. return;
  1456. }
  1457. /***************** From the file "error.c" *********************************/
  1458. /*
  1459. ** Code for printing error message.
  1460. */
  1461. void ErrorMsg(const char *filename, int lineno, const char *format, ...){
  1462. va_list ap;
  1463. fprintf(stderr, "%s:%d: ", filename, lineno);
  1464. va_start(ap, format);
  1465. vfprintf(stderr,format,ap);
  1466. va_end(ap);
  1467. fprintf(stderr, "\n");
  1468. }
  1469. /**************** From the file "main.c" ************************************/
  1470. /*
  1471. ** Main program file for the LEMON parser generator.
  1472. */
  1473. /* Report an out-of-memory condition and abort. This function
  1474. ** is used mostly by the "MemoryCheck" macro in struct.h
  1475. */
  1476. void memory_error(void){
  1477. fprintf(stderr,"Out of memory. Aborting...\n");
  1478. exit(1);
  1479. }
  1480. static int nDefine = 0; /* Number of -D options on the command line */
  1481. static int nDefineUsed = 0; /* Number of -D options actually used */
  1482. static char **azDefine = 0; /* Name of the -D macros */
  1483. static char *bDefineUsed = 0; /* True for every -D macro actually used */
  1484. /* This routine is called with the argument to each -D command-line option.
  1485. ** Add the macro defined to the azDefine array.
  1486. */
  1487. static void handle_D_option(char *z){
  1488. char **paz;
  1489. nDefine++;
  1490. azDefine = (char **) lemon_realloc(azDefine, sizeof(azDefine[0])*nDefine);
  1491. if( azDefine==0 ){
  1492. fprintf(stderr,"out of memory\n");
  1493. exit(1);
  1494. }
  1495. bDefineUsed = (char*)lemon_realloc(bDefineUsed, nDefine);
  1496. if( bDefineUsed==0 ){
  1497. fprintf(stderr,"out of memory\n");
  1498. exit(1);
  1499. }
  1500. bDefineUsed[nDefine-1] = 0;
  1501. paz = &azDefine[nDefine-1];
  1502. *paz = (char *) lemon_malloc( lemonStrlen(z)+1 );
  1503. if( *paz==0 ){
  1504. fprintf(stderr,"out of memory\n");
  1505. exit(1);
  1506. }
  1507. lemon_strcpy(*paz, z);
  1508. for(z=*paz; *z && *z!='='; z++){}
  1509. *z = 0;
  1510. }
  1511. /* This routine is called with the argument to each -U command-line option.
  1512. ** Omit a previously defined macro.
  1513. */
  1514. static void handle_U_option(char *z){
  1515. int i;
  1516. for(i=0; i<nDefine; i++){
  1517. if( strcmp(azDefine[i],z)==0 ){
  1518. nDefine--;
  1519. if( i<nDefine ){
  1520. azDefine[i] = azDefine[nDefine];
  1521. bDefineUsed[i] = bDefineUsed[nDefine];
  1522. }
  1523. break;
  1524. }
  1525. }
  1526. }
  1527. /* Rember the name of the output directory
  1528. */
  1529. static char *outputDir = NULL;
  1530. static void handle_d_option(char *z){
  1531. outputDir = (char *) lemon_malloc( lemonStrlen(z)+1 );
  1532. if( outputDir==0 ){
  1533. fprintf(stderr,"out of memory\n");
  1534. exit(1);
  1535. }
  1536. lemon_strcpy(outputDir, z);
  1537. }
  1538. static char *user_templatename = NULL;
  1539. static void handle_T_option(char *z){
  1540. user_templatename = (char *) lemon_malloc( lemonStrlen(z)+1 );
  1541. if( user_templatename==0 ){
  1542. memory_error();
  1543. }
  1544. lemon_strcpy(user_templatename, z);
  1545. }
  1546. /* Merge together to lists of rules ordered by rule.iRule */
  1547. static struct rule *Rule_merge(struct rule *pA, struct rule *pB){
  1548. struct rule *pFirst = 0;
  1549. struct rule **ppPrev = &pFirst;
  1550. while( pA && pB ){
  1551. if( pA->iRule<pB->iRule ){
  1552. *ppPrev = pA;
  1553. ppPrev = &pA->next;
  1554. pA = pA->next;
  1555. }else{
  1556. *ppPrev = pB;
  1557. ppPrev = &pB->next;
  1558. pB = pB->next;
  1559. }
  1560. }
  1561. if( pA ){
  1562. *ppPrev = pA;
  1563. }else{
  1564. *ppPrev = pB;
  1565. }
  1566. return pFirst;
  1567. }
  1568. /*
  1569. ** Sort a list of rules in order of increasing iRule value
  1570. */
  1571. static struct rule *Rule_sort(struct rule *rp){
  1572. unsigned int i;
  1573. struct rule *pNext;
  1574. struct rule *x[32];
  1575. memset(x, 0, sizeof(x));
  1576. while( rp ){
  1577. pNext = rp->next;
  1578. rp->next = 0;
  1579. for(i=0; i<sizeof(x)/sizeof(x[0])-1 && x[i]; i++){
  1580. rp = Rule_merge(x[i], rp);
  1581. x[i] = 0;
  1582. }
  1583. x[i] = rp;
  1584. rp = pNext;
  1585. }
  1586. rp = 0;
  1587. for(i=0; i<sizeof(x)/sizeof(x[0]); i++){
  1588. rp = Rule_merge(x[i], rp);
  1589. }
  1590. return rp;
  1591. }
  1592. /* forward reference */
  1593. static const char *minimum_size_type(int lwr, int upr, int *pnByte);
  1594. /* Print a single line of the "Parser Stats" output
  1595. */
  1596. static void stats_line(const char *zLabel, int iValue){
  1597. int nLabel = lemonStrlen(zLabel);
  1598. printf(" %s%.*s %5d\n", zLabel,
  1599. 35-nLabel, "................................",
  1600. iValue);
  1601. }
  1602. /*
  1603. ** Comparison function used by qsort() to sort the azDefine[] array.
  1604. */
  1605. static int defineCmp(const void *pA, const void *pB){
  1606. const char *zA = *(const char**)pA;
  1607. const char *zB = *(const char**)pB;
  1608. return strcmp(zA,zB);
  1609. }
  1610. /* The main program. Parse the command line and do it... */
  1611. int main(int argc, char **argv){
  1612. static int version = 0;
  1613. static int rpflag = 0;
  1614. static int basisflag = 0;
  1615. static int compress = 0;
  1616. static int quiet = 0;
  1617. static int statistics = 0;
  1618. static int mhflag = 0;
  1619. static int nolinenosflag = 0;
  1620. static int noResort = 0;
  1621. static int sqlFlag = 0;
  1622. static int printPP = 0;
  1623. static struct s_options options[] = {
  1624. {OPT_FLAG, "b", (char*)&basisflag, "Print only the basis in report."},
  1625. {OPT_FLAG, "c", (char*)&compress, "Don't compress the action table."},
  1626. {OPT_FSTR, "d", (char*)&handle_d_option, "Output directory. Default '.'"},
  1627. {OPT_FSTR, "D", (char*)handle_D_option, "Define an %ifdef macro."},
  1628. {OPT_FLAG, "E", (char*)&printPP, "Print input file after preprocessing."},
  1629. {OPT_FSTR, "f", 0, "Ignored. (Placeholder for -f compiler options.)"},
  1630. {OPT_FLAG, "g", (char*)&rpflag, "Print grammar without actions."},
  1631. {OPT_FSTR, "I", 0, "Ignored. (Placeholder for '-I' compiler options.)"},
  1632. {OPT_FLAG, "m", (char*)&mhflag, "Output a makeheaders compatible file."},
  1633. {OPT_FLAG, "l", (char*)&nolinenosflag, "Do not print #line statements."},
  1634. {OPT_FSTR, "O", 0, "Ignored. (Placeholder for '-O' compiler options.)"},
  1635. {OPT_FLAG, "p", (char*)&showPrecedenceConflict,
  1636. "Show conflicts resolved by precedence rules"},
  1637. {OPT_FLAG, "q", (char*)&quiet, "(Quiet) Don't print the report file."},
  1638. {OPT_FLAG, "r", (char*)&noResort, "Do not sort or renumber states"},
  1639. {OPT_FLAG, "s", (char*)&statistics,
  1640. "Print parser stats to standard output."},
  1641. {OPT_FLAG, "S", (char*)&sqlFlag,
  1642. "Generate the *.sql file describing the parser tables."},
  1643. {OPT_FLAG, "x", (char*)&version, "Print the version number."},
  1644. {OPT_FSTR, "T", (char*)handle_T_option, "Specify a template file."},
  1645. {OPT_FSTR, "U", (char*)handle_U_option, "Undefine a macro."},
  1646. {OPT_FSTR, "W", 0, "Ignored. (Placeholder for '-W' compiler options.)"},
  1647. {OPT_FLAG,0,0,0}
  1648. };
  1649. int i;
  1650. int exitcode;
  1651. struct lemon lem;
  1652. struct rule *rp;
  1653. OptInit(argv,options,stderr);
  1654. if( version ){
  1655. printf("Lemon version 1.0\n");
  1656. exit(0);
  1657. }
  1658. if( OptNArgs()!=1 ){
  1659. fprintf(stderr,"Exactly one filename argument is required.\n");
  1660. exit(1);
  1661. }
  1662. memset(&lem, 0, sizeof(lem));
  1663. lem.errorcnt = 0;
  1664. qsort(azDefine, nDefine, sizeof(azDefine[0]), defineCmp);
  1665. /* Initialize the machine */
  1666. Strsafe_init();
  1667. Symbol_init();
  1668. State_init();
  1669. lem.argv = argv;
  1670. lem.argc = argc;
  1671. lem.filename = OptArg(0);
  1672. lem.basisflag = basisflag;
  1673. lem.nolinenosflag = nolinenosflag;
  1674. lem.printPreprocessed = printPP;
  1675. Symbol_new("$");
  1676. /* Parse the input file */
  1677. Parse(&lem);
  1678. if( lem.printPreprocessed || lem.errorcnt ) exit(lem.errorcnt);
  1679. if( lem.nrule==0 ){
  1680. fprintf(stderr,"Empty grammar.\n");
  1681. exit(1);
  1682. }
  1683. lem.errsym = Symbol_find("error");
  1684. /* Count and index the symbols of the grammar */
  1685. Symbol_new("{default}");
  1686. lem.nsymbol = Symbol_count();
  1687. lem.symbols = Symbol_arrayof();
  1688. for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
  1689. qsort(lem.symbols,lem.nsymbol,sizeof(struct symbol*), Symbolcmpp);
  1690. for(i=0; i<lem.nsymbol; i++) lem.symbols[i]->index = i;
  1691. while( lem.symbols[i-1]->type==MULTITERMINAL ){ i--; }
  1692. assert( strcmp(lem.symbols[i-1]->name,"{default}")==0 );
  1693. lem.nsymbol = i - 1;
  1694. for(i=1; ISUPPER(lem.symbols[i]->name[0]); i++);
  1695. lem.nterminal = i;
  1696. /* Assign sequential rule numbers. Start with 0. Put rules that have no
  1697. ** reduce action C-code associated with them last, so that the switch()
  1698. ** statement that selects reduction actions will have a smaller jump table.
  1699. */
  1700. for(i=0, rp=lem.rule; rp; rp=rp->next){
  1701. rp->iRule = rp->code ? i++ : -1;
  1702. }
  1703. lem.nruleWithAction = i;
  1704. for(rp=lem.rule; rp; rp=rp->next){
  1705. if( rp->iRule<0 ) rp->iRule = i++;
  1706. }
  1707. lem.startRule = lem.rule;
  1708. lem.rule = Rule_sort(lem.rule);
  1709. /* Generate a reprint of the grammar, if requested on the command line */
  1710. if( rpflag ){
  1711. Reprint(&lem);
  1712. }else{
  1713. /* Initialize the size for all follow and first sets */
  1714. SetSize(lem.nterminal+1);
  1715. /* Find the precedence for every production rule (that has one) */
  1716. FindRulePrecedences(&lem);
  1717. /* Compute the lambda-nonterminals and the first-sets for every
  1718. ** nonterminal */
  1719. FindFirstSets(&lem);
  1720. /* Compute all LR(0) states. Also record follow-set propagation
  1721. ** links so that the follow-set can be computed later */
  1722. lem.nstate = 0;
  1723. FindStates(&lem);
  1724. lem.sorted = State_arrayof();
  1725. /* Tie up loose ends on the propagation links */
  1726. FindLinks(&lem);
  1727. /* Compute the follow set of every reducible configuration */
  1728. FindFollowSets(&lem);
  1729. /* Compute the action tables */
  1730. FindActions(&lem);
  1731. /* Compress the action tables */
  1732. if( compress==0 ) CompressTables(&lem);
  1733. /* Reorder and renumber the states so that states with fewer choices
  1734. ** occur at the end. This is an optimization that helps make the
  1735. ** generated parser tables smaller. */
  1736. if( noResort==0 ) ResortStates(&lem);
  1737. /* Generate a report of the parser generated. (the "y.output" file) */
  1738. if( !quiet ) ReportOutput(&lem);
  1739. /* Generate the source code for the parser */
  1740. ReportTable(&lem, mhflag, sqlFlag);
  1741. /* Produce a header file for use by the scanner. (This step is
  1742. ** omitted if the "-m" option is used because makeheaders will
  1743. ** generate the file for us.) */
  1744. if( !mhflag ) ReportHeader(&lem);
  1745. }
  1746. if( statistics ){
  1747. printf("Parser statistics:\n");
  1748. stats_line("terminal symbols", lem.nterminal);
  1749. stats_line("non-terminal symbols", lem.nsymbol - lem.nterminal);
  1750. stats_line("total symbols", lem.nsymbol);
  1751. stats_line("rules", lem.nrule);
  1752. stats_line("states", lem.nxstate);
  1753. stats_line("conflicts", lem.nconflict);
  1754. stats_line("action table entries", lem.nactiontab);
  1755. stats_line("lookahead table entries", lem.nlookaheadtab);
  1756. stats_line("total table size (bytes)", lem.tablesize);
  1757. }
  1758. if( lem.nconflict > 0 ){
  1759. fprintf(stderr,"%d parsing conflicts.\n",lem.nconflict);
  1760. }
  1761. /* return 0 on success, 1 on failure. */
  1762. exitcode = ((lem.errorcnt > 0) || (lem.nconflict > 0)) ? 1 : 0;
  1763. lemon_free_all();
  1764. exit(exitcode);
  1765. return (exitcode);
  1766. }
  1767. /******************** From the file "msort.c" *******************************/
  1768. /*
  1769. ** A generic merge-sort program.
  1770. **
  1771. ** USAGE:
  1772. ** Let "ptr" be a pointer to some structure which is at the head of
  1773. ** a null-terminated list. Then to sort the list call:
  1774. **
  1775. ** ptr = msort(ptr,&(ptr->next),cmpfnc);
  1776. **
  1777. ** In the above, "cmpfnc" is a pointer to a function which compares
  1778. ** two instances of the structure and returns an integer, as in
  1779. ** strcmp. The second argument is a pointer to the pointer to the
  1780. ** second element of the linked list. This address is used to compute
  1781. ** the offset to the "next" field within the structure. The offset to
  1782. ** the "next" field must be constant for all structures in the list.
  1783. **
  1784. ** The function returns a new pointer which is the head of the list
  1785. ** after sorting.
  1786. **
  1787. ** ALGORITHM:
  1788. ** Merge-sort.
  1789. */
  1790. /*
  1791. ** Return a pointer to the next structure in the linked list.
  1792. */
  1793. #define NEXT(A) (*(char**)(((char*)A)+offset))
  1794. /*
  1795. ** Inputs:
  1796. ** a: A sorted, null-terminated linked list. (May be null).
  1797. ** b: A sorted, null-terminated linked list. (May be null).
  1798. ** cmp: A pointer to the comparison function.
  1799. ** offset: Offset in the structure to the "next" field.
  1800. **
  1801. ** Return Value:
  1802. ** A pointer to the head of a sorted list containing the elements
  1803. ** of both a and b.
  1804. **
  1805. ** Side effects:
  1806. ** The "next" pointers for elements in the lists a and b are
  1807. ** changed.
  1808. */
  1809. static char *merge(
  1810. char *a,
  1811. char *b,
  1812. int (*cmp)(const char*,const char*),
  1813. int offset
  1814. ){
  1815. char *ptr, *head;
  1816. if( a==0 ){
  1817. head = b;
  1818. }else if( b==0 ){
  1819. head = a;
  1820. }else{
  1821. if( (*cmp)(a,b)<=0 ){
  1822. ptr = a;
  1823. a = NEXT(a);
  1824. }else{
  1825. ptr = b;
  1826. b = NEXT(b);
  1827. }
  1828. head = ptr;
  1829. while( a && b ){
  1830. if( (*cmp)(a,b)<=0 ){
  1831. NEXT(ptr) = a;
  1832. ptr = a;
  1833. a = NEXT(a);
  1834. }else{
  1835. NEXT(ptr) = b;
  1836. ptr = b;
  1837. b = NEXT(b);
  1838. }
  1839. }
  1840. if( a ) NEXT(ptr) = a;
  1841. else NEXT(ptr) = b;
  1842. }
  1843. return head;
  1844. }
  1845. /*
  1846. ** Inputs:
  1847. ** list: Pointer to a singly-linked list of structures.
  1848. ** next: Pointer to pointer to the second element of the list.
  1849. ** cmp: A comparison function.
  1850. **
  1851. ** Return Value:
  1852. ** A pointer to the head of a sorted list containing the elements
  1853. ** originally in list.
  1854. **
  1855. ** Side effects:
  1856. ** The "next" pointers for elements in list are changed.
  1857. */
  1858. #define LISTSIZE 30
  1859. static char *msort(
  1860. char *list,
  1861. char **next,
  1862. int (*cmp)(const char*,const char*)
  1863. ){
  1864. unsigned long offset;
  1865. char *ep;
  1866. char *set[LISTSIZE];
  1867. int i;
  1868. offset = (unsigned long)((char*)next - (char*)list);
  1869. for(i=0; i<LISTSIZE; i++) set[i] = 0;
  1870. while( list ){
  1871. ep = list;
  1872. list = NEXT(list);
  1873. NEXT(ep) = 0;
  1874. for(i=0; i<LISTSIZE-1 && set[i]!=0; i++){
  1875. ep = merge(ep,set[i],cmp,offset);
  1876. set[i] = 0;
  1877. }
  1878. set[i] = ep;
  1879. }
  1880. ep = 0;
  1881. for(i=0; i<LISTSIZE; i++) if( set[i] ) ep = merge(set[i],ep,cmp,offset);
  1882. return ep;
  1883. }
  1884. /************************ From the file "option.c" **************************/
  1885. static char **g_argv;
  1886. static struct s_options *op;
  1887. static FILE *errstream;
  1888. #define ISOPT(X) ((X)[0]=='-'||(X)[0]=='+'||strchr((X),'=')!=0)
  1889. /*
  1890. ** Print the command line with a carrot pointing to the k-th character
  1891. ** of the n-th field.
  1892. */
  1893. static void errline(int n, int k, FILE *err)
  1894. {
  1895. int spcnt, i;
  1896. if( g_argv[0] ){
  1897. fprintf(err,"%s",g_argv[0]);
  1898. spcnt = lemonStrlen(g_argv[0]) + 1;
  1899. }else{
  1900. spcnt = 0;
  1901. }
  1902. for(i=1; i<n && g_argv[i]; i++){
  1903. fprintf(err," %s",g_argv[i]);
  1904. spcnt += lemonStrlen(g_argv[i])+1;
  1905. }
  1906. spcnt += k;
  1907. for(; g_argv[i]; i++) fprintf(err," %s",g_argv[i]);
  1908. if( spcnt<20 ){
  1909. fprintf(err,"\n%*s^-- here\n",spcnt,"");
  1910. }else{
  1911. fprintf(err,"\n%*shere --^\n",spcnt-7,"");
  1912. }
  1913. }
  1914. /*
  1915. ** Return the index of the N-th non-switch argument. Return -1
  1916. ** if N is out of range.
  1917. */
  1918. static int argindex(int n)
  1919. {
  1920. int i;
  1921. int dashdash = 0;
  1922. if( g_argv!=0 && *g_argv!=0 ){
  1923. for(i=1; g_argv[i]; i++){
  1924. if( dashdash || !ISOPT(g_argv[i]) ){
  1925. if( n==0 ) return i;
  1926. n--;
  1927. }
  1928. if( strcmp(g_argv[i],"--")==0 ) dashdash = 1;
  1929. }
  1930. }
  1931. return -1;
  1932. }
  1933. static char emsg[] = "Command line syntax error: ";
  1934. /*
  1935. ** Process a flag command line argument.
  1936. */
  1937. static int handleflags(int i, FILE *err)
  1938. {
  1939. int v;
  1940. int errcnt = 0;
  1941. int j;
  1942. for(j=0; op[j].label; j++){
  1943. if( strncmp(&g_argv[i][1],op[j].label,lemonStrlen(op[j].label))==0 ) break;
  1944. }
  1945. v = g_argv[i][0]=='-' ? 1 : 0;
  1946. if( op[j].label==0 ){
  1947. if( err ){
  1948. fprintf(err,"%sundefined option.\n",emsg);
  1949. errline(i,1,err);
  1950. }
  1951. errcnt++;
  1952. }else if( op[j].arg==0 ){
  1953. /* Ignore this option */
  1954. }else if( op[j].type==OPT_FLAG ){
  1955. *((int*)op[j].arg) = v;
  1956. }else if( op[j].type==OPT_FFLAG ){
  1957. (*(void(*)(int))(op[j].arg))(v);
  1958. }else if( op[j].type==OPT_FSTR ){
  1959. (*(void(*)(char *))(op[j].arg))(&g_argv[i][2]);
  1960. }else{
  1961. if( err ){
  1962. fprintf(err,"%smissing argument on switch.\n",emsg);
  1963. errline(i,1,err);
  1964. }
  1965. errcnt++;
  1966. }
  1967. return errcnt;
  1968. }
  1969. /*
  1970. ** Process a command line switch which has an argument.
  1971. */
  1972. static int handleswitch(int i, FILE *err)
  1973. {
  1974. int lv = 0;
  1975. double dv = 0.0;
  1976. char *sv = 0, *end;
  1977. char *cp;
  1978. int j;
  1979. int errcnt = 0;
  1980. cp = strchr(g_argv[i],'=');
  1981. assert( cp!=0 );
  1982. *cp = 0;
  1983. for(j=0; op[j].label; j++){
  1984. if( strcmp(g_argv[i],op[j].label)==0 ) break;
  1985. }
  1986. *cp = '=';
  1987. if( op[j].label==0 ){
  1988. if( err ){
  1989. fprintf(err,"%sundefined option.\n",emsg);
  1990. errline(i,0,err);
  1991. }
  1992. errcnt++;
  1993. }else{
  1994. cp++;
  1995. switch( op[j].type ){
  1996. case OPT_FLAG:
  1997. case OPT_FFLAG:
  1998. if( err ){
  1999. fprintf(err,"%soption requires an argument.\n",emsg);
  2000. errline(i,0,err);
  2001. }
  2002. errcnt++;
  2003. break;
  2004. case OPT_DBL:
  2005. case OPT_FDBL:
  2006. dv = strtod(cp,&end);
  2007. if( *end ){
  2008. if( err ){
  2009. fprintf(err,
  2010. "%sillegal character in floating-point argument.\n",emsg);
  2011. errline(i,(int)((char*)end-(char*)g_argv[i]),err);
  2012. }
  2013. errcnt++;
  2014. }
  2015. break;
  2016. case OPT_INT:
  2017. case OPT_FINT:
  2018. lv = strtol(cp,&end,0);
  2019. if( *end ){
  2020. if( err ){
  2021. fprintf(err,"%sillegal character in integer argument.\n",emsg);
  2022. errline(i,(int)((char*)end-(char*)g_argv[i]),err);
  2023. }
  2024. errcnt++;
  2025. }
  2026. break;
  2027. case OPT_STR:
  2028. case OPT_FSTR:
  2029. sv = cp;
  2030. break;
  2031. }
  2032. switch( op[j].type ){
  2033. case OPT_FLAG:
  2034. case OPT_FFLAG:
  2035. break;
  2036. case OPT_DBL:
  2037. *(double*)(op[j].arg) = dv;
  2038. break;
  2039. case OPT_FDBL:
  2040. (*(void(*)(double))(op[j].arg))(dv);
  2041. break;
  2042. case OPT_INT:
  2043. *(int*)(op[j].arg) = lv;
  2044. break;
  2045. case OPT_FINT:
  2046. (*(void(*)(int))(op[j].arg))((int)lv);
  2047. break;
  2048. case OPT_STR:
  2049. *(char**)(op[j].arg) = sv;
  2050. break;
  2051. case OPT_FSTR:
  2052. (*(void(*)(char *))(op[j].arg))(sv);
  2053. break;
  2054. }
  2055. }
  2056. return errcnt;
  2057. }
  2058. int OptInit(char **a, struct s_options *o, FILE *err)
  2059. {
  2060. int errcnt = 0;
  2061. g_argv = a;
  2062. op = o;
  2063. errstream = err;
  2064. if( g_argv && *g_argv && op ){
  2065. int i;
  2066. for(i=1; g_argv[i]; i++){
  2067. if( g_argv[i][0]=='+' || g_argv[i][0]=='-' ){
  2068. errcnt += handleflags(i,err);
  2069. }else if( strchr(g_argv[i],'=') ){
  2070. errcnt += handleswitch(i,err);
  2071. }
  2072. }
  2073. }
  2074. if( errcnt>0 ){
  2075. fprintf(err,"Valid command line options for \"%s\" are:\n",*a);
  2076. OptPrint();
  2077. exit(1);
  2078. }
  2079. return 0;
  2080. }
  2081. int OptNArgs(void){
  2082. int cnt = 0;
  2083. int dashdash = 0;
  2084. int i;
  2085. if( g_argv!=0 && g_argv[0]!=0 ){
  2086. for(i=1; g_argv[i]; i++){
  2087. if( dashdash || !ISOPT(g_argv[i]) ) cnt++;
  2088. if( strcmp(g_argv[i],"--")==0 ) dashdash = 1;
  2089. }
  2090. }
  2091. return cnt;
  2092. }
  2093. char *OptArg(int n)
  2094. {
  2095. int i;
  2096. i = argindex(n);
  2097. return i>=0 ? g_argv[i] : 0;
  2098. }
  2099. void OptErr(int n)
  2100. {
  2101. int i;
  2102. i = argindex(n);
  2103. if( i>=0 ) errline(i,0,errstream);
  2104. }
  2105. void OptPrint(void){
  2106. int i;
  2107. int max, len;
  2108. max = 0;
  2109. for(i=0; op[i].label; i++){
  2110. len = lemonStrlen(op[i].label) + 1;
  2111. switch( op[i].type ){
  2112. case OPT_FLAG:
  2113. case OPT_FFLAG:
  2114. break;
  2115. case OPT_INT:
  2116. case OPT_FINT:
  2117. len += 9; /* length of "<integer>" */
  2118. break;
  2119. case OPT_DBL:
  2120. case OPT_FDBL:
  2121. len += 6; /* length of "<real>" */
  2122. break;
  2123. case OPT_STR:
  2124. case OPT_FSTR:
  2125. len += 8; /* length of "<string>" */
  2126. break;
  2127. }
  2128. if( len>max ) max = len;
  2129. }
  2130. for(i=0; op[i].label; i++){
  2131. switch( op[i].type ){
  2132. case OPT_FLAG:
  2133. case OPT_FFLAG:
  2134. fprintf(errstream," -%-*s %s\n",max,op[i].label,op[i].message);
  2135. break;
  2136. case OPT_INT:
  2137. case OPT_FINT:
  2138. fprintf(errstream," -%s<integer>%*s %s\n",op[i].label,
  2139. (int)(max-lemonStrlen(op[i].label)-9),"",op[i].message);
  2140. break;
  2141. case OPT_DBL:
  2142. case OPT_FDBL:
  2143. fprintf(errstream," -%s<real>%*s %s\n",op[i].label,
  2144. (int)(max-lemonStrlen(op[i].label)-6),"",op[i].message);
  2145. break;
  2146. case OPT_STR:
  2147. case OPT_FSTR:
  2148. fprintf(errstream," -%s<string>%*s %s\n",op[i].label,
  2149. (int)(max-lemonStrlen(op[i].label)-8),"",op[i].message);
  2150. break;
  2151. }
  2152. }
  2153. }
  2154. /*********************** From the file "parse.c" ****************************/
  2155. /*
  2156. ** Input file parser for the LEMON parser generator.
  2157. */
  2158. /* The state of the parser */
  2159. enum e_state {
  2160. INITIALIZE,
  2161. WAITING_FOR_DECL_OR_RULE,
  2162. WAITING_FOR_DECL_KEYWORD,
  2163. WAITING_FOR_DECL_ARG,
  2164. WAITING_FOR_PRECEDENCE_SYMBOL,
  2165. WAITING_FOR_ARROW,
  2166. IN_RHS,
  2167. LHS_ALIAS_1,
  2168. LHS_ALIAS_2,
  2169. LHS_ALIAS_3,
  2170. RHS_ALIAS_1,
  2171. RHS_ALIAS_2,
  2172. PRECEDENCE_MARK_1,
  2173. PRECEDENCE_MARK_2,
  2174. RESYNC_AFTER_RULE_ERROR,
  2175. RESYNC_AFTER_DECL_ERROR,
  2176. WAITING_FOR_DESTRUCTOR_SYMBOL,
  2177. WAITING_FOR_DATATYPE_SYMBOL,
  2178. WAITING_FOR_FALLBACK_ID,
  2179. WAITING_FOR_WILDCARD_ID,
  2180. WAITING_FOR_CLASS_ID,
  2181. WAITING_FOR_CLASS_TOKEN,
  2182. WAITING_FOR_TOKEN_NAME
  2183. };
  2184. struct pstate {
  2185. char *filename; /* Name of the input file */
  2186. int tokenlineno; /* Linenumber at which current token starts */
  2187. int errorcnt; /* Number of errors so far */
  2188. char *tokenstart; /* Text of current token */
  2189. struct lemon *gp; /* Global state vector */
  2190. enum e_state state; /* The state of the parser */
  2191. struct symbol *fallback; /* The fallback token */
  2192. struct symbol *tkclass; /* Token class symbol */
  2193. struct symbol *lhs; /* Left-hand side of current rule */
  2194. const char *lhsalias; /* Alias for the LHS */
  2195. int nrhs; /* Number of right-hand side symbols seen */
  2196. struct symbol *rhs[MAXRHS]; /* RHS symbols */
  2197. const char *alias[MAXRHS]; /* Aliases for each RHS symbol (or NULL) */
  2198. struct rule *prevrule; /* Previous rule parsed */
  2199. const char *declkeyword; /* Keyword of a declaration */
  2200. char **declargslot; /* Where the declaration argument should be put */
  2201. int insertLineMacro; /* Add #line before declaration insert */
  2202. int *decllinenoslot; /* Where to write declaration line number */
  2203. enum e_assoc declassoc; /* Assign this association to decl arguments */
  2204. int preccounter; /* Assign this precedence to decl arguments */
  2205. struct rule *firstrule; /* Pointer to first rule in the grammar */
  2206. struct rule *lastrule; /* Pointer to the most recently parsed rule */
  2207. };
  2208. /* Parse a single token */
  2209. static void parseonetoken(struct pstate *psp)
  2210. {
  2211. const char *x;
  2212. x = Strsafe(psp->tokenstart); /* Save the token permanently */
  2213. #if 0
  2214. printf("%s:%d: Token=[%s] state=%d\n",psp->filename,psp->tokenlineno,
  2215. x,psp->state);
  2216. #endif
  2217. switch( psp->state ){
  2218. case INITIALIZE:
  2219. psp->prevrule = 0;
  2220. psp->preccounter = 0;
  2221. psp->firstrule = psp->lastrule = 0;
  2222. psp->gp->nrule = 0;
  2223. /* fall through */
  2224. case WAITING_FOR_DECL_OR_RULE:
  2225. if( x[0]=='%' ){
  2226. psp->state = WAITING_FOR_DECL_KEYWORD;
  2227. }else if( ISLOWER(x[0]) ){
  2228. psp->lhs = Symbol_new(x);
  2229. psp->nrhs = 0;
  2230. psp->lhsalias = 0;
  2231. psp->state = WAITING_FOR_ARROW;
  2232. }else if( x[0]=='{' ){
  2233. if( psp->prevrule==0 ){
  2234. ErrorMsg(psp->filename,psp->tokenlineno,
  2235. "There is no prior rule upon which to attach the code "
  2236. "fragment which begins on this line.");
  2237. psp->errorcnt++;
  2238. }else if( psp->prevrule->code!=0 ){
  2239. ErrorMsg(psp->filename,psp->tokenlineno,
  2240. "Code fragment beginning on this line is not the first "
  2241. "to follow the previous rule.");
  2242. psp->errorcnt++;
  2243. }else if( strcmp(x, "{NEVER-REDUCE")==0 ){
  2244. psp->prevrule->neverReduce = 1;
  2245. }else{
  2246. psp->prevrule->line = psp->tokenlineno;
  2247. psp->prevrule->code = &x[1];
  2248. psp->prevrule->noCode = 0;
  2249. }
  2250. }else if( x[0]=='[' ){
  2251. psp->state = PRECEDENCE_MARK_1;
  2252. }else{
  2253. ErrorMsg(psp->filename,psp->tokenlineno,
  2254. "Token \"%s\" should be either \"%%\" or a nonterminal name.",
  2255. x);
  2256. psp->errorcnt++;
  2257. }
  2258. break;
  2259. case PRECEDENCE_MARK_1:
  2260. if( !ISUPPER(x[0]) ){
  2261. ErrorMsg(psp->filename,psp->tokenlineno,
  2262. "The precedence symbol must be a terminal.");
  2263. psp->errorcnt++;
  2264. }else if( psp->prevrule==0 ){
  2265. ErrorMsg(psp->filename,psp->tokenlineno,
  2266. "There is no prior rule to assign precedence \"[%s]\".",x);
  2267. psp->errorcnt++;
  2268. }else if( psp->prevrule->precsym!=0 ){
  2269. ErrorMsg(psp->filename,psp->tokenlineno,
  2270. "Precedence mark on this line is not the first "
  2271. "to follow the previous rule.");
  2272. psp->errorcnt++;
  2273. }else{
  2274. psp->prevrule->precsym = Symbol_new(x);
  2275. }
  2276. psp->state = PRECEDENCE_MARK_2;
  2277. break;
  2278. case PRECEDENCE_MARK_2:
  2279. if( x[0]!=']' ){
  2280. ErrorMsg(psp->filename,psp->tokenlineno,
  2281. "Missing \"]\" on precedence mark.");
  2282. psp->errorcnt++;
  2283. }
  2284. psp->state = WAITING_FOR_DECL_OR_RULE;
  2285. break;
  2286. case WAITING_FOR_ARROW:
  2287. if( x[0]==':' && x[1]==':' && x[2]=='=' ){
  2288. psp->state = IN_RHS;
  2289. }else if( x[0]=='(' ){
  2290. psp->state = LHS_ALIAS_1;
  2291. }else{
  2292. ErrorMsg(psp->filename,psp->tokenlineno,
  2293. "Expected to see a \":\" following the LHS symbol \"%s\".",
  2294. psp->lhs->name);
  2295. psp->errorcnt++;
  2296. psp->state = RESYNC_AFTER_RULE_ERROR;
  2297. }
  2298. break;
  2299. case LHS_ALIAS_1:
  2300. if( ISALPHA(x[0]) ){
  2301. psp->lhsalias = x;
  2302. psp->state = LHS_ALIAS_2;
  2303. }else{
  2304. ErrorMsg(psp->filename,psp->tokenlineno,
  2305. "\"%s\" is not a valid alias for the LHS \"%s\"\n",
  2306. x,psp->lhs->name);
  2307. psp->errorcnt++;
  2308. psp->state = RESYNC_AFTER_RULE_ERROR;
  2309. }
  2310. break;
  2311. case LHS_ALIAS_2:
  2312. if( x[0]==')' ){
  2313. psp->state = LHS_ALIAS_3;
  2314. }else{
  2315. ErrorMsg(psp->filename,psp->tokenlineno,
  2316. "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
  2317. psp->errorcnt++;
  2318. psp->state = RESYNC_AFTER_RULE_ERROR;
  2319. }
  2320. break;
  2321. case LHS_ALIAS_3:
  2322. if( x[0]==':' && x[1]==':' && x[2]=='=' ){
  2323. psp->state = IN_RHS;
  2324. }else{
  2325. ErrorMsg(psp->filename,psp->tokenlineno,
  2326. "Missing \"->\" following: \"%s(%s)\".",
  2327. psp->lhs->name,psp->lhsalias);
  2328. psp->errorcnt++;
  2329. psp->state = RESYNC_AFTER_RULE_ERROR;
  2330. }
  2331. break;
  2332. case IN_RHS:
  2333. if( x[0]=='.' ){
  2334. struct rule *rp;
  2335. rp = (struct rule *)lemon_calloc( sizeof(struct rule) +
  2336. sizeof(struct symbol*)*psp->nrhs + sizeof(char*)*psp->nrhs, 1);
  2337. if( rp==0 ){
  2338. ErrorMsg(psp->filename,psp->tokenlineno,
  2339. "Can't allocate enough memory for this rule.");
  2340. psp->errorcnt++;
  2341. psp->prevrule = 0;
  2342. }else{
  2343. int i;
  2344. rp->ruleline = psp->tokenlineno;
  2345. rp->rhs = (struct symbol**)&rp[1];
  2346. rp->rhsalias = (const char**)&(rp->rhs[psp->nrhs]);
  2347. for(i=0; i<psp->nrhs; i++){
  2348. rp->rhs[i] = psp->rhs[i];
  2349. rp->rhsalias[i] = psp->alias[i];
  2350. if( rp->rhsalias[i]!=0 ){ rp->rhs[i]->bContent = 1; }
  2351. }
  2352. rp->lhs = psp->lhs;
  2353. rp->lhsalias = psp->lhsalias;
  2354. rp->nrhs = psp->nrhs;
  2355. rp->code = 0;
  2356. rp->noCode = 1;
  2357. rp->precsym = 0;
  2358. rp->index = psp->gp->nrule++;
  2359. rp->nextlhs = rp->lhs->rule;
  2360. rp->lhs->rule = rp;
  2361. rp->next = 0;
  2362. if( psp->firstrule==0 ){
  2363. psp->firstrule = psp->lastrule = rp;
  2364. }else{
  2365. psp->lastrule->next = rp;
  2366. psp->lastrule = rp;
  2367. }
  2368. psp->prevrule = rp;
  2369. }
  2370. psp->state = WAITING_FOR_DECL_OR_RULE;
  2371. }else if( ISALPHA(x[0]) ){
  2372. if( psp->nrhs>=MAXRHS ){
  2373. ErrorMsg(psp->filename,psp->tokenlineno,
  2374. "Too many symbols on RHS of rule beginning at \"%s\".",
  2375. x);
  2376. psp->errorcnt++;
  2377. psp->state = RESYNC_AFTER_RULE_ERROR;
  2378. }else{
  2379. psp->rhs[psp->nrhs] = Symbol_new(x);
  2380. psp->alias[psp->nrhs] = 0;
  2381. psp->nrhs++;
  2382. }
  2383. }else if( (x[0]=='|' || x[0]=='/') && psp->nrhs>0 && ISUPPER(x[1]) ){
  2384. struct symbol *msp = psp->rhs[psp->nrhs-1];
  2385. if( msp->type!=MULTITERMINAL ){
  2386. struct symbol *origsp = msp;
  2387. msp = (struct symbol *) lemon_calloc(1,sizeof(*msp));
  2388. memset(msp, 0, sizeof(*msp));
  2389. msp->type = MULTITERMINAL;
  2390. msp->nsubsym = 1;
  2391. msp->subsym = (struct symbol**)lemon_calloc(1,sizeof(struct symbol*));
  2392. msp->subsym[0] = origsp;
  2393. msp->name = origsp->name;
  2394. psp->rhs[psp->nrhs-1] = msp;
  2395. }
  2396. msp->nsubsym++;
  2397. msp->subsym = (struct symbol **) lemon_realloc(msp->subsym,
  2398. sizeof(struct symbol*)*msp->nsubsym);
  2399. msp->subsym[msp->nsubsym-1] = Symbol_new(&x[1]);
  2400. if( ISLOWER(x[1]) || ISLOWER(msp->subsym[0]->name[0]) ){
  2401. ErrorMsg(psp->filename,psp->tokenlineno,
  2402. "Cannot form a compound containing a non-terminal");
  2403. psp->errorcnt++;
  2404. }
  2405. }else if( x[0]=='(' && psp->nrhs>0 ){
  2406. psp->state = RHS_ALIAS_1;
  2407. }else{
  2408. ErrorMsg(psp->filename,psp->tokenlineno,
  2409. "Illegal character on RHS of rule: \"%s\".",x);
  2410. psp->errorcnt++;
  2411. psp->state = RESYNC_AFTER_RULE_ERROR;
  2412. }
  2413. break;
  2414. case RHS_ALIAS_1:
  2415. if( ISALPHA(x[0]) ){
  2416. psp->alias[psp->nrhs-1] = x;
  2417. psp->state = RHS_ALIAS_2;
  2418. }else{
  2419. ErrorMsg(psp->filename,psp->tokenlineno,
  2420. "\"%s\" is not a valid alias for the RHS symbol \"%s\"\n",
  2421. x,psp->rhs[psp->nrhs-1]->name);
  2422. psp->errorcnt++;
  2423. psp->state = RESYNC_AFTER_RULE_ERROR;
  2424. }
  2425. break;
  2426. case RHS_ALIAS_2:
  2427. if( x[0]==')' ){
  2428. psp->state = IN_RHS;
  2429. }else{
  2430. ErrorMsg(psp->filename,psp->tokenlineno,
  2431. "Missing \")\" following LHS alias name \"%s\".",psp->lhsalias);
  2432. psp->errorcnt++;
  2433. psp->state = RESYNC_AFTER_RULE_ERROR;
  2434. }
  2435. break;
  2436. case WAITING_FOR_DECL_KEYWORD:
  2437. if( ISALPHA(x[0]) ){
  2438. psp->declkeyword = x;
  2439. psp->declargslot = 0;
  2440. psp->decllinenoslot = 0;
  2441. psp->insertLineMacro = 1;
  2442. psp->state = WAITING_FOR_DECL_ARG;
  2443. if( strcmp(x,"name")==0 ){
  2444. psp->declargslot = &(psp->gp->name);
  2445. psp->insertLineMacro = 0;
  2446. }else if( strcmp(x,"include")==0 ){
  2447. psp->declargslot = &(psp->gp->include);
  2448. }else if( strcmp(x,"code")==0 ){
  2449. psp->declargslot = &(psp->gp->extracode);
  2450. }else if( strcmp(x,"token_destructor")==0 ){
  2451. psp->declargslot = &psp->gp->tokendest;
  2452. }else if( strcmp(x,"default_destructor")==0 ){
  2453. psp->declargslot = &psp->gp->vardest;
  2454. }else if( strcmp(x,"token_prefix")==0 ){
  2455. psp->declargslot = &psp->gp->tokenprefix;
  2456. psp->insertLineMacro = 0;
  2457. }else if( strcmp(x,"syntax_error")==0 ){
  2458. psp->declargslot = &(psp->gp->error);
  2459. }else if( strcmp(x,"parse_accept")==0 ){
  2460. psp->declargslot = &(psp->gp->accept);
  2461. }else if( strcmp(x,"parse_failure")==0 ){
  2462. psp->declargslot = &(psp->gp->failure);
  2463. }else if( strcmp(x,"stack_overflow")==0 ){
  2464. psp->declargslot = &(psp->gp->overflow);
  2465. }else if( strcmp(x,"extra_argument")==0 ){
  2466. psp->declargslot = &(psp->gp->arg);
  2467. psp->insertLineMacro = 0;
  2468. }else if( strcmp(x,"extra_context")==0 ){
  2469. psp->declargslot = &(psp->gp->ctx);
  2470. psp->insertLineMacro = 0;
  2471. }else if( strcmp(x,"token_type")==0 ){
  2472. psp->declargslot = &(psp->gp->tokentype);
  2473. psp->insertLineMacro = 0;
  2474. }else if( strcmp(x,"default_type")==0 ){
  2475. psp->declargslot = &(psp->gp->vartype);
  2476. psp->insertLineMacro = 0;
  2477. }else if( strcmp(x,"realloc")==0 ){
  2478. psp->declargslot = &(psp->gp->reallocFunc);
  2479. psp->insertLineMacro = 0;
  2480. }else if( strcmp(x,"free")==0 ){
  2481. psp->declargslot = &(psp->gp->freeFunc);
  2482. psp->insertLineMacro = 0;
  2483. }else if( strcmp(x,"stack_size")==0 ){
  2484. psp->declargslot = &(psp->gp->stacksize);
  2485. psp->insertLineMacro = 0;
  2486. }else if( strcmp(x,"start_symbol")==0 ){
  2487. psp->declargslot = &(psp->gp->start);
  2488. psp->insertLineMacro = 0;
  2489. }else if( strcmp(x,"left")==0 ){
  2490. psp->preccounter++;
  2491. psp->declassoc = LEFT;
  2492. psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
  2493. }else if( strcmp(x,"right")==0 ){
  2494. psp->preccounter++;
  2495. psp->declassoc = RIGHT;
  2496. psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
  2497. }else if( strcmp(x,"nonassoc")==0 ){
  2498. psp->preccounter++;
  2499. psp->declassoc = NONE;
  2500. psp->state = WAITING_FOR_PRECEDENCE_SYMBOL;
  2501. }else if( strcmp(x,"destructor")==0 ){
  2502. psp->state = WAITING_FOR_DESTRUCTOR_SYMBOL;
  2503. }else if( strcmp(x,"type")==0 ){
  2504. psp->state = WAITING_FOR_DATATYPE_SYMBOL;
  2505. }else if( strcmp(x,"fallback")==0 ){
  2506. psp->fallback = 0;
  2507. psp->state = WAITING_FOR_FALLBACK_ID;
  2508. }else if( strcmp(x,"token")==0 ){
  2509. psp->state = WAITING_FOR_TOKEN_NAME;
  2510. }else if( strcmp(x,"wildcard")==0 ){
  2511. psp->state = WAITING_FOR_WILDCARD_ID;
  2512. }else if( strcmp(x,"token_class")==0 ){
  2513. psp->state = WAITING_FOR_CLASS_ID;
  2514. }else{
  2515. ErrorMsg(psp->filename,psp->tokenlineno,
  2516. "Unknown declaration keyword: \"%%%s\".",x);
  2517. psp->errorcnt++;
  2518. psp->state = RESYNC_AFTER_DECL_ERROR;
  2519. }
  2520. }else{
  2521. ErrorMsg(psp->filename,psp->tokenlineno,
  2522. "Illegal declaration keyword: \"%s\".",x);
  2523. psp->errorcnt++;
  2524. psp->state = RESYNC_AFTER_DECL_ERROR;
  2525. }
  2526. break;
  2527. case WAITING_FOR_DESTRUCTOR_SYMBOL:
  2528. if( !ISALPHA(x[0]) ){
  2529. ErrorMsg(psp->filename,psp->tokenlineno,
  2530. "Symbol name missing after %%destructor keyword");
  2531. psp->errorcnt++;
  2532. psp->state = RESYNC_AFTER_DECL_ERROR;
  2533. }else{
  2534. struct symbol *sp = Symbol_new(x);
  2535. psp->declargslot = &sp->destructor;
  2536. psp->decllinenoslot = &sp->destLineno;
  2537. psp->insertLineMacro = 1;
  2538. psp->state = WAITING_FOR_DECL_ARG;
  2539. }
  2540. break;
  2541. case WAITING_FOR_DATATYPE_SYMBOL:
  2542. if( !ISALPHA(x[0]) ){
  2543. ErrorMsg(psp->filename,psp->tokenlineno,
  2544. "Symbol name missing after %%type keyword");
  2545. psp->errorcnt++;
  2546. psp->state = RESYNC_AFTER_DECL_ERROR;
  2547. }else{
  2548. struct symbol *sp = Symbol_find(x);
  2549. if((sp) && (sp->datatype)){
  2550. ErrorMsg(psp->filename,psp->tokenlineno,
  2551. "Symbol %%type \"%s\" already defined", x);
  2552. psp->errorcnt++;
  2553. psp->state = RESYNC_AFTER_DECL_ERROR;
  2554. }else{
  2555. if (!sp){
  2556. sp = Symbol_new(x);
  2557. }
  2558. psp->declargslot = &sp->datatype;
  2559. psp->insertLineMacro = 0;
  2560. psp->state = WAITING_FOR_DECL_ARG;
  2561. }
  2562. }
  2563. break;
  2564. case WAITING_FOR_PRECEDENCE_SYMBOL:
  2565. if( x[0]=='.' ){
  2566. psp->state = WAITING_FOR_DECL_OR_RULE;
  2567. }else if( ISUPPER(x[0]) ){
  2568. struct symbol *sp;
  2569. sp = Symbol_new(x);
  2570. if( sp->prec>=0 ){
  2571. ErrorMsg(psp->filename,psp->tokenlineno,
  2572. "Symbol \"%s\" has already be given a precedence.",x);
  2573. psp->errorcnt++;
  2574. }else{
  2575. sp->prec = psp->preccounter;
  2576. sp->assoc = psp->declassoc;
  2577. }
  2578. }else{
  2579. ErrorMsg(psp->filename,psp->tokenlineno,
  2580. "Can't assign a precedence to \"%s\".",x);
  2581. psp->errorcnt++;
  2582. }
  2583. break;
  2584. case WAITING_FOR_DECL_ARG:
  2585. if( x[0]=='{' || x[0]=='\"' || ISALNUM(x[0]) ){
  2586. const char *zOld, *zNew;
  2587. char *zBuf, *z;
  2588. int nOld, n, nLine = 0, nNew, nBack;
  2589. int addLineMacro;
  2590. char zLine[50];
  2591. zNew = x;
  2592. if( zNew[0]=='"' || zNew[0]=='{' ) zNew++;
  2593. nNew = lemonStrlen(zNew);
  2594. if( *psp->declargslot ){
  2595. zOld = *psp->declargslot;
  2596. }else{
  2597. zOld = "";
  2598. }
  2599. nOld = lemonStrlen(zOld);
  2600. n = nOld + nNew + 20;
  2601. addLineMacro = !psp->gp->nolinenosflag
  2602. && psp->insertLineMacro
  2603. && psp->tokenlineno>1
  2604. && (psp->decllinenoslot==0 || psp->decllinenoslot[0]!=0);
  2605. if( addLineMacro ){
  2606. for(z=psp->filename, nBack=0; *z; z++){
  2607. if( *z=='\\' ) nBack++;
  2608. }
  2609. lemon_sprintf(zLine, "#line %d ", psp->tokenlineno);
  2610. nLine = lemonStrlen(zLine);
  2611. n += nLine + lemonStrlen(psp->filename) + nBack;
  2612. }
  2613. *psp->declargslot = (char *) lemon_realloc(*psp->declargslot, n);
  2614. zBuf = *psp->declargslot + nOld;
  2615. if( addLineMacro ){
  2616. if( nOld && zBuf[-1]!='\n' ){
  2617. *(zBuf++) = '\n';
  2618. }
  2619. memcpy(zBuf, zLine, nLine);
  2620. zBuf += nLine;
  2621. *(zBuf++) = '"';
  2622. for(z=psp->filename; *z; z++){
  2623. if( *z=='\\' ){
  2624. *(zBuf++) = '\\';
  2625. }
  2626. *(zBuf++) = *z;
  2627. }
  2628. *(zBuf++) = '"';
  2629. *(zBuf++) = '\n';
  2630. }
  2631. if( psp->decllinenoslot && psp->decllinenoslot[0]==0 ){
  2632. psp->decllinenoslot[0] = psp->tokenlineno;
  2633. }
  2634. memcpy(zBuf, zNew, nNew);
  2635. zBuf += nNew;
  2636. *zBuf = 0;
  2637. psp->state = WAITING_FOR_DECL_OR_RULE;
  2638. }else{
  2639. ErrorMsg(psp->filename,psp->tokenlineno,
  2640. "Illegal argument to %%%s: %s",psp->declkeyword,x);
  2641. psp->errorcnt++;
  2642. psp->state = RESYNC_AFTER_DECL_ERROR;
  2643. }
  2644. break;
  2645. case WAITING_FOR_FALLBACK_ID:
  2646. if( x[0]=='.' ){
  2647. psp->state = WAITING_FOR_DECL_OR_RULE;
  2648. }else if( !ISUPPER(x[0]) ){
  2649. ErrorMsg(psp->filename, psp->tokenlineno,
  2650. "%%fallback argument \"%s\" should be a token", x);
  2651. psp->errorcnt++;
  2652. }else{
  2653. struct symbol *sp = Symbol_new(x);
  2654. if( psp->fallback==0 ){
  2655. psp->fallback = sp;
  2656. }else if( sp->fallback ){
  2657. ErrorMsg(psp->filename, psp->tokenlineno,
  2658. "More than one fallback assigned to token %s", x);
  2659. psp->errorcnt++;
  2660. }else{
  2661. sp->fallback = psp->fallback;
  2662. psp->gp->has_fallback = 1;
  2663. }
  2664. }
  2665. break;
  2666. case WAITING_FOR_TOKEN_NAME:
  2667. /* Tokens do not have to be declared before use. But they can be
  2668. ** in order to control their assigned integer number. The number for
  2669. ** each token is assigned when it is first seen. So by including
  2670. **
  2671. ** %token ONE TWO THREE.
  2672. **
  2673. ** early in the grammar file, that assigns small consecutive values
  2674. ** to each of the tokens ONE TWO and THREE.
  2675. */
  2676. if( x[0]=='.' ){
  2677. psp->state = WAITING_FOR_DECL_OR_RULE;
  2678. }else if( !ISUPPER(x[0]) ){
  2679. ErrorMsg(psp->filename, psp->tokenlineno,
  2680. "%%token argument \"%s\" should be a token", x);
  2681. psp->errorcnt++;
  2682. }else{
  2683. (void)Symbol_new(x);
  2684. }
  2685. break;
  2686. case WAITING_FOR_WILDCARD_ID:
  2687. if( x[0]=='.' ){
  2688. psp->state = WAITING_FOR_DECL_OR_RULE;
  2689. }else if( !ISUPPER(x[0]) ){
  2690. ErrorMsg(psp->filename, psp->tokenlineno,
  2691. "%%wildcard argument \"%s\" should be a token", x);
  2692. psp->errorcnt++;
  2693. }else{
  2694. struct symbol *sp = Symbol_new(x);
  2695. if( psp->gp->wildcard==0 ){
  2696. psp->gp->wildcard = sp;
  2697. }else{
  2698. ErrorMsg(psp->filename, psp->tokenlineno,
  2699. "Extra wildcard to token: %s", x);
  2700. psp->errorcnt++;
  2701. }
  2702. }
  2703. break;
  2704. case WAITING_FOR_CLASS_ID:
  2705. if( !ISLOWER(x[0]) ){
  2706. ErrorMsg(psp->filename, psp->tokenlineno,
  2707. "%%token_class must be followed by an identifier: %s", x);
  2708. psp->errorcnt++;
  2709. psp->state = RESYNC_AFTER_DECL_ERROR;
  2710. }else if( Symbol_find(x) ){
  2711. ErrorMsg(psp->filename, psp->tokenlineno,
  2712. "Symbol \"%s\" already used", x);
  2713. psp->errorcnt++;
  2714. psp->state = RESYNC_AFTER_DECL_ERROR;
  2715. }else{
  2716. psp->tkclass = Symbol_new(x);
  2717. psp->tkclass->type = MULTITERMINAL;
  2718. psp->state = WAITING_FOR_CLASS_TOKEN;
  2719. }
  2720. break;
  2721. case WAITING_FOR_CLASS_TOKEN:
  2722. if( x[0]=='.' ){
  2723. psp->state = WAITING_FOR_DECL_OR_RULE;
  2724. }else if( ISUPPER(x[0]) || ((x[0]=='|' || x[0]=='/') && ISUPPER(x[1])) ){
  2725. struct symbol *msp = psp->tkclass;
  2726. msp->nsubsym++;
  2727. msp->subsym = (struct symbol **) lemon_realloc(msp->subsym,
  2728. sizeof(struct symbol*)*msp->nsubsym);
  2729. if( !ISUPPER(x[0]) ) x++;
  2730. msp->subsym[msp->nsubsym-1] = Symbol_new(x);
  2731. }else{
  2732. ErrorMsg(psp->filename, psp->tokenlineno,
  2733. "%%token_class argument \"%s\" should be a token", x);
  2734. psp->errorcnt++;
  2735. psp->state = RESYNC_AFTER_DECL_ERROR;
  2736. }
  2737. break;
  2738. case RESYNC_AFTER_RULE_ERROR:
  2739. /* if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
  2740. ** break; */
  2741. case RESYNC_AFTER_DECL_ERROR:
  2742. if( x[0]=='.' ) psp->state = WAITING_FOR_DECL_OR_RULE;
  2743. if( x[0]=='%' ) psp->state = WAITING_FOR_DECL_KEYWORD;
  2744. break;
  2745. }
  2746. }
  2747. /* The text in the input is part of the argument to an %ifdef or %ifndef.
  2748. ** Evaluate the text as a boolean expression. Return true or false.
  2749. */
  2750. static int eval_preprocessor_boolean(char *z, int lineno){
  2751. int neg = 0;
  2752. int res = 0;
  2753. int okTerm = 1;
  2754. int i;
  2755. for(i=0; z[i]!=0; i++){
  2756. if( ISSPACE(z[i]) ) continue;
  2757. if( z[i]=='!' ){
  2758. if( !okTerm ) goto pp_syntax_error;
  2759. neg = !neg;
  2760. continue;
  2761. }
  2762. if( z[i]=='|' && z[i+1]=='|' ){
  2763. if( okTerm ) goto pp_syntax_error;
  2764. if( res ) return 1;
  2765. i++;
  2766. okTerm = 1;
  2767. continue;
  2768. }
  2769. if( z[i]=='&' && z[i+1]=='&' ){
  2770. if( okTerm ) goto pp_syntax_error;
  2771. if( !res ) return 0;
  2772. i++;
  2773. okTerm = 1;
  2774. continue;
  2775. }
  2776. if( z[i]=='(' ){
  2777. int k;
  2778. int n = 1;
  2779. if( !okTerm ) goto pp_syntax_error;
  2780. for(k=i+1; z[k]; k++){
  2781. if( z[k]==')' ){
  2782. n--;
  2783. if( n==0 ){
  2784. z[k] = 0;
  2785. res = eval_preprocessor_boolean(&z[i+1], -1);
  2786. z[k] = ')';
  2787. if( res<0 ){
  2788. i = i-res;
  2789. goto pp_syntax_error;
  2790. }
  2791. i = k;
  2792. break;
  2793. }
  2794. }else if( z[k]=='(' ){
  2795. n++;
  2796. }else if( z[k]==0 ){
  2797. i = k;
  2798. goto pp_syntax_error;
  2799. }
  2800. }
  2801. if( neg ){
  2802. res = !res;
  2803. neg = 0;
  2804. }
  2805. okTerm = 0;
  2806. continue;
  2807. }
  2808. if( ISALPHA(z[i]) ){
  2809. int j, k, n;
  2810. if( !okTerm ) goto pp_syntax_error;
  2811. for(k=i+1; ISALNUM(z[k]) || z[k]=='_'; k++){}
  2812. n = k - i;
  2813. res = 0;
  2814. for(j=0; j<nDefine; j++){
  2815. if( strncmp(azDefine[j],&z[i],n)==0 && azDefine[j][n]==0 ){
  2816. if( !bDefineUsed[j] ){
  2817. bDefineUsed[j] = 1;
  2818. nDefineUsed++;
  2819. }
  2820. res = 1;
  2821. break;
  2822. }
  2823. }
  2824. i = k-1;
  2825. if( neg ){
  2826. res = !res;
  2827. neg = 0;
  2828. }
  2829. okTerm = 0;
  2830. continue;
  2831. }
  2832. goto pp_syntax_error;
  2833. }
  2834. return res;
  2835. pp_syntax_error:
  2836. if( lineno>0 ){
  2837. fprintf(stderr, "%%if syntax error on line %d.\n", lineno);
  2838. fprintf(stderr, " %.*s <-- syntax error here\n", i+1, z);
  2839. exit(1);
  2840. }else{
  2841. return -(i+1);
  2842. }
  2843. }
  2844. /* Run the preprocessor over the input file text. The global variables
  2845. ** azDefine[0] through azDefine[nDefine-1] contains the names of all defined
  2846. ** macros. This routine looks for "%ifdef" and "%ifndef" and "%endif" and
  2847. ** comments them out. Text in between is also commented out as appropriate.
  2848. */
  2849. static void preprocess_input(char *z){
  2850. int i, j, k;
  2851. int exclude = 0;
  2852. int start = 0;
  2853. int lineno = 1;
  2854. int start_lineno = 1;
  2855. for(i=0; z[i]; i++){
  2856. if( z[i]=='\n' ) lineno++;
  2857. if( z[i]!='%' || (i>0 && z[i-1]!='\n') ) continue;
  2858. if( strncmp(&z[i],"%endif",6)==0 && ISSPACE(z[i+6]) ){
  2859. if( exclude ){
  2860. exclude--;
  2861. if( exclude==0 ){
  2862. for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' ';
  2863. }
  2864. }
  2865. for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
  2866. }else if( strncmp(&z[i],"%else",5)==0 && ISSPACE(z[i+5]) ){
  2867. if( exclude==1){
  2868. exclude = 0;
  2869. for(j=start; j<i; j++) if( z[j]!='\n' ) z[j] = ' ';
  2870. }else if( exclude==0 ){
  2871. exclude = 1;
  2872. start = i;
  2873. start_lineno = lineno;
  2874. }
  2875. for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
  2876. }else if( strncmp(&z[i],"%ifdef ",7)==0
  2877. || strncmp(&z[i],"%if ",4)==0
  2878. || strncmp(&z[i],"%ifndef ",8)==0 ){
  2879. if( exclude ){
  2880. exclude++;
  2881. }else{
  2882. int isNot;
  2883. int iBool;
  2884. for(j=i; z[j] && !ISSPACE(z[j]); j++){}
  2885. iBool = j;
  2886. isNot = (j==i+7);
  2887. while( z[j] && z[j]!='\n' ){ j++; }
  2888. k = z[j];
  2889. z[j] = 0;
  2890. exclude = eval_preprocessor_boolean(&z[iBool], lineno);
  2891. z[j] = k;
  2892. if( !isNot ) exclude = !exclude;
  2893. if( exclude ){
  2894. start = i;
  2895. start_lineno = lineno;
  2896. }
  2897. }
  2898. for(j=i; z[j] && z[j]!='\n'; j++) z[j] = ' ';
  2899. }
  2900. }
  2901. if( exclude ){
  2902. fprintf(stderr,"unterminated %%ifdef starting on line %d\n", start_lineno);
  2903. exit(1);
  2904. }
  2905. }
  2906. /* In spite of its name, this function is really a scanner. It read
  2907. ** in the entire input file (all at once) then tokenizes it. Each
  2908. ** token is passed to the function "parseonetoken" which builds all
  2909. ** the appropriate data structures in the global state vector "gp".
  2910. */
  2911. void Parse(struct lemon *gp)
  2912. {
  2913. struct pstate ps;
  2914. FILE *fp;
  2915. char *filebuf;
  2916. unsigned int filesize;
  2917. int lineno;
  2918. int c;
  2919. char *cp, *nextcp;
  2920. int startline = 0;
  2921. memset(&ps, '\0', sizeof(ps));
  2922. ps.gp = gp;
  2923. ps.filename = gp->filename;
  2924. ps.errorcnt = 0;
  2925. ps.state = INITIALIZE;
  2926. /* Begin by reading the input file */
  2927. fp = fopen(ps.filename,"rb");
  2928. if( fp==0 ){
  2929. ErrorMsg(ps.filename,0,"Can't open this file for reading.");
  2930. gp->errorcnt++;
  2931. return;
  2932. }
  2933. fseek(fp,0,2);
  2934. filesize = ftell(fp);
  2935. rewind(fp);
  2936. filebuf = (char *)lemon_malloc( filesize+1 );
  2937. if( filesize>100000000 || filebuf==0 ){
  2938. ErrorMsg(ps.filename,0,"Input file too large.");
  2939. lemon_free(filebuf);
  2940. gp->errorcnt++;
  2941. fclose(fp);
  2942. return;
  2943. }
  2944. if( fread(filebuf,1,filesize,fp)!=filesize ){
  2945. ErrorMsg(ps.filename,0,"Can't read in all %d bytes of this file.",
  2946. filesize);
  2947. lemon_free(filebuf);
  2948. gp->errorcnt++;
  2949. fclose(fp);
  2950. return;
  2951. }
  2952. fclose(fp);
  2953. filebuf[filesize] = 0;
  2954. /* Make an initial pass through the file to handle %ifdef and %ifndef */
  2955. preprocess_input(filebuf);
  2956. if( gp->printPreprocessed ){
  2957. printf("%s\n", filebuf);
  2958. return;
  2959. }
  2960. /* Now scan the text of the input file */
  2961. lineno = 1;
  2962. for(cp=filebuf; (c= *cp)!=0; ){
  2963. if( c=='\n' ) lineno++; /* Keep track of the line number */
  2964. if( ISSPACE(c) ){ cp++; continue; } /* Skip all white space */
  2965. if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments */
  2966. cp+=2;
  2967. while( (c= *cp)!=0 && c!='\n' ) cp++;
  2968. continue;
  2969. }
  2970. if( c=='/' && cp[1]=='*' ){ /* Skip C style comments */
  2971. cp+=2;
  2972. if( (*cp)=='/' ) cp++;
  2973. while( (c= *cp)!=0 && (c!='/' || cp[-1]!='*') ){
  2974. if( c=='\n' ) lineno++;
  2975. cp++;
  2976. }
  2977. if( c ) cp++;
  2978. continue;
  2979. }
  2980. ps.tokenstart = cp; /* Mark the beginning of the token */
  2981. ps.tokenlineno = lineno; /* Linenumber on which token begins */
  2982. if( c=='\"' ){ /* String literals */
  2983. cp++;
  2984. while( (c= *cp)!=0 && c!='\"' ){
  2985. if( c=='\n' ) lineno++;
  2986. cp++;
  2987. }
  2988. if( c==0 ){
  2989. ErrorMsg(ps.filename,startline,
  2990. "String starting on this line is not terminated before "
  2991. "the end of the file.");
  2992. ps.errorcnt++;
  2993. nextcp = cp;
  2994. }else{
  2995. nextcp = cp+1;
  2996. }
  2997. }else if( c=='{' ){ /* A block of C code */
  2998. int level;
  2999. cp++;
  3000. for(level=1; (c= *cp)!=0 && (level>1 || c!='}'); cp++){
  3001. if( c=='\n' ) lineno++;
  3002. else if( c=='{' ) level++;
  3003. else if( c=='}' ) level--;
  3004. else if( c=='/' && cp[1]=='*' ){ /* Skip comments */
  3005. int prevc;
  3006. cp = &cp[2];
  3007. prevc = 0;
  3008. while( (c= *cp)!=0 && (c!='/' || prevc!='*') ){
  3009. if( c=='\n' ) lineno++;
  3010. prevc = c;
  3011. cp++;
  3012. }
  3013. }else if( c=='/' && cp[1]=='/' ){ /* Skip C++ style comments too */
  3014. cp = &cp[2];
  3015. while( (c= *cp)!=0 && c!='\n' ) cp++;
  3016. if( c ) lineno++;
  3017. }else if( c=='\'' || c=='\"' ){ /* String a character literals */
  3018. int startchar, prevc;
  3019. startchar = c;
  3020. prevc = 0;
  3021. for(cp++; (c= *cp)!=0 && (c!=startchar || prevc=='\\'); cp++){
  3022. if( c=='\n' ) lineno++;
  3023. if( prevc=='\\' ) prevc = 0;
  3024. else prevc = c;
  3025. }
  3026. }
  3027. }
  3028. if( c==0 ){
  3029. ErrorMsg(ps.filename,ps.tokenlineno,
  3030. "C code starting on this line is not terminated before "
  3031. "the end of the file.");
  3032. ps.errorcnt++;
  3033. nextcp = cp;
  3034. }else{
  3035. nextcp = cp+1;
  3036. }
  3037. }else if( ISALNUM(c) ){ /* Identifiers */
  3038. while( (c= *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
  3039. nextcp = cp;
  3040. }else if( c==':' && cp[1]==':' && cp[2]=='=' ){ /* The operator "::=" */
  3041. cp += 3;
  3042. nextcp = cp;
  3043. }else if( (c=='/' || c=='|') && ISALPHA(cp[1]) ){
  3044. cp += 2;
  3045. while( (c = *cp)!=0 && (ISALNUM(c) || c=='_') ) cp++;
  3046. nextcp = cp;
  3047. }else{ /* All other (one character) operators */
  3048. cp++;
  3049. nextcp = cp;
  3050. }
  3051. c = *cp;
  3052. *cp = 0; /* Null terminate the token */
  3053. parseonetoken(&ps); /* Parse the token */
  3054. *cp = (char)c; /* Restore the buffer */
  3055. cp = nextcp;
  3056. }
  3057. lemon_free(filebuf); /* Release the buffer after parsing */
  3058. gp->rule = ps.firstrule;
  3059. gp->errorcnt = ps.errorcnt;
  3060. }
  3061. /*************************** From the file "plink.c" *********************/
  3062. /*
  3063. ** Routines processing configuration follow-set propagation links
  3064. ** in the LEMON parser generator.
  3065. */
  3066. static struct plink *plink_freelist = 0;
  3067. /* Allocate a new plink */
  3068. struct plink *Plink_new(void){
  3069. struct plink *newlink;
  3070. if( plink_freelist==0 ){
  3071. int i;
  3072. int amt = 100;
  3073. plink_freelist = (struct plink *)lemon_calloc( amt, sizeof(struct plink) );
  3074. if( plink_freelist==0 ){
  3075. fprintf(stderr,
  3076. "Unable to allocate memory for a new follow-set propagation link.\n");
  3077. exit(1);
  3078. }
  3079. for(i=0; i<amt-1; i++) plink_freelist[i].next = &plink_freelist[i+1];
  3080. plink_freelist[amt-1].next = 0;
  3081. }
  3082. newlink = plink_freelist;
  3083. plink_freelist = plink_freelist->next;
  3084. return newlink;
  3085. }
  3086. /* Add a plink to a plink list */
  3087. void Plink_add(struct plink **plpp, struct config *cfp)
  3088. {
  3089. struct plink *newlink;
  3090. newlink = Plink_new();
  3091. newlink->next = *plpp;
  3092. *plpp = newlink;
  3093. newlink->cfp = cfp;
  3094. }
  3095. /* Transfer every plink on the list "from" to the list "to" */
  3096. void Plink_copy(struct plink **to, struct plink *from)
  3097. {
  3098. struct plink *nextpl;
  3099. while( from ){
  3100. nextpl = from->next;
  3101. from->next = *to;
  3102. *to = from;
  3103. from = nextpl;
  3104. }
  3105. }
  3106. /* Delete every plink on the list */
  3107. void Plink_delete(struct plink *plp)
  3108. {
  3109. struct plink *nextpl;
  3110. while( plp ){
  3111. nextpl = plp->next;
  3112. plp->next = plink_freelist;
  3113. plink_freelist = plp;
  3114. plp = nextpl;
  3115. }
  3116. }
  3117. /*********************** From the file "report.c" **************************/
  3118. /*
  3119. ** Procedures for generating reports and tables in the LEMON parser generator.
  3120. */
  3121. /* Generate a filename with the given suffix.
  3122. */
  3123. PRIVATE char *file_makename(struct lemon *lemp, const char *suffix)
  3124. {
  3125. char *name;
  3126. char *cp;
  3127. char *filename = lemp->filename;
  3128. int sz;
  3129. if( outputDir ){
  3130. cp = strrchr(filename, '/');
  3131. if( cp ) filename = cp + 1;
  3132. }
  3133. sz = lemonStrlen(filename);
  3134. sz += lemonStrlen(suffix);
  3135. if( outputDir ) sz += lemonStrlen(outputDir) + 1;
  3136. sz += 5;
  3137. name = (char*)lemon_malloc( sz );
  3138. if( name==0 ){
  3139. fprintf(stderr,"Can't allocate space for a filename.\n");
  3140. exit(1);
  3141. }
  3142. name[0] = 0;
  3143. if( outputDir ){
  3144. lemon_strcpy(name, outputDir);
  3145. lemon_strcat(name, "/");
  3146. }
  3147. lemon_strcat(name,filename);
  3148. cp = strrchr(name,'.');
  3149. if( cp ) *cp = 0;
  3150. lemon_strcat(name,suffix);
  3151. return name;
  3152. }
  3153. /* Open a file with a name based on the name of the input file,
  3154. ** but with a different (specified) suffix, and return a pointer
  3155. ** to the stream */
  3156. PRIVATE FILE *file_open(
  3157. struct lemon *lemp,
  3158. const char *suffix,
  3159. const char *mode
  3160. ){
  3161. FILE *fp;
  3162. if( lemp->outname ) lemon_free(lemp->outname);
  3163. lemp->outname = file_makename(lemp, suffix);
  3164. fp = fopen(lemp->outname,mode);
  3165. if( fp==0 && *mode=='w' ){
  3166. fprintf(stderr,"Can't open file \"%s\".\n",lemp->outname);
  3167. lemp->errorcnt++;
  3168. return 0;
  3169. }
  3170. return fp;
  3171. }
  3172. /* Print the text of a rule
  3173. */
  3174. void rule_print(FILE *out, struct rule *rp){
  3175. int i, j;
  3176. fprintf(out, "%s",rp->lhs->name);
  3177. /* if( rp->lhsalias ) fprintf(out,"(%s)",rp->lhsalias); */
  3178. fprintf(out," ::=");
  3179. for(i=0; i<rp->nrhs; i++){
  3180. struct symbol *sp = rp->rhs[i];
  3181. if( sp->type==MULTITERMINAL ){
  3182. fprintf(out," %s", sp->subsym[0]->name);
  3183. for(j=1; j<sp->nsubsym; j++){
  3184. fprintf(out,"|%s", sp->subsym[j]->name);
  3185. }
  3186. }else{
  3187. fprintf(out," %s", sp->name);
  3188. }
  3189. /* if( rp->rhsalias[i] ) fprintf(out,"(%s)",rp->rhsalias[i]); */
  3190. }
  3191. }
  3192. /* Duplicate the input file without comments and without actions
  3193. ** on rules */
  3194. void Reprint(struct lemon *lemp)
  3195. {
  3196. struct rule *rp;
  3197. struct symbol *sp;
  3198. int i, j, maxlen, len, ncolumns, skip;
  3199. printf("// Reprint of input file \"%s\".\n// Symbols:\n",lemp->filename);
  3200. maxlen = 10;
  3201. for(i=0; i<lemp->nsymbol; i++){
  3202. sp = lemp->symbols[i];
  3203. len = lemonStrlen(sp->name);
  3204. if( len>maxlen ) maxlen = len;
  3205. }
  3206. ncolumns = 76/(maxlen+5);
  3207. if( ncolumns<1 ) ncolumns = 1;
  3208. skip = (lemp->nsymbol + ncolumns - 1)/ncolumns;
  3209. for(i=0; i<skip; i++){
  3210. printf("//");
  3211. for(j=i; j<lemp->nsymbol; j+=skip){
  3212. sp = lemp->symbols[j];
  3213. assert( sp->index==j );
  3214. printf(" %3d %-*.*s",j,maxlen,maxlen,sp->name);
  3215. }
  3216. printf("\n");
  3217. }
  3218. for(rp=lemp->rule; rp; rp=rp->next){
  3219. rule_print(stdout, rp);
  3220. printf(".");
  3221. if( rp->precsym ) printf(" [%s]",rp->precsym->name);
  3222. /* if( rp->code ) printf("\n %s",rp->code); */
  3223. printf("\n");
  3224. }
  3225. }
  3226. /* Print a single rule.
  3227. */
  3228. void RulePrint(FILE *fp, struct rule *rp, int iCursor){
  3229. struct symbol *sp;
  3230. int i, j;
  3231. fprintf(fp,"%s ::=",rp->lhs->name);
  3232. for(i=0; i<=rp->nrhs; i++){
  3233. if( i==iCursor ) fprintf(fp," *");
  3234. if( i==rp->nrhs ) break;
  3235. sp = rp->rhs[i];
  3236. if( sp->type==MULTITERMINAL ){
  3237. fprintf(fp," %s", sp->subsym[0]->name);
  3238. for(j=1; j<sp->nsubsym; j++){
  3239. fprintf(fp,"|%s",sp->subsym[j]->name);
  3240. }
  3241. }else{
  3242. fprintf(fp," %s", sp->name);
  3243. }
  3244. }
  3245. }
  3246. /* Print the rule for a configuration.
  3247. */
  3248. void ConfigPrint(FILE *fp, struct config *cfp){
  3249. RulePrint(fp, cfp->rp, cfp->dot);
  3250. }
  3251. /* #define TEST */
  3252. #if 0
  3253. /* Print a set */
  3254. PRIVATE void SetPrint(out,set,lemp)
  3255. FILE *out;
  3256. char *set;
  3257. struct lemon *lemp;
  3258. {
  3259. int i;
  3260. char *spacer;
  3261. spacer = "";
  3262. fprintf(out,"%12s[","");
  3263. for(i=0; i<lemp->nterminal; i++){
  3264. if( SetFind(set,i) ){
  3265. fprintf(out,"%s%s",spacer,lemp->symbols[i]->name);
  3266. spacer = " ";
  3267. }
  3268. }
  3269. fprintf(out,"]\n");
  3270. }
  3271. /* Print a plink chain */
  3272. PRIVATE void PlinkPrint(out,plp,tag)
  3273. FILE *out;
  3274. struct plink *plp;
  3275. char *tag;
  3276. {
  3277. while( plp ){
  3278. fprintf(out,"%12s%s (state %2d) ","",tag,plp->cfp->stp->statenum);
  3279. ConfigPrint(out,plp->cfp);
  3280. fprintf(out,"\n");
  3281. plp = plp->next;
  3282. }
  3283. }
  3284. #endif
  3285. /* Print an action to the given file descriptor. Return FALSE if
  3286. ** nothing was actually printed.
  3287. */
  3288. int PrintAction(
  3289. struct action *ap, /* The action to print */
  3290. FILE *fp, /* Print the action here */
  3291. int indent /* Indent by this amount */
  3292. ){
  3293. int result = 1;
  3294. switch( ap->type ){
  3295. case SHIFT: {
  3296. struct state *stp = ap->x.stp;
  3297. fprintf(fp,"%*s shift %-7d",indent,ap->sp->name,stp->statenum);
  3298. break;
  3299. }
  3300. case REDUCE: {
  3301. struct rule *rp = ap->x.rp;
  3302. fprintf(fp,"%*s reduce %-7d",indent,ap->sp->name,rp->iRule);
  3303. RulePrint(fp, rp, -1);
  3304. break;
  3305. }
  3306. case SHIFTREDUCE: {
  3307. struct rule *rp = ap->x.rp;
  3308. fprintf(fp,"%*s shift-reduce %-7d",indent,ap->sp->name,rp->iRule);
  3309. RulePrint(fp, rp, -1);
  3310. break;
  3311. }
  3312. case ACCEPT:
  3313. fprintf(fp,"%*s accept",indent,ap->sp->name);
  3314. break;
  3315. case ERROR:
  3316. fprintf(fp,"%*s error",indent,ap->sp->name);
  3317. break;
  3318. case SRCONFLICT:
  3319. case RRCONFLICT:
  3320. fprintf(fp,"%*s reduce %-7d ** Parsing conflict **",
  3321. indent,ap->sp->name,ap->x.rp->iRule);
  3322. break;
  3323. case SSCONFLICT:
  3324. fprintf(fp,"%*s shift %-7d ** Parsing conflict **",
  3325. indent,ap->sp->name,ap->x.stp->statenum);
  3326. break;
  3327. case SH_RESOLVED:
  3328. if( showPrecedenceConflict ){
  3329. fprintf(fp,"%*s shift %-7d -- dropped by precedence",
  3330. indent,ap->sp->name,ap->x.stp->statenum);
  3331. }else{
  3332. result = 0;
  3333. }
  3334. break;
  3335. case RD_RESOLVED:
  3336. if( showPrecedenceConflict ){
  3337. fprintf(fp,"%*s reduce %-7d -- dropped by precedence",
  3338. indent,ap->sp->name,ap->x.rp->iRule);
  3339. }else{
  3340. result = 0;
  3341. }
  3342. break;
  3343. case NOT_USED:
  3344. result = 0;
  3345. break;
  3346. }
  3347. if( result && ap->spOpt ){
  3348. fprintf(fp," /* because %s==%s */", ap->sp->name, ap->spOpt->name);
  3349. }
  3350. return result;
  3351. }
  3352. /* Generate the "*.out" log file */
  3353. void ReportOutput(struct lemon *lemp)
  3354. {
  3355. int i, n;
  3356. struct state *stp;
  3357. struct config *cfp;
  3358. struct action *ap;
  3359. struct rule *rp;
  3360. FILE *fp;
  3361. fp = file_open(lemp,".out","wb");
  3362. if( fp==0 ) return;
  3363. for(i=0; i<lemp->nxstate; i++){
  3364. stp = lemp->sorted[i];
  3365. fprintf(fp,"State %d:\n",stp->statenum);
  3366. if( lemp->basisflag ) cfp=stp->bp;
  3367. else cfp=stp->cfp;
  3368. while( cfp ){
  3369. char buf[20];
  3370. if( cfp->dot==cfp->rp->nrhs ){
  3371. lemon_sprintf(buf,"(%d)",cfp->rp->iRule);
  3372. fprintf(fp," %5s ",buf);
  3373. }else{
  3374. fprintf(fp," ");
  3375. }
  3376. ConfigPrint(fp,cfp);
  3377. fprintf(fp,"\n");
  3378. #if 0
  3379. SetPrint(fp,cfp->fws,lemp);
  3380. PlinkPrint(fp,cfp->fplp,"To ");
  3381. PlinkPrint(fp,cfp->bplp,"From");
  3382. #endif
  3383. if( lemp->basisflag ) cfp=cfp->bp;
  3384. else cfp=cfp->next;
  3385. }
  3386. fprintf(fp,"\n");
  3387. for(ap=stp->ap; ap; ap=ap->next){
  3388. if( PrintAction(ap,fp,30) ) fprintf(fp,"\n");
  3389. }
  3390. fprintf(fp,"\n");
  3391. }
  3392. fprintf(fp, "----------------------------------------------------\n");
  3393. fprintf(fp, "Symbols:\n");
  3394. fprintf(fp, "The first-set of non-terminals is shown after the name.\n\n");
  3395. for(i=0; i<lemp->nsymbol; i++){
  3396. int j;
  3397. struct symbol *sp;
  3398. sp = lemp->symbols[i];
  3399. fprintf(fp, " %3d: %s", i, sp->name);
  3400. if( sp->type==NONTERMINAL ){
  3401. fprintf(fp, ":");
  3402. if( sp->lambda ){
  3403. fprintf(fp, " <lambda>");
  3404. }
  3405. for(j=0; j<lemp->nterminal; j++){
  3406. if( sp->firstset && SetFind(sp->firstset, j) ){
  3407. fprintf(fp, " %s", lemp->symbols[j]->name);
  3408. }
  3409. }
  3410. }
  3411. if( sp->prec>=0 ) fprintf(fp," (precedence=%d)", sp->prec);
  3412. fprintf(fp, "\n");
  3413. }
  3414. fprintf(fp, "----------------------------------------------------\n");
  3415. fprintf(fp, "Syntax-only Symbols:\n");
  3416. fprintf(fp, "The following symbols never carry semantic content.\n\n");
  3417. for(i=n=0; i<lemp->nsymbol; i++){
  3418. int w;
  3419. struct symbol *sp = lemp->symbols[i];
  3420. if( sp->bContent ) continue;
  3421. w = (int)strlen(sp->name);
  3422. if( n>0 && n+w>75 ){
  3423. fprintf(fp,"\n");
  3424. n = 0;
  3425. }
  3426. if( n>0 ){
  3427. fprintf(fp, " ");
  3428. n++;
  3429. }
  3430. fprintf(fp, "%s", sp->name);
  3431. n += w;
  3432. }
  3433. if( n>0 ) fprintf(fp, "\n");
  3434. fprintf(fp, "----------------------------------------------------\n");
  3435. fprintf(fp, "Rules:\n");
  3436. for(rp=lemp->rule; rp; rp=rp->next){
  3437. fprintf(fp, "%4d: ", rp->iRule);
  3438. rule_print(fp, rp);
  3439. fprintf(fp,".");
  3440. if( rp->precsym ){
  3441. fprintf(fp," [%s precedence=%d]",
  3442. rp->precsym->name, rp->precsym->prec);
  3443. }
  3444. fprintf(fp,"\n");
  3445. }
  3446. fclose(fp);
  3447. return;
  3448. }
  3449. /* Search for the file "name" which is in the same directory as
  3450. ** the executable */
  3451. PRIVATE char *pathsearch(char *argv0, char *name, int modemask)
  3452. {
  3453. const char *pathlist;
  3454. char *pathbufptr = 0;
  3455. char *pathbuf = 0;
  3456. char *path,*cp;
  3457. char c;
  3458. #ifdef __WIN32__
  3459. cp = strrchr(argv0,'\\');
  3460. #else
  3461. cp = strrchr(argv0,'/');
  3462. #endif
  3463. if( cp ){
  3464. c = *cp;
  3465. *cp = 0;
  3466. path = (char *)lemon_malloc( lemonStrlen(argv0) + lemonStrlen(name) + 2 );
  3467. if( path ) lemon_sprintf(path,"%s/%s",argv0,name);
  3468. *cp = c;
  3469. }else{
  3470. pathlist = getenv("PATH");
  3471. if( pathlist==0 ) pathlist = ".:/bin:/usr/bin";
  3472. pathbuf = (char *) lemon_malloc( lemonStrlen(pathlist) + 1 );
  3473. path = (char *)lemon_malloc( lemonStrlen(pathlist)+lemonStrlen(name)+2 );
  3474. if( (pathbuf != 0) && (path!=0) ){
  3475. pathbufptr = pathbuf;
  3476. lemon_strcpy(pathbuf, pathlist);
  3477. while( *pathbuf ){
  3478. cp = strchr(pathbuf,':');
  3479. if( cp==0 ) cp = &pathbuf[lemonStrlen(pathbuf)];
  3480. c = *cp;
  3481. *cp = 0;
  3482. lemon_sprintf(path,"%s/%s",pathbuf,name);
  3483. *cp = c;
  3484. if( c==0 ) pathbuf[0] = 0;
  3485. else pathbuf = &cp[1];
  3486. if( access(path,modemask)==0 ) break;
  3487. }
  3488. }
  3489. lemon_free(pathbufptr);
  3490. }
  3491. return path;
  3492. }
  3493. /* Given an action, compute the integer value for that action
  3494. ** which is to be put in the action table of the generated machine.
  3495. ** Return negative if no action should be generated.
  3496. */
  3497. PRIVATE int compute_action(struct lemon *lemp, struct action *ap)
  3498. {
  3499. int act;
  3500. switch( ap->type ){
  3501. case SHIFT: act = ap->x.stp->statenum; break;
  3502. case SHIFTREDUCE: {
  3503. /* Since a SHIFT is inherient after a prior REDUCE, convert any
  3504. ** SHIFTREDUCE action with a nonterminal on the LHS into a simple
  3505. ** REDUCE action: */
  3506. if( ap->sp->index>=lemp->nterminal
  3507. && (lemp->errsym==0 || ap->sp->index!=lemp->errsym->index)
  3508. ){
  3509. act = lemp->minReduce + ap->x.rp->iRule;
  3510. }else{
  3511. act = lemp->minShiftReduce + ap->x.rp->iRule;
  3512. }
  3513. break;
  3514. }
  3515. case REDUCE: act = lemp->minReduce + ap->x.rp->iRule; break;
  3516. case ERROR: act = lemp->errAction; break;
  3517. case ACCEPT: act = lemp->accAction; break;
  3518. default: act = -1; break;
  3519. }
  3520. return act;
  3521. }
  3522. #define LINESIZE 1000
  3523. /* The next cluster of routines are for reading the template file
  3524. ** and writing the results to the generated parser */
  3525. /* The first function transfers data from "in" to "out" until
  3526. ** a line is seen which begins with "%%". The line number is
  3527. ** tracked.
  3528. **
  3529. ** if name!=0, then any word that begin with "Parse" is changed to
  3530. ** begin with *name instead.
  3531. */
  3532. PRIVATE void tplt_xfer(char *name, FILE *in, FILE *out, int *lineno)
  3533. {
  3534. int i, iStart;
  3535. char line[LINESIZE];
  3536. while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
  3537. (*lineno)++;
  3538. iStart = 0;
  3539. if( name ){
  3540. for(i=0; line[i]; i++){
  3541. if( line[i]=='P' && strncmp(&line[i],"Parse",5)==0
  3542. && (i==0 || !ISALPHA(line[i-1]))
  3543. ){
  3544. if( i>iStart ) fprintf(out,"%.*s",i-iStart,&line[iStart]);
  3545. fprintf(out,"%s",name);
  3546. i += 4;
  3547. iStart = i+1;
  3548. }
  3549. }
  3550. }
  3551. fprintf(out,"%s",&line[iStart]);
  3552. }
  3553. }
  3554. /* Skip forward past the header of the template file to the first "%%"
  3555. */
  3556. PRIVATE void tplt_skip_header(FILE *in, int *lineno)
  3557. {
  3558. char line[LINESIZE];
  3559. while( fgets(line,LINESIZE,in) && (line[0]!='%' || line[1]!='%') ){
  3560. (*lineno)++;
  3561. }
  3562. }
  3563. /* The next function finds the template file and opens it, returning
  3564. ** a pointer to the opened file. */
  3565. PRIVATE FILE *tplt_open(struct lemon *lemp)
  3566. {
  3567. static char templatename[] = "lempar.c";
  3568. char buf[1000];
  3569. FILE *in;
  3570. char *tpltname;
  3571. char *toFree = 0;
  3572. char *cp;
  3573. /* first, see if user specified a template filename on the command line. */
  3574. if (user_templatename != 0) {
  3575. if( access(user_templatename,004)==-1 ){
  3576. fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
  3577. user_templatename);
  3578. lemp->errorcnt++;
  3579. return 0;
  3580. }
  3581. in = fopen(user_templatename,"rb");
  3582. if( in==0 ){
  3583. fprintf(stderr,"Can't open the template file \"%s\".\n",
  3584. user_templatename);
  3585. lemp->errorcnt++;
  3586. return 0;
  3587. }
  3588. return in;
  3589. }
  3590. cp = strrchr(lemp->filename,'.');
  3591. if( cp ){
  3592. lemon_sprintf(buf,"%.*s.lt",(int)(cp-lemp->filename),lemp->filename);
  3593. }else{
  3594. lemon_sprintf(buf,"%s.lt",lemp->filename);
  3595. }
  3596. if( access(buf,004)==0 ){
  3597. tpltname = buf;
  3598. }else if( access(templatename,004)==0 ){
  3599. tpltname = templatename;
  3600. }else{
  3601. toFree = tpltname = pathsearch(lemp->argv[0],templatename,0);
  3602. }
  3603. if( tpltname==0 ){
  3604. fprintf(stderr,"Can't find the parser driver template file \"%s\".\n",
  3605. templatename);
  3606. lemp->errorcnt++;
  3607. return 0;
  3608. }
  3609. in = fopen(tpltname,"rb");
  3610. if( in==0 ){
  3611. fprintf(stderr,"Can't open the template file \"%s\".\n",tpltname);
  3612. lemp->errorcnt++;
  3613. }
  3614. lemon_free(toFree);
  3615. return in;
  3616. }
  3617. /* Print a #line directive line to the output file. */
  3618. PRIVATE void tplt_linedir(FILE *out, int lineno, char *filename)
  3619. {
  3620. fprintf(out,"#line %d \"",lineno);
  3621. while( *filename ){
  3622. if( *filename == '\\' ) putc('\\',out);
  3623. putc(*filename,out);
  3624. filename++;
  3625. }
  3626. fprintf(out,"\"\n");
  3627. }
  3628. /* Print a string to the file and keep the linenumber up to date */
  3629. PRIVATE void tplt_print(FILE *out, struct lemon *lemp, char *str, int *lineno)
  3630. {
  3631. if( str==0 ) return;
  3632. while( *str ){
  3633. putc(*str,out);
  3634. if( *str=='\n' ) (*lineno)++;
  3635. str++;
  3636. }
  3637. if( str[-1]!='\n' ){
  3638. putc('\n',out);
  3639. (*lineno)++;
  3640. }
  3641. if (!lemp->nolinenosflag) {
  3642. (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
  3643. }
  3644. return;
  3645. }
  3646. /*
  3647. ** The following routine emits code for the destructor for the
  3648. ** symbol sp
  3649. */
  3650. void emit_destructor_code(
  3651. FILE *out,
  3652. struct symbol *sp,
  3653. struct lemon *lemp,
  3654. int *lineno
  3655. ){
  3656. char *cp = 0;
  3657. if( sp->type==TERMINAL ){
  3658. cp = lemp->tokendest;
  3659. if( cp==0 ) return;
  3660. fprintf(out,"{\n"); (*lineno)++;
  3661. }else if( sp->destructor ){
  3662. cp = sp->destructor;
  3663. fprintf(out,"{\n"); (*lineno)++;
  3664. if( !lemp->nolinenosflag ){
  3665. (*lineno)++;
  3666. tplt_linedir(out,sp->destLineno,lemp->filename);
  3667. }
  3668. }else if( lemp->vardest ){
  3669. cp = lemp->vardest;
  3670. if( cp==0 ) return;
  3671. fprintf(out,"{\n"); (*lineno)++;
  3672. }else{
  3673. assert( 0 ); /* Cannot happen */
  3674. }
  3675. for(; *cp; cp++){
  3676. if( *cp=='$' && cp[1]=='$' ){
  3677. fprintf(out,"(yypminor->yy%d)",sp->dtnum);
  3678. cp++;
  3679. continue;
  3680. }
  3681. if( *cp=='\n' ) (*lineno)++;
  3682. fputc(*cp,out);
  3683. }
  3684. fprintf(out,"\n"); (*lineno)++;
  3685. if (!lemp->nolinenosflag) {
  3686. (*lineno)++; tplt_linedir(out,*lineno,lemp->outname);
  3687. }
  3688. fprintf(out,"}\n"); (*lineno)++;
  3689. return;
  3690. }
  3691. /*
  3692. ** Return TRUE (non-zero) if the given symbol has a destructor.
  3693. */
  3694. int has_destructor(struct symbol *sp, struct lemon *lemp)
  3695. {
  3696. int ret;
  3697. if( sp->type==TERMINAL ){
  3698. ret = lemp->tokendest!=0;
  3699. }else{
  3700. ret = lemp->vardest!=0 || sp->destructor!=0;
  3701. }
  3702. return ret;
  3703. }
  3704. /*
  3705. ** Append text to a dynamically allocated string. If zText is 0 then
  3706. ** reset the string to be empty again. Always return the complete text
  3707. ** of the string (which is overwritten with each call).
  3708. **
  3709. ** n bytes of zText are stored. If n==0 then all of zText up to the first
  3710. ** \000 terminator is stored. zText can contain up to two instances of
  3711. ** %d. The values of p1 and p2 are written into the first and second
  3712. ** %d.
  3713. **
  3714. ** If n==-1, then the previous character is overwritten.
  3715. */
  3716. PRIVATE char *append_str(const char *zText, int n, int p1, int p2){
  3717. static char empty[1] = { 0 };
  3718. static char *z = 0;
  3719. static int alloced = 0;
  3720. static int used = 0;
  3721. int c;
  3722. char zInt[40];
  3723. if( zText==0 ){
  3724. if( used==0 && z!=0 ) z[0] = 0;
  3725. used = 0;
  3726. return z;
  3727. }
  3728. if( n<=0 ){
  3729. if( n<0 ){
  3730. used += n;
  3731. assert( used>=0 );
  3732. }
  3733. n = lemonStrlen(zText);
  3734. }
  3735. if( (int) (n+sizeof(zInt)*2+used) >= alloced ){
  3736. alloced = n + sizeof(zInt)*2 + used + 200;
  3737. z = (char *) lemon_realloc(z, alloced);
  3738. }
  3739. if( z==0 ) return empty;
  3740. while( n-- > 0 ){
  3741. c = *(zText++);
  3742. if( c=='%' && n>0 && zText[0]=='d' ){
  3743. lemon_sprintf(zInt, "%d", p1);
  3744. p1 = p2;
  3745. lemon_strcpy(&z[used], zInt);
  3746. used += lemonStrlen(&z[used]);
  3747. zText++;
  3748. n--;
  3749. }else{
  3750. z[used++] = (char)c;
  3751. }
  3752. }
  3753. z[used] = 0;
  3754. return z;
  3755. }
  3756. /*
  3757. ** Write and transform the rp->code string so that symbols are expanded.
  3758. ** Populate the rp->codePrefix and rp->codeSuffix strings, as appropriate.
  3759. **
  3760. ** Return 1 if the expanded code requires that "yylhsminor" local variable
  3761. ** to be defined.
  3762. */
  3763. PRIVATE int translate_code(struct lemon *lemp, struct rule *rp){
  3764. char *cp, *xp;
  3765. int i;
  3766. int rc = 0; /* True if yylhsminor is used */
  3767. int dontUseRhs0 = 0; /* If true, use of left-most RHS label is illegal */
  3768. const char *zSkip = 0; /* The zOvwrt comment within rp->code, or NULL */
  3769. char lhsused = 0; /* True if the LHS element has been used */
  3770. char lhsdirect; /* True if LHS writes directly into stack */
  3771. char used[MAXRHS]; /* True for each RHS element which is used */
  3772. char zLhs[50]; /* Convert the LHS symbol into this string */
  3773. char zOvwrt[900]; /* Comment that to allow LHS to overwrite RHS */
  3774. for(i=0; i<rp->nrhs; i++) used[i] = 0;
  3775. lhsused = 0;
  3776. if( rp->code==0 ){
  3777. static char newlinestr[2] = { '\n', '\0' };
  3778. rp->code = newlinestr;
  3779. rp->line = rp->ruleline;
  3780. rp->noCode = 1;
  3781. }else{
  3782. rp->noCode = 0;
  3783. }
  3784. if( rp->nrhs==0 ){
  3785. /* If there are no RHS symbols, then writing directly to the LHS is ok */
  3786. lhsdirect = 1;
  3787. }else if( rp->rhsalias[0]==0 ){
  3788. /* The left-most RHS symbol has no value. LHS direct is ok. But
  3789. ** we have to call the destructor on the RHS symbol first. */
  3790. lhsdirect = 1;
  3791. if( has_destructor(rp->rhs[0],lemp) ){
  3792. append_str(0,0,0,0);
  3793. append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
  3794. rp->rhs[0]->index,1-rp->nrhs);
  3795. rp->codePrefix = Strsafe(append_str(0,0,0,0));
  3796. rp->noCode = 0;
  3797. }
  3798. }else if( rp->lhsalias==0 ){
  3799. /* There is no LHS value symbol. */
  3800. lhsdirect = 1;
  3801. }else if( strcmp(rp->lhsalias,rp->rhsalias[0])==0 ){
  3802. /* The LHS symbol and the left-most RHS symbol are the same, so
  3803. ** direct writing is allowed */
  3804. lhsdirect = 1;
  3805. lhsused = 1;
  3806. used[0] = 1;
  3807. if( rp->lhs->dtnum!=rp->rhs[0]->dtnum ){
  3808. ErrorMsg(lemp->filename,rp->ruleline,
  3809. "%s(%s) and %s(%s) share the same label but have "
  3810. "different datatypes.",
  3811. rp->lhs->name, rp->lhsalias, rp->rhs[0]->name, rp->rhsalias[0]);
  3812. lemp->errorcnt++;
  3813. }
  3814. }else{
  3815. lemon_sprintf(zOvwrt, "/*%s-overwrites-%s*/",
  3816. rp->lhsalias, rp->rhsalias[0]);
  3817. zSkip = strstr(rp->code, zOvwrt);
  3818. if( zSkip!=0 ){
  3819. /* The code contains a special comment that indicates that it is safe
  3820. ** for the LHS label to overwrite left-most RHS label. */
  3821. lhsdirect = 1;
  3822. }else{
  3823. lhsdirect = 0;
  3824. }
  3825. }
  3826. if( lhsdirect ){
  3827. sprintf(zLhs, "yymsp[%d].minor.yy%d",1-rp->nrhs,rp->lhs->dtnum);
  3828. }else{
  3829. rc = 1;
  3830. sprintf(zLhs, "yylhsminor.yy%d",rp->lhs->dtnum);
  3831. }
  3832. append_str(0,0,0,0);
  3833. /* This const cast is wrong but harmless, if we're careful. */
  3834. for(cp=(char *)rp->code; *cp; cp++){
  3835. if( cp==zSkip ){
  3836. append_str(zOvwrt,0,0,0);
  3837. cp += lemonStrlen(zOvwrt)-1;
  3838. dontUseRhs0 = 1;
  3839. continue;
  3840. }
  3841. if( ISALPHA(*cp) && (cp==rp->code || (!ISALNUM(cp[-1]) && cp[-1]!='_')) ){
  3842. char saved;
  3843. for(xp= &cp[1]; ISALNUM(*xp) || *xp=='_'; xp++);
  3844. saved = *xp;
  3845. *xp = 0;
  3846. if( rp->lhsalias && strcmp(cp,rp->lhsalias)==0 ){
  3847. append_str(zLhs,0,0,0);
  3848. cp = xp;
  3849. lhsused = 1;
  3850. }else{
  3851. for(i=0; i<rp->nrhs; i++){
  3852. if( rp->rhsalias[i] && strcmp(cp,rp->rhsalias[i])==0 ){
  3853. if( i==0 && dontUseRhs0 ){
  3854. ErrorMsg(lemp->filename,rp->ruleline,
  3855. "Label %s used after '%s'.",
  3856. rp->rhsalias[0], zOvwrt);
  3857. lemp->errorcnt++;
  3858. }else if( cp!=rp->code && cp[-1]=='@' ){
  3859. /* If the argument is of the form @X then substituted
  3860. ** the token number of X, not the value of X */
  3861. append_str("yymsp[%d].major",-1,i-rp->nrhs+1,0);
  3862. }else{
  3863. struct symbol *sp = rp->rhs[i];
  3864. int dtnum;
  3865. if( sp->type==MULTITERMINAL ){
  3866. dtnum = sp->subsym[0]->dtnum;
  3867. }else{
  3868. dtnum = sp->dtnum;
  3869. }
  3870. append_str("yymsp[%d].minor.yy%d",0,i-rp->nrhs+1, dtnum);
  3871. }
  3872. cp = xp;
  3873. used[i] = 1;
  3874. break;
  3875. }
  3876. }
  3877. }
  3878. *xp = saved;
  3879. }
  3880. append_str(cp, 1, 0, 0);
  3881. } /* End loop */
  3882. /* Main code generation completed */
  3883. cp = append_str(0,0,0,0);
  3884. if( cp && cp[0] ) rp->code = Strsafe(cp);
  3885. append_str(0,0,0,0);
  3886. /* Check to make sure the LHS has been used */
  3887. if( rp->lhsalias && !lhsused ){
  3888. ErrorMsg(lemp->filename,rp->ruleline,
  3889. "Label \"%s\" for \"%s(%s)\" is never used.",
  3890. rp->lhsalias,rp->lhs->name,rp->lhsalias);
  3891. lemp->errorcnt++;
  3892. }
  3893. /* Generate destructor code for RHS minor values which are not referenced.
  3894. ** Generate error messages for unused labels and duplicate labels.
  3895. */
  3896. for(i=0; i<rp->nrhs; i++){
  3897. if( rp->rhsalias[i] ){
  3898. if( i>0 ){
  3899. int j;
  3900. if( rp->lhsalias && strcmp(rp->lhsalias,rp->rhsalias[i])==0 ){
  3901. ErrorMsg(lemp->filename,rp->ruleline,
  3902. "%s(%s) has the same label as the LHS but is not the left-most "
  3903. "symbol on the RHS.",
  3904. rp->rhs[i]->name, rp->rhsalias[i]);
  3905. lemp->errorcnt++;
  3906. }
  3907. for(j=0; j<i; j++){
  3908. if( rp->rhsalias[j] && strcmp(rp->rhsalias[j],rp->rhsalias[i])==0 ){
  3909. ErrorMsg(lemp->filename,rp->ruleline,
  3910. "Label %s used for multiple symbols on the RHS of a rule.",
  3911. rp->rhsalias[i]);
  3912. lemp->errorcnt++;
  3913. break;
  3914. }
  3915. }
  3916. }
  3917. if( !used[i] ){
  3918. ErrorMsg(lemp->filename,rp->ruleline,
  3919. "Label %s for \"%s(%s)\" is never used.",
  3920. rp->rhsalias[i],rp->rhs[i]->name,rp->rhsalias[i]);
  3921. lemp->errorcnt++;
  3922. }
  3923. }else if( i>0 && has_destructor(rp->rhs[i],lemp) ){
  3924. append_str(" yy_destructor(yypParser,%d,&yymsp[%d].minor);\n", 0,
  3925. rp->rhs[i]->index,i-rp->nrhs+1);
  3926. }
  3927. }
  3928. /* If unable to write LHS values directly into the stack, write the
  3929. ** saved LHS value now. */
  3930. if( lhsdirect==0 ){
  3931. append_str(" yymsp[%d].minor.yy%d = ", 0, 1-rp->nrhs, rp->lhs->dtnum);
  3932. append_str(zLhs, 0, 0, 0);
  3933. append_str(";\n", 0, 0, 0);
  3934. }
  3935. /* Suffix code generation complete */
  3936. cp = append_str(0,0,0,0);
  3937. if( cp && cp[0] ){
  3938. rp->codeSuffix = Strsafe(cp);
  3939. rp->noCode = 0;
  3940. }
  3941. return rc;
  3942. }
  3943. /*
  3944. ** Generate code which executes when the rule "rp" is reduced. Write
  3945. ** the code to "out". Make sure lineno stays up-to-date.
  3946. */
  3947. PRIVATE void emit_code(
  3948. FILE *out,
  3949. struct rule *rp,
  3950. struct lemon *lemp,
  3951. int *lineno
  3952. ){
  3953. const char *cp;
  3954. /* Setup code prior to the #line directive */
  3955. if( rp->codePrefix && rp->codePrefix[0] ){
  3956. fprintf(out, "{%s", rp->codePrefix);
  3957. for(cp=rp->codePrefix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
  3958. }
  3959. /* Generate code to do the reduce action */
  3960. if( rp->code ){
  3961. if( !lemp->nolinenosflag ){
  3962. (*lineno)++;
  3963. tplt_linedir(out,rp->line,lemp->filename);
  3964. }
  3965. fprintf(out,"{%s",rp->code);
  3966. for(cp=rp->code; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
  3967. fprintf(out,"}\n"); (*lineno)++;
  3968. if( !lemp->nolinenosflag ){
  3969. (*lineno)++;
  3970. tplt_linedir(out,*lineno,lemp->outname);
  3971. }
  3972. }
  3973. /* Generate breakdown code that occurs after the #line directive */
  3974. if( rp->codeSuffix && rp->codeSuffix[0] ){
  3975. fprintf(out, "%s", rp->codeSuffix);
  3976. for(cp=rp->codeSuffix; *cp; cp++){ if( *cp=='\n' ) (*lineno)++; }
  3977. }
  3978. if( rp->codePrefix ){
  3979. fprintf(out, "}\n"); (*lineno)++;
  3980. }
  3981. return;
  3982. }
  3983. /*
  3984. ** Print the definition of the union used for the parser's data stack.
  3985. ** This union contains fields for every possible data type for tokens
  3986. ** and nonterminals. In the process of computing and printing this
  3987. ** union, also set the ".dtnum" field of every terminal and nonterminal
  3988. ** symbol.
  3989. */
  3990. void print_stack_union(
  3991. FILE *out, /* The output stream */
  3992. struct lemon *lemp, /* The main info structure for this parser */
  3993. int *plineno, /* Pointer to the line number */
  3994. int mhflag /* True if generating makeheaders output */
  3995. ){
  3996. int lineno; /* The line number of the output */
  3997. char **types; /* A hash table of datatypes */
  3998. int arraysize; /* Size of the "types" array */
  3999. int maxdtlength; /* Maximum length of any ".datatype" field. */
  4000. char *stddt; /* Standardized name for a datatype */
  4001. int i,j; /* Loop counters */
  4002. unsigned hash; /* For hashing the name of a type */
  4003. const char *name; /* Name of the parser */
  4004. /* Allocate and initialize types[] and allocate stddt[] */
  4005. arraysize = lemp->nsymbol * 2;
  4006. types = (char**)lemon_calloc( arraysize, sizeof(char*) );
  4007. if( types==0 ){
  4008. fprintf(stderr,"Out of memory.\n");
  4009. exit(1);
  4010. }
  4011. for(i=0; i<arraysize; i++) types[i] = 0;
  4012. maxdtlength = 0;
  4013. if( lemp->vartype ){
  4014. maxdtlength = lemonStrlen(lemp->vartype);
  4015. }
  4016. for(i=0; i<lemp->nsymbol; i++){
  4017. int len;
  4018. struct symbol *sp = lemp->symbols[i];
  4019. if( sp->datatype==0 ) continue;
  4020. len = lemonStrlen(sp->datatype);
  4021. if( len>maxdtlength ) maxdtlength = len;
  4022. }
  4023. stddt = (char*)lemon_malloc( maxdtlength*2 + 1 );
  4024. if( stddt==0 ){
  4025. fprintf(stderr,"Out of memory.\n");
  4026. exit(1);
  4027. }
  4028. /* Build a hash table of datatypes. The ".dtnum" field of each symbol
  4029. ** is filled in with the hash index plus 1. A ".dtnum" value of 0 is
  4030. ** used for terminal symbols. If there is no %default_type defined then
  4031. ** 0 is also used as the .dtnum value for nonterminals which do not specify
  4032. ** a datatype using the %type directive.
  4033. */
  4034. for(i=0; i<lemp->nsymbol; i++){
  4035. struct symbol *sp = lemp->symbols[i];
  4036. char *cp;
  4037. if( sp==lemp->errsym ){
  4038. sp->dtnum = arraysize+1;
  4039. continue;
  4040. }
  4041. if( sp->type!=NONTERMINAL || (sp->datatype==0 && lemp->vartype==0) ){
  4042. sp->dtnum = 0;
  4043. continue;
  4044. }
  4045. cp = sp->datatype;
  4046. if( cp==0 ) cp = lemp->vartype;
  4047. j = 0;
  4048. while( ISSPACE(*cp) ) cp++;
  4049. while( *cp ) stddt[j++] = *cp++;
  4050. while( j>0 && ISSPACE(stddt[j-1]) ) j--;
  4051. stddt[j] = 0;
  4052. if( lemp->tokentype && strcmp(stddt, lemp->tokentype)==0 ){
  4053. sp->dtnum = 0;
  4054. continue;
  4055. }
  4056. hash = 0;
  4057. for(j=0; stddt[j]; j++){
  4058. hash = hash*53 + stddt[j];
  4059. }
  4060. hash = (hash & 0x7fffffff)%arraysize;
  4061. while( types[hash] ){
  4062. if( strcmp(types[hash],stddt)==0 ){
  4063. sp->dtnum = hash + 1;
  4064. break;
  4065. }
  4066. hash++;
  4067. if( hash>=(unsigned)arraysize ) hash = 0;
  4068. }
  4069. if( types[hash]==0 ){
  4070. sp->dtnum = hash + 1;
  4071. types[hash] = (char*)lemon_malloc( lemonStrlen(stddt)+1 );
  4072. if( types[hash]==0 ){
  4073. fprintf(stderr,"Out of memory.\n");
  4074. exit(1);
  4075. }
  4076. lemon_strcpy(types[hash],stddt);
  4077. }
  4078. }
  4079. /* Print out the definition of YYTOKENTYPE and YYMINORTYPE */
  4080. name = lemp->name ? lemp->name : "Parse";
  4081. lineno = *plineno;
  4082. if( mhflag ){ fprintf(out,"#if INTERFACE\n"); lineno++; }
  4083. fprintf(out,"#define %sTOKENTYPE %s\n",name,
  4084. lemp->tokentype?lemp->tokentype:"void*"); lineno++;
  4085. if( mhflag ){ fprintf(out,"#endif\n"); lineno++; }
  4086. fprintf(out,"typedef union {\n"); lineno++;
  4087. fprintf(out," int yyinit;\n"); lineno++;
  4088. fprintf(out," %sTOKENTYPE yy0;\n",name); lineno++;
  4089. for(i=0; i<arraysize; i++){
  4090. if( types[i]==0 ) continue;
  4091. fprintf(out," %s yy%d;\n",types[i],i+1); lineno++;
  4092. lemon_free(types[i]);
  4093. }
  4094. if( lemp->errsym && lemp->errsym->useCnt ){
  4095. fprintf(out," int yy%d;\n",lemp->errsym->dtnum); lineno++;
  4096. }
  4097. lemon_free(stddt);
  4098. lemon_free(types);
  4099. fprintf(out,"} YYMINORTYPE;\n"); lineno++;
  4100. *plineno = lineno;
  4101. }
  4102. /*
  4103. ** Return the name of a C datatype able to represent values between
  4104. ** lwr and upr, inclusive. If pnByte!=NULL then also write the sizeof
  4105. ** for that type (1, 2, or 4) into *pnByte.
  4106. */
  4107. static const char *minimum_size_type(int lwr, int upr, int *pnByte){
  4108. const char *zType = "int";
  4109. int nByte = 4;
  4110. if( lwr>=0 ){
  4111. if( upr<=255 ){
  4112. zType = "unsigned char";
  4113. nByte = 1;
  4114. }else if( upr<65535 ){
  4115. zType = "unsigned short int";
  4116. nByte = 2;
  4117. }else{
  4118. zType = "unsigned int";
  4119. nByte = 4;
  4120. }
  4121. }else if( lwr>=-127 && upr<=127 ){
  4122. zType = "signed char";
  4123. nByte = 1;
  4124. }else if( lwr>=-32767 && upr<32767 ){
  4125. zType = "short";
  4126. nByte = 2;
  4127. }
  4128. if( pnByte ) *pnByte = nByte;
  4129. return zType;
  4130. }
  4131. /*
  4132. ** Each state contains a set of token transaction and a set of
  4133. ** nonterminal transactions. Each of these sets makes an instance
  4134. ** of the following structure. An array of these structures is used
  4135. ** to order the creation of entries in the yy_action[] table.
  4136. */
  4137. struct axset {
  4138. struct state *stp; /* A pointer to a state */
  4139. int isTkn; /* True to use tokens. False for non-terminals */
  4140. int nAction; /* Number of actions */
  4141. int iOrder; /* Original order of action sets */
  4142. };
  4143. /*
  4144. ** Compare to axset structures for sorting purposes
  4145. */
  4146. static int axset_compare(const void *a, const void *b){
  4147. struct axset *p1 = (struct axset*)a;
  4148. struct axset *p2 = (struct axset*)b;
  4149. int c;
  4150. c = p2->nAction - p1->nAction;
  4151. if( c==0 ){
  4152. c = p1->iOrder - p2->iOrder;
  4153. }
  4154. assert( c!=0 || p1==p2 );
  4155. return c;
  4156. }
  4157. /*
  4158. ** Write text on "out" that describes the rule "rp".
  4159. */
  4160. static void writeRuleText(FILE *out, struct rule *rp){
  4161. int j;
  4162. fprintf(out,"%s ::=", rp->lhs->name);
  4163. for(j=0; j<rp->nrhs; j++){
  4164. struct symbol *sp = rp->rhs[j];
  4165. if( sp->type!=MULTITERMINAL ){
  4166. fprintf(out," %s", sp->name);
  4167. }else{
  4168. int k;
  4169. fprintf(out," %s", sp->subsym[0]->name);
  4170. for(k=1; k<sp->nsubsym; k++){
  4171. fprintf(out,"|%s",sp->subsym[k]->name);
  4172. }
  4173. }
  4174. }
  4175. }
  4176. /* Generate C source code for the parser */
  4177. void ReportTable(
  4178. struct lemon *lemp,
  4179. int mhflag, /* Output in makeheaders format if true */
  4180. int sqlFlag /* Generate the *.sql file too */
  4181. ){
  4182. FILE *out, *in, *sql;
  4183. int lineno;
  4184. struct state *stp;
  4185. struct action *ap;
  4186. struct rule *rp;
  4187. struct acttab *pActtab;
  4188. int i, j, n, sz, mn, mx;
  4189. int nLookAhead;
  4190. int szActionType; /* sizeof(YYACTIONTYPE) */
  4191. int szCodeType; /* sizeof(YYCODETYPE) */
  4192. const char *name;
  4193. int mnTknOfst, mxTknOfst;
  4194. int mnNtOfst, mxNtOfst;
  4195. struct axset *ax;
  4196. char *prefix;
  4197. lemp->minShiftReduce = lemp->nstate;
  4198. lemp->errAction = lemp->minShiftReduce + lemp->nrule;
  4199. lemp->accAction = lemp->errAction + 1;
  4200. lemp->noAction = lemp->accAction + 1;
  4201. lemp->minReduce = lemp->noAction + 1;
  4202. lemp->maxAction = lemp->minReduce + lemp->nrule;
  4203. in = tplt_open(lemp);
  4204. if( in==0 ) return;
  4205. out = file_open(lemp,".c","wb");
  4206. if( out==0 ){
  4207. fclose(in);
  4208. return;
  4209. }
  4210. if( sqlFlag==0 ){
  4211. sql = 0;
  4212. }else{
  4213. sql = file_open(lemp, ".sql", "wb");
  4214. if( sql==0 ){
  4215. fclose(in);
  4216. fclose(out);
  4217. return;
  4218. }
  4219. fprintf(sql,
  4220. "BEGIN;\n"
  4221. "CREATE TABLE symbol(\n"
  4222. " id INTEGER PRIMARY KEY,\n"
  4223. " name TEXT NOT NULL,\n"
  4224. " isTerminal BOOLEAN NOT NULL,\n"
  4225. " fallback INTEGER REFERENCES symbol"
  4226. " DEFERRABLE INITIALLY DEFERRED\n"
  4227. ");\n"
  4228. );
  4229. for(i=0; i<lemp->nsymbol; i++){
  4230. fprintf(sql,
  4231. "INSERT INTO symbol(id,name,isTerminal,fallback)"
  4232. "VALUES(%d,'%s',%s",
  4233. i, lemp->symbols[i]->name,
  4234. i<lemp->nterminal ? "TRUE" : "FALSE"
  4235. );
  4236. if( lemp->symbols[i]->fallback ){
  4237. fprintf(sql, ",%d);\n", lemp->symbols[i]->fallback->index);
  4238. }else{
  4239. fprintf(sql, ",NULL);\n");
  4240. }
  4241. }
  4242. fprintf(sql,
  4243. "CREATE TABLE rule(\n"
  4244. " ruleid INTEGER PRIMARY KEY,\n"
  4245. " lhs INTEGER REFERENCES symbol(id),\n"
  4246. " txt TEXT\n"
  4247. ");\n"
  4248. "CREATE TABLE rulerhs(\n"
  4249. " ruleid INTEGER REFERENCES rule(ruleid),\n"
  4250. " pos INTEGER,\n"
  4251. " sym INTEGER REFERENCES symbol(id)\n"
  4252. ");\n"
  4253. );
  4254. for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
  4255. assert( i==rp->iRule );
  4256. fprintf(sql,
  4257. "INSERT INTO rule(ruleid,lhs,txt)VALUES(%d,%d,'",
  4258. rp->iRule, rp->lhs->index
  4259. );
  4260. writeRuleText(sql, rp);
  4261. fprintf(sql,"');\n");
  4262. for(j=0; j<rp->nrhs; j++){
  4263. struct symbol *sp = rp->rhs[j];
  4264. if( sp->type!=MULTITERMINAL ){
  4265. fprintf(sql,
  4266. "INSERT INTO rulerhs(ruleid,pos,sym)VALUES(%d,%d,%d);\n",
  4267. i,j,sp->index
  4268. );
  4269. }else{
  4270. int k;
  4271. for(k=0; k<sp->nsubsym; k++){
  4272. fprintf(sql,
  4273. "INSERT INTO rulerhs(ruleid,pos,sym)VALUES(%d,%d,%d);\n",
  4274. i,j,sp->subsym[k]->index
  4275. );
  4276. }
  4277. }
  4278. }
  4279. }
  4280. fprintf(sql, "COMMIT;\n");
  4281. }
  4282. lineno = 1;
  4283. fprintf(out,
  4284. "/* This file is automatically generated by Lemon from input grammar\n"
  4285. "** source file \"%s\"", lemp->filename); lineno++;
  4286. if( nDefineUsed==0 ){
  4287. fprintf(out, ".\n*/\n"); lineno += 2;
  4288. }else{
  4289. fprintf(out, " with these options:\n**\n"); lineno += 2;
  4290. for(i=0; i<nDefine; i++){
  4291. if( !bDefineUsed[i] ) continue;
  4292. fprintf(out, "** -D%s\n", azDefine[i]); lineno++;
  4293. }
  4294. fprintf(out, "*/\n"); lineno++;
  4295. }
  4296. /* The first %include directive begins with a C-language comment,
  4297. ** then skip over the header comment of the template file
  4298. */
  4299. if( lemp->include==0 ) lemp->include = "";
  4300. for(i=0; ISSPACE(lemp->include[i]); i++){
  4301. if( lemp->include[i]=='\n' ){
  4302. lemp->include += i+1;
  4303. i = -1;
  4304. }
  4305. }
  4306. if( lemp->include[0]=='/' ){
  4307. tplt_skip_header(in,&lineno);
  4308. }else{
  4309. tplt_xfer(lemp->name,in,out,&lineno);
  4310. }
  4311. /* Generate the include code, if any */
  4312. tplt_print(out,lemp,lemp->include,&lineno);
  4313. if( mhflag ){
  4314. char *incName = file_makename(lemp, ".h");
  4315. fprintf(out,"#include \"%s\"\n", incName); lineno++;
  4316. lemon_free(incName);
  4317. }
  4318. tplt_xfer(lemp->name,in,out,&lineno);
  4319. /* Generate #defines for all tokens */
  4320. if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
  4321. else prefix = "";
  4322. if( mhflag ){
  4323. fprintf(out,"#if INTERFACE\n"); lineno++;
  4324. }else{
  4325. fprintf(out,"#ifndef %s%s\n", prefix, lemp->symbols[1]->name);
  4326. }
  4327. for(i=1; i<lemp->nterminal; i++){
  4328. fprintf(out,"#define %s%-30s %2d\n",prefix,lemp->symbols[i]->name,i);
  4329. lineno++;
  4330. }
  4331. fprintf(out,"#endif\n"); lineno++;
  4332. tplt_xfer(lemp->name,in,out,&lineno);
  4333. /* Generate the defines */
  4334. fprintf(out,"#define YYCODETYPE %s\n",
  4335. minimum_size_type(0, lemp->nsymbol, &szCodeType)); lineno++;
  4336. fprintf(out,"#define YYNOCODE %d\n",lemp->nsymbol); lineno++;
  4337. fprintf(out,"#define YYACTIONTYPE %s\n",
  4338. minimum_size_type(0,lemp->maxAction,&szActionType)); lineno++;
  4339. if( lemp->wildcard ){
  4340. fprintf(out,"#define YYWILDCARD %d\n",
  4341. lemp->wildcard->index); lineno++;
  4342. }
  4343. print_stack_union(out,lemp,&lineno,mhflag);
  4344. fprintf(out, "#ifndef YYSTACKDEPTH\n"); lineno++;
  4345. if( lemp->stacksize ){
  4346. fprintf(out,"#define YYSTACKDEPTH %s\n",lemp->stacksize); lineno++;
  4347. }else{
  4348. fprintf(out,"#define YYSTACKDEPTH 100\n"); lineno++;
  4349. }
  4350. fprintf(out, "#endif\n"); lineno++;
  4351. if( mhflag ){
  4352. fprintf(out,"#if INTERFACE\n"); lineno++;
  4353. }
  4354. name = lemp->name ? lemp->name : "Parse";
  4355. if( lemp->arg && lemp->arg[0] ){
  4356. i = lemonStrlen(lemp->arg);
  4357. while( i>=1 && ISSPACE(lemp->arg[i-1]) ) i--;
  4358. while( i>=1 && (ISALNUM(lemp->arg[i-1]) || lemp->arg[i-1]=='_') ) i--;
  4359. fprintf(out,"#define %sARG_SDECL %s;\n",name,lemp->arg); lineno++;
  4360. fprintf(out,"#define %sARG_PDECL ,%s\n",name,lemp->arg); lineno++;
  4361. fprintf(out,"#define %sARG_PARAM ,%s\n",name,&lemp->arg[i]); lineno++;
  4362. fprintf(out,"#define %sARG_FETCH %s=yypParser->%s;\n",
  4363. name,lemp->arg,&lemp->arg[i]); lineno++;
  4364. fprintf(out,"#define %sARG_STORE yypParser->%s=%s;\n",
  4365. name,&lemp->arg[i],&lemp->arg[i]); lineno++;
  4366. }else{
  4367. fprintf(out,"#define %sARG_SDECL\n",name); lineno++;
  4368. fprintf(out,"#define %sARG_PDECL\n",name); lineno++;
  4369. fprintf(out,"#define %sARG_PARAM\n",name); lineno++;
  4370. fprintf(out,"#define %sARG_FETCH\n",name); lineno++;
  4371. fprintf(out,"#define %sARG_STORE\n",name); lineno++;
  4372. }
  4373. if( lemp->reallocFunc ){
  4374. fprintf(out,"#define YYREALLOC %s\n", lemp->reallocFunc); lineno++;
  4375. }else{
  4376. fprintf(out,"#define YYREALLOC realloc\n"); lineno++;
  4377. }
  4378. if( lemp->freeFunc ){
  4379. fprintf(out,"#define YYFREE %s\n", lemp->freeFunc); lineno++;
  4380. }else{
  4381. fprintf(out,"#define YYFREE free\n"); lineno++;
  4382. }
  4383. if( lemp->reallocFunc && lemp->freeFunc ){
  4384. fprintf(out,"#define YYDYNSTACK 1\n"); lineno++;
  4385. }else{
  4386. fprintf(out,"#define YYDYNSTACK 0\n"); lineno++;
  4387. }
  4388. if( lemp->ctx && lemp->ctx[0] ){
  4389. i = lemonStrlen(lemp->ctx);
  4390. while( i>=1 && ISSPACE(lemp->ctx[i-1]) ) i--;
  4391. while( i>=1 && (ISALNUM(lemp->ctx[i-1]) || lemp->ctx[i-1]=='_') ) i--;
  4392. fprintf(out,"#define %sCTX_SDECL %s;\n",name,lemp->ctx); lineno++;
  4393. fprintf(out,"#define %sCTX_PDECL ,%s\n",name,lemp->ctx); lineno++;
  4394. fprintf(out,"#define %sCTX_PARAM ,%s\n",name,&lemp->ctx[i]); lineno++;
  4395. fprintf(out,"#define %sCTX_FETCH %s=yypParser->%s;\n",
  4396. name,lemp->ctx,&lemp->ctx[i]); lineno++;
  4397. fprintf(out,"#define %sCTX_STORE yypParser->%s=%s;\n",
  4398. name,&lemp->ctx[i],&lemp->ctx[i]); lineno++;
  4399. }else{
  4400. fprintf(out,"#define %sCTX_SDECL\n",name); lineno++;
  4401. fprintf(out,"#define %sCTX_PDECL\n",name); lineno++;
  4402. fprintf(out,"#define %sCTX_PARAM\n",name); lineno++;
  4403. fprintf(out,"#define %sCTX_FETCH\n",name); lineno++;
  4404. fprintf(out,"#define %sCTX_STORE\n",name); lineno++;
  4405. }
  4406. if( mhflag ){
  4407. fprintf(out,"#endif\n"); lineno++;
  4408. }
  4409. if( lemp->errsym && lemp->errsym->useCnt ){
  4410. fprintf(out,"#define YYERRORSYMBOL %d\n",lemp->errsym->index); lineno++;
  4411. fprintf(out,"#define YYERRSYMDT yy%d\n",lemp->errsym->dtnum); lineno++;
  4412. }
  4413. if( lemp->has_fallback ){
  4414. fprintf(out,"#define YYFALLBACK 1\n"); lineno++;
  4415. }
  4416. /* Compute the action table, but do not output it yet. The action
  4417. ** table must be computed before generating the YYNSTATE macro because
  4418. ** we need to know how many states can be eliminated.
  4419. */
  4420. ax = (struct axset *) lemon_calloc(lemp->nxstate*2, sizeof(ax[0]));
  4421. if( ax==0 ){
  4422. fprintf(stderr,"malloc failed\n");
  4423. exit(1);
  4424. }
  4425. for(i=0; i<lemp->nxstate; i++){
  4426. stp = lemp->sorted[i];
  4427. ax[i*2].stp = stp;
  4428. ax[i*2].isTkn = 1;
  4429. ax[i*2].nAction = stp->nTknAct;
  4430. ax[i*2+1].stp = stp;
  4431. ax[i*2+1].isTkn = 0;
  4432. ax[i*2+1].nAction = stp->nNtAct;
  4433. }
  4434. mxTknOfst = mnTknOfst = 0;
  4435. mxNtOfst = mnNtOfst = 0;
  4436. /* In an effort to minimize the action table size, use the heuristic
  4437. ** of placing the largest action sets first */
  4438. for(i=0; i<lemp->nxstate*2; i++) ax[i].iOrder = i;
  4439. qsort(ax, lemp->nxstate*2, sizeof(ax[0]), axset_compare);
  4440. pActtab = acttab_alloc(lemp->nsymbol, lemp->nterminal);
  4441. for(i=0; i<lemp->nxstate*2 && ax[i].nAction>0; i++){
  4442. stp = ax[i].stp;
  4443. if( ax[i].isTkn ){
  4444. for(ap=stp->ap; ap; ap=ap->next){
  4445. int action;
  4446. if( ap->sp->index>=lemp->nterminal ) continue;
  4447. action = compute_action(lemp, ap);
  4448. if( action<0 ) continue;
  4449. acttab_action(pActtab, ap->sp->index, action);
  4450. }
  4451. stp->iTknOfst = acttab_insert(pActtab, 1);
  4452. if( stp->iTknOfst<mnTknOfst ) mnTknOfst = stp->iTknOfst;
  4453. if( stp->iTknOfst>mxTknOfst ) mxTknOfst = stp->iTknOfst;
  4454. }else{
  4455. for(ap=stp->ap; ap; ap=ap->next){
  4456. int action;
  4457. if( ap->sp->index<lemp->nterminal ) continue;
  4458. if( ap->sp->index==lemp->nsymbol ) continue;
  4459. action = compute_action(lemp, ap);
  4460. if( action<0 ) continue;
  4461. acttab_action(pActtab, ap->sp->index, action);
  4462. }
  4463. stp->iNtOfst = acttab_insert(pActtab, 0);
  4464. if( stp->iNtOfst<mnNtOfst ) mnNtOfst = stp->iNtOfst;
  4465. if( stp->iNtOfst>mxNtOfst ) mxNtOfst = stp->iNtOfst;
  4466. }
  4467. #if 0 /* Uncomment for a trace of how the yy_action[] table fills out */
  4468. { int jj, nn;
  4469. for(jj=nn=0; jj<pActtab->nAction; jj++){
  4470. if( pActtab->aAction[jj].action<0 ) nn++;
  4471. }
  4472. printf("%4d: State %3d %s n: %2d size: %5d freespace: %d\n",
  4473. i, stp->statenum, ax[i].isTkn ? "Token" : "Var ",
  4474. ax[i].nAction, pActtab->nAction, nn);
  4475. }
  4476. #endif
  4477. }
  4478. lemon_free(ax);
  4479. /* Mark rules that are actually used for reduce actions after all
  4480. ** optimizations have been applied
  4481. */
  4482. for(rp=lemp->rule; rp; rp=rp->next) rp->doesReduce = LEMON_FALSE;
  4483. for(i=0; i<lemp->nxstate; i++){
  4484. for(ap=lemp->sorted[i]->ap; ap; ap=ap->next){
  4485. if( ap->type==REDUCE || ap->type==SHIFTREDUCE ){
  4486. ap->x.rp->doesReduce = 1;
  4487. }
  4488. }
  4489. }
  4490. /* Finish rendering the constants now that the action table has
  4491. ** been computed */
  4492. fprintf(out,"#define YYNSTATE %d\n",lemp->nxstate); lineno++;
  4493. fprintf(out,"#define YYNRULE %d\n",lemp->nrule); lineno++;
  4494. fprintf(out,"#define YYNRULE_WITH_ACTION %d\n",lemp->nruleWithAction);
  4495. lineno++;
  4496. fprintf(out,"#define YYNTOKEN %d\n",lemp->nterminal); lineno++;
  4497. fprintf(out,"#define YY_MAX_SHIFT %d\n",lemp->nxstate-1); lineno++;
  4498. i = lemp->minShiftReduce;
  4499. fprintf(out,"#define YY_MIN_SHIFTREDUCE %d\n",i); lineno++;
  4500. i += lemp->nrule;
  4501. fprintf(out,"#define YY_MAX_SHIFTREDUCE %d\n", i-1); lineno++;
  4502. fprintf(out,"#define YY_ERROR_ACTION %d\n", lemp->errAction); lineno++;
  4503. fprintf(out,"#define YY_ACCEPT_ACTION %d\n", lemp->accAction); lineno++;
  4504. fprintf(out,"#define YY_NO_ACTION %d\n", lemp->noAction); lineno++;
  4505. fprintf(out,"#define YY_MIN_REDUCE %d\n", lemp->minReduce); lineno++;
  4506. i = lemp->minReduce + lemp->nrule;
  4507. fprintf(out,"#define YY_MAX_REDUCE %d\n", i-1); lineno++;
  4508. /* Minimum and maximum token values that have a destructor */
  4509. mn = mx = 0;
  4510. for(i=0; i<lemp->nsymbol; i++){
  4511. struct symbol *sp = lemp->symbols[i];
  4512. if( sp && sp->type!=TERMINAL && sp->destructor ){
  4513. if( mn==0 || sp->index<mn ) mn = sp->index;
  4514. if( sp->index>mx ) mx = sp->index;
  4515. }
  4516. }
  4517. if( lemp->tokendest ) mn = 0;
  4518. if( lemp->vardest ) mx = lemp->nsymbol-1;
  4519. fprintf(out,"#define YY_MIN_DSTRCTR %d\n", mn); lineno++;
  4520. fprintf(out,"#define YY_MAX_DSTRCTR %d\n", mx); lineno++;
  4521. tplt_xfer(lemp->name,in,out,&lineno);
  4522. /* Now output the action table and its associates:
  4523. **
  4524. ** yy_action[] A single table containing all actions.
  4525. ** yy_lookahead[] A table containing the lookahead for each entry in
  4526. ** yy_action. Used to detect hash collisions.
  4527. ** yy_shift_ofst[] For each state, the offset into yy_action for
  4528. ** shifting terminals.
  4529. ** yy_reduce_ofst[] For each state, the offset into yy_action for
  4530. ** shifting non-terminals after a reduce.
  4531. ** yy_default[] Default action for each state.
  4532. */
  4533. /* Output the yy_action table */
  4534. lemp->nactiontab = n = acttab_action_size(pActtab);
  4535. lemp->tablesize += n*szActionType;
  4536. fprintf(out,"#define YY_ACTTAB_COUNT (%d)\n", n); lineno++;
  4537. fprintf(out,"static const YYACTIONTYPE yy_action[] = {\n"); lineno++;
  4538. for(i=j=0; i<n; i++){
  4539. int action = acttab_yyaction(pActtab, i);
  4540. if( action<0 ) action = lemp->noAction;
  4541. if( j==0 ) fprintf(out," /* %5d */ ", i);
  4542. fprintf(out, " %4d,", action);
  4543. if( j==9 || i==n-1 ){
  4544. fprintf(out, "\n"); lineno++;
  4545. j = 0;
  4546. }else{
  4547. j++;
  4548. }
  4549. }
  4550. fprintf(out, "};\n"); lineno++;
  4551. /* Output the yy_lookahead table */
  4552. lemp->nlookaheadtab = n = acttab_lookahead_size(pActtab);
  4553. lemp->tablesize += n*szCodeType;
  4554. fprintf(out,"static const YYCODETYPE yy_lookahead[] = {\n"); lineno++;
  4555. for(i=j=0; i<n; i++){
  4556. int la = acttab_yylookahead(pActtab, i);
  4557. if( la<0 ) la = lemp->nsymbol;
  4558. if( j==0 ) fprintf(out," /* %5d */ ", i);
  4559. fprintf(out, " %4d,", la);
  4560. if( j==9 ){
  4561. fprintf(out, "\n"); lineno++;
  4562. j = 0;
  4563. }else{
  4564. j++;
  4565. }
  4566. }
  4567. /* Add extra entries to the end of the yy_lookahead[] table so that
  4568. ** yy_shift_ofst[]+iToken will always be a valid index into the array,
  4569. ** even for the largest possible value of yy_shift_ofst[] and iToken. */
  4570. nLookAhead = lemp->nterminal + lemp->nactiontab;
  4571. while( i<nLookAhead ){
  4572. if( j==0 ) fprintf(out," /* %5d */ ", i);
  4573. fprintf(out, " %4d,", lemp->nterminal);
  4574. if( j==9 ){
  4575. fprintf(out, "\n"); lineno++;
  4576. j = 0;
  4577. }else{
  4578. j++;
  4579. }
  4580. i++;
  4581. }
  4582. if( j>0 ){ fprintf(out, "\n"); lineno++; }
  4583. fprintf(out, "};\n"); lineno++;
  4584. /* Output the yy_shift_ofst[] table */
  4585. n = lemp->nxstate;
  4586. while( n>0 && lemp->sorted[n-1]->iTknOfst==NO_OFFSET ) n--;
  4587. fprintf(out, "#define YY_SHIFT_COUNT (%d)\n", n-1); lineno++;
  4588. fprintf(out, "#define YY_SHIFT_MIN (%d)\n", mnTknOfst); lineno++;
  4589. fprintf(out, "#define YY_SHIFT_MAX (%d)\n", mxTknOfst); lineno++;
  4590. fprintf(out, "static const %s yy_shift_ofst[] = {\n",
  4591. minimum_size_type(mnTknOfst, lemp->nterminal+lemp->nactiontab, &sz));
  4592. lineno++;
  4593. lemp->tablesize += n*sz;
  4594. for(i=j=0; i<n; i++){
  4595. int ofst;
  4596. stp = lemp->sorted[i];
  4597. ofst = stp->iTknOfst;
  4598. if( ofst==NO_OFFSET ) ofst = lemp->nactiontab;
  4599. if( j==0 ) fprintf(out," /* %5d */ ", i);
  4600. fprintf(out, " %4d,", ofst);
  4601. if( j==9 || i==n-1 ){
  4602. fprintf(out, "\n"); lineno++;
  4603. j = 0;
  4604. }else{
  4605. j++;
  4606. }
  4607. }
  4608. fprintf(out, "};\n"); lineno++;
  4609. /* Output the yy_reduce_ofst[] table */
  4610. n = lemp->nxstate;
  4611. while( n>0 && lemp->sorted[n-1]->iNtOfst==NO_OFFSET ) n--;
  4612. fprintf(out, "#define YY_REDUCE_COUNT (%d)\n", n-1); lineno++;
  4613. fprintf(out, "#define YY_REDUCE_MIN (%d)\n", mnNtOfst); lineno++;
  4614. fprintf(out, "#define YY_REDUCE_MAX (%d)\n", mxNtOfst); lineno++;
  4615. fprintf(out, "static const %s yy_reduce_ofst[] = {\n",
  4616. minimum_size_type(mnNtOfst-1, mxNtOfst, &sz)); lineno++;
  4617. lemp->tablesize += n*sz;
  4618. for(i=j=0; i<n; i++){
  4619. int ofst;
  4620. stp = lemp->sorted[i];
  4621. ofst = stp->iNtOfst;
  4622. if( ofst==NO_OFFSET ) ofst = mnNtOfst - 1;
  4623. if( j==0 ) fprintf(out," /* %5d */ ", i);
  4624. fprintf(out, " %4d,", ofst);
  4625. if( j==9 || i==n-1 ){
  4626. fprintf(out, "\n"); lineno++;
  4627. j = 0;
  4628. }else{
  4629. j++;
  4630. }
  4631. }
  4632. fprintf(out, "};\n"); lineno++;
  4633. /* Output the default action table */
  4634. fprintf(out, "static const YYACTIONTYPE yy_default[] = {\n"); lineno++;
  4635. n = lemp->nxstate;
  4636. lemp->tablesize += n*szActionType;
  4637. for(i=j=0; i<n; i++){
  4638. stp = lemp->sorted[i];
  4639. if( j==0 ) fprintf(out," /* %5d */ ", i);
  4640. if( stp->iDfltReduce<0 ){
  4641. fprintf(out, " %4d,", lemp->errAction);
  4642. }else{
  4643. fprintf(out, " %4d,", stp->iDfltReduce + lemp->minReduce);
  4644. }
  4645. if( j==9 || i==n-1 ){
  4646. fprintf(out, "\n"); lineno++;
  4647. j = 0;
  4648. }else{
  4649. j++;
  4650. }
  4651. }
  4652. fprintf(out, "};\n"); lineno++;
  4653. tplt_xfer(lemp->name,in,out,&lineno);
  4654. /* Generate the table of fallback tokens.
  4655. */
  4656. if( lemp->has_fallback ){
  4657. mx = lemp->nterminal - 1;
  4658. /* 2019-08-28: Generate fallback entries for every token to avoid
  4659. ** having to do a range check on the index */
  4660. /* while( mx>0 && lemp->symbols[mx]->fallback==0 ){ mx--; } */
  4661. lemp->tablesize += (mx+1)*szCodeType;
  4662. for(i=0; i<=mx; i++){
  4663. struct symbol *p = lemp->symbols[i];
  4664. if( p->fallback==0 ){
  4665. fprintf(out, " 0, /* %10s => nothing */\n", p->name);
  4666. }else{
  4667. fprintf(out, " %3d, /* %10s => %s */\n", p->fallback->index,
  4668. p->name, p->fallback->name);
  4669. }
  4670. lineno++;
  4671. }
  4672. }
  4673. tplt_xfer(lemp->name, in, out, &lineno);
  4674. /* Generate a table containing the symbolic name of every symbol
  4675. */
  4676. for(i=0; i<lemp->nsymbol; i++){
  4677. fprintf(out," /* %4d */ \"%s\",\n",i, lemp->symbols[i]->name); lineno++;
  4678. }
  4679. tplt_xfer(lemp->name,in,out,&lineno);
  4680. /* Generate a table containing a text string that describes every
  4681. ** rule in the rule set of the grammar. This information is used
  4682. ** when tracing REDUCE actions.
  4683. */
  4684. for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
  4685. assert( rp->iRule==i );
  4686. fprintf(out," /* %3d */ \"", i);
  4687. writeRuleText(out, rp);
  4688. fprintf(out,"\",\n"); lineno++;
  4689. }
  4690. tplt_xfer(lemp->name,in,out,&lineno);
  4691. /* Generate code which executes every time a symbol is popped from
  4692. ** the stack while processing errors or while destroying the parser.
  4693. ** (In other words, generate the %destructor actions)
  4694. */
  4695. if( lemp->tokendest ){
  4696. int once = 1;
  4697. for(i=0; i<lemp->nsymbol; i++){
  4698. struct symbol *sp = lemp->symbols[i];
  4699. if( sp==0 || sp->type!=TERMINAL ) continue;
  4700. if( once ){
  4701. fprintf(out, " /* TERMINAL Destructor */\n"); lineno++;
  4702. once = 0;
  4703. }
  4704. fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
  4705. }
  4706. for(i=0; i<lemp->nsymbol && lemp->symbols[i]->type!=TERMINAL; i++);
  4707. if( i<lemp->nsymbol ){
  4708. emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
  4709. fprintf(out," break;\n"); lineno++;
  4710. }
  4711. }
  4712. if( lemp->vardest ){
  4713. struct symbol *dflt_sp = 0;
  4714. int once = 1;
  4715. for(i=0; i<lemp->nsymbol; i++){
  4716. struct symbol *sp = lemp->symbols[i];
  4717. if( sp==0 || sp->type==TERMINAL ||
  4718. sp->index<=0 || sp->destructor!=0 ) continue;
  4719. if( once ){
  4720. fprintf(out, " /* Default NON-TERMINAL Destructor */\n");lineno++;
  4721. once = 0;
  4722. }
  4723. fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
  4724. dflt_sp = sp;
  4725. }
  4726. if( dflt_sp!=0 ){
  4727. emit_destructor_code(out,dflt_sp,lemp,&lineno);
  4728. }
  4729. fprintf(out," break;\n"); lineno++;
  4730. }
  4731. for(i=0; i<lemp->nsymbol; i++){
  4732. struct symbol *sp = lemp->symbols[i];
  4733. if( sp==0 || sp->type==TERMINAL || sp->destructor==0 ) continue;
  4734. if( sp->destLineno<0 ) continue; /* Already emitted */
  4735. fprintf(out," case %d: /* %s */\n", sp->index, sp->name); lineno++;
  4736. /* Combine duplicate destructors into a single case */
  4737. for(j=i+1; j<lemp->nsymbol; j++){
  4738. struct symbol *sp2 = lemp->symbols[j];
  4739. if( sp2 && sp2->type!=TERMINAL && sp2->destructor
  4740. && sp2->dtnum==sp->dtnum
  4741. && strcmp(sp->destructor,sp2->destructor)==0 ){
  4742. fprintf(out," case %d: /* %s */\n",
  4743. sp2->index, sp2->name); lineno++;
  4744. sp2->destLineno = -1; /* Avoid emitting this destructor again */
  4745. }
  4746. }
  4747. emit_destructor_code(out,lemp->symbols[i],lemp,&lineno);
  4748. fprintf(out," break;\n"); lineno++;
  4749. }
  4750. tplt_xfer(lemp->name,in,out,&lineno);
  4751. /* Generate code which executes whenever the parser stack overflows */
  4752. tplt_print(out,lemp,lemp->overflow,&lineno);
  4753. tplt_xfer(lemp->name,in,out,&lineno);
  4754. /* Generate the tables of rule information. yyRuleInfoLhs[] and
  4755. ** yyRuleInfoNRhs[].
  4756. **
  4757. ** Note: This code depends on the fact that rules are number
  4758. ** sequentially beginning with 0.
  4759. */
  4760. for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
  4761. fprintf(out," %4d, /* (%d) ", rp->lhs->index, i);
  4762. rule_print(out, rp);
  4763. fprintf(out," */\n"); lineno++;
  4764. }
  4765. tplt_xfer(lemp->name,in,out,&lineno);
  4766. for(i=0, rp=lemp->rule; rp; rp=rp->next, i++){
  4767. fprintf(out," %3d, /* (%d) ", -rp->nrhs, i);
  4768. rule_print(out, rp);
  4769. fprintf(out," */\n"); lineno++;
  4770. }
  4771. tplt_xfer(lemp->name,in,out,&lineno);
  4772. /* Generate code which execution during each REDUCE action */
  4773. i = 0;
  4774. for(rp=lemp->rule; rp; rp=rp->next){
  4775. i += translate_code(lemp, rp);
  4776. }
  4777. if( i ){
  4778. fprintf(out," YYMINORTYPE yylhsminor;\n"); lineno++;
  4779. }
  4780. /* First output rules other than the default: rule */
  4781. for(rp=lemp->rule; rp; rp=rp->next){
  4782. struct rule *rp2; /* Other rules with the same action */
  4783. if( rp->codeEmitted ) continue;
  4784. if( rp->noCode ){
  4785. /* No C code actions, so this will be part of the "default:" rule */
  4786. continue;
  4787. }
  4788. fprintf(out," case %d: /* ", rp->iRule);
  4789. writeRuleText(out, rp);
  4790. fprintf(out, " */\n"); lineno++;
  4791. for(rp2=rp->next; rp2; rp2=rp2->next){
  4792. if( rp2->code==rp->code && rp2->codePrefix==rp->codePrefix
  4793. && rp2->codeSuffix==rp->codeSuffix ){
  4794. fprintf(out," case %d: /* ", rp2->iRule);
  4795. writeRuleText(out, rp2);
  4796. fprintf(out," */ yytestcase(yyruleno==%d);\n", rp2->iRule); lineno++;
  4797. rp2->codeEmitted = 1;
  4798. }
  4799. }
  4800. emit_code(out,rp,lemp,&lineno);
  4801. fprintf(out," break;\n"); lineno++;
  4802. rp->codeEmitted = 1;
  4803. }
  4804. /* Finally, output the default: rule. We choose as the default: all
  4805. ** empty actions. */
  4806. fprintf(out," default:\n"); lineno++;
  4807. for(rp=lemp->rule; rp; rp=rp->next){
  4808. if( rp->codeEmitted ) continue;
  4809. assert( rp->noCode );
  4810. fprintf(out," /* (%d) ", rp->iRule);
  4811. writeRuleText(out, rp);
  4812. if( rp->neverReduce ){
  4813. fprintf(out, " (NEVER REDUCES) */ assert(yyruleno!=%d);\n",
  4814. rp->iRule); lineno++;
  4815. }else if( rp->doesReduce ){
  4816. fprintf(out, " */ yytestcase(yyruleno==%d);\n", rp->iRule); lineno++;
  4817. }else{
  4818. fprintf(out, " (OPTIMIZED OUT) */ assert(yyruleno!=%d);\n",
  4819. rp->iRule); lineno++;
  4820. }
  4821. }
  4822. fprintf(out," break;\n"); lineno++;
  4823. tplt_xfer(lemp->name,in,out,&lineno);
  4824. /* Generate code which executes if a parse fails */
  4825. tplt_print(out,lemp,lemp->failure,&lineno);
  4826. tplt_xfer(lemp->name,in,out,&lineno);
  4827. /* Generate code which executes when a syntax error occurs */
  4828. tplt_print(out,lemp,lemp->error,&lineno);
  4829. tplt_xfer(lemp->name,in,out,&lineno);
  4830. /* Generate code which executes when the parser accepts its input */
  4831. tplt_print(out,lemp,lemp->accept,&lineno);
  4832. tplt_xfer(lemp->name,in,out,&lineno);
  4833. /* Append any addition code the user desires */
  4834. tplt_print(out,lemp,lemp->extracode,&lineno);
  4835. acttab_free(pActtab);
  4836. fclose(in);
  4837. fclose(out);
  4838. if( sql ) fclose(sql);
  4839. return;
  4840. }
  4841. /* Generate a header file for the parser */
  4842. void ReportHeader(struct lemon *lemp)
  4843. {
  4844. FILE *out, *in;
  4845. const char *prefix;
  4846. char line[LINESIZE];
  4847. char pattern[LINESIZE];
  4848. int i;
  4849. if( lemp->tokenprefix ) prefix = lemp->tokenprefix;
  4850. else prefix = "";
  4851. in = file_open(lemp,".h","rb");
  4852. if( in ){
  4853. int nextChar;
  4854. for(i=1; i<lemp->nterminal && fgets(line,LINESIZE,in); i++){
  4855. lemon_sprintf(pattern,"#define %s%-30s %3d\n",
  4856. prefix,lemp->symbols[i]->name,i);
  4857. if( strcmp(line,pattern) ) break;
  4858. }
  4859. nextChar = fgetc(in);
  4860. fclose(in);
  4861. if( i==lemp->nterminal && nextChar==EOF ){
  4862. /* No change in the file. Don't rewrite it. */
  4863. return;
  4864. }
  4865. }
  4866. out = file_open(lemp,".h","wb");
  4867. if( out ){
  4868. for(i=1; i<lemp->nterminal; i++){
  4869. fprintf(out,"#define %s%-30s %3d\n",prefix,lemp->symbols[i]->name,i);
  4870. }
  4871. fclose(out);
  4872. }
  4873. return;
  4874. }
  4875. /* Reduce the size of the action tables, if possible, by making use
  4876. ** of defaults.
  4877. **
  4878. ** In this version, we take the most frequent REDUCE action and make
  4879. ** it the default. Except, there is no default if the wildcard token
  4880. ** is a possible look-ahead.
  4881. */
  4882. void CompressTables(struct lemon *lemp)
  4883. {
  4884. struct state *stp;
  4885. struct action *ap, *ap2, *nextap;
  4886. struct rule *rp, *rp2, *rbest;
  4887. int nbest, n;
  4888. int i;
  4889. int usesWildcard;
  4890. for(i=0; i<lemp->nstate; i++){
  4891. stp = lemp->sorted[i];
  4892. nbest = 0;
  4893. rbest = 0;
  4894. usesWildcard = 0;
  4895. for(ap=stp->ap; ap; ap=ap->next){
  4896. if( ap->type==SHIFT && ap->sp==lemp->wildcard ){
  4897. usesWildcard = 1;
  4898. }
  4899. if( ap->type!=REDUCE ) continue;
  4900. rp = ap->x.rp;
  4901. if( rp->lhsStart ) continue;
  4902. if( rp==rbest ) continue;
  4903. n = 1;
  4904. for(ap2=ap->next; ap2; ap2=ap2->next){
  4905. if( ap2->type!=REDUCE ) continue;
  4906. rp2 = ap2->x.rp;
  4907. if( rp2==rbest ) continue;
  4908. if( rp2==rp ) n++;
  4909. }
  4910. if( n>nbest ){
  4911. nbest = n;
  4912. rbest = rp;
  4913. }
  4914. }
  4915. /* Do not make a default if the number of rules to default
  4916. ** is not at least 1 or if the wildcard token is a possible
  4917. ** lookahead.
  4918. */
  4919. if( nbest<1 || usesWildcard ) continue;
  4920. /* Combine matching REDUCE actions into a single default */
  4921. for(ap=stp->ap; ap; ap=ap->next){
  4922. if( ap->type==REDUCE && ap->x.rp==rbest ) break;
  4923. }
  4924. assert( ap );
  4925. ap->sp = Symbol_new("{default}");
  4926. for(ap=ap->next; ap; ap=ap->next){
  4927. if( ap->type==REDUCE && ap->x.rp==rbest ) ap->type = NOT_USED;
  4928. }
  4929. stp->ap = Action_sort(stp->ap);
  4930. for(ap=stp->ap; ap; ap=ap->next){
  4931. if( ap->type==SHIFT ) break;
  4932. if( ap->type==REDUCE && ap->x.rp!=rbest ) break;
  4933. }
  4934. if( ap==0 ){
  4935. stp->autoReduce = 1;
  4936. stp->pDfltReduce = rbest;
  4937. }
  4938. }
  4939. /* Make a second pass over all states and actions. Convert
  4940. ** every action that is a SHIFT to an autoReduce state into
  4941. ** a SHIFTREDUCE action.
  4942. */
  4943. for(i=0; i<lemp->nstate; i++){
  4944. stp = lemp->sorted[i];
  4945. for(ap=stp->ap; ap; ap=ap->next){
  4946. struct state *pNextState;
  4947. if( ap->type!=SHIFT ) continue;
  4948. pNextState = ap->x.stp;
  4949. if( pNextState->autoReduce && pNextState->pDfltReduce!=0 ){
  4950. ap->type = SHIFTREDUCE;
  4951. ap->x.rp = pNextState->pDfltReduce;
  4952. }
  4953. }
  4954. }
  4955. /* If a SHIFTREDUCE action specifies a rule that has a single RHS term
  4956. ** (meaning that the SHIFTREDUCE will land back in the state where it
  4957. ** started) and if there is no C-code associated with the reduce action,
  4958. ** then we can go ahead and convert the action to be the same as the
  4959. ** action for the RHS of the rule.
  4960. */
  4961. for(i=0; i<lemp->nstate; i++){
  4962. stp = lemp->sorted[i];
  4963. for(ap=stp->ap; ap; ap=nextap){
  4964. nextap = ap->next;
  4965. if( ap->type!=SHIFTREDUCE ) continue;
  4966. rp = ap->x.rp;
  4967. if( rp->noCode==0 ) continue;
  4968. if( rp->nrhs!=1 ) continue;
  4969. #if 1
  4970. /* Only apply this optimization to non-terminals. It would be OK to
  4971. ** apply it to terminal symbols too, but that makes the parser tables
  4972. ** larger. */
  4973. if( ap->sp->index<lemp->nterminal ) continue;
  4974. #endif
  4975. /* If we reach this point, it means the optimization can be applied */
  4976. nextap = ap;
  4977. for(ap2=stp->ap; ap2 && (ap2==ap || ap2->sp!=rp->lhs); ap2=ap2->next){}
  4978. assert( ap2!=0 );
  4979. ap->spOpt = ap2->sp;
  4980. ap->type = ap2->type;
  4981. ap->x = ap2->x;
  4982. }
  4983. }
  4984. }
  4985. /*
  4986. ** Compare two states for sorting purposes. The smaller state is the
  4987. ** one with the most non-terminal actions. If they have the same number
  4988. ** of non-terminal actions, then the smaller is the one with the most
  4989. ** token actions.
  4990. */
  4991. static int stateResortCompare(const void *a, const void *b){
  4992. const struct state *pA = *(const struct state**)a;
  4993. const struct state *pB = *(const struct state**)b;
  4994. int n;
  4995. n = pB->nNtAct - pA->nNtAct;
  4996. if( n==0 ){
  4997. n = pB->nTknAct - pA->nTknAct;
  4998. if( n==0 ){
  4999. n = pB->statenum - pA->statenum;
  5000. }
  5001. }
  5002. assert( n!=0 );
  5003. return n;
  5004. }
  5005. /*
  5006. ** Renumber and resort states so that states with fewer choices
  5007. ** occur at the end. Except, keep state 0 as the first state.
  5008. */
  5009. void ResortStates(struct lemon *lemp)
  5010. {
  5011. int i;
  5012. struct state *stp;
  5013. struct action *ap;
  5014. for(i=0; i<lemp->nstate; i++){
  5015. stp = lemp->sorted[i];
  5016. stp->nTknAct = stp->nNtAct = 0;
  5017. stp->iDfltReduce = -1; /* Init dflt action to "syntax error" */
  5018. stp->iTknOfst = NO_OFFSET;
  5019. stp->iNtOfst = NO_OFFSET;
  5020. for(ap=stp->ap; ap; ap=ap->next){
  5021. int iAction = compute_action(lemp,ap);
  5022. if( iAction>=0 ){
  5023. if( ap->sp->index<lemp->nterminal ){
  5024. stp->nTknAct++;
  5025. }else if( ap->sp->index<lemp->nsymbol ){
  5026. stp->nNtAct++;
  5027. }else{
  5028. assert( stp->autoReduce==0 || stp->pDfltReduce==ap->x.rp );
  5029. stp->iDfltReduce = iAction;
  5030. }
  5031. }
  5032. }
  5033. }
  5034. qsort(&lemp->sorted[1], lemp->nstate-1, sizeof(lemp->sorted[0]),
  5035. stateResortCompare);
  5036. for(i=0; i<lemp->nstate; i++){
  5037. lemp->sorted[i]->statenum = i;
  5038. }
  5039. lemp->nxstate = lemp->nstate;
  5040. while( lemp->nxstate>1 && lemp->sorted[lemp->nxstate-1]->autoReduce ){
  5041. lemp->nxstate--;
  5042. }
  5043. }
  5044. /***************** From the file "set.c" ************************************/
  5045. /*
  5046. ** Set manipulation routines for the LEMON parser generator.
  5047. */
  5048. static int size = 0;
  5049. /* Set the set size */
  5050. void SetSize(int n)
  5051. {
  5052. size = n+1;
  5053. }
  5054. /* Allocate a new set */
  5055. char *SetNew(void){
  5056. char *s;
  5057. s = (char*)lemon_calloc( size, 1);
  5058. if( s==0 ){
  5059. memory_error();
  5060. }
  5061. return s;
  5062. }
  5063. /* Deallocate a set */
  5064. void SetFree(char *s)
  5065. {
  5066. lemon_free(s);
  5067. }
  5068. /* Add a new element to the set. Return TRUE if the element was added
  5069. ** and FALSE if it was already there. */
  5070. int SetAdd(char *s, int e)
  5071. {
  5072. int rv;
  5073. assert( e>=0 && e<size );
  5074. rv = s[e];
  5075. s[e] = 1;
  5076. return !rv;
  5077. }
  5078. /* Add every element of s2 to s1. Return TRUE if s1 changes. */
  5079. int SetUnion(char *s1, char *s2)
  5080. {
  5081. int i, progress;
  5082. progress = 0;
  5083. for(i=0; i<size; i++){
  5084. if( s2[i]==0 ) continue;
  5085. if( s1[i]==0 ){
  5086. progress = 1;
  5087. s1[i] = 1;
  5088. }
  5089. }
  5090. return progress;
  5091. }
  5092. /********************** From the file "table.c" ****************************/
  5093. /*
  5094. ** All code in this file has been automatically generated
  5095. ** from a specification in the file
  5096. ** "table.q"
  5097. ** by the associative array code building program "aagen".
  5098. ** Do not edit this file! Instead, edit the specification
  5099. ** file, then rerun aagen.
  5100. */
  5101. /*
  5102. ** Code for processing tables in the LEMON parser generator.
  5103. */
  5104. PRIVATE unsigned strhash(const char *x)
  5105. {
  5106. unsigned h = 0;
  5107. while( *x ) h = h*13 + *(x++);
  5108. return h;
  5109. }
  5110. /* Works like strdup, sort of. Save a string in malloced memory, but
  5111. ** keep strings in a table so that the same string is not in more
  5112. ** than one place.
  5113. */
  5114. const char *Strsafe(const char *y)
  5115. {
  5116. const char *z;
  5117. char *cpy;
  5118. if( y==0 ) return 0;
  5119. z = Strsafe_find(y);
  5120. if( z==0 && (cpy=(char *)lemon_malloc( lemonStrlen(y)+1 ))!=0 ){
  5121. lemon_strcpy(cpy,y);
  5122. z = cpy;
  5123. Strsafe_insert(z);
  5124. }
  5125. MemoryCheck(z);
  5126. return z;
  5127. }
  5128. /* There is one instance of the following structure for each
  5129. ** associative array of type "x1".
  5130. */
  5131. struct s_x1 {
  5132. int size; /* The number of available slots. */
  5133. /* Must be a power of 2 greater than or */
  5134. /* equal to 1 */
  5135. int count; /* Number of currently slots filled */
  5136. struct s_x1node *tbl; /* The data stored here */
  5137. struct s_x1node **ht; /* Hash table for lookups */
  5138. };
  5139. /* There is one instance of this structure for every data element
  5140. ** in an associative array of type "x1".
  5141. */
  5142. typedef struct s_x1node {
  5143. const char *data; /* The data */
  5144. struct s_x1node *next; /* Next entry with the same hash */
  5145. struct s_x1node **from; /* Previous link */
  5146. } x1node;
  5147. /* There is only one instance of the array, which is the following */
  5148. static struct s_x1 *x1a;
  5149. /* Allocate a new associative array */
  5150. void Strsafe_init(void){
  5151. if( x1a ) return;
  5152. x1a = (struct s_x1*)lemon_malloc( sizeof(struct s_x1) );
  5153. if( x1a ){
  5154. x1a->size = 1024;
  5155. x1a->count = 0;
  5156. x1a->tbl = (x1node*)lemon_calloc(1024, sizeof(x1node) + sizeof(x1node*));
  5157. if( x1a->tbl==0 ){
  5158. lemon_free(x1a);
  5159. x1a = 0;
  5160. }else{
  5161. int i;
  5162. x1a->ht = (x1node**)&(x1a->tbl[1024]);
  5163. for(i=0; i<1024; i++) x1a->ht[i] = 0;
  5164. }
  5165. }
  5166. }
  5167. /* Insert a new record into the array. Return TRUE if successful.
  5168. ** Prior data with the same key is NOT overwritten */
  5169. int Strsafe_insert(const char *data)
  5170. {
  5171. x1node *np;
  5172. unsigned h;
  5173. unsigned ph;
  5174. if( x1a==0 ) return 0;
  5175. ph = strhash(data);
  5176. h = ph & (x1a->size-1);
  5177. np = x1a->ht[h];
  5178. while( np ){
  5179. if( strcmp(np->data,data)==0 ){
  5180. /* An existing entry with the same key is found. */
  5181. /* Fail because overwrite is not allows. */
  5182. return 0;
  5183. }
  5184. np = np->next;
  5185. }
  5186. if( x1a->count>=x1a->size ){
  5187. /* Need to make the hash table bigger */
  5188. int i,arrSize;
  5189. struct s_x1 array;
  5190. array.size = arrSize = x1a->size*2;
  5191. array.count = x1a->count;
  5192. array.tbl = (x1node*)lemon_calloc(arrSize, sizeof(x1node)+sizeof(x1node*));
  5193. if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
  5194. array.ht = (x1node**)&(array.tbl[arrSize]);
  5195. for(i=0; i<arrSize; i++) array.ht[i] = 0;
  5196. for(i=0; i<x1a->count; i++){
  5197. x1node *oldnp, *newnp;
  5198. oldnp = &(x1a->tbl[i]);
  5199. h = strhash(oldnp->data) & (arrSize-1);
  5200. newnp = &(array.tbl[i]);
  5201. if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
  5202. newnp->next = array.ht[h];
  5203. newnp->data = oldnp->data;
  5204. newnp->from = &(array.ht[h]);
  5205. array.ht[h] = newnp;
  5206. }
  5207. /* lemon_free(x1a->tbl); // This program was originally for 16-bit machines.
  5208. ** Don't worry about freeing memory on modern platforms. */
  5209. *x1a = array;
  5210. }
  5211. /* Insert the new data */
  5212. h = ph & (x1a->size-1);
  5213. np = &(x1a->tbl[x1a->count++]);
  5214. np->data = data;
  5215. if( x1a->ht[h] ) x1a->ht[h]->from = &(np->next);
  5216. np->next = x1a->ht[h];
  5217. x1a->ht[h] = np;
  5218. np->from = &(x1a->ht[h]);
  5219. return 1;
  5220. }
  5221. /* Return a pointer to data assigned to the given key. Return NULL
  5222. ** if no such key. */
  5223. const char *Strsafe_find(const char *key)
  5224. {
  5225. unsigned h;
  5226. x1node *np;
  5227. if( x1a==0 ) return 0;
  5228. h = strhash(key) & (x1a->size-1);
  5229. np = x1a->ht[h];
  5230. while( np ){
  5231. if( strcmp(np->data,key)==0 ) break;
  5232. np = np->next;
  5233. }
  5234. return np ? np->data : 0;
  5235. }
  5236. /* Return a pointer to the (terminal or nonterminal) symbol "x".
  5237. ** Create a new symbol if this is the first time "x" has been seen.
  5238. */
  5239. struct symbol *Symbol_new(const char *x)
  5240. {
  5241. struct symbol *sp;
  5242. sp = Symbol_find(x);
  5243. if( sp==0 ){
  5244. sp = (struct symbol *)lemon_calloc(1, sizeof(struct symbol) );
  5245. MemoryCheck(sp);
  5246. sp->name = Strsafe(x);
  5247. sp->type = ISUPPER(*x) ? TERMINAL : NONTERMINAL;
  5248. sp->rule = 0;
  5249. sp->fallback = 0;
  5250. sp->prec = -1;
  5251. sp->assoc = UNK;
  5252. sp->firstset = 0;
  5253. sp->lambda = LEMON_FALSE;
  5254. sp->destructor = 0;
  5255. sp->destLineno = 0;
  5256. sp->datatype = 0;
  5257. sp->useCnt = 0;
  5258. Symbol_insert(sp,sp->name);
  5259. }
  5260. sp->useCnt++;
  5261. return sp;
  5262. }
  5263. /* Compare two symbols for sorting purposes. Return negative,
  5264. ** zero, or positive if a is less then, equal to, or greater
  5265. ** than b.
  5266. **
  5267. ** Symbols that begin with upper case letters (terminals or tokens)
  5268. ** must sort before symbols that begin with lower case letters
  5269. ** (non-terminals). And MULTITERMINAL symbols (created using the
  5270. ** %token_class directive) must sort at the very end. Other than
  5271. ** that, the order does not matter.
  5272. **
  5273. ** We find experimentally that leaving the symbols in their original
  5274. ** order (the order they appeared in the grammar file) gives the
  5275. ** smallest parser tables in SQLite.
  5276. */
  5277. int Symbolcmpp(const void *_a, const void *_b)
  5278. {
  5279. const struct symbol *a = *(const struct symbol **) _a;
  5280. const struct symbol *b = *(const struct symbol **) _b;
  5281. int i1 = a->type==MULTITERMINAL ? 3 : a->name[0]>'Z' ? 2 : 1;
  5282. int i2 = b->type==MULTITERMINAL ? 3 : b->name[0]>'Z' ? 2 : 1;
  5283. return i1==i2 ? a->index - b->index : i1 - i2;
  5284. }
  5285. /* There is one instance of the following structure for each
  5286. ** associative array of type "x2".
  5287. */
  5288. struct s_x2 {
  5289. int size; /* The number of available slots. */
  5290. /* Must be a power of 2 greater than or */
  5291. /* equal to 1 */
  5292. int count; /* Number of currently slots filled */
  5293. struct s_x2node *tbl; /* The data stored here */
  5294. struct s_x2node **ht; /* Hash table for lookups */
  5295. };
  5296. /* There is one instance of this structure for every data element
  5297. ** in an associative array of type "x2".
  5298. */
  5299. typedef struct s_x2node {
  5300. struct symbol *data; /* The data */
  5301. const char *key; /* The key */
  5302. struct s_x2node *next; /* Next entry with the same hash */
  5303. struct s_x2node **from; /* Previous link */
  5304. } x2node;
  5305. /* There is only one instance of the array, which is the following */
  5306. static struct s_x2 *x2a;
  5307. /* Allocate a new associative array */
  5308. void Symbol_init(void){
  5309. if( x2a ) return;
  5310. x2a = (struct s_x2*)lemon_malloc( sizeof(struct s_x2) );
  5311. if( x2a ){
  5312. x2a->size = 128;
  5313. x2a->count = 0;
  5314. x2a->tbl = (x2node*)lemon_calloc(128, sizeof(x2node) + sizeof(x2node*));
  5315. if( x2a->tbl==0 ){
  5316. lemon_free(x2a);
  5317. x2a = 0;
  5318. }else{
  5319. int i;
  5320. x2a->ht = (x2node**)&(x2a->tbl[128]);
  5321. for(i=0; i<128; i++) x2a->ht[i] = 0;
  5322. }
  5323. }
  5324. }
  5325. /* Insert a new record into the array. Return TRUE if successful.
  5326. ** Prior data with the same key is NOT overwritten */
  5327. int Symbol_insert(struct symbol *data, const char *key)
  5328. {
  5329. x2node *np;
  5330. unsigned h;
  5331. unsigned ph;
  5332. if( x2a==0 ) return 0;
  5333. ph = strhash(key);
  5334. h = ph & (x2a->size-1);
  5335. np = x2a->ht[h];
  5336. while( np ){
  5337. if( strcmp(np->key,key)==0 ){
  5338. /* An existing entry with the same key is found. */
  5339. /* Fail because overwrite is not allows. */
  5340. return 0;
  5341. }
  5342. np = np->next;
  5343. }
  5344. if( x2a->count>=x2a->size ){
  5345. /* Need to make the hash table bigger */
  5346. int i,arrSize;
  5347. struct s_x2 array;
  5348. array.size = arrSize = x2a->size*2;
  5349. array.count = x2a->count;
  5350. array.tbl = (x2node*)lemon_calloc(arrSize, sizeof(x2node)+sizeof(x2node*));
  5351. if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
  5352. array.ht = (x2node**)&(array.tbl[arrSize]);
  5353. for(i=0; i<arrSize; i++) array.ht[i] = 0;
  5354. for(i=0; i<x2a->count; i++){
  5355. x2node *oldnp, *newnp;
  5356. oldnp = &(x2a->tbl[i]);
  5357. h = strhash(oldnp->key) & (arrSize-1);
  5358. newnp = &(array.tbl[i]);
  5359. if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
  5360. newnp->next = array.ht[h];
  5361. newnp->key = oldnp->key;
  5362. newnp->data = oldnp->data;
  5363. newnp->from = &(array.ht[h]);
  5364. array.ht[h] = newnp;
  5365. }
  5366. /* lemon_free(x2a->tbl); // This program was originally written for 16-bit
  5367. ** machines. Don't worry about freeing this trivial amount of memory
  5368. ** on modern platforms. Just leak it. */
  5369. *x2a = array;
  5370. }
  5371. /* Insert the new data */
  5372. h = ph & (x2a->size-1);
  5373. np = &(x2a->tbl[x2a->count++]);
  5374. np->key = key;
  5375. np->data = data;
  5376. if( x2a->ht[h] ) x2a->ht[h]->from = &(np->next);
  5377. np->next = x2a->ht[h];
  5378. x2a->ht[h] = np;
  5379. np->from = &(x2a->ht[h]);
  5380. return 1;
  5381. }
  5382. /* Return a pointer to data assigned to the given key. Return NULL
  5383. ** if no such key. */
  5384. struct symbol *Symbol_find(const char *key)
  5385. {
  5386. unsigned h;
  5387. x2node *np;
  5388. if( x2a==0 ) return 0;
  5389. h = strhash(key) & (x2a->size-1);
  5390. np = x2a->ht[h];
  5391. while( np ){
  5392. if( strcmp(np->key,key)==0 ) break;
  5393. np = np->next;
  5394. }
  5395. return np ? np->data : 0;
  5396. }
  5397. /* Return the n-th data. Return NULL if n is out of range. */
  5398. struct symbol *Symbol_Nth(int n)
  5399. {
  5400. struct symbol *data;
  5401. if( x2a && n>0 && n<=x2a->count ){
  5402. data = x2a->tbl[n-1].data;
  5403. }else{
  5404. data = 0;
  5405. }
  5406. return data;
  5407. }
  5408. /* Return the size of the array */
  5409. int Symbol_count()
  5410. {
  5411. return x2a ? x2a->count : 0;
  5412. }
  5413. /* Return an array of pointers to all data in the table.
  5414. ** The array is obtained from malloc. Return NULL if memory allocation
  5415. ** problems, or if the array is empty. */
  5416. struct symbol **Symbol_arrayof()
  5417. {
  5418. struct symbol **array;
  5419. int i,arrSize;
  5420. if( x2a==0 ) return 0;
  5421. arrSize = x2a->count;
  5422. array = (struct symbol **)lemon_calloc(arrSize, sizeof(struct symbol *));
  5423. if( array ){
  5424. for(i=0; i<arrSize; i++) array[i] = x2a->tbl[i].data;
  5425. }
  5426. return array;
  5427. }
  5428. /* Compare two configurations */
  5429. int Configcmp(const char *_a,const char *_b)
  5430. {
  5431. const struct config *a = (struct config *) _a;
  5432. const struct config *b = (struct config *) _b;
  5433. int x;
  5434. x = a->rp->index - b->rp->index;
  5435. if( x==0 ) x = a->dot - b->dot;
  5436. return x;
  5437. }
  5438. /* Compare two states */
  5439. PRIVATE int statecmp(struct config *a, struct config *b)
  5440. {
  5441. int rc;
  5442. for(rc=0; rc==0 && a && b; a=a->bp, b=b->bp){
  5443. rc = a->rp->index - b->rp->index;
  5444. if( rc==0 ) rc = a->dot - b->dot;
  5445. }
  5446. if( rc==0 ){
  5447. if( a ) rc = 1;
  5448. if( b ) rc = -1;
  5449. }
  5450. return rc;
  5451. }
  5452. /* Hash a state */
  5453. PRIVATE unsigned statehash(struct config *a)
  5454. {
  5455. unsigned h=0;
  5456. while( a ){
  5457. h = h*571 + a->rp->index*37 + a->dot;
  5458. a = a->bp;
  5459. }
  5460. return h;
  5461. }
  5462. /* Allocate a new state structure */
  5463. struct state *State_new()
  5464. {
  5465. struct state *newstate;
  5466. newstate = (struct state *)lemon_calloc(1, sizeof(struct state) );
  5467. MemoryCheck(newstate);
  5468. return newstate;
  5469. }
  5470. /* There is one instance of the following structure for each
  5471. ** associative array of type "x3".
  5472. */
  5473. struct s_x3 {
  5474. int size; /* The number of available slots. */
  5475. /* Must be a power of 2 greater than or */
  5476. /* equal to 1 */
  5477. int count; /* Number of currently slots filled */
  5478. struct s_x3node *tbl; /* The data stored here */
  5479. struct s_x3node **ht; /* Hash table for lookups */
  5480. };
  5481. /* There is one instance of this structure for every data element
  5482. ** in an associative array of type "x3".
  5483. */
  5484. typedef struct s_x3node {
  5485. struct state *data; /* The data */
  5486. struct config *key; /* The key */
  5487. struct s_x3node *next; /* Next entry with the same hash */
  5488. struct s_x3node **from; /* Previous link */
  5489. } x3node;
  5490. /* There is only one instance of the array, which is the following */
  5491. static struct s_x3 *x3a;
  5492. /* Allocate a new associative array */
  5493. void State_init(void){
  5494. if( x3a ) return;
  5495. x3a = (struct s_x3*)lemon_malloc( sizeof(struct s_x3) );
  5496. if( x3a ){
  5497. x3a->size = 128;
  5498. x3a->count = 0;
  5499. x3a->tbl = (x3node*)lemon_calloc(128, sizeof(x3node) + sizeof(x3node*));
  5500. if( x3a->tbl==0 ){
  5501. lemon_free(x3a);
  5502. x3a = 0;
  5503. }else{
  5504. int i;
  5505. x3a->ht = (x3node**)&(x3a->tbl[128]);
  5506. for(i=0; i<128; i++) x3a->ht[i] = 0;
  5507. }
  5508. }
  5509. }
  5510. /* Insert a new record into the array. Return TRUE if successful.
  5511. ** Prior data with the same key is NOT overwritten */
  5512. int State_insert(struct state *data, struct config *key)
  5513. {
  5514. x3node *np;
  5515. unsigned h;
  5516. unsigned ph;
  5517. if( x3a==0 ) return 0;
  5518. ph = statehash(key);
  5519. h = ph & (x3a->size-1);
  5520. np = x3a->ht[h];
  5521. while( np ){
  5522. if( statecmp(np->key,key)==0 ){
  5523. /* An existing entry with the same key is found. */
  5524. /* Fail because overwrite is not allows. */
  5525. return 0;
  5526. }
  5527. np = np->next;
  5528. }
  5529. if( x3a->count>=x3a->size ){
  5530. /* Need to make the hash table bigger */
  5531. int i,arrSize;
  5532. struct s_x3 array;
  5533. array.size = arrSize = x3a->size*2;
  5534. array.count = x3a->count;
  5535. array.tbl = (x3node*)lemon_calloc(arrSize, sizeof(x3node)+sizeof(x3node*));
  5536. if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
  5537. array.ht = (x3node**)&(array.tbl[arrSize]);
  5538. for(i=0; i<arrSize; i++) array.ht[i] = 0;
  5539. for(i=0; i<x3a->count; i++){
  5540. x3node *oldnp, *newnp;
  5541. oldnp = &(x3a->tbl[i]);
  5542. h = statehash(oldnp->key) & (arrSize-1);
  5543. newnp = &(array.tbl[i]);
  5544. if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
  5545. newnp->next = array.ht[h];
  5546. newnp->key = oldnp->key;
  5547. newnp->data = oldnp->data;
  5548. newnp->from = &(array.ht[h]);
  5549. array.ht[h] = newnp;
  5550. }
  5551. lemon_free(x3a->tbl);
  5552. *x3a = array;
  5553. }
  5554. /* Insert the new data */
  5555. h = ph & (x3a->size-1);
  5556. np = &(x3a->tbl[x3a->count++]);
  5557. np->key = key;
  5558. np->data = data;
  5559. if( x3a->ht[h] ) x3a->ht[h]->from = &(np->next);
  5560. np->next = x3a->ht[h];
  5561. x3a->ht[h] = np;
  5562. np->from = &(x3a->ht[h]);
  5563. return 1;
  5564. }
  5565. /* Return a pointer to data assigned to the given key. Return NULL
  5566. ** if no such key. */
  5567. struct state *State_find(struct config *key)
  5568. {
  5569. unsigned h;
  5570. x3node *np;
  5571. if( x3a==0 ) return 0;
  5572. h = statehash(key) & (x3a->size-1);
  5573. np = x3a->ht[h];
  5574. while( np ){
  5575. if( statecmp(np->key,key)==0 ) break;
  5576. np = np->next;
  5577. }
  5578. return np ? np->data : 0;
  5579. }
  5580. /* Return an array of pointers to all data in the table.
  5581. ** The array is obtained from malloc. Return NULL if memory allocation
  5582. ** problems, or if the array is empty. */
  5583. struct state **State_arrayof(void)
  5584. {
  5585. struct state **array;
  5586. int i,arrSize;
  5587. if( x3a==0 ) return 0;
  5588. arrSize = x3a->count;
  5589. array = (struct state **)lemon_calloc(arrSize, sizeof(struct state *));
  5590. if( array ){
  5591. for(i=0; i<arrSize; i++) array[i] = x3a->tbl[i].data;
  5592. }
  5593. return array;
  5594. }
  5595. /* Hash a configuration */
  5596. PRIVATE unsigned confighash(struct config *a)
  5597. {
  5598. unsigned h=0;
  5599. h = h*571 + a->rp->index*37 + a->dot;
  5600. return h;
  5601. }
  5602. /* There is one instance of the following structure for each
  5603. ** associative array of type "x4".
  5604. */
  5605. struct s_x4 {
  5606. int size; /* The number of available slots. */
  5607. /* Must be a power of 2 greater than or */
  5608. /* equal to 1 */
  5609. int count; /* Number of currently slots filled */
  5610. struct s_x4node *tbl; /* The data stored here */
  5611. struct s_x4node **ht; /* Hash table for lookups */
  5612. };
  5613. /* There is one instance of this structure for every data element
  5614. ** in an associative array of type "x4".
  5615. */
  5616. typedef struct s_x4node {
  5617. struct config *data; /* The data */
  5618. struct s_x4node *next; /* Next entry with the same hash */
  5619. struct s_x4node **from; /* Previous link */
  5620. } x4node;
  5621. /* There is only one instance of the array, which is the following */
  5622. static struct s_x4 *x4a;
  5623. /* Allocate a new associative array */
  5624. void Configtable_init(void){
  5625. if( x4a ) return;
  5626. x4a = (struct s_x4*)lemon_malloc( sizeof(struct s_x4) );
  5627. if( x4a ){
  5628. x4a->size = 64;
  5629. x4a->count = 0;
  5630. x4a->tbl = (x4node*)lemon_calloc(64, sizeof(x4node) + sizeof(x4node*));
  5631. if( x4a->tbl==0 ){
  5632. lemon_free(x4a);
  5633. x4a = 0;
  5634. }else{
  5635. int i;
  5636. x4a->ht = (x4node**)&(x4a->tbl[64]);
  5637. for(i=0; i<64; i++) x4a->ht[i] = 0;
  5638. }
  5639. }
  5640. }
  5641. /* Insert a new record into the array. Return TRUE if successful.
  5642. ** Prior data with the same key is NOT overwritten */
  5643. int Configtable_insert(struct config *data)
  5644. {
  5645. x4node *np;
  5646. unsigned h;
  5647. unsigned ph;
  5648. if( x4a==0 ) return 0;
  5649. ph = confighash(data);
  5650. h = ph & (x4a->size-1);
  5651. np = x4a->ht[h];
  5652. while( np ){
  5653. if( Configcmp((const char *) np->data,(const char *) data)==0 ){
  5654. /* An existing entry with the same key is found. */
  5655. /* Fail because overwrite is not allows. */
  5656. return 0;
  5657. }
  5658. np = np->next;
  5659. }
  5660. if( x4a->count>=x4a->size ){
  5661. /* Need to make the hash table bigger */
  5662. int i,arrSize;
  5663. struct s_x4 array;
  5664. array.size = arrSize = x4a->size*2;
  5665. array.count = x4a->count;
  5666. array.tbl = (x4node*)lemon_calloc(arrSize,
  5667. sizeof(x4node) + sizeof(x4node*));
  5668. if( array.tbl==0 ) return 0; /* Fail due to malloc failure */
  5669. array.ht = (x4node**)&(array.tbl[arrSize]);
  5670. for(i=0; i<arrSize; i++) array.ht[i] = 0;
  5671. for(i=0; i<x4a->count; i++){
  5672. x4node *oldnp, *newnp;
  5673. oldnp = &(x4a->tbl[i]);
  5674. h = confighash(oldnp->data) & (arrSize-1);
  5675. newnp = &(array.tbl[i]);
  5676. if( array.ht[h] ) array.ht[h]->from = &(newnp->next);
  5677. newnp->next = array.ht[h];
  5678. newnp->data = oldnp->data;
  5679. newnp->from = &(array.ht[h]);
  5680. array.ht[h] = newnp;
  5681. }
  5682. *x4a = array;
  5683. }
  5684. /* Insert the new data */
  5685. h = ph & (x4a->size-1);
  5686. np = &(x4a->tbl[x4a->count++]);
  5687. np->data = data;
  5688. if( x4a->ht[h] ) x4a->ht[h]->from = &(np->next);
  5689. np->next = x4a->ht[h];
  5690. x4a->ht[h] = np;
  5691. np->from = &(x4a->ht[h]);
  5692. return 1;
  5693. }
  5694. /* Return a pointer to data assigned to the given key. Return NULL
  5695. ** if no such key. */
  5696. struct config *Configtable_find(struct config *key)
  5697. {
  5698. int h;
  5699. x4node *np;
  5700. if( x4a==0 ) return 0;
  5701. h = confighash(key) & (x4a->size-1);
  5702. np = x4a->ht[h];
  5703. while( np ){
  5704. if( Configcmp((const char *) np->data,(const char *) key)==0 ) break;
  5705. np = np->next;
  5706. }
  5707. return np ? np->data : 0;
  5708. }
  5709. /* Remove all data from the table. Pass each data to the function "f"
  5710. ** as it is removed. ("f" may be null to avoid this step.) */
  5711. void Configtable_clear(int(*f)(struct config *))
  5712. {
  5713. int i;
  5714. if( x4a==0 || x4a->count==0 ) return;
  5715. if( f ) for(i=0; i<x4a->count; i++) (*f)(x4a->tbl[i].data);
  5716. for(i=0; i<x4a->size; i++) x4a->ht[i] = 0;
  5717. x4a->count = 0;
  5718. return;
  5719. }