yacc.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #define Bungetrune Bungetc /* ok for now. */
  6. /*
  7. * all these are 32 bit
  8. */
  9. #define TBITSET ((32+NTERMS)/32) /* BOTCH?? +31 */
  10. #define BIT(a,i) ((a)[(i)>>5] & (1<<((i)&037)))
  11. #define SETBIT(a,i) ((a)[(i)>>5] |= (1<<((i)&037)))
  12. #define NWORDS(n) (((n)+32)/32)
  13. #define PARSER "/sys/lib/yaccpar"
  14. #define PARSERS "/sys/lib/yaccpars"
  15. #define TEMPNAME "y.tmp.XXXXXX"
  16. #define ACTNAME "y.acts.XXXXXX"
  17. #define OFILE "tab.c"
  18. #define FILEU "output"
  19. #define FILED "tab.h"
  20. #define FILEDEBUG "debug"
  21. enum
  22. {
  23. /*
  24. * the following are adjustable
  25. * according to memory size
  26. */
  27. ACTSIZE = 40000,
  28. MEMSIZE = 40000,
  29. NSTATES = 2000,
  30. NTERMS = 511,
  31. NPROD = 1600,
  32. NNONTERM = 600,
  33. TEMPSIZE = 2000,
  34. CNAMSZ = 10000,
  35. LSETSIZE = 2400,
  36. WSETSIZE = 350,
  37. NAMESIZE = 50,
  38. NTYPES = 63,
  39. ISIZE = 400,
  40. PRIVATE = 0xE000, /* unicode private use */
  41. /* relationships which must hold:
  42. TBITSET ints must hold NTERMS+1 bits...
  43. WSETSIZE >= NNONTERM
  44. LSETSIZE >= NNONTERM
  45. TEMPSIZE >= NTERMS + NNONTERM + 1
  46. TEMPSIZE >= NSTATES
  47. */
  48. NTBASE = 010000,
  49. ERRCODE = 8190,
  50. ACCEPTCODE = 8191,
  51. NOASC = 0, /* no assoc. */
  52. LASC = 1, /* left assoc. */
  53. RASC = 2, /* right assoc. */
  54. BASC = 3, /* binary assoc. */
  55. /* flags for state generation */
  56. DONE = 0,
  57. MUSTDO = 1,
  58. MUSTLOOKAHEAD = 2,
  59. /* flags for a rule having an action, and being reduced */
  60. ACTFLAG = 04,
  61. REDFLAG = 010,
  62. /* output parser flags */
  63. YYFLAG1 = -1000,
  64. /* parse tokens */
  65. IDENTIFIER = PRIVATE,
  66. MARK,
  67. TERM,
  68. LEFT,
  69. RIGHT,
  70. BINARY,
  71. PREC,
  72. LCURLY,
  73. IDENTCOLON,
  74. NUMBER,
  75. START,
  76. TYPEDEF,
  77. TYPENAME,
  78. UNION,
  79. ENDFILE = 0,
  80. EMPTY = 1,
  81. WHOKNOWS = 0,
  82. OK = 1,
  83. NOMORE = -1000,
  84. };
  85. /* macros for getting associativity and precedence levels */
  86. #define ASSOC(i) ((i)&03)
  87. #define PLEVEL(i) (((i)>>4)&077)
  88. #define TYPE(i) (((i)>>10)&077)
  89. /* macros for setting associativity and precedence levels */
  90. #define SETASC(i,j) i |= j
  91. #define SETPLEV(i,j) i |= (j<<4)
  92. #define SETTYPE(i,j) i |= (j<<10)
  93. /* looping macros */
  94. #define TLOOP(i) for(i=1; i<=ntokens; i++)
  95. #define NTLOOP(i) for(i=0; i<=nnonter; i++)
  96. #define PLOOP(s,i) for(i=s; i<nprod; i++)
  97. #define SLOOP(i) for(i=0; i<nstate; i++)
  98. #define WSBUMP(x) x++
  99. #define WSLOOP(s,j) for(j=s; j<cwp; j++)
  100. #define ITMLOOP(i,p,q) for(q=pstate[i+1], p=pstate[i]; p<q; p++)
  101. #define SETLOOP(i) for(i=0; i<tbitset; i++)
  102. /* command to clobber tempfiles after use */
  103. #define ZAPFILE(x) if(x) remove(x)
  104. /* I/O descriptors */
  105. Biobuf* faction; /* file for saving actions */
  106. Biobuf* fdefine; /* file for #defines */
  107. Biobuf* fdebug; /* y.debug for strings for debugging */
  108. Biobuf* ftable; /* y.tab.c file */
  109. Biobuf* ftemp; /* tempfile to pass 2 */
  110. Biobuf* finput; /* input file */
  111. Biobuf* foutput; /* y.output file */
  112. /* communication variables between various I/O routines */
  113. char* infile; /* input file name */
  114. int numbval; /* value of an input number */
  115. char tokname[NAMESIZE+4]; /* input token name, slop for runes and 0 */
  116. /* structure declarations */
  117. typedef
  118. struct
  119. {
  120. int lset[TBITSET];
  121. } Lkset;
  122. typedef
  123. struct
  124. {
  125. int* pitem;
  126. Lkset* look;
  127. } Item;
  128. typedef
  129. struct
  130. {
  131. char* name;
  132. int value;
  133. } Symb;
  134. typedef
  135. struct
  136. {
  137. int* pitem;
  138. int flag;
  139. Lkset ws;
  140. } Wset;
  141. /* storage of names */
  142. char cnames[CNAMSZ]; /* place where token and nonterminal names are stored */
  143. int cnamsz = CNAMSZ; /* size of cnames */
  144. char* cnamp = cnames; /* place where next name is to be put in */
  145. int ndefout = 4; /* number of defined symbols output */
  146. char* tempname;
  147. char* actname;
  148. char ttempname[] = TEMPNAME;
  149. char tactname[] = ACTNAME;
  150. char* parser = PARSER;
  151. char* yydebug;
  152. /* storage of types */
  153. int ntypes; /* number of types defined */
  154. char* typeset[NTYPES]; /* pointers to type tags */
  155. /* token information */
  156. int ntokens = 0 ; /* number of tokens */
  157. Symb tokset[NTERMS];
  158. int toklev[NTERMS]; /* vector with the precedence of the terminals */
  159. /* nonterminal information */
  160. int nnonter = -1; /* the number of nonterminals */
  161. Symb nontrst[NNONTERM];
  162. int start; /* start symbol */
  163. /* assigned token type values */
  164. int extval = 0;
  165. char* ytabc = OFILE; /* name of y.tab.c */
  166. /* grammar rule information */
  167. int mem0[MEMSIZE] ; /* production storage */
  168. int* mem = mem0;
  169. int nprod = 1; /* number of productions */
  170. int* prdptr[NPROD]; /* pointers to descriptions of productions */
  171. int levprd[NPROD]; /* precedence levels for the productions */
  172. int rlines[NPROD]; /* line number for this rule */
  173. /* state information */
  174. int nstate = 0; /* number of states */
  175. Item* pstate[NSTATES+2]; /* pointers to the descriptions of the states */
  176. int tystate[NSTATES]; /* contains type information about the states */
  177. int defact[NSTATES]; /* the default actions of states */
  178. int tstates[NTERMS]; /* states generated by terminal gotos */
  179. int ntstates[NNONTERM]; /* states generated by nonterminal gotos */
  180. int mstates[NSTATES]; /* chain of overflows of term/nonterm generation lists */
  181. int lastred; /* the number of the last reduction of a state */
  182. /* lookahead set information */
  183. Lkset lkst[LSETSIZE];
  184. int nolook; /* flag to turn off lookahead computations */
  185. int tbitset; /* size of lookahead sets */
  186. int nlset = 0; /* next lookahead set index */
  187. int nolook = 0; /* flag to suppress lookahead computations */
  188. Lkset clset; /* temporary storage for lookahead computations */
  189. /* working set information */
  190. Wset wsets[WSETSIZE];
  191. Wset* cwp;
  192. /* storage for action table */
  193. int amem[ACTSIZE]; /* action table storage */
  194. int* memp = amem; /* next free action table position */
  195. int indgo[NSTATES]; /* index to the stored goto table */
  196. /* temporary vector, indexable by states, terms, or ntokens */
  197. int temp1[TEMPSIZE]; /* temporary storage, indexed by terms + ntokens or states */
  198. int lineno = 1; /* current input line number */
  199. int fatfl = 1; /* if on, error is fatal */
  200. int nerrors = 0; /* number of errors */
  201. /* statistics collection variables */
  202. int zzgoent;
  203. int zzgobest;
  204. int zzacent;
  205. int zzexcp;
  206. int zzclose;
  207. int zzrrconf;
  208. int zzsrconf;
  209. int* ggreed = lkst[0].lset;
  210. int* pgo = wsets[0].ws.lset;
  211. int* yypgo = &nontrst[0].value;
  212. int maxspr = 0; /* maximum spread of any entry */
  213. int maxoff = 0; /* maximum offset into a array */
  214. int* pmem = mem0;
  215. int* maxa;
  216. int nxdb = 0;
  217. int adb = 0;
  218. /* storage for information about the nonterminals */
  219. int** pres[NNONTERM+2]; /* vector of pointers to productions yielding each nonterminal */
  220. Lkset* pfirst[NNONTERM+2]; /* vector of pointers to first sets for each nonterminal */
  221. int pempty[NNONTERM+1]; /* vector of nonterminals nontrivially deriving e */
  222. /* random stuff picked out from between functions */
  223. int indebug = 0;
  224. Wset* zzcwp = wsets;
  225. int zzgoent = 0;
  226. int zzgobest = 0;
  227. int zzacent = 0;
  228. int zzexcp = 0;
  229. int zzclose = 0;
  230. int zzsrconf = 0;
  231. int* zzmemsz = mem0;
  232. int zzrrconf = 0;
  233. int pidebug = 0; /* debugging flag for putitem */
  234. int gsdebug = 0;
  235. int cldebug = 0; /* debugging flag for closure */
  236. int pkdebug = 0;
  237. int g2debug = 0;
  238. struct
  239. {
  240. char* name;
  241. long value;
  242. } resrv[] =
  243. {
  244. "binary", BINARY,
  245. "left", LEFT,
  246. "nonassoc", BINARY,
  247. "prec", PREC,
  248. "right", RIGHT,
  249. "start", START,
  250. "term", TERM,
  251. "token", TERM,
  252. "type", TYPEDEF,
  253. "union", UNION,
  254. 0,
  255. };
  256. /* define functions */
  257. void main(int, char**);
  258. void others(void);
  259. char* chcopy(char*, char*);
  260. char* writem(int*);
  261. char* symnam(int);
  262. void summary(void);
  263. void error(char*, ...);
  264. void aryfil(int*, int, int);
  265. int setunion(int*, int*);
  266. void prlook(Lkset*);
  267. void cpres(void);
  268. void cpfir(void);
  269. int state(int);
  270. void putitem(int*, Lkset*);
  271. void cempty(void);
  272. void stagen(void);
  273. void closure(int);
  274. Lkset* flset(Lkset*);
  275. void cleantmp(void);
  276. void intr(void);
  277. void setup(int, char**);
  278. void finact(void);
  279. int defin(int, char*);
  280. void defout(int);
  281. char* cstash(char*);
  282. long gettok(void);
  283. int fdtype(int);
  284. int chfind(int, char*);
  285. void cpyunion(void);
  286. void cpycode(void);
  287. int skipcom(void);
  288. void cpyact(int);
  289. void openup(char*, int, int, int, char*);
  290. void output(void);
  291. int apack(int*, int);
  292. void go2out(void);
  293. void go2gen(int);
  294. void precftn(int, int, int);
  295. void wract(int);
  296. void wrstate(int);
  297. void warray(char*, int*, int);
  298. void hideprod(void);
  299. void callopt(void);
  300. void gin(int);
  301. void stin(int);
  302. int nxti(void);
  303. void osummary(void);
  304. void aoutput(void);
  305. void arout(char*, int*, int);
  306. int gtnm(void);
  307. void
  308. main(int argc, char *argv[])
  309. {
  310. setup(argc, argv); /* initialize and read productions */
  311. tbitset = NWORDS(ntokens);
  312. cpres(); /* make table of which productions yield a given nonterminal */
  313. cempty(); /* make a table of which nonterminals can match the empty string */
  314. cpfir(); /* make a table of firsts of nonterminals */
  315. stagen(); /* generate the states */
  316. output(); /* write the states and the tables */
  317. go2out();
  318. hideprod();
  319. summary();
  320. callopt();
  321. others();
  322. exits(0);
  323. }
  324. /*
  325. * put out other arrays, copy the parsers
  326. */
  327. void
  328. others(void)
  329. {
  330. int c, i, j;
  331. finput = Bopen(parser, OREAD);
  332. if(finput == 0)
  333. error("cannot find parser %s", parser);
  334. warray("yyr1", levprd, nprod);
  335. aryfil(temp1, nprod, 0);
  336. PLOOP(1, i)
  337. temp1[i] = prdptr[i+1]-prdptr[i]-2;
  338. warray("yyr2", temp1, nprod);
  339. aryfil(temp1, nstate, -1000);
  340. TLOOP(i)
  341. for(j=tstates[i]; j!=0; j=mstates[j])
  342. temp1[j] = i;
  343. NTLOOP(i)
  344. for(j=ntstates[i]; j!=0; j=mstates[j])
  345. temp1[j] = -i;
  346. warray("yychk", temp1, nstate);
  347. warray("yydef", defact, nstate);
  348. /* put out token translation tables */
  349. /* table 1 has 0-256 */
  350. aryfil(temp1, 256, 0);
  351. c = 0;
  352. TLOOP(i) {
  353. j = tokset[i].value;
  354. if(j >= 0 && j < 256) {
  355. if(temp1[j]) {
  356. print("yacc bug -- cant have 2 different Ts with same value\n");
  357. print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name);
  358. nerrors++;
  359. }
  360. temp1[j] = i;
  361. if(j > c)
  362. c = j;
  363. }
  364. }
  365. warray("yytok1", temp1, c+1);
  366. /* table 2 has PRIVATE-PRIVATE+256 */
  367. aryfil(temp1, 256, 0);
  368. c = 0;
  369. TLOOP(i) {
  370. j = tokset[i].value - PRIVATE;
  371. if(j >= 0 && j < 256) {
  372. if(temp1[j]) {
  373. print("yacc bug -- cant have 2 different Ts with same value\n");
  374. print(" %s and %s\n", tokset[i].name, tokset[temp1[j]].name);
  375. nerrors++;
  376. }
  377. temp1[j] = i;
  378. if(j > c)
  379. c = j;
  380. }
  381. }
  382. warray("yytok2", temp1, c+1);
  383. /* table 3 has everything else */
  384. Bprint(ftable, "long yytok3[] =\n{\n");
  385. c = 0;
  386. TLOOP(i) {
  387. j = tokset[i].value;
  388. if(j >= 0 && j < 256)
  389. continue;
  390. if(j >= PRIVATE && j < 256+PRIVATE)
  391. continue;
  392. Bprint(ftable, "%4d,%4d,", j, i);
  393. c++;
  394. if(c%5 == 0)
  395. Bprint(ftable, "\n");
  396. }
  397. Bprint(ftable, "%4d\n};\n", 0);
  398. /* copy parser text */
  399. while((c=Bgetrune(finput)) != Beof) {
  400. if(c == '$') {
  401. if((c = Bgetrune(finput)) != 'A')
  402. Bputrune(ftable, '$');
  403. else { /* copy actions */
  404. faction = Bopen(actname, OREAD);
  405. if(faction == 0)
  406. error("cannot reopen action tempfile");
  407. while((c=Bgetrune(faction)) != Beof)
  408. Bputrune(ftable, c);
  409. Bterm(faction);
  410. ZAPFILE(actname);
  411. c = Bgetrune(finput);
  412. }
  413. }
  414. Bputrune(ftable, c);
  415. }
  416. Bterm(ftable);
  417. }
  418. /*
  419. * copies string q into p, returning next free char ptr
  420. */
  421. char*
  422. chcopy(char* p, char* q)
  423. {
  424. int c;
  425. while(c = *q) {
  426. if(c == '"')
  427. *p++ = '\\';
  428. *p++ = c;
  429. q++;
  430. }
  431. *p = 0;
  432. return p;
  433. }
  434. /*
  435. * creates output string for item pointed to by pp
  436. */
  437. char*
  438. writem(int *pp)
  439. {
  440. int i,*p;
  441. static char sarr[ISIZE];
  442. char* q;
  443. for(p=pp; *p>0; p++)
  444. ;
  445. p = prdptr[-*p];
  446. q = chcopy(sarr, nontrst[*p-NTBASE].name);
  447. q = chcopy(q, ": ");
  448. for(;;) {
  449. *q = ' ';
  450. p++;
  451. if(p == pp)
  452. *q = '.';
  453. q++;
  454. *q = '\0';
  455. i = *p;
  456. if(i <= 0)
  457. break;
  458. q = chcopy(q, symnam(i));
  459. if(q > &sarr[ISIZE-30])
  460. error("item too big");
  461. }
  462. /* an item calling for a reduction */
  463. i = *pp;
  464. if(i < 0 ) {
  465. q = chcopy(q, " (");
  466. sprint(q, "%d)", -i);
  467. }
  468. return sarr;
  469. }
  470. /*
  471. * return a pointer to the name of symbol i
  472. */
  473. char*
  474. symnam(int i)
  475. {
  476. char* cp;
  477. cp = (i >= NTBASE)? nontrst[i-NTBASE].name: tokset[i].name;
  478. if(*cp == ' ')
  479. cp++;
  480. return cp;
  481. }
  482. /*
  483. * output the summary on y.output
  484. */
  485. void
  486. summary(void)
  487. {
  488. if(foutput != 0) {
  489. Bprint(foutput, "\n%d/%d terminals, %d/%d nonterminals\n",
  490. ntokens, NTERMS, nnonter, NNONTERM);
  491. Bprint(foutput, "%d/%d grammar rules, %d/%d states\n",
  492. nprod, NPROD, nstate, NSTATES);
  493. Bprint(foutput, "%d shift/reduce, %d reduce/reduce conflicts reported\n",
  494. zzsrconf, zzrrconf);
  495. Bprint(foutput, "%d/%d working sets used\n",
  496. (int)(zzcwp-wsets), WSETSIZE);
  497. Bprint(foutput, "memory: states,etc. %d/%d, parser %d/%d\n",
  498. (int)(zzmemsz-mem0), MEMSIZE, (int)(memp-amem), ACTSIZE);
  499. Bprint(foutput, "%d/%d distinct lookahead sets\n", nlset, LSETSIZE);
  500. Bprint(foutput, "%d extra closures\n", zzclose - 2*nstate);
  501. Bprint(foutput, "%d shift entries, %d exceptions\n", zzacent, zzexcp);
  502. Bprint(foutput, "%d goto entries\n", zzgoent);
  503. Bprint(foutput, "%d entries saved by goto default\n", zzgobest);
  504. }
  505. if(zzsrconf != 0 || zzrrconf != 0) {
  506. print("\nconflicts: ");
  507. if(zzsrconf)
  508. print("%d shift/reduce", zzsrconf);
  509. if(zzsrconf && zzrrconf)
  510. print(", ");
  511. if(zzrrconf)
  512. print("%d reduce/reduce", zzrrconf);
  513. print("\n");
  514. }
  515. if(ftemp != 0) {
  516. Bterm(ftemp);
  517. ftemp = 0;
  518. }
  519. if(fdefine != 0) {
  520. Bterm(fdefine);
  521. fdefine = 0;
  522. }
  523. }
  524. /*
  525. * write out error comment -- NEEDS WORK
  526. */
  527. void
  528. error(char *s, ...)
  529. {
  530. nerrors++;
  531. fprint(2, "\n fatal error:");
  532. fprint(2, s, (&s)[1]);
  533. fprint(2, ", %s:%d\n", infile, lineno);
  534. if(!fatfl)
  535. return;
  536. summary();
  537. cleantmp();
  538. exits("error");
  539. }
  540. /*
  541. * set elements 0 through n-1 to c
  542. */
  543. void
  544. aryfil(int *v, int n, int c)
  545. {
  546. int i;
  547. for(i=0; i<n; i++)
  548. v[i] = c;
  549. }
  550. /*
  551. * set a to the union of a and b
  552. * return 1 if b is not a subset of a, 0 otherwise
  553. */
  554. int
  555. setunion(int *a, int *b)
  556. {
  557. int i, x, sub;
  558. sub = 0;
  559. SETLOOP(i) {
  560. x = *a;
  561. *a |= *b;
  562. if(*a != x)
  563. sub = 1;
  564. a++;
  565. b++;
  566. }
  567. return sub;
  568. }
  569. void
  570. prlook(Lkset* p)
  571. {
  572. int j, *pp;
  573. pp = p->lset;
  574. if(pp == 0)
  575. Bprint(foutput, "\tNULL");
  576. else {
  577. Bprint(foutput, " { ");
  578. TLOOP(j)
  579. if(BIT(pp,j))
  580. Bprint(foutput, "%s ", symnam(j));
  581. Bprint(foutput, "}");
  582. }
  583. }
  584. /*
  585. * compute an array with the beginnings of productions yielding given nonterminals
  586. * The array pres points to these lists
  587. * the array pyield has the lists: the total size is only NPROD+1
  588. */
  589. void
  590. cpres(void)
  591. {
  592. int c, j, i, **pmem;
  593. static int *pyield[NPROD];
  594. pmem = pyield;
  595. NTLOOP(i) {
  596. c = i+NTBASE;
  597. pres[i] = pmem;
  598. fatfl = 0; /* make undefined symbols nonfatal */
  599. PLOOP(0, j)
  600. if(*prdptr[j] == c)
  601. *pmem++ = prdptr[j]+1;
  602. if(pres[i] == pmem)
  603. error("nonterminal %s not defined!", nontrst[i].name);
  604. }
  605. pres[i] = pmem;
  606. fatfl = 1;
  607. if(nerrors) {
  608. summary();
  609. cleantmp();
  610. exits("error");
  611. }
  612. if(pmem != &pyield[nprod])
  613. error("internal Yacc error: pyield %d", pmem-&pyield[nprod]);
  614. }
  615. /*
  616. * compute an array with the first of nonterminals
  617. */
  618. void
  619. cpfir(void)
  620. {
  621. int *p, **s, i, **t, ch, changes;
  622. zzcwp = &wsets[nnonter];
  623. NTLOOP(i) {
  624. aryfil(wsets[i].ws.lset, tbitset, 0);
  625. t = pres[i+1];
  626. /* initially fill the sets */
  627. for(s=pres[i]; s<t; ++s)
  628. for(p = *s; (ch = *p) > 0; ++p) {
  629. if(ch < NTBASE) {
  630. SETBIT(wsets[i].ws.lset, ch);
  631. break;
  632. }
  633. if(!pempty[ch-NTBASE])
  634. break;
  635. }
  636. }
  637. /* now, reflect transitivity */
  638. changes = 1;
  639. while(changes) {
  640. changes = 0;
  641. NTLOOP(i) {
  642. t = pres[i+1];
  643. for(s = pres[i]; s < t; ++s)
  644. for(p = *s; (ch = (*p-NTBASE)) >= 0; ++p) {
  645. changes |= setunion(wsets[i].ws.lset, wsets[ch].ws.lset);
  646. if(!pempty[ch])
  647. break;
  648. }
  649. }
  650. }
  651. NTLOOP(i)
  652. pfirst[i] = flset(&wsets[i].ws);
  653. if(!indebug)
  654. return;
  655. if(foutput != 0)
  656. NTLOOP(i) {
  657. Bprint(foutput, "\n%s: ", nontrst[i].name);
  658. prlook(pfirst[i]);
  659. Bprint(foutput, " %d\n", pempty[i]);
  660. }
  661. }
  662. /*
  663. * sorts last state,and sees if it equals earlier ones. returns state number
  664. */
  665. int
  666. state(int c)
  667. {
  668. Item *p1, *p2, *k, *l, *q1, *q2;
  669. int size1, size2, i;
  670. p1 = pstate[nstate];
  671. p2 = pstate[nstate+1];
  672. if(p1 == p2)
  673. return 0; /* null state */
  674. /* sort the items */
  675. for(k = p2-1; k > p1; k--) /* make k the biggest */
  676. for(l = k-1; l >= p1; --l)
  677. if(l->pitem > k->pitem) {
  678. int *s;
  679. Lkset *ss;
  680. s = k->pitem;
  681. k->pitem = l->pitem;
  682. l->pitem = s;
  683. ss = k->look;
  684. k->look = l->look;
  685. l->look = ss;
  686. }
  687. size1 = p2 - p1; /* size of state */
  688. for(i = (c>=NTBASE)? ntstates[c-NTBASE]: tstates[c]; i != 0; i = mstates[i]) {
  689. /* get ith state */
  690. q1 = pstate[i];
  691. q2 = pstate[i+1];
  692. size2 = q2 - q1;
  693. if(size1 != size2)
  694. continue;
  695. k = p1;
  696. for(l = q1; l < q2; l++) {
  697. if(l->pitem != k->pitem)
  698. break;
  699. k++;
  700. }
  701. if(l != q2)
  702. continue;
  703. /* found it */
  704. pstate[nstate+1] = pstate[nstate]; /* delete last state */
  705. /* fix up lookaheads */
  706. if(nolook)
  707. return i;
  708. for(l = q1, k = p1; l < q2; ++l, ++k ) {
  709. int s;
  710. SETLOOP(s)
  711. clset.lset[s] = l->look->lset[s];
  712. if(setunion(clset.lset, k->look->lset)) {
  713. tystate[i] = MUSTDO;
  714. /* register the new set */
  715. l->look = flset( &clset );
  716. }
  717. }
  718. return i;
  719. }
  720. /* state is new */
  721. if(nolook)
  722. error("yacc state/nolook error");
  723. pstate[nstate+2] = p2;
  724. if(nstate+1 >= NSTATES)
  725. error("too many states");
  726. if(c >= NTBASE) {
  727. mstates[nstate] = ntstates[c-NTBASE];
  728. ntstates[c-NTBASE] = nstate;
  729. } else {
  730. mstates[nstate] = tstates[c];
  731. tstates[c] = nstate;
  732. }
  733. tystate[nstate] = MUSTDO;
  734. return nstate++;
  735. }
  736. void
  737. putitem(int *ptr, Lkset *lptr)
  738. {
  739. Item *j;
  740. if(pidebug && foutput != 0)
  741. Bprint(foutput, "putitem(%s), state %d\n", writem(ptr), nstate);
  742. j = pstate[nstate+1];
  743. j->pitem = ptr;
  744. if(!nolook)
  745. j->look = flset(lptr);
  746. pstate[nstate+1] = ++j;
  747. if((int*)j > zzmemsz) {
  748. zzmemsz = (int*)j;
  749. if(zzmemsz >= &mem0[MEMSIZE])
  750. error("out of state space");
  751. }
  752. }
  753. /*
  754. * mark nonterminals which derive the empty string
  755. * also, look for nonterminals which don't derive any token strings
  756. */
  757. void
  758. cempty(void)
  759. {
  760. int i, *p;
  761. /* first, use the array pempty to detect productions that can never be reduced */
  762. /* set pempty to WHONOWS */
  763. aryfil(pempty, nnonter+1, WHOKNOWS);
  764. /* now, look at productions, marking nonterminals which derive something */
  765. more:
  766. PLOOP(0, i) {
  767. if(pempty[*prdptr[i] - NTBASE])
  768. continue;
  769. for(p = prdptr[i]+1; *p >= 0; ++p)
  770. if(*p >= NTBASE && pempty[*p-NTBASE] == WHOKNOWS)
  771. break;
  772. /* production can be derived */
  773. if(*p < 0) {
  774. pempty[*prdptr[i]-NTBASE] = OK;
  775. goto more;
  776. }
  777. }
  778. /* now, look at the nonterminals, to see if they are all OK */
  779. NTLOOP(i) {
  780. /* the added production rises or falls as the start symbol ... */
  781. if(i == 0)
  782. continue;
  783. if(pempty[i] != OK) {
  784. fatfl = 0;
  785. error("nonterminal %s never derives any token string", nontrst[i].name);
  786. }
  787. }
  788. if(nerrors) {
  789. summary();
  790. cleantmp();
  791. exits("error");
  792. }
  793. /* now, compute the pempty array, to see which nonterminals derive the empty string */
  794. /* set pempty to WHOKNOWS */
  795. aryfil( pempty, nnonter+1, WHOKNOWS);
  796. /* loop as long as we keep finding empty nonterminals */
  797. again:
  798. PLOOP(1, i) {
  799. /* not known to be empty */
  800. if(pempty[*prdptr[i]-NTBASE] == WHOKNOWS) {
  801. for(p = prdptr[i]+1; *p >= NTBASE && pempty[*p-NTBASE] == EMPTY ; ++p)
  802. ;
  803. /* we have a nontrivially empty nonterminal */
  804. if(*p < 0) {
  805. pempty[*prdptr[i]-NTBASE] = EMPTY;
  806. /* got one ... try for another */
  807. goto again;
  808. }
  809. }
  810. }
  811. }
  812. /*
  813. * generate the states
  814. */
  815. void
  816. stagen(void)
  817. {
  818. int c, i, j, more;
  819. Wset *p, *q;
  820. /* initialize */
  821. nstate = 0;
  822. /* THIS IS FUNNY from the standpoint of portability
  823. * it represents the magic moment when the mem0 array, which has
  824. * been holding the productions, starts to hold item pointers, of a
  825. * different type...
  826. * someday, alloc should be used to allocate all this stuff... for now, we
  827. * accept that if pointers don't fit in integers, there is a problem...
  828. */
  829. pstate[0] = pstate[1] = (Item*)mem;
  830. aryfil(clset.lset, tbitset, 0);
  831. putitem(prdptr[0]+1, &clset);
  832. tystate[0] = MUSTDO;
  833. nstate = 1;
  834. pstate[2] = pstate[1];
  835. aryfil(amem, ACTSIZE, 0);
  836. /* now, the main state generation loop */
  837. for(more=1; more;) {
  838. more = 0;
  839. SLOOP(i) {
  840. if(tystate[i] != MUSTDO)
  841. continue;
  842. tystate[i] = DONE;
  843. aryfil(temp1, nnonter+1, 0);
  844. /* take state i, close it, and do gotos */
  845. closure(i);
  846. /* generate goto's */
  847. WSLOOP(wsets, p) {
  848. if(p->flag)
  849. continue;
  850. p->flag = 1;
  851. c = *(p->pitem);
  852. if(c <= 1) {
  853. if(pstate[i+1]-pstate[i] <= p-wsets)
  854. tystate[i] = MUSTLOOKAHEAD;
  855. continue;
  856. }
  857. /* do a goto on c */
  858. WSLOOP(p, q)
  859. /* this item contributes to the goto */
  860. if(c == *(q->pitem)) {
  861. putitem(q->pitem+1, &q->ws);
  862. q->flag = 1;
  863. }
  864. if(c < NTBASE)
  865. state(c); /* register new state */
  866. else
  867. temp1[c-NTBASE] = state(c);
  868. }
  869. if(gsdebug && foutput != 0) {
  870. Bprint(foutput, "%d: ", i);
  871. NTLOOP(j)
  872. if(temp1[j])
  873. Bprint(foutput, "%s %d, ",
  874. nontrst[j].name, temp1[j]);
  875. Bprint(foutput, "\n");
  876. }
  877. indgo[i] = apack(&temp1[1], nnonter-1) - 1;
  878. /* do some more */
  879. more = 1;
  880. }
  881. }
  882. }
  883. /*
  884. * generate the closure of state i
  885. */
  886. void
  887. closure(int i)
  888. {
  889. Wset *u, *v;
  890. Item *p, *q;
  891. int c, ch, work, k, *pi, **s, **t;
  892. zzclose++;
  893. /* first, copy kernel of state i to wsets */
  894. cwp = wsets;
  895. ITMLOOP(i, p, q) {
  896. cwp->pitem = p->pitem;
  897. cwp->flag = 1; /* this item must get closed */
  898. SETLOOP(k)
  899. cwp->ws.lset[k] = p->look->lset[k];
  900. WSBUMP(cwp);
  901. }
  902. /* now, go through the loop, closing each item */
  903. work = 1;
  904. while(work) {
  905. work = 0;
  906. WSLOOP(wsets, u) {
  907. if(u->flag == 0)
  908. continue;
  909. /* dot is before c */
  910. c = *(u->pitem);
  911. if(c < NTBASE) {
  912. u->flag = 0;
  913. /* only interesting case is where . is before nonterminal */
  914. continue;
  915. }
  916. /* compute the lookahead */
  917. aryfil(clset.lset, tbitset, 0);
  918. /* find items involving c */
  919. WSLOOP(u, v)
  920. if(v->flag == 1 && *(pi=v->pitem) == c) {
  921. v->flag = 0;
  922. if(nolook)
  923. continue;
  924. while((ch = *++pi) > 0) {
  925. /* terminal symbol */
  926. if(ch < NTBASE) {
  927. SETBIT(clset.lset, ch);
  928. break;
  929. }
  930. /* nonterminal symbol */
  931. setunion(clset.lset, pfirst[ch-NTBASE]->lset);
  932. if(!pempty[ch-NTBASE])
  933. break;
  934. }
  935. if(ch <= 0)
  936. setunion(clset.lset, v->ws.lset);
  937. }
  938. /*
  939. * now loop over productions derived from c
  940. * c is now nonterminal number
  941. */
  942. c -= NTBASE;
  943. t = pres[c+1];
  944. for(s = pres[c]; s < t; ++s) {
  945. /*
  946. * put these items into the closure
  947. * is the item there
  948. */
  949. WSLOOP(wsets, v)
  950. /* yes, it is there */
  951. if(v->pitem == *s) {
  952. if(nolook)
  953. goto nexts;
  954. if(setunion(v->ws.lset, clset.lset))
  955. v->flag = work = 1;
  956. goto nexts;
  957. }
  958. /* not there; make a new entry */
  959. if(cwp-wsets+1 >= WSETSIZE)
  960. error( "working set overflow");
  961. cwp->pitem = *s;
  962. cwp->flag = 1;
  963. if(!nolook) {
  964. work = 1;
  965. SETLOOP(k) cwp->ws.lset[k] = clset.lset[k];
  966. }
  967. WSBUMP(cwp);
  968. nexts:;
  969. }
  970. }
  971. }
  972. /* have computed closure; flags are reset; return */
  973. if(cwp > zzcwp)
  974. zzcwp = cwp;
  975. if(cldebug && foutput != 0) {
  976. Bprint(foutput, "\nState %d, nolook = %d\n", i, nolook);
  977. WSLOOP(wsets, u) {
  978. if(u->flag)
  979. Bprint(foutput, "flag set!\n");
  980. u->flag = 0;
  981. Bprint(foutput, "\t%s", writem(u->pitem));
  982. prlook(&u->ws);
  983. Bprint(foutput, "\n");
  984. }
  985. }
  986. }
  987. /*
  988. * decide if the lookahead set pointed to by p is known
  989. * return pointer to a perminent location for the set
  990. */
  991. Lkset*
  992. flset(Lkset *p)
  993. {
  994. Lkset *q;
  995. int *u, *v, *w, j;
  996. for(q = &lkst[nlset]; q-- > lkst;) {
  997. u = p->lset;
  998. v = q->lset;
  999. w = &v[tbitset];
  1000. while(v < w)
  1001. if(*u++ != *v++)
  1002. goto more;
  1003. /* we have matched */
  1004. return q;
  1005. more:;
  1006. }
  1007. /* add a new one */
  1008. q = &lkst[nlset++];
  1009. if(nlset >= LSETSIZE)
  1010. error("too many lookahead sets");
  1011. SETLOOP(j)
  1012. q->lset[j] = p->lset[j];
  1013. return q;
  1014. }
  1015. void
  1016. cleantmp(void)
  1017. {
  1018. ZAPFILE(actname);
  1019. ZAPFILE(tempname);
  1020. }
  1021. void
  1022. intr(void)
  1023. {
  1024. cleantmp();
  1025. exits("interrupted");
  1026. }
  1027. void
  1028. usage(void)
  1029. {
  1030. fprint(2, "usage: yacc [-Dn] [-vdS] [-o outputfile] [-s stem] grammar\n");
  1031. exits("usage");
  1032. }
  1033. void
  1034. setup(int argc, char *argv[])
  1035. {
  1036. long c, t;
  1037. int i, j, lev, ty, ytab, *p;
  1038. int vflag, dflag, stem;
  1039. char actnm[8], *stemc, *s, dirbuf[128];
  1040. ytab = 0;
  1041. vflag = 0;
  1042. dflag = 0;
  1043. stem = 0;
  1044. stemc = "y";
  1045. foutput = 0;
  1046. fdefine = 0;
  1047. fdebug = 0;
  1048. ARGBEGIN{
  1049. case 'v':
  1050. case 'V':
  1051. vflag++;
  1052. break;
  1053. case 'D':
  1054. yydebug = EARGF(usage());
  1055. break;
  1056. case 'd':
  1057. dflag++;
  1058. break;
  1059. case 'o':
  1060. ytab++;
  1061. ytabc = EARGF(usage());
  1062. break;
  1063. case 's':
  1064. stem++;
  1065. stemc = ARGF();
  1066. break;
  1067. case 'S':
  1068. parser = PARSERS;
  1069. break;
  1070. default:
  1071. error("illegal option: %c", ARGC());
  1072. }ARGEND
  1073. openup(stemc, dflag, vflag, ytab, ytabc);
  1074. ftemp = Bopen(tempname = mktemp(ttempname), OWRITE);
  1075. faction = Bopen(actname = mktemp(tactname), OWRITE);
  1076. if(ftemp == 0 || faction == 0)
  1077. error("cannot open temp file");
  1078. if(argc < 1)
  1079. error("no input file");
  1080. infile = argv[0];
  1081. if(infile[0] != '/' && getwd(dirbuf, sizeof dirbuf)!=nil){
  1082. i = strlen(infile)+1+strlen(dirbuf)+1+10;
  1083. s = malloc(i);
  1084. if(s != nil){
  1085. snprint(s, i, "%s/%s", dirbuf, infile);
  1086. cleanname(s);
  1087. infile = s;
  1088. }
  1089. }
  1090. finput = Bopen(infile, OREAD);
  1091. if(finput == 0)
  1092. error("cannot open '%s'", argv[0]);
  1093. cnamp = cnames;
  1094. defin(0, "$end");
  1095. extval = PRIVATE; /* tokens start in unicode 'private use' */
  1096. defin(0, "error");
  1097. defin(1, "$accept");
  1098. defin(0, "$unk");
  1099. mem = mem0;
  1100. i = 0;
  1101. for(t = gettok(); t != MARK && t != ENDFILE;)
  1102. switch(t) {
  1103. case ';':
  1104. t = gettok();
  1105. break;
  1106. case START:
  1107. if(gettok() != IDENTIFIER)
  1108. error("bad %%start construction");
  1109. start = chfind(1, tokname);
  1110. t = gettok();
  1111. continue;
  1112. case TYPEDEF:
  1113. if(gettok() != TYPENAME)
  1114. error("bad syntax in %%type");
  1115. ty = numbval;
  1116. for(;;) {
  1117. t = gettok();
  1118. switch(t) {
  1119. case IDENTIFIER:
  1120. if((t=chfind(1, tokname)) < NTBASE) {
  1121. j = TYPE(toklev[t]);
  1122. if(j != 0 && j != ty)
  1123. error("type redeclaration of token %s",
  1124. tokset[t].name);
  1125. else
  1126. SETTYPE(toklev[t], ty);
  1127. } else {
  1128. j = nontrst[t-NTBASE].value;
  1129. if(j != 0 && j != ty)
  1130. error("type redeclaration of nonterminal %s",
  1131. nontrst[t-NTBASE].name );
  1132. else
  1133. nontrst[t-NTBASE].value = ty;
  1134. }
  1135. case ',':
  1136. continue;
  1137. case ';':
  1138. t = gettok();
  1139. default:
  1140. break;
  1141. }
  1142. break;
  1143. }
  1144. continue;
  1145. case UNION:
  1146. /* copy the union declaration to the output */
  1147. cpyunion();
  1148. t = gettok();
  1149. continue;
  1150. case LEFT:
  1151. case BINARY:
  1152. case RIGHT:
  1153. i++;
  1154. case TERM:
  1155. /* nonzero means new prec. and assoc. */
  1156. lev = t-TERM;
  1157. ty = 0;
  1158. /* get identifiers so defined */
  1159. t = gettok();
  1160. /* there is a type defined */
  1161. if(t == TYPENAME) {
  1162. ty = numbval;
  1163. t = gettok();
  1164. }
  1165. for(;;) {
  1166. switch(t) {
  1167. case ',':
  1168. t = gettok();
  1169. continue;
  1170. case ';':
  1171. break;
  1172. case IDENTIFIER:
  1173. j = chfind(0, tokname);
  1174. if(j >= NTBASE)
  1175. error("%s defined earlier as nonterminal", tokname);
  1176. if(lev) {
  1177. if(ASSOC(toklev[j]))
  1178. error("redeclaration of precedence of %s", tokname);
  1179. SETASC(toklev[j], lev);
  1180. SETPLEV(toklev[j], i);
  1181. }
  1182. if(ty) {
  1183. if(TYPE(toklev[j]))
  1184. error("redeclaration of type of %s", tokname);
  1185. SETTYPE(toklev[j],ty);
  1186. }
  1187. t = gettok();
  1188. if(t == NUMBER) {
  1189. tokset[j].value = numbval;
  1190. if(j < ndefout && j > 3)
  1191. error("please define type number of %s earlier",
  1192. tokset[j].name);
  1193. t = gettok();
  1194. }
  1195. continue;
  1196. }
  1197. break;
  1198. }
  1199. continue;
  1200. case LCURLY:
  1201. defout(0);
  1202. cpycode();
  1203. t = gettok();
  1204. continue;
  1205. default:
  1206. error("syntax error");
  1207. }
  1208. if(t == ENDFILE)
  1209. error("unexpected EOF before %%");
  1210. /* t is MARK */
  1211. Bprint(ftable, "extern int yyerrflag;\n");
  1212. Bprint(ftable, "#ifndef YYMAXDEPTH\n");
  1213. Bprint(ftable, "#define YYMAXDEPTH 150\n");
  1214. Bprint(ftable, "#endif\n" );
  1215. if(!ntypes) {
  1216. Bprint(ftable, "#ifndef YYSTYPE\n");
  1217. Bprint(ftable, "#define YYSTYPE int\n");
  1218. Bprint(ftable, "#endif\n");
  1219. }
  1220. Bprint(ftable, "YYSTYPE yylval;\n");
  1221. Bprint(ftable, "YYSTYPE yyval;\n");
  1222. prdptr[0] = mem;
  1223. /* added production */
  1224. *mem++ = NTBASE;
  1225. /* if start is 0, we will overwrite with the lhs of the first rule */
  1226. *mem++ = start;
  1227. *mem++ = 1;
  1228. *mem++ = 0;
  1229. prdptr[1] = mem;
  1230. while((t=gettok()) == LCURLY)
  1231. cpycode();
  1232. if(t != IDENTCOLON)
  1233. error("bad syntax on first rule");
  1234. if(!start)
  1235. prdptr[0][1] = chfind(1, tokname);
  1236. /* read rules */
  1237. while(t != MARK && t != ENDFILE) {
  1238. /* process a rule */
  1239. rlines[nprod] = lineno;
  1240. if(t == '|')
  1241. *mem++ = *prdptr[nprod-1];
  1242. else
  1243. if(t == IDENTCOLON) {
  1244. *mem = chfind(1, tokname);
  1245. if(*mem < NTBASE)
  1246. error("token illegal on LHS of grammar rule");
  1247. mem++;
  1248. } else
  1249. error("illegal rule: missing semicolon or | ?");
  1250. /* read rule body */
  1251. t = gettok();
  1252. more_rule:
  1253. while(t == IDENTIFIER) {
  1254. *mem = chfind(1, tokname);
  1255. if(*mem < NTBASE)
  1256. levprd[nprod] = toklev[*mem];
  1257. mem++;
  1258. t = gettok();
  1259. }
  1260. if(t == PREC) {
  1261. if(gettok() != IDENTIFIER)
  1262. error("illegal %%prec syntax");
  1263. j = chfind(2, tokname);
  1264. if(j >= NTBASE)
  1265. error("nonterminal %s illegal after %%prec",
  1266. nontrst[j-NTBASE].name);
  1267. levprd[nprod] = toklev[j];
  1268. t = gettok();
  1269. }
  1270. if(t == '=') {
  1271. levprd[nprod] |= ACTFLAG;
  1272. Bprint(faction, "\ncase %d:", nprod);
  1273. cpyact(mem-prdptr[nprod]-1);
  1274. Bprint(faction, " break;");
  1275. if((t=gettok()) == IDENTIFIER) {
  1276. /* action within rule... */
  1277. sprint(actnm, "$$%d", nprod);
  1278. /* make it a nonterminal */
  1279. j = chfind(1, actnm);
  1280. /*
  1281. * the current rule will become rule number nprod+1
  1282. * move the contents down, and make room for the null
  1283. */
  1284. for(p = mem; p >= prdptr[nprod]; --p)
  1285. p[2] = *p;
  1286. mem += 2;
  1287. /* enter null production for action */
  1288. p = prdptr[nprod];
  1289. *p++ = j;
  1290. *p++ = -nprod;
  1291. /* update the production information */
  1292. levprd[nprod+1] = levprd[nprod] & ~ACTFLAG;
  1293. levprd[nprod] = ACTFLAG;
  1294. if(++nprod >= NPROD)
  1295. error("more than %d rules", NPROD);
  1296. prdptr[nprod] = p;
  1297. /* make the action appear in the original rule */
  1298. *mem++ = j;
  1299. /* get some more of the rule */
  1300. goto more_rule;
  1301. }
  1302. }
  1303. while(t == ';')
  1304. t = gettok();
  1305. *mem++ = -nprod;
  1306. /* check that default action is reasonable */
  1307. if(ntypes && !(levprd[nprod]&ACTFLAG) && nontrst[*prdptr[nprod]-NTBASE].value) {
  1308. /* no explicit action, LHS has value */
  1309. int tempty;
  1310. tempty = prdptr[nprod][1];
  1311. if(tempty < 0)
  1312. error("must return a value, since LHS has a type");
  1313. else
  1314. if(tempty >= NTBASE)
  1315. tempty = nontrst[tempty-NTBASE].value;
  1316. else
  1317. tempty = TYPE(toklev[tempty]);
  1318. if(tempty != nontrst[*prdptr[nprod]-NTBASE].value)
  1319. error("default action causes potential type clash");
  1320. }
  1321. nprod++;
  1322. if(nprod >= NPROD)
  1323. error("more than %d rules", NPROD);
  1324. prdptr[nprod] = mem;
  1325. levprd[nprod] = 0;
  1326. }
  1327. /* end of all rules */
  1328. defout(1);
  1329. finact();
  1330. if(t == MARK) {
  1331. Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
  1332. while((c=Bgetrune(finput)) != Beof)
  1333. Bputrune(ftable, c);
  1334. }
  1335. Bterm(finput);
  1336. }
  1337. /*
  1338. * finish action routine
  1339. */
  1340. void
  1341. finact(void)
  1342. {
  1343. Bterm(faction);
  1344. Bprint(ftable, "#define YYEOFCODE %d\n", 1);
  1345. Bprint(ftable, "#define YYERRCODE %d\n", 2);
  1346. }
  1347. /*
  1348. * define s to be a terminal if t=0
  1349. * or a nonterminal if t=1
  1350. */
  1351. int
  1352. defin(int nt, char *s)
  1353. {
  1354. int val;
  1355. Rune rune;
  1356. val = 0;
  1357. if(nt) {
  1358. nnonter++;
  1359. if(nnonter >= NNONTERM)
  1360. error("too many nonterminals, limit %d",NNONTERM);
  1361. nontrst[nnonter].name = cstash(s);
  1362. return NTBASE + nnonter;
  1363. }
  1364. /* must be a token */
  1365. ntokens++;
  1366. if(ntokens >= NTERMS)
  1367. error("too many terminals, limit %d", NTERMS);
  1368. tokset[ntokens].name = cstash(s);
  1369. /* establish value for token */
  1370. /* single character literal */
  1371. if(s[0] == ' ') {
  1372. val = chartorune(&rune, &s[1]);
  1373. if(s[val+1] == 0) {
  1374. val = rune;
  1375. goto out;
  1376. }
  1377. }
  1378. /* escape sequence */
  1379. if(s[0] == ' ' && s[1] == '\\') {
  1380. if(s[3] == 0) {
  1381. /* single character escape sequence */
  1382. switch(s[2]) {
  1383. case 'n': val = '\n'; break;
  1384. case 'r': val = '\r'; break;
  1385. case 'b': val = '\b'; break;
  1386. case 't': val = '\t'; break;
  1387. case 'f': val = '\f'; break;
  1388. case '\'': val = '\''; break;
  1389. case '"': val = '"'; break;
  1390. case '\\': val = '\\'; break;
  1391. default: error("invalid escape");
  1392. }
  1393. goto out;
  1394. }
  1395. /* \nnn sequence */
  1396. if(s[2] >= '0' && s[2] <= '7') {
  1397. if(s[3] < '0' ||
  1398. s[3] > '7' ||
  1399. s[4] < '0' ||
  1400. s[4] > '7' ||
  1401. s[5] != 0)
  1402. error("illegal \\nnn construction");
  1403. val = 64*s[2] + 8*s[3] + s[4] - 73*'0';
  1404. if(val == 0)
  1405. error("'\\000' is illegal");
  1406. goto out;
  1407. }
  1408. error("unknown escape");
  1409. }
  1410. val = extval++;
  1411. out:
  1412. tokset[ntokens].value = val;
  1413. toklev[ntokens] = 0;
  1414. return ntokens;
  1415. }
  1416. /*
  1417. * write out the defines (at the end of the declaration section)
  1418. */
  1419. void
  1420. defout(int last)
  1421. {
  1422. int i, c;
  1423. char sar[NAMESIZE+10];
  1424. for(i=ndefout; i<=ntokens; i++) {
  1425. /* non-literals */
  1426. c = tokset[i].name[0];
  1427. if(c != ' ' && c != '$') {
  1428. Bprint(ftable, "#define %s %d\n",
  1429. tokset[i].name, tokset[i].value);
  1430. if(fdefine)
  1431. Bprint(fdefine, "#define\t%s\t%d\n",
  1432. tokset[i].name, tokset[i].value);
  1433. }
  1434. }
  1435. ndefout = ntokens+1;
  1436. if(last && fdebug) {
  1437. Bprint(fdebug, "char* yytoknames[] =\n{\n");
  1438. TLOOP(i) {
  1439. if(tokset[i].name) {
  1440. chcopy(sar, tokset[i].name);
  1441. Bprint(fdebug, "\t\"%s\",\n", sar);
  1442. continue;
  1443. }
  1444. Bprint(fdebug, "\t0,\n");
  1445. }
  1446. Bprint(fdebug, "};\n");
  1447. }
  1448. }
  1449. char*
  1450. cstash(char *s)
  1451. {
  1452. char *temp;
  1453. temp = cnamp;
  1454. do {
  1455. if(cnamp >= &cnames[cnamsz])
  1456. error("too many characters in id's and literals");
  1457. else
  1458. *cnamp++ = *s;
  1459. } while(*s++);
  1460. return temp;
  1461. }
  1462. long
  1463. gettok(void)
  1464. {
  1465. long c;
  1466. Rune rune;
  1467. int i, base, match, reserve;
  1468. static int peekline;
  1469. begin:
  1470. reserve = 0;
  1471. lineno += peekline;
  1472. peekline = 0;
  1473. c = Bgetrune(finput);
  1474. while(c == ' ' || c == '\n' || c == '\t' || c == '\f') {
  1475. if(c == '\n')
  1476. lineno++;
  1477. c = Bgetrune(finput);
  1478. }
  1479. /* skip comment */
  1480. if(c == '/') {
  1481. lineno += skipcom();
  1482. goto begin;
  1483. }
  1484. switch(c) {
  1485. case Beof:
  1486. return ENDFILE;
  1487. case '{':
  1488. Bungetrune(finput);
  1489. return '=';
  1490. case '<':
  1491. /* get, and look up, a type name (union member name) */
  1492. i = 0;
  1493. while((c=Bgetrune(finput)) != '>' && c >= 0 && c != '\n') {
  1494. rune = c;
  1495. c = runetochar(&tokname[i], &rune);
  1496. if(i < NAMESIZE)
  1497. i += c;
  1498. }
  1499. if(c != '>')
  1500. error("unterminated < ... > clause");
  1501. tokname[i] = 0;
  1502. for(i=1; i<=ntypes; i++)
  1503. if(!strcmp(typeset[i], tokname)) {
  1504. numbval = i;
  1505. return TYPENAME;
  1506. }
  1507. ntypes++;
  1508. numbval = ntypes;
  1509. typeset[numbval] = cstash(tokname);
  1510. return TYPENAME;
  1511. case '"':
  1512. case '\'':
  1513. match = c;
  1514. tokname[0] = ' ';
  1515. i = 1;
  1516. for(;;) {
  1517. c = Bgetrune(finput);
  1518. if(c == '\n' || c <= 0)
  1519. error("illegal or missing ' or \"" );
  1520. if(c == '\\') {
  1521. tokname[i] = '\\';
  1522. if(i < NAMESIZE)
  1523. i++;
  1524. c = Bgetrune(finput);
  1525. } else
  1526. if(c == match)
  1527. break;
  1528. rune = c;
  1529. c = runetochar(&tokname[i], &rune);
  1530. if(i < NAMESIZE)
  1531. i += c;
  1532. }
  1533. break;
  1534. case '%':
  1535. case '\\':
  1536. switch(c = Bgetrune(finput)) {
  1537. case '0': return TERM;
  1538. case '<': return LEFT;
  1539. case '2': return BINARY;
  1540. case '>': return RIGHT;
  1541. case '%':
  1542. case '\\': return MARK;
  1543. case '=': return PREC;
  1544. case '{': return LCURLY;
  1545. default: reserve = 1;
  1546. }
  1547. default:
  1548. /* number */
  1549. if(isdigit(c)) {
  1550. numbval = c-'0';
  1551. base = (c=='0')? 8: 10;
  1552. for(c = Bgetrune(finput); isdigit(c); c = Bgetrune(finput))
  1553. numbval = numbval*base + (c-'0');
  1554. Bungetrune(finput);
  1555. return NUMBER;
  1556. }
  1557. if(islower(c) || isupper(c) || c=='_' || c=='.' || c=='$') {
  1558. i = 0;
  1559. while(islower(c) || isupper(c) || isdigit(c) ||
  1560. c == '-' || c=='_' || c=='.' || c=='$') {
  1561. if(reserve && isupper(c))
  1562. c += 'a'-'A';
  1563. rune = c;
  1564. c = runetochar(&tokname[i], &rune);
  1565. if(i < NAMESIZE)
  1566. i += c;
  1567. c = Bgetrune(finput);
  1568. }
  1569. } else
  1570. return c;
  1571. Bungetrune(finput);
  1572. }
  1573. tokname[i] = 0;
  1574. /* find a reserved word */
  1575. if(reserve) {
  1576. for(c=0; resrv[c].name; c++)
  1577. if(strcmp(tokname, resrv[c].name) == 0)
  1578. return resrv[c].value;
  1579. error("invalid escape, or illegal reserved word: %s", tokname);
  1580. }
  1581. /* look ahead to distinguish IDENTIFIER from IDENTCOLON */
  1582. c = Bgetrune(finput);
  1583. while(c == ' ' || c == '\t'|| c == '\n' || c == '\f' || c == '/') {
  1584. if(c == '\n')
  1585. peekline++;
  1586. /* look for comments */
  1587. if(c == '/')
  1588. peekline += skipcom();
  1589. c = Bgetrune(finput);
  1590. }
  1591. if(c == ':')
  1592. return IDENTCOLON;
  1593. Bungetrune(finput);
  1594. return IDENTIFIER;
  1595. }
  1596. /*
  1597. * determine the type of a symbol
  1598. */
  1599. int
  1600. fdtype(int t)
  1601. {
  1602. int v;
  1603. if(t >= NTBASE)
  1604. v = nontrst[t-NTBASE].value;
  1605. else
  1606. v = TYPE(toklev[t]);
  1607. if(v <= 0)
  1608. error("must specify type for %s", (t>=NTBASE)?
  1609. nontrst[t-NTBASE].name: tokset[t].name);
  1610. return v;
  1611. }
  1612. int
  1613. chfind(int t, char *s)
  1614. {
  1615. int i;
  1616. if(s[0] == ' ')
  1617. t = 0;
  1618. TLOOP(i)
  1619. if(!strcmp(s, tokset[i].name))
  1620. return i;
  1621. NTLOOP(i)
  1622. if(!strcmp(s, nontrst[i].name))
  1623. return NTBASE+i;
  1624. /* cannot find name */
  1625. if(t > 1)
  1626. error("%s should have been defined earlier", s);
  1627. return defin(t, s);
  1628. }
  1629. /*
  1630. * copy the union declaration to the output, and the define file if present
  1631. */
  1632. void
  1633. cpyunion(void)
  1634. {
  1635. long c;
  1636. int level;
  1637. Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
  1638. Bprint(ftable, "typedef union ");
  1639. if(fdefine != 0)
  1640. Bprint(fdefine, "\ntypedef union ");
  1641. level = 0;
  1642. for(;;) {
  1643. if((c=Bgetrune(finput)) == Beof)
  1644. error("EOF encountered while processing %%union");
  1645. Bputrune(ftable, c);
  1646. if(fdefine != 0)
  1647. Bputrune(fdefine, c);
  1648. switch(c) {
  1649. case '\n':
  1650. lineno++;
  1651. break;
  1652. case '{':
  1653. level++;
  1654. break;
  1655. case '}':
  1656. level--;
  1657. /* we are finished copying */
  1658. if(level == 0) {
  1659. Bprint(ftable, " YYSTYPE;\n");
  1660. if(fdefine != 0)
  1661. Bprint(fdefine, "\tYYSTYPE;\nextern\tYYSTYPE\tyylval;\n");
  1662. return;
  1663. }
  1664. }
  1665. }
  1666. }
  1667. /*
  1668. * copies code between \{ and \}
  1669. */
  1670. void
  1671. cpycode(void)
  1672. {
  1673. long c;
  1674. c = Bgetrune(finput);
  1675. if(c == '\n') {
  1676. c = Bgetrune(finput);
  1677. lineno++;
  1678. }
  1679. Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
  1680. while(c != Beof) {
  1681. if(c == '\\') {
  1682. if((c=Bgetrune(finput)) == '}')
  1683. return;
  1684. Bputc(ftable, '\\');
  1685. }
  1686. if(c == '%') {
  1687. if((c=Bgetrune(finput)) == '}')
  1688. return;
  1689. Bputc(ftable, '%');
  1690. }
  1691. Bputrune(ftable, c);
  1692. if(c == '\n')
  1693. lineno++;
  1694. c = Bgetrune(finput);
  1695. }
  1696. error("eof before %%}");
  1697. }
  1698. /*
  1699. * skip over comments
  1700. * skipcom is called after reading a '/'
  1701. */
  1702. int
  1703. skipcom(void)
  1704. {
  1705. long c;
  1706. int i;
  1707. /* i is the number of lines skipped */
  1708. i = 0;
  1709. if(Bgetrune(finput) != '*')
  1710. error("illegal comment");
  1711. c = Bgetrune(finput);
  1712. while(c != Beof) {
  1713. while(c == '*')
  1714. if((c=Bgetrune(finput)) == '/')
  1715. return i;
  1716. if(c == '\n')
  1717. i++;
  1718. c = Bgetrune(finput);
  1719. }
  1720. error("EOF inside comment");
  1721. return 0;
  1722. }
  1723. /*
  1724. * copy C action to the next ; or closing }
  1725. */
  1726. void
  1727. cpyact(int offset)
  1728. {
  1729. long c;
  1730. int brac, match, j, s, fnd, tok;
  1731. Bprint(faction, "\n#line\t%d\t\"%s\"\n", lineno, infile);
  1732. brac = 0;
  1733. loop:
  1734. c = Bgetrune(finput);
  1735. swt:
  1736. switch(c) {
  1737. case ';':
  1738. if(brac == 0) {
  1739. Bputrune(faction, c);
  1740. return;
  1741. }
  1742. goto lcopy;
  1743. case '{':
  1744. brac++;
  1745. goto lcopy;
  1746. case '$':
  1747. s = 1;
  1748. tok = -1;
  1749. c = Bgetrune(finput);
  1750. /* type description */
  1751. if(c == '<') {
  1752. Bungetrune(finput);
  1753. if(gettok() != TYPENAME)
  1754. error("bad syntax on $<ident> clause");
  1755. tok = numbval;
  1756. c = Bgetrune(finput);
  1757. }
  1758. if(c == '$') {
  1759. Bprint(faction, "yyval");
  1760. /* put out the proper tag... */
  1761. if(ntypes) {
  1762. if(tok < 0)
  1763. tok = fdtype(*prdptr[nprod]);
  1764. Bprint(faction, ".%s", typeset[tok]);
  1765. }
  1766. goto loop;
  1767. }
  1768. if(c == '-') {
  1769. s = -s;
  1770. c = Bgetrune(finput);
  1771. }
  1772. if(isdigit(c)) {
  1773. j = 0;
  1774. while(isdigit(c)) {
  1775. j = j*10 + (c-'0');
  1776. c = Bgetrune(finput);
  1777. }
  1778. Bungetrune(finput);
  1779. j = j*s - offset;
  1780. if(j > 0)
  1781. error("Illegal use of $%d", j+offset);
  1782. dollar:
  1783. Bprint(faction, "yypt[-%d].yyv", -j);
  1784. /* put out the proper tag */
  1785. if(ntypes) {
  1786. if(j+offset <= 0 && tok < 0)
  1787. error("must specify type of $%d", j+offset);
  1788. if(tok < 0)
  1789. tok = fdtype(prdptr[nprod][j+offset]);
  1790. Bprint(faction, ".%s", typeset[tok]);
  1791. }
  1792. goto loop;
  1793. }
  1794. if(isupper(c) || islower(c) || c == '_' || c == '.') {
  1795. int tok; /* tok used oustide for type info */
  1796. /* look for $name */
  1797. Bungetrune(finput);
  1798. if(gettok() != IDENTIFIER)
  1799. error("$ must be followed by an identifier");
  1800. tok = chfind(2, tokname);
  1801. if((c = Bgetrune(finput)) != '#') {
  1802. Bungetrune(finput);
  1803. fnd = -1;
  1804. } else
  1805. if(gettok() != NUMBER) {
  1806. error("# must be followed by number");
  1807. fnd = -1;
  1808. } else
  1809. fnd = numbval;
  1810. for(j=1; j<=offset; ++j)
  1811. if(tok == prdptr[nprod][j]) {
  1812. if(--fnd <= 0) {
  1813. j -= offset;
  1814. goto dollar;
  1815. }
  1816. }
  1817. error("$name or $name#number not found");
  1818. }
  1819. Bputc(faction, '$');
  1820. if(s < 0 )
  1821. Bputc(faction, '-');
  1822. goto swt;
  1823. case '}':
  1824. brac--;
  1825. if(brac)
  1826. goto lcopy;
  1827. Bputrune(faction, c);
  1828. return;
  1829. case '/':
  1830. /* look for comments */
  1831. Bputrune(faction, c);
  1832. c = Bgetrune(finput);
  1833. if(c != '*')
  1834. goto swt;
  1835. /* it really is a comment */
  1836. Bputrune(faction, c);
  1837. c = Bgetrune(finput);
  1838. while(c >= 0) {
  1839. while(c == '*') {
  1840. Bputrune(faction, c);
  1841. if((c=Bgetrune(finput)) == '/')
  1842. goto lcopy;
  1843. }
  1844. Bputrune(faction, c);
  1845. if(c == '\n')
  1846. lineno++;
  1847. c = Bgetrune(finput);
  1848. }
  1849. error("EOF inside comment");
  1850. case '\'':
  1851. /* character constant */
  1852. match = '\'';
  1853. goto string;
  1854. case '"':
  1855. /* character string */
  1856. match = '"';
  1857. string:
  1858. Bputrune(faction, c);
  1859. while(c = Bgetrune(finput)) {
  1860. if(c == '\\') {
  1861. Bputrune(faction, c);
  1862. c = Bgetrune(finput);
  1863. if(c == '\n')
  1864. lineno++;
  1865. } else
  1866. if(c == match)
  1867. goto lcopy;
  1868. if(c == '\n')
  1869. error("newline in string or char. const.");
  1870. Bputrune(faction, c);
  1871. }
  1872. error("EOF in string or character constant");
  1873. case Beof:
  1874. error("action does not terminate");
  1875. case '\n':
  1876. lineno++;
  1877. goto lcopy;
  1878. }
  1879. lcopy:
  1880. Bputrune(faction, c);
  1881. goto loop;
  1882. }
  1883. void
  1884. openup(char *stem, int dflag, int vflag, int ytab, char *ytabc)
  1885. {
  1886. char buf[256];
  1887. if(vflag) {
  1888. snprint(buf, sizeof buf, "%s.%s", stem, FILEU);
  1889. foutput = Bopen(buf, OWRITE);
  1890. if(foutput == 0)
  1891. error("cannot open %s", buf);
  1892. }
  1893. if(yydebug) {
  1894. snprint(buf, sizeof buf, "%s.%s", stem, FILEDEBUG);
  1895. if((fdebug = Bopen(buf, OWRITE)) == 0)
  1896. error("can't open %s", buf);
  1897. }
  1898. if(dflag) {
  1899. snprint(buf, sizeof buf, "%s.%s", stem, FILED);
  1900. fdefine = Bopen(buf, OWRITE);
  1901. if(fdefine == 0)
  1902. error("can't create %s", buf);
  1903. }
  1904. if(ytab == 0)
  1905. snprint(buf, sizeof buf, "%s.%s", stem, OFILE);
  1906. else
  1907. strecpy(buf, buf+sizeof buf, ytabc);
  1908. ftable = Bopen(buf, OWRITE);
  1909. if(ftable == 0)
  1910. error("cannot open table file %s", buf);
  1911. }
  1912. /*
  1913. * print the output for the states
  1914. */
  1915. void
  1916. output(void)
  1917. {
  1918. int i, k, c;
  1919. Wset *u, *v;
  1920. Bprint(ftable, "short yyexca[] =\n{");
  1921. if(fdebug)
  1922. Bprint(fdebug, "char* yystates[] =\n{\n");
  1923. /* output the stuff for state i */
  1924. SLOOP(i) {
  1925. nolook = tystate[i]!=MUSTLOOKAHEAD;
  1926. closure(i);
  1927. /* output actions */
  1928. nolook = 1;
  1929. aryfil(temp1, ntokens+nnonter+1, 0);
  1930. WSLOOP(wsets, u) {
  1931. c = *(u->pitem);
  1932. if(c > 1 && c < NTBASE && temp1[c] == 0) {
  1933. WSLOOP(u, v)
  1934. if(c == *(v->pitem))
  1935. putitem(v->pitem+1, (Lkset*)0);
  1936. temp1[c] = state(c);
  1937. } else
  1938. if(c > NTBASE && temp1[(c -= NTBASE) + ntokens] == 0)
  1939. temp1[c+ntokens] = amem[indgo[i]+c];
  1940. }
  1941. if(i == 1)
  1942. temp1[1] = ACCEPTCODE;
  1943. /* now, we have the shifts; look at the reductions */
  1944. lastred = 0;
  1945. WSLOOP(wsets, u) {
  1946. c = *u->pitem;
  1947. /* reduction */
  1948. if(c <= 0) {
  1949. lastred = -c;
  1950. TLOOP(k)
  1951. if(BIT(u->ws.lset, k)) {
  1952. if(temp1[k] == 0)
  1953. temp1[k] = c;
  1954. else
  1955. if(temp1[k] < 0) { /* reduce/reduce conflict */
  1956. if(foutput)
  1957. Bprint(foutput,
  1958. "\n%d: reduce/reduce conflict"
  1959. " (red'ns %d and %d ) on %s",
  1960. i, -temp1[k], lastred,
  1961. symnam(k));
  1962. if(-temp1[k] > lastred)
  1963. temp1[k] = -lastred;
  1964. zzrrconf++;
  1965. } else
  1966. /* potential shift/reduce conflict */
  1967. precftn( lastred, k, i );
  1968. }
  1969. }
  1970. }
  1971. wract(i);
  1972. }
  1973. if(fdebug)
  1974. Bprint(fdebug, "};\n");
  1975. Bprint(ftable, "};\n");
  1976. Bprint(ftable, "#define YYNPROD %d\n", nprod);
  1977. Bprint(ftable, "#define YYPRIVATE %d\n", PRIVATE);
  1978. if(yydebug)
  1979. Bprint(ftable, "#define yydebug %s\n", yydebug);
  1980. }
  1981. /*
  1982. * pack state i from temp1 into amem
  1983. */
  1984. int
  1985. apack(int *p, int n)
  1986. {
  1987. int *pp, *qq, *rr, off, *q, *r;
  1988. /* we don't need to worry about checking because
  1989. * we will only look at entries known to be there...
  1990. * eliminate leading and trailing 0's
  1991. */
  1992. q = p+n;
  1993. for(pp = p, off = 0; *pp == 0 && pp <= q; ++pp, --off)
  1994. ;
  1995. /* no actions */
  1996. if(pp > q)
  1997. return 0;
  1998. p = pp;
  1999. /* now, find a place for the elements from p to q, inclusive */
  2000. r = &amem[ACTSIZE-1];
  2001. for(rr = amem; rr <= r; rr++, off++) {
  2002. for(qq = rr, pp = p; pp <= q; pp++, qq++)
  2003. if(*pp != 0)
  2004. if(*pp != *qq && *qq != 0)
  2005. goto nextk;
  2006. /* we have found an acceptable k */
  2007. if(pkdebug && foutput != 0)
  2008. Bprint(foutput, "off = %d, k = %d\n", off, (int)(rr-amem));
  2009. for(qq = rr, pp = p; pp <= q; pp++, qq++)
  2010. if(*pp) {
  2011. if(qq > r)
  2012. error("action table overflow");
  2013. if(qq > memp)
  2014. memp = qq;
  2015. *qq = *pp;
  2016. }
  2017. if(pkdebug && foutput != 0)
  2018. for(pp = amem; pp <= memp; pp += 10) {
  2019. Bprint(foutput, "\t");
  2020. for(qq = pp; qq <= pp+9; qq++)
  2021. Bprint(foutput, "%d ", *qq);
  2022. Bprint(foutput, "\n");
  2023. }
  2024. return(off);
  2025. nextk:;
  2026. }
  2027. error("no space in action table");
  2028. return 0;
  2029. }
  2030. /*
  2031. * output the gotos for the nontermninals
  2032. */
  2033. void
  2034. go2out(void)
  2035. {
  2036. int i, j, k, best, count, cbest, times;
  2037. /* mark begining of gotos */
  2038. Bprint(ftemp, "$\n");
  2039. for(i = 1; i <= nnonter; i++) {
  2040. go2gen(i);
  2041. /* find the best one to make default */
  2042. best = -1;
  2043. times = 0;
  2044. /* is j the most frequent */
  2045. for(j = 0; j <= nstate; j++) {
  2046. if(tystate[j] == 0)
  2047. continue;
  2048. if(tystate[j] == best)
  2049. continue;
  2050. /* is tystate[j] the most frequent */
  2051. count = 0;
  2052. cbest = tystate[j];
  2053. for(k = j; k <= nstate; k++)
  2054. if(tystate[k] == cbest)
  2055. count++;
  2056. if(count > times) {
  2057. best = cbest;
  2058. times = count;
  2059. }
  2060. }
  2061. /* best is now the default entry */
  2062. zzgobest += times-1;
  2063. for(j = 0; j <= nstate; j++)
  2064. if(tystate[j] != 0 && tystate[j] != best) {
  2065. Bprint(ftemp, "%d,%d,", j, tystate[j]);
  2066. zzgoent++;
  2067. }
  2068. /* now, the default */
  2069. if(best == -1)
  2070. best = 0;
  2071. zzgoent++;
  2072. Bprint(ftemp, "%d\n", best);
  2073. }
  2074. }
  2075. /*
  2076. * output the gotos for nonterminal c
  2077. */
  2078. void
  2079. go2gen(int c)
  2080. {
  2081. int i, work, cc;
  2082. Item *p, *q;
  2083. /* first, find nonterminals with gotos on c */
  2084. aryfil(temp1, nnonter+1, 0);
  2085. temp1[c] = 1;
  2086. work = 1;
  2087. while(work) {
  2088. work = 0;
  2089. PLOOP(0, i)
  2090. /* cc is a nonterminal */
  2091. if((cc=prdptr[i][1]-NTBASE) >= 0)
  2092. /* cc has a goto on c */
  2093. if(temp1[cc] != 0) {
  2094. /* thus, the left side of production i does too */
  2095. cc = *prdptr[i]-NTBASE;
  2096. if(temp1[cc] == 0) {
  2097. work = 1;
  2098. temp1[cc] = 1;
  2099. }
  2100. }
  2101. }
  2102. /* now, we have temp1[c] = 1 if a goto on c in closure of cc */
  2103. if(g2debug && foutput != 0) {
  2104. Bprint(foutput, "%s: gotos on ", nontrst[c].name);
  2105. NTLOOP(i)
  2106. if(temp1[i])
  2107. Bprint(foutput, "%s ", nontrst[i].name);
  2108. Bprint(foutput, "\n");
  2109. }
  2110. /* now, go through and put gotos into tystate */
  2111. aryfil(tystate, nstate, 0);
  2112. SLOOP(i)
  2113. ITMLOOP(i, p, q)
  2114. if((cc = *p->pitem) >= NTBASE)
  2115. /* goto on c is possible */
  2116. if(temp1[cc-NTBASE]) {
  2117. tystate[i] = amem[indgo[i]+c];
  2118. break;
  2119. }
  2120. }
  2121. /*
  2122. * decide a shift/reduce conflict by precedence.
  2123. * r is a rule number, t a token number
  2124. * the conflict is in state s
  2125. * temp1[t] is changed to reflect the action
  2126. */
  2127. void
  2128. precftn(int r, int t, int s)
  2129. {
  2130. int lp, lt, action;
  2131. lp = levprd[r];
  2132. lt = toklev[t];
  2133. if(PLEVEL(lt) == 0 || PLEVEL(lp) == 0) {
  2134. /* conflict */
  2135. if(foutput != 0)
  2136. Bprint(foutput,
  2137. "\n%d: shift/reduce conflict (shift %d(%d), red'n %d(%d)) on %s",
  2138. s, temp1[t], PLEVEL(lt), r, PLEVEL(lp), symnam(t));
  2139. zzsrconf++;
  2140. return;
  2141. }
  2142. if(PLEVEL(lt) == PLEVEL(lp))
  2143. action = ASSOC(lt);
  2144. else
  2145. if(PLEVEL(lt) > PLEVEL(lp))
  2146. action = RASC; /* shift */
  2147. else
  2148. action = LASC; /* reduce */
  2149. switch(action) {
  2150. case BASC: /* error action */
  2151. temp1[t] = ERRCODE;
  2152. break;
  2153. case LASC: /* reduce */
  2154. temp1[t] = -r;
  2155. break;
  2156. }
  2157. }
  2158. /*
  2159. * output state i
  2160. * temp1 has the actions, lastred the default
  2161. */
  2162. void
  2163. wract(int i)
  2164. {
  2165. int p, p0, p1, ntimes, tred, count, j, flag;
  2166. /* find the best choice for lastred */
  2167. lastred = 0;
  2168. ntimes = 0;
  2169. TLOOP(j) {
  2170. if(temp1[j] >= 0)
  2171. continue;
  2172. if(temp1[j]+lastred == 0)
  2173. continue;
  2174. /* count the number of appearances of temp1[j] */
  2175. count = 0;
  2176. tred = -temp1[j];
  2177. levprd[tred] |= REDFLAG;
  2178. TLOOP(p)
  2179. if(temp1[p]+tred == 0)
  2180. count++;
  2181. if(count > ntimes) {
  2182. lastred = tred;
  2183. ntimes = count;
  2184. }
  2185. }
  2186. /*
  2187. * for error recovery, arrange that, if there is a shift on the
  2188. * error recovery token, `error', that the default be the error action
  2189. */
  2190. if(temp1[2] > 0)
  2191. lastred = 0;
  2192. /* clear out entries in temp1 which equal lastred */
  2193. TLOOP(p)
  2194. if(temp1[p]+lastred == 0)
  2195. temp1[p] = 0;
  2196. wrstate(i);
  2197. defact[i] = lastred;
  2198. flag = 0;
  2199. TLOOP(p0)
  2200. if((p1=temp1[p0]) != 0) {
  2201. if(p1 < 0) {
  2202. p1 = -p1;
  2203. goto exc;
  2204. }
  2205. if(p1 == ACCEPTCODE) {
  2206. p1 = -1;
  2207. goto exc;
  2208. }
  2209. if(p1 == ERRCODE) {
  2210. p1 = 0;
  2211. exc:
  2212. if(flag++ == 0)
  2213. Bprint(ftable, "-1, %d,\n", i);
  2214. Bprint(ftable, "\t%d, %d,\n", p0, p1);
  2215. zzexcp++;
  2216. continue;
  2217. }
  2218. Bprint(ftemp, "%d,%d,", p0, p1);
  2219. zzacent++;
  2220. }
  2221. if(flag) {
  2222. defact[i] = -2;
  2223. Bprint(ftable, "\t-2, %d,\n", lastred);
  2224. }
  2225. Bprint(ftemp, "\n");
  2226. }
  2227. /*
  2228. * writes state i
  2229. */
  2230. void
  2231. wrstate(int i)
  2232. {
  2233. int j0, j1;
  2234. Item *pp, *qq;
  2235. Wset *u;
  2236. if(fdebug) {
  2237. if(lastred) {
  2238. Bprint(fdebug, " 0, /*%d*/\n", i);
  2239. } else {
  2240. Bprint(fdebug, " \"");
  2241. ITMLOOP(i, pp, qq)
  2242. Bprint(fdebug, "%s\\n", writem(pp->pitem));
  2243. if(tystate[i] == MUSTLOOKAHEAD)
  2244. WSLOOP(wsets + (pstate[i+1] - pstate[i]), u)
  2245. if(*u->pitem < 0)
  2246. Bprint(fdebug, "%s\\n", writem(u->pitem));
  2247. Bprint(fdebug, "\", /*%d*/\n", i);
  2248. }
  2249. }
  2250. if(foutput == 0)
  2251. return;
  2252. Bprint(foutput, "\nstate %d\n", i);
  2253. ITMLOOP(i, pp, qq)
  2254. Bprint(foutput, "\t%s\n", writem(pp->pitem));
  2255. if(tystate[i] == MUSTLOOKAHEAD)
  2256. /* print out empty productions in closure */
  2257. WSLOOP(wsets+(pstate[i+1]-pstate[i]), u)
  2258. if(*u->pitem < 0)
  2259. Bprint(foutput, "\t%s\n", writem(u->pitem));
  2260. /* check for state equal to another */
  2261. TLOOP(j0)
  2262. if((j1=temp1[j0]) != 0) {
  2263. Bprint(foutput, "\n\t%s ", symnam(j0));
  2264. /* shift, error, or accept */
  2265. if(j1 > 0) {
  2266. if(j1 == ACCEPTCODE)
  2267. Bprint(foutput, "accept");
  2268. else
  2269. if(j1 == ERRCODE)
  2270. Bprint(foutput, "error");
  2271. else
  2272. Bprint(foutput, "shift %d", j1);
  2273. } else
  2274. Bprint(foutput, "reduce %d (src line %d)", -j1, rlines[-j1]);
  2275. }
  2276. /* output the final production */
  2277. if(lastred)
  2278. Bprint(foutput, "\n\t. reduce %d (src line %d)\n\n",
  2279. lastred, rlines[lastred]);
  2280. else
  2281. Bprint(foutput, "\n\t. error\n\n");
  2282. /* now, output nonterminal actions */
  2283. j1 = ntokens;
  2284. for(j0 = 1; j0 <= nnonter; j0++) {
  2285. j1++;
  2286. if(temp1[j1])
  2287. Bprint(foutput, "\t%s goto %d\n", symnam(j0+NTBASE), temp1[j1]);
  2288. }
  2289. }
  2290. void
  2291. warray(char *s, int *v, int n)
  2292. {
  2293. int i;
  2294. Bprint(ftable, "short %s[] =\n{", s);
  2295. for(i=0;;) {
  2296. if(i%10 == 0)
  2297. Bprint(ftable, "\n");
  2298. Bprint(ftable, "%4d", v[i]);
  2299. i++;
  2300. if(i >= n) {
  2301. Bprint(ftable, "\n};\n");
  2302. break;
  2303. }
  2304. Bprint(ftable, ",");
  2305. }
  2306. }
  2307. /*
  2308. * in order to free up the mem and amem arrays for the optimizer,
  2309. * and still be able to output yyr1, etc., after the sizes of
  2310. * the action array is known, we hide the nonterminals
  2311. * derived by productions in levprd.
  2312. */
  2313. void
  2314. hideprod(void)
  2315. {
  2316. int i, j;
  2317. j = 0;
  2318. levprd[0] = 0;
  2319. PLOOP(1, i) {
  2320. if(!(levprd[i] & REDFLAG)) {
  2321. j++;
  2322. if(foutput != 0)
  2323. Bprint(foutput, "Rule not reduced: %s\n", writem(prdptr[i]));
  2324. }
  2325. levprd[i] = *prdptr[i] - NTBASE;
  2326. }
  2327. if(j)
  2328. print("%d rules never reduced\n", j);
  2329. }
  2330. void
  2331. callopt(void)
  2332. {
  2333. int i, *p, j, k, *q;
  2334. /* read the arrays from tempfile and set parameters */
  2335. finput = Bopen(tempname, OREAD);
  2336. if(finput == 0)
  2337. error("optimizer cannot open tempfile");
  2338. pgo[0] = 0;
  2339. temp1[0] = 0;
  2340. nstate = 0;
  2341. nnonter = 0;
  2342. for(;;) {
  2343. switch(gtnm()) {
  2344. case '\n':
  2345. nstate++;
  2346. pmem--;
  2347. temp1[nstate] = pmem - mem0;
  2348. case ',':
  2349. continue;
  2350. case '$':
  2351. break;
  2352. default:
  2353. error("bad tempfile");
  2354. }
  2355. break;
  2356. }
  2357. pmem--;
  2358. temp1[nstate] = yypgo[0] = pmem - mem0;
  2359. for(;;) {
  2360. switch(gtnm()) {
  2361. case '\n':
  2362. nnonter++;
  2363. yypgo[nnonter] = pmem-mem0;
  2364. case ',':
  2365. continue;
  2366. case -1:
  2367. break;
  2368. default:
  2369. error("bad tempfile");
  2370. }
  2371. break;
  2372. }
  2373. pmem--;
  2374. yypgo[nnonter--] = pmem - mem0;
  2375. for(i = 0; i < nstate; i++) {
  2376. k = 32000;
  2377. j = 0;
  2378. q = mem0 + temp1[i+1];
  2379. for(p = mem0 + temp1[i]; p < q ; p += 2) {
  2380. if(*p > j)
  2381. j = *p;
  2382. if(*p < k)
  2383. k = *p;
  2384. }
  2385. /* nontrivial situation */
  2386. if(k <= j) {
  2387. /* j is now the range */
  2388. /* j -= k; /* call scj */
  2389. if(k > maxoff)
  2390. maxoff = k;
  2391. }
  2392. tystate[i] = (temp1[i+1]-temp1[i]) + 2*j;
  2393. if(j > maxspr)
  2394. maxspr = j;
  2395. }
  2396. /* initialize ggreed table */
  2397. for(i = 1; i <= nnonter; i++) {
  2398. ggreed[i] = 1;
  2399. j = 0;
  2400. /* minimum entry index is always 0 */
  2401. q = mem0 + yypgo[i+1] - 1;
  2402. for(p = mem0+yypgo[i]; p < q ; p += 2) {
  2403. ggreed[i] += 2;
  2404. if(*p > j)
  2405. j = *p;
  2406. }
  2407. ggreed[i] = ggreed[i] + 2*j;
  2408. if(j > maxoff)
  2409. maxoff = j;
  2410. }
  2411. /* now, prepare to put the shift actions into the amem array */
  2412. for(i = 0; i < ACTSIZE; i++)
  2413. amem[i] = 0;
  2414. maxa = amem;
  2415. for(i = 0; i < nstate; i++) {
  2416. if(tystate[i] == 0 && adb > 1)
  2417. Bprint(ftable, "State %d: null\n", i);
  2418. indgo[i] = YYFLAG1;
  2419. }
  2420. while((i = nxti()) != NOMORE)
  2421. if(i >= 0)
  2422. stin(i);
  2423. else
  2424. gin(-i);
  2425. /* print amem array */
  2426. if(adb > 2 )
  2427. for(p = amem; p <= maxa; p += 10) {
  2428. Bprint(ftable, "%4d ", (int)(p-amem));
  2429. for(i = 0; i < 10; ++i)
  2430. Bprint(ftable, "%4d ", p[i]);
  2431. Bprint(ftable, "\n");
  2432. }
  2433. /* write out the output appropriate to the language */
  2434. aoutput();
  2435. osummary();
  2436. ZAPFILE(tempname);
  2437. }
  2438. void
  2439. gin(int i)
  2440. {
  2441. int *p, *r, *s, *q1, *q2;
  2442. /* enter gotos on nonterminal i into array amem */
  2443. ggreed[i] = 0;
  2444. q2 = mem0+ yypgo[i+1] - 1;
  2445. q1 = mem0 + yypgo[i];
  2446. /* now, find amem place for it */
  2447. for(p = amem; p < &amem[ACTSIZE]; p++) {
  2448. if(*p)
  2449. continue;
  2450. for(r = q1; r < q2; r += 2) {
  2451. s = p + *r + 1;
  2452. if(*s)
  2453. goto nextgp;
  2454. if(s > maxa)
  2455. if((maxa = s) > &amem[ACTSIZE])
  2456. error("a array overflow");
  2457. }
  2458. /* we have found amem spot */
  2459. *p = *q2;
  2460. if(p > maxa)
  2461. if((maxa = p) > &amem[ACTSIZE])
  2462. error("a array overflow");
  2463. for(r = q1; r < q2; r += 2) {
  2464. s = p + *r + 1;
  2465. *s = r[1];
  2466. }
  2467. pgo[i] = p-amem;
  2468. if(adb > 1)
  2469. Bprint(ftable, "Nonterminal %d, entry at %d\n", i, pgo[i]);
  2470. return;
  2471. nextgp:;
  2472. }
  2473. error("cannot place goto %d\n", i);
  2474. }
  2475. void
  2476. stin(int i)
  2477. {
  2478. int *r, *s, n, flag, j, *q1, *q2;
  2479. tystate[i] = 0;
  2480. /* enter state i into the amem array */
  2481. q2 = mem0+temp1[i+1];
  2482. q1 = mem0+temp1[i];
  2483. /* find an acceptable place */
  2484. for(n = -maxoff; n < ACTSIZE; n++) {
  2485. flag = 0;
  2486. for(r = q1; r < q2; r += 2) {
  2487. if((s = *r + n + amem) < amem)
  2488. goto nextn;
  2489. if(*s == 0)
  2490. flag++;
  2491. else
  2492. if(*s != r[1])
  2493. goto nextn;
  2494. }
  2495. /* check that the position equals another only if the states are identical */
  2496. for(j=0; j<nstate; j++) {
  2497. if(indgo[j] == n) {
  2498. /* we have some disagreement */
  2499. if(flag)
  2500. goto nextn;
  2501. if(temp1[j+1]+temp1[i] == temp1[j]+temp1[i+1]) {
  2502. /* states are equal */
  2503. indgo[i] = n;
  2504. if(adb > 1)
  2505. Bprint(ftable,
  2506. "State %d: entry at %d equals state %d\n",
  2507. i, n, j);
  2508. return;
  2509. }
  2510. /* we have some disagreement */
  2511. goto nextn;
  2512. }
  2513. }
  2514. for(r = q1; r < q2; r += 2) {
  2515. if((s = *r+n+amem) >= &amem[ACTSIZE])
  2516. error("out of space in optimizer a array");
  2517. if(s > maxa)
  2518. maxa = s;
  2519. if(*s != 0 && *s != r[1])
  2520. error("clobber of a array, pos'n %d, by %d", s-amem, r[1]);
  2521. *s = r[1];
  2522. }
  2523. indgo[i] = n;
  2524. if(adb > 1)
  2525. Bprint(ftable, "State %d: entry at %d\n", i, indgo[i]);
  2526. return;
  2527. nextn:;
  2528. }
  2529. error("Error; failure to place state %d\n", i);
  2530. }
  2531. /*
  2532. * finds the next i
  2533. */
  2534. int
  2535. nxti(void)
  2536. {
  2537. int i, max, maxi;
  2538. max = 0;
  2539. maxi = 0;
  2540. for(i = 1; i <= nnonter; i++)
  2541. if(ggreed[i] >= max) {
  2542. max = ggreed[i];
  2543. maxi = -i;
  2544. }
  2545. for(i = 0; i < nstate; ++i)
  2546. if(tystate[i] >= max) {
  2547. max = tystate[i];
  2548. maxi = i;
  2549. }
  2550. if(nxdb)
  2551. Bprint(ftable, "nxti = %d, max = %d\n", maxi, max);
  2552. if(max == 0)
  2553. return NOMORE;
  2554. return maxi;
  2555. }
  2556. /*
  2557. * write summary
  2558. */
  2559. void
  2560. osummary(void)
  2561. {
  2562. int i, *p;
  2563. if(foutput == 0)
  2564. return;
  2565. i = 0;
  2566. for(p = maxa; p >= amem; p--)
  2567. if(*p == 0)
  2568. i++;
  2569. Bprint(foutput, "Optimizer space used: input %d/%d, output %d/%d\n",
  2570. (int)(pmem-mem0+1), MEMSIZE, (int)(maxa-amem+1), ACTSIZE);
  2571. Bprint(foutput, "%d table entries, %d zero\n", (int)(maxa-amem+1), i);
  2572. Bprint(foutput, "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff);
  2573. }
  2574. /*
  2575. * this version is for C
  2576. * write out the optimized parser
  2577. */
  2578. void
  2579. aoutput(void)
  2580. {
  2581. Bprint(ftable, "#define\tYYLAST\t%d\n", (int)(maxa-amem+1));
  2582. arout("yyact", amem, (maxa-amem)+1);
  2583. arout("yypact", indgo, nstate);
  2584. arout("yypgo", pgo, nnonter+1);
  2585. }
  2586. void
  2587. arout(char *s, int *v, int n)
  2588. {
  2589. int i;
  2590. Bprint(ftable, "short %s[] =\n{", s);
  2591. for(i = 0; i < n;) {
  2592. if(i%10 == 0)
  2593. Bprint(ftable, "\n");
  2594. Bprint(ftable, "%4d", v[i]);
  2595. i++;
  2596. if(i == n)
  2597. Bprint(ftable, "\n};\n");
  2598. else
  2599. Bprint(ftable, ",");
  2600. }
  2601. }
  2602. /*
  2603. * read and convert an integer from the standard input
  2604. * return the terminating character
  2605. * blanks, tabs, and newlines are ignored
  2606. */
  2607. int
  2608. gtnm(void)
  2609. {
  2610. int sign, val, c;
  2611. sign = 0;
  2612. val = 0;
  2613. while((c=Bgetrune(finput)) != Beof) {
  2614. if(isdigit(c)) {
  2615. val = val*10 + c-'0';
  2616. continue;
  2617. }
  2618. if(c == '-') {
  2619. sign = 1;
  2620. continue;
  2621. }
  2622. break;
  2623. }
  2624. if(sign)
  2625. val = -val;
  2626. *pmem++ = val;
  2627. if(pmem >= &mem0[MEMSIZE])
  2628. error("out of space");
  2629. return c;
  2630. }