lemon.c 163 KB

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