lemon.c 131 KB

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