yacc.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933
  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. setup(int argc, char *argv[])
  1029. {
  1030. long c, t;
  1031. int i, j, lev, ty, ytab, *p;
  1032. int vflag, dflag, stem;
  1033. char actnm[8], *stemc, *s, dirbuf[128];
  1034. ytab = 0;
  1035. vflag = 0;
  1036. dflag = 0;
  1037. stem = 0;
  1038. stemc = "y";
  1039. foutput = 0;
  1040. fdefine = 0;
  1041. fdebug = 0;
  1042. ARGBEGIN{
  1043. case 'v':
  1044. case 'V':
  1045. vflag++;
  1046. break;
  1047. case 'D':
  1048. yydebug = ARGF();
  1049. break;
  1050. case 'd':
  1051. dflag++;
  1052. break;
  1053. case 'o':
  1054. ytab++;
  1055. ytabc = ARGF();
  1056. break;
  1057. case 's':
  1058. stem++;
  1059. stemc = ARGF();
  1060. break;
  1061. case 'S':
  1062. parser = PARSERS;
  1063. break;
  1064. default:
  1065. error("illegal option: %c", ARGC());
  1066. }ARGEND
  1067. openup(stemc, dflag, vflag, ytab, ytabc);
  1068. ftemp = Bopen(tempname = mktemp(ttempname), OWRITE);
  1069. faction = Bopen(actname = mktemp(tactname), OWRITE);
  1070. if(ftemp == 0 || faction == 0)
  1071. error("cannot open temp file");
  1072. if(argc < 1)
  1073. error("no input file");
  1074. infile = argv[0];
  1075. if(infile[0] != '/' && getwd(dirbuf, sizeof dirbuf)!=nil){
  1076. i = strlen(infile)+1+strlen(dirbuf)+1+10;
  1077. s = malloc(i);
  1078. if(s != nil){
  1079. snprint(s, i, "%s/%s", dirbuf, infile);
  1080. cleanname(s);
  1081. infile = s;
  1082. }
  1083. }
  1084. finput = Bopen(infile, OREAD);
  1085. if(finput == 0)
  1086. error("cannot open '%s'", argv[0]);
  1087. cnamp = cnames;
  1088. defin(0, "$end");
  1089. extval = PRIVATE; /* tokens start in unicode 'private use' */
  1090. defin(0, "error");
  1091. defin(1, "$accept");
  1092. defin(0, "$unk");
  1093. mem = mem0;
  1094. i = 0;
  1095. for(t = gettok(); t != MARK && t != ENDFILE;)
  1096. switch(t) {
  1097. case ';':
  1098. t = gettok();
  1099. break;
  1100. case START:
  1101. if(gettok() != IDENTIFIER)
  1102. error("bad %%start construction");
  1103. start = chfind(1, tokname);
  1104. t = gettok();
  1105. continue;
  1106. case TYPEDEF:
  1107. if(gettok() != TYPENAME)
  1108. error("bad syntax in %%type");
  1109. ty = numbval;
  1110. for(;;) {
  1111. t = gettok();
  1112. switch(t) {
  1113. case IDENTIFIER:
  1114. if((t=chfind(1, tokname)) < NTBASE) {
  1115. j = TYPE(toklev[t]);
  1116. if(j != 0 && j != ty)
  1117. error("type redeclaration of token %s",
  1118. tokset[t].name);
  1119. else
  1120. SETTYPE(toklev[t], ty);
  1121. } else {
  1122. j = nontrst[t-NTBASE].value;
  1123. if(j != 0 && j != ty)
  1124. error("type redeclaration of nonterminal %s",
  1125. nontrst[t-NTBASE].name );
  1126. else
  1127. nontrst[t-NTBASE].value = ty;
  1128. }
  1129. case ',':
  1130. continue;
  1131. case ';':
  1132. t = gettok();
  1133. default:
  1134. break;
  1135. }
  1136. break;
  1137. }
  1138. continue;
  1139. case UNION:
  1140. /* copy the union declaration to the output */
  1141. cpyunion();
  1142. t = gettok();
  1143. continue;
  1144. case LEFT:
  1145. case BINARY:
  1146. case RIGHT:
  1147. i++;
  1148. case TERM:
  1149. /* nonzero means new prec. and assoc. */
  1150. lev = t-TERM;
  1151. ty = 0;
  1152. /* get identifiers so defined */
  1153. t = gettok();
  1154. /* there is a type defined */
  1155. if(t == TYPENAME) {
  1156. ty = numbval;
  1157. t = gettok();
  1158. }
  1159. for(;;) {
  1160. switch(t) {
  1161. case ',':
  1162. t = gettok();
  1163. continue;
  1164. case ';':
  1165. break;
  1166. case IDENTIFIER:
  1167. j = chfind(0, tokname);
  1168. if(j >= NTBASE)
  1169. error("%s defined earlier as nonterminal", tokname);
  1170. if(lev) {
  1171. if(ASSOC(toklev[j]))
  1172. error("redeclaration of precedence of %s", tokname);
  1173. SETASC(toklev[j], lev);
  1174. SETPLEV(toklev[j], i);
  1175. }
  1176. if(ty) {
  1177. if(TYPE(toklev[j]))
  1178. error("redeclaration of type of %s", tokname);
  1179. SETTYPE(toklev[j],ty);
  1180. }
  1181. t = gettok();
  1182. if(t == NUMBER) {
  1183. tokset[j].value = numbval;
  1184. if(j < ndefout && j > 3)
  1185. error("please define type number of %s earlier",
  1186. tokset[j].name);
  1187. t = gettok();
  1188. }
  1189. continue;
  1190. }
  1191. break;
  1192. }
  1193. continue;
  1194. case LCURLY:
  1195. defout(0);
  1196. cpycode();
  1197. t = gettok();
  1198. continue;
  1199. default:
  1200. error("syntax error");
  1201. }
  1202. if(t == ENDFILE)
  1203. error("unexpected EOF before %%");
  1204. /* t is MARK */
  1205. Bprint(ftable, "extern int yyerrflag;\n");
  1206. Bprint(ftable, "#ifndef YYMAXDEPTH\n");
  1207. Bprint(ftable, "#define YYMAXDEPTH 150\n");
  1208. Bprint(ftable, "#endif\n" );
  1209. if(!ntypes) {
  1210. Bprint(ftable, "#ifndef YYSTYPE\n");
  1211. Bprint(ftable, "#define YYSTYPE int\n");
  1212. Bprint(ftable, "#endif\n");
  1213. }
  1214. Bprint(ftable, "YYSTYPE yylval;\n");
  1215. Bprint(ftable, "YYSTYPE yyval;\n");
  1216. prdptr[0] = mem;
  1217. /* added production */
  1218. *mem++ = NTBASE;
  1219. /* if start is 0, we will overwrite with the lhs of the first rule */
  1220. *mem++ = start;
  1221. *mem++ = 1;
  1222. *mem++ = 0;
  1223. prdptr[1] = mem;
  1224. while((t=gettok()) == LCURLY)
  1225. cpycode();
  1226. if(t != IDENTCOLON)
  1227. error("bad syntax on first rule");
  1228. if(!start)
  1229. prdptr[0][1] = chfind(1, tokname);
  1230. /* read rules */
  1231. while(t != MARK && t != ENDFILE) {
  1232. /* process a rule */
  1233. rlines[nprod] = lineno;
  1234. if(t == '|')
  1235. *mem++ = *prdptr[nprod-1];
  1236. else
  1237. if(t == IDENTCOLON) {
  1238. *mem = chfind(1, tokname);
  1239. if(*mem < NTBASE)
  1240. error("token illegal on LHS of grammar rule");
  1241. mem++;
  1242. } else
  1243. error("illegal rule: missing semicolon or | ?");
  1244. /* read rule body */
  1245. t = gettok();
  1246. more_rule:
  1247. while(t == IDENTIFIER) {
  1248. *mem = chfind(1, tokname);
  1249. if(*mem < NTBASE)
  1250. levprd[nprod] = toklev[*mem];
  1251. mem++;
  1252. t = gettok();
  1253. }
  1254. if(t == PREC) {
  1255. if(gettok() != IDENTIFIER)
  1256. error("illegal %%prec syntax");
  1257. j = chfind(2, tokname);
  1258. if(j >= NTBASE)
  1259. error("nonterminal %s illegal after %%prec",
  1260. nontrst[j-NTBASE].name);
  1261. levprd[nprod] = toklev[j];
  1262. t = gettok();
  1263. }
  1264. if(t == '=') {
  1265. levprd[nprod] |= ACTFLAG;
  1266. Bprint(faction, "\ncase %d:", nprod);
  1267. cpyact(mem-prdptr[nprod]-1);
  1268. Bprint(faction, " break;");
  1269. if((t=gettok()) == IDENTIFIER) {
  1270. /* action within rule... */
  1271. sprint(actnm, "$$%d", nprod);
  1272. /* make it a nonterminal */
  1273. j = chfind(1, actnm);
  1274. /*
  1275. * the current rule will become rule number nprod+1
  1276. * move the contents down, and make room for the null
  1277. */
  1278. for(p = mem; p >= prdptr[nprod]; --p)
  1279. p[2] = *p;
  1280. mem += 2;
  1281. /* enter null production for action */
  1282. p = prdptr[nprod];
  1283. *p++ = j;
  1284. *p++ = -nprod;
  1285. /* update the production information */
  1286. levprd[nprod+1] = levprd[nprod] & ~ACTFLAG;
  1287. levprd[nprod] = ACTFLAG;
  1288. if(++nprod >= NPROD)
  1289. error("more than %d rules", NPROD);
  1290. prdptr[nprod] = p;
  1291. /* make the action appear in the original rule */
  1292. *mem++ = j;
  1293. /* get some more of the rule */
  1294. goto more_rule;
  1295. }
  1296. }
  1297. while(t == ';')
  1298. t = gettok();
  1299. *mem++ = -nprod;
  1300. /* check that default action is reasonable */
  1301. if(ntypes && !(levprd[nprod]&ACTFLAG) && nontrst[*prdptr[nprod]-NTBASE].value) {
  1302. /* no explicit action, LHS has value */
  1303. int tempty;
  1304. tempty = prdptr[nprod][1];
  1305. if(tempty < 0)
  1306. error("must return a value, since LHS has a type");
  1307. else
  1308. if(tempty >= NTBASE)
  1309. tempty = nontrst[tempty-NTBASE].value;
  1310. else
  1311. tempty = TYPE(toklev[tempty]);
  1312. if(tempty != nontrst[*prdptr[nprod]-NTBASE].value)
  1313. error("default action causes potential type clash");
  1314. }
  1315. nprod++;
  1316. if(nprod >= NPROD)
  1317. error("more than %d rules", NPROD);
  1318. prdptr[nprod] = mem;
  1319. levprd[nprod] = 0;
  1320. }
  1321. /* end of all rules */
  1322. defout(1);
  1323. finact();
  1324. if(t == MARK) {
  1325. Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
  1326. while((c=Bgetrune(finput)) != Beof)
  1327. Bputrune(ftable, c);
  1328. }
  1329. Bterm(finput);
  1330. }
  1331. /*
  1332. * finish action routine
  1333. */
  1334. void
  1335. finact(void)
  1336. {
  1337. Bterm(faction);
  1338. Bprint(ftable, "#define YYEOFCODE %d\n", 1);
  1339. Bprint(ftable, "#define YYERRCODE %d\n", 2);
  1340. }
  1341. /*
  1342. * define s to be a terminal if t=0
  1343. * or a nonterminal if t=1
  1344. */
  1345. int
  1346. defin(int nt, char *s)
  1347. {
  1348. int val;
  1349. Rune rune;
  1350. val = 0;
  1351. if(nt) {
  1352. nnonter++;
  1353. if(nnonter >= NNONTERM)
  1354. error("too many nonterminals, limit %d",NNONTERM);
  1355. nontrst[nnonter].name = cstash(s);
  1356. return NTBASE + nnonter;
  1357. }
  1358. /* must be a token */
  1359. ntokens++;
  1360. if(ntokens >= NTERMS)
  1361. error("too many terminals, limit %d", NTERMS);
  1362. tokset[ntokens].name = cstash(s);
  1363. /* establish value for token */
  1364. /* single character literal */
  1365. if(s[0] == ' ') {
  1366. val = chartorune(&rune, &s[1]);
  1367. if(s[val+1] == 0) {
  1368. val = rune;
  1369. goto out;
  1370. }
  1371. }
  1372. /* escape sequence */
  1373. if(s[0] == ' ' && s[1] == '\\') {
  1374. if(s[3] == 0) {
  1375. /* single character escape sequence */
  1376. switch(s[2]) {
  1377. case 'n': val = '\n'; break;
  1378. case 'r': val = '\r'; break;
  1379. case 'b': val = '\b'; break;
  1380. case 't': val = '\t'; break;
  1381. case 'f': val = '\f'; break;
  1382. case '\'': val = '\''; break;
  1383. case '"': val = '"'; break;
  1384. case '\\': val = '\\'; break;
  1385. default: error("invalid escape");
  1386. }
  1387. goto out;
  1388. }
  1389. /* \nnn sequence */
  1390. if(s[2] >= '0' && s[2] <= '7') {
  1391. if(s[3] < '0' ||
  1392. s[3] > '7' ||
  1393. s[4] < '0' ||
  1394. s[4] > '7' ||
  1395. s[5] != 0)
  1396. error("illegal \\nnn construction");
  1397. val = 64*s[2] + 8*s[3] + s[4] - 73*'0';
  1398. if(val == 0)
  1399. error("'\\000' is illegal");
  1400. goto out;
  1401. }
  1402. error("unknown escape");
  1403. }
  1404. val = extval++;
  1405. out:
  1406. tokset[ntokens].value = val;
  1407. toklev[ntokens] = 0;
  1408. return ntokens;
  1409. }
  1410. /*
  1411. * write out the defines (at the end of the declaration section)
  1412. */
  1413. void
  1414. defout(int last)
  1415. {
  1416. int i, c;
  1417. char sar[NAMESIZE+10];
  1418. for(i=ndefout; i<=ntokens; i++) {
  1419. /* non-literals */
  1420. c = tokset[i].name[0];
  1421. if(c != ' ' && c != '$') {
  1422. Bprint(ftable, "#define %s %d\n",
  1423. tokset[i].name, tokset[i].value);
  1424. if(fdefine)
  1425. Bprint(fdefine, "#define\t%s\t%d\n",
  1426. tokset[i].name, tokset[i].value);
  1427. }
  1428. }
  1429. ndefout = ntokens+1;
  1430. if(last && fdebug) {
  1431. Bprint(fdebug, "char* yytoknames[] =\n{\n");
  1432. TLOOP(i) {
  1433. if(tokset[i].name) {
  1434. chcopy(sar, tokset[i].name);
  1435. Bprint(fdebug, "\t\"%s\",\n", sar);
  1436. continue;
  1437. }
  1438. Bprint(fdebug, "\t0,\n");
  1439. }
  1440. Bprint(fdebug, "};\n");
  1441. }
  1442. }
  1443. char*
  1444. cstash(char *s)
  1445. {
  1446. char *temp;
  1447. temp = cnamp;
  1448. do {
  1449. if(cnamp >= &cnames[cnamsz])
  1450. error("too many characters in id's and literals");
  1451. else
  1452. *cnamp++ = *s;
  1453. } while(*s++);
  1454. return temp;
  1455. }
  1456. long
  1457. gettok(void)
  1458. {
  1459. long c;
  1460. Rune rune;
  1461. int i, base, match, reserve;
  1462. static int peekline;
  1463. begin:
  1464. reserve = 0;
  1465. lineno += peekline;
  1466. peekline = 0;
  1467. c = Bgetrune(finput);
  1468. while(c == ' ' || c == '\n' || c == '\t' || c == '\f') {
  1469. if(c == '\n')
  1470. lineno++;
  1471. c = Bgetrune(finput);
  1472. }
  1473. /* skip comment */
  1474. if(c == '/') {
  1475. lineno += skipcom();
  1476. goto begin;
  1477. }
  1478. switch(c) {
  1479. case Beof:
  1480. return ENDFILE;
  1481. case '{':
  1482. Bungetrune(finput);
  1483. return '=';
  1484. case '<':
  1485. /* get, and look up, a type name (union member name) */
  1486. i = 0;
  1487. while((c=Bgetrune(finput)) != '>' && c >= 0 && c != '\n') {
  1488. rune = c;
  1489. c = runetochar(&tokname[i], &rune);
  1490. if(i < NAMESIZE)
  1491. i += c;
  1492. }
  1493. if(c != '>')
  1494. error("unterminated < ... > clause");
  1495. tokname[i] = 0;
  1496. for(i=1; i<=ntypes; i++)
  1497. if(!strcmp(typeset[i], tokname)) {
  1498. numbval = i;
  1499. return TYPENAME;
  1500. }
  1501. ntypes++;
  1502. numbval = ntypes;
  1503. typeset[numbval] = cstash(tokname);
  1504. return TYPENAME;
  1505. case '"':
  1506. case '\'':
  1507. match = c;
  1508. tokname[0] = ' ';
  1509. i = 1;
  1510. for(;;) {
  1511. c = Bgetrune(finput);
  1512. if(c == '\n' || c <= 0)
  1513. error("illegal or missing ' or \"" );
  1514. if(c == '\\') {
  1515. tokname[i] = '\\';
  1516. if(i < NAMESIZE)
  1517. i++;
  1518. c = Bgetrune(finput);
  1519. } else
  1520. if(c == match)
  1521. break;
  1522. rune = c;
  1523. c = runetochar(&tokname[i], &rune);
  1524. if(i < NAMESIZE)
  1525. i += c;
  1526. }
  1527. break;
  1528. case '%':
  1529. case '\\':
  1530. switch(c = Bgetrune(finput)) {
  1531. case '0': return TERM;
  1532. case '<': return LEFT;
  1533. case '2': return BINARY;
  1534. case '>': return RIGHT;
  1535. case '%':
  1536. case '\\': return MARK;
  1537. case '=': return PREC;
  1538. case '{': return LCURLY;
  1539. default: reserve = 1;
  1540. }
  1541. default:
  1542. /* number */
  1543. if(isdigit(c)) {
  1544. numbval = c-'0';
  1545. base = (c=='0')? 8: 10;
  1546. for(c = Bgetrune(finput); isdigit(c); c = Bgetrune(finput))
  1547. numbval = numbval*base + (c-'0');
  1548. Bungetrune(finput);
  1549. return NUMBER;
  1550. }
  1551. if(islower(c) || isupper(c) || c=='_' || c=='.' || c=='$') {
  1552. i = 0;
  1553. while(islower(c) || isupper(c) || isdigit(c) ||
  1554. c == '-' || c=='_' || c=='.' || c=='$') {
  1555. if(reserve && isupper(c))
  1556. c += 'a'-'A';
  1557. rune = c;
  1558. c = runetochar(&tokname[i], &rune);
  1559. if(i < NAMESIZE)
  1560. i += c;
  1561. c = Bgetrune(finput);
  1562. }
  1563. } else
  1564. return c;
  1565. Bungetrune(finput);
  1566. }
  1567. tokname[i] = 0;
  1568. /* find a reserved word */
  1569. if(reserve) {
  1570. for(c=0; resrv[c].name; c++)
  1571. if(strcmp(tokname, resrv[c].name) == 0)
  1572. return resrv[c].value;
  1573. error("invalid escape, or illegal reserved word: %s", tokname);
  1574. }
  1575. /* look ahead to distinguish IDENTIFIER from IDENTCOLON */
  1576. c = Bgetrune(finput);
  1577. while(c == ' ' || c == '\t'|| c == '\n' || c == '\f' || c == '/') {
  1578. if(c == '\n')
  1579. peekline++;
  1580. /* look for comments */
  1581. if(c == '/')
  1582. peekline += skipcom();
  1583. c = Bgetrune(finput);
  1584. }
  1585. if(c == ':')
  1586. return IDENTCOLON;
  1587. Bungetrune(finput);
  1588. return IDENTIFIER;
  1589. }
  1590. /*
  1591. * determine the type of a symbol
  1592. */
  1593. int
  1594. fdtype(int t)
  1595. {
  1596. int v;
  1597. if(t >= NTBASE)
  1598. v = nontrst[t-NTBASE].value;
  1599. else
  1600. v = TYPE(toklev[t]);
  1601. if(v <= 0)
  1602. error("must specify type for %s", (t>=NTBASE)?
  1603. nontrst[t-NTBASE].name: tokset[t].name);
  1604. return v;
  1605. }
  1606. int
  1607. chfind(int t, char *s)
  1608. {
  1609. int i;
  1610. if(s[0] == ' ')
  1611. t = 0;
  1612. TLOOP(i)
  1613. if(!strcmp(s, tokset[i].name))
  1614. return i;
  1615. NTLOOP(i)
  1616. if(!strcmp(s, nontrst[i].name))
  1617. return NTBASE+i;
  1618. /* cannot find name */
  1619. if(t > 1)
  1620. error("%s should have been defined earlier", s);
  1621. return defin(t, s);
  1622. }
  1623. /*
  1624. * copy the union declaration to the output, and the define file if present
  1625. */
  1626. void
  1627. cpyunion(void)
  1628. {
  1629. long c;
  1630. int level;
  1631. Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
  1632. Bprint(ftable, "typedef union ");
  1633. if(fdefine != 0)
  1634. Bprint(fdefine, "\ntypedef union ");
  1635. level = 0;
  1636. for(;;) {
  1637. if((c=Bgetrune(finput)) == Beof)
  1638. error("EOF encountered while processing %%union");
  1639. Bputrune(ftable, c);
  1640. if(fdefine != 0)
  1641. Bputrune(fdefine, c);
  1642. switch(c) {
  1643. case '\n':
  1644. lineno++;
  1645. break;
  1646. case '{':
  1647. level++;
  1648. break;
  1649. case '}':
  1650. level--;
  1651. /* we are finished copying */
  1652. if(level == 0) {
  1653. Bprint(ftable, " YYSTYPE;\n");
  1654. if(fdefine != 0)
  1655. Bprint(fdefine, "\tYYSTYPE;\nextern\tYYSTYPE\tyylval;\n");
  1656. return;
  1657. }
  1658. }
  1659. }
  1660. }
  1661. /*
  1662. * copies code between \{ and \}
  1663. */
  1664. void
  1665. cpycode(void)
  1666. {
  1667. long c;
  1668. c = Bgetrune(finput);
  1669. if(c == '\n') {
  1670. c = Bgetrune(finput);
  1671. lineno++;
  1672. }
  1673. Bprint(ftable, "\n#line\t%d\t\"%s\"\n", lineno, infile);
  1674. while(c != Beof) {
  1675. if(c == '\\') {
  1676. if((c=Bgetrune(finput)) == '}')
  1677. return;
  1678. Bputc(ftable, '\\');
  1679. }
  1680. if(c == '%') {
  1681. if((c=Bgetrune(finput)) == '}')
  1682. return;
  1683. Bputc(ftable, '%');
  1684. }
  1685. Bputrune(ftable, c);
  1686. if(c == '\n')
  1687. lineno++;
  1688. c = Bgetrune(finput);
  1689. }
  1690. error("eof before %%}");
  1691. }
  1692. /*
  1693. * skip over comments
  1694. * skipcom is called after reading a '/'
  1695. */
  1696. int
  1697. skipcom(void)
  1698. {
  1699. long c;
  1700. int i;
  1701. /* i is the number of lines skipped */
  1702. i = 0;
  1703. if(Bgetrune(finput) != '*')
  1704. error("illegal comment");
  1705. c = Bgetrune(finput);
  1706. while(c != Beof) {
  1707. while(c == '*')
  1708. if((c=Bgetrune(finput)) == '/')
  1709. return i;
  1710. if(c == '\n')
  1711. i++;
  1712. c = Bgetrune(finput);
  1713. }
  1714. error("EOF inside comment");
  1715. return 0;
  1716. }
  1717. /*
  1718. * copy C action to the next ; or closing }
  1719. */
  1720. void
  1721. cpyact(int offset)
  1722. {
  1723. long c;
  1724. int brac, match, j, s, fnd, tok;
  1725. Bprint(faction, "\n#line\t%d\t\"%s\"\n", lineno, infile);
  1726. brac = 0;
  1727. loop:
  1728. c = Bgetrune(finput);
  1729. swt:
  1730. switch(c) {
  1731. case ';':
  1732. if(brac == 0) {
  1733. Bputrune(faction, c);
  1734. return;
  1735. }
  1736. goto lcopy;
  1737. case '{':
  1738. brac++;
  1739. goto lcopy;
  1740. case '$':
  1741. s = 1;
  1742. tok = -1;
  1743. c = Bgetrune(finput);
  1744. /* type description */
  1745. if(c == '<') {
  1746. Bungetrune(finput);
  1747. if(gettok() != TYPENAME)
  1748. error("bad syntax on $<ident> clause");
  1749. tok = numbval;
  1750. c = Bgetrune(finput);
  1751. }
  1752. if(c == '$') {
  1753. Bprint(faction, "yyval");
  1754. /* put out the proper tag... */
  1755. if(ntypes) {
  1756. if(tok < 0)
  1757. tok = fdtype(*prdptr[nprod]);
  1758. Bprint(faction, ".%s", typeset[tok]);
  1759. }
  1760. goto loop;
  1761. }
  1762. if(c == '-') {
  1763. s = -s;
  1764. c = Bgetrune(finput);
  1765. }
  1766. if(isdigit(c)) {
  1767. j = 0;
  1768. while(isdigit(c)) {
  1769. j = j*10 + (c-'0');
  1770. c = Bgetrune(finput);
  1771. }
  1772. Bungetrune(finput);
  1773. j = j*s - offset;
  1774. if(j > 0)
  1775. error("Illegal use of $%d", j+offset);
  1776. dollar:
  1777. Bprint(faction, "yypt[-%d].yyv", -j);
  1778. /* put out the proper tag */
  1779. if(ntypes) {
  1780. if(j+offset <= 0 && tok < 0)
  1781. error("must specify type of $%d", j+offset);
  1782. if(tok < 0)
  1783. tok = fdtype(prdptr[nprod][j+offset]);
  1784. Bprint(faction, ".%s", typeset[tok]);
  1785. }
  1786. goto loop;
  1787. }
  1788. if(isupper(c) || islower(c) || c == '_' || c == '.') {
  1789. int tok; /* tok used oustide for type info */
  1790. /* look for $name */
  1791. Bungetrune(finput);
  1792. if(gettok() != IDENTIFIER)
  1793. error("$ must be followed by an identifier");
  1794. tok = chfind(2, tokname);
  1795. if((c = Bgetrune(finput)) != '#') {
  1796. Bungetrune(finput);
  1797. fnd = -1;
  1798. } else
  1799. if(gettok() != NUMBER) {
  1800. error("# must be followed by number");
  1801. fnd = -1;
  1802. } else
  1803. fnd = numbval;
  1804. for(j=1; j<=offset; ++j)
  1805. if(tok == prdptr[nprod][j]) {
  1806. if(--fnd <= 0) {
  1807. j -= offset;
  1808. goto dollar;
  1809. }
  1810. }
  1811. error("$name or $name#number not found");
  1812. }
  1813. Bputc(faction, '$');
  1814. if(s < 0 )
  1815. Bputc(faction, '-');
  1816. goto swt;
  1817. case '}':
  1818. brac--;
  1819. if(brac)
  1820. goto lcopy;
  1821. Bputrune(faction, c);
  1822. return;
  1823. case '/':
  1824. /* look for comments */
  1825. Bputrune(faction, c);
  1826. c = Bgetrune(finput);
  1827. if(c != '*')
  1828. goto swt;
  1829. /* it really is a comment */
  1830. Bputrune(faction, c);
  1831. c = Bgetrune(finput);
  1832. while(c >= 0) {
  1833. while(c == '*') {
  1834. Bputrune(faction, c);
  1835. if((c=Bgetrune(finput)) == '/')
  1836. goto lcopy;
  1837. }
  1838. Bputrune(faction, c);
  1839. if(c == '\n')
  1840. lineno++;
  1841. c = Bgetrune(finput);
  1842. }
  1843. error("EOF inside comment");
  1844. case '\'':
  1845. /* character constant */
  1846. match = '\'';
  1847. goto string;
  1848. case '"':
  1849. /* character string */
  1850. match = '"';
  1851. string:
  1852. Bputrune(faction, c);
  1853. while(c = Bgetrune(finput)) {
  1854. if(c == '\\') {
  1855. Bputrune(faction, c);
  1856. c = Bgetrune(finput);
  1857. if(c == '\n')
  1858. lineno++;
  1859. } else
  1860. if(c == match)
  1861. goto lcopy;
  1862. if(c == '\n')
  1863. error("newline in string or char. const.");
  1864. Bputrune(faction, c);
  1865. }
  1866. error("EOF in string or character constant");
  1867. case Beof:
  1868. error("action does not terminate");
  1869. case '\n':
  1870. lineno++;
  1871. goto lcopy;
  1872. }
  1873. lcopy:
  1874. Bputrune(faction, c);
  1875. goto loop;
  1876. }
  1877. void
  1878. openup(char *stem, int dflag, int vflag, int ytab, char *ytabc)
  1879. {
  1880. char buf[256];
  1881. if(vflag) {
  1882. snprint(buf, sizeof buf, "%s.%s", stem, FILEU);
  1883. foutput = Bopen(buf, OWRITE);
  1884. if(foutput == 0)
  1885. error("cannot open %s", buf);
  1886. }
  1887. if(yydebug) {
  1888. snprint(buf, sizeof buf, "%s.%s", stem, FILEDEBUG);
  1889. if((fdebug = Bopen(buf, OWRITE)) == 0)
  1890. error("can't open %s", buf);
  1891. }
  1892. if(dflag) {
  1893. snprint(buf, sizeof buf, "%s.%s", stem, FILED);
  1894. fdefine = Bopen(buf, OWRITE);
  1895. if(fdefine == 0)
  1896. error("can't create %s", buf);
  1897. }
  1898. if(ytab == 0)
  1899. snprint(buf, sizeof buf, "%s.%s", stem, OFILE);
  1900. else
  1901. strecpy(buf, buf+sizeof buf, ytabc);
  1902. ftable = Bopen(buf, OWRITE);
  1903. if(ftable == 0)
  1904. error("cannot open table file %s", buf);
  1905. }
  1906. /*
  1907. * print the output for the states
  1908. */
  1909. void
  1910. output(void)
  1911. {
  1912. int i, k, c;
  1913. Wset *u, *v;
  1914. Bprint(ftable, "short yyexca[] =\n{");
  1915. if(fdebug)
  1916. Bprint(fdebug, "char* yystates[] =\n{\n");
  1917. /* output the stuff for state i */
  1918. SLOOP(i) {
  1919. nolook = tystate[i]!=MUSTLOOKAHEAD;
  1920. closure(i);
  1921. /* output actions */
  1922. nolook = 1;
  1923. aryfil(temp1, ntokens+nnonter+1, 0);
  1924. WSLOOP(wsets, u) {
  1925. c = *(u->pitem);
  1926. if(c > 1 && c < NTBASE && temp1[c] == 0) {
  1927. WSLOOP(u, v)
  1928. if(c == *(v->pitem))
  1929. putitem(v->pitem+1, (Lkset*)0);
  1930. temp1[c] = state(c);
  1931. } else
  1932. if(c > NTBASE && temp1[(c -= NTBASE) + ntokens] == 0)
  1933. temp1[c+ntokens] = amem[indgo[i]+c];
  1934. }
  1935. if(i == 1)
  1936. temp1[1] = ACCEPTCODE;
  1937. /* now, we have the shifts; look at the reductions */
  1938. lastred = 0;
  1939. WSLOOP(wsets, u) {
  1940. c = *u->pitem;
  1941. /* reduction */
  1942. if(c <= 0) {
  1943. lastred = -c;
  1944. TLOOP(k)
  1945. if(BIT(u->ws.lset, k)) {
  1946. if(temp1[k] == 0)
  1947. temp1[k] = c;
  1948. else
  1949. if(temp1[k] < 0) { /* reduce/reduce conflict */
  1950. if(foutput)
  1951. Bprint(foutput,
  1952. "\n%d: reduce/reduce conflict"
  1953. " (red'ns %d and %d ) on %s",
  1954. i, -temp1[k], lastred,
  1955. symnam(k));
  1956. if(-temp1[k] > lastred)
  1957. temp1[k] = -lastred;
  1958. zzrrconf++;
  1959. } else
  1960. /* potential shift/reduce conflict */
  1961. precftn( lastred, k, i );
  1962. }
  1963. }
  1964. }
  1965. wract(i);
  1966. }
  1967. if(fdebug)
  1968. Bprint(fdebug, "};\n");
  1969. Bprint(ftable, "};\n");
  1970. Bprint(ftable, "#define YYNPROD %d\n", nprod);
  1971. Bprint(ftable, "#define YYPRIVATE %d\n", PRIVATE);
  1972. if(yydebug)
  1973. Bprint(ftable, "#define yydebug %s\n", yydebug);
  1974. }
  1975. /*
  1976. * pack state i from temp1 into amem
  1977. */
  1978. int
  1979. apack(int *p, int n)
  1980. {
  1981. int *pp, *qq, *rr, off, *q, *r;
  1982. /* we don't need to worry about checking because
  1983. * we will only look at entries known to be there...
  1984. * eliminate leading and trailing 0's
  1985. */
  1986. q = p+n;
  1987. for(pp = p, off = 0; *pp == 0 && pp <= q; ++pp, --off)
  1988. ;
  1989. /* no actions */
  1990. if(pp > q)
  1991. return 0;
  1992. p = pp;
  1993. /* now, find a place for the elements from p to q, inclusive */
  1994. r = &amem[ACTSIZE-1];
  1995. for(rr = amem; rr <= r; rr++, off++) {
  1996. for(qq = rr, pp = p; pp <= q; pp++, qq++)
  1997. if(*pp != 0)
  1998. if(*pp != *qq && *qq != 0)
  1999. goto nextk;
  2000. /* we have found an acceptable k */
  2001. if(pkdebug && foutput != 0)
  2002. Bprint(foutput, "off = %d, k = %d\n", off, (int)(rr-amem));
  2003. for(qq = rr, pp = p; pp <= q; pp++, qq++)
  2004. if(*pp) {
  2005. if(qq > r)
  2006. error("action table overflow");
  2007. if(qq > memp)
  2008. memp = qq;
  2009. *qq = *pp;
  2010. }
  2011. if(pkdebug && foutput != 0)
  2012. for(pp = amem; pp <= memp; pp += 10) {
  2013. Bprint(foutput, "\t");
  2014. for(qq = pp; qq <= pp+9; qq++)
  2015. Bprint(foutput, "%d ", *qq);
  2016. Bprint(foutput, "\n");
  2017. }
  2018. return(off);
  2019. nextk:;
  2020. }
  2021. error("no space in action table");
  2022. return 0;
  2023. }
  2024. /*
  2025. * output the gotos for the nontermninals
  2026. */
  2027. void
  2028. go2out(void)
  2029. {
  2030. int i, j, k, best, count, cbest, times;
  2031. /* mark begining of gotos */
  2032. Bprint(ftemp, "$\n");
  2033. for(i = 1; i <= nnonter; i++) {
  2034. go2gen(i);
  2035. /* find the best one to make default */
  2036. best = -1;
  2037. times = 0;
  2038. /* is j the most frequent */
  2039. for(j = 0; j <= nstate; j++) {
  2040. if(tystate[j] == 0)
  2041. continue;
  2042. if(tystate[j] == best)
  2043. continue;
  2044. /* is tystate[j] the most frequent */
  2045. count = 0;
  2046. cbest = tystate[j];
  2047. for(k = j; k <= nstate; k++)
  2048. if(tystate[k] == cbest)
  2049. count++;
  2050. if(count > times) {
  2051. best = cbest;
  2052. times = count;
  2053. }
  2054. }
  2055. /* best is now the default entry */
  2056. zzgobest += times-1;
  2057. for(j = 0; j <= nstate; j++)
  2058. if(tystate[j] != 0 && tystate[j] != best) {
  2059. Bprint(ftemp, "%d,%d,", j, tystate[j]);
  2060. zzgoent++;
  2061. }
  2062. /* now, the default */
  2063. if(best == -1)
  2064. best = 0;
  2065. zzgoent++;
  2066. Bprint(ftemp, "%d\n", best);
  2067. }
  2068. }
  2069. /*
  2070. * output the gotos for nonterminal c
  2071. */
  2072. void
  2073. go2gen(int c)
  2074. {
  2075. int i, work, cc;
  2076. Item *p, *q;
  2077. /* first, find nonterminals with gotos on c */
  2078. aryfil(temp1, nnonter+1, 0);
  2079. temp1[c] = 1;
  2080. work = 1;
  2081. while(work) {
  2082. work = 0;
  2083. PLOOP(0, i)
  2084. /* cc is a nonterminal */
  2085. if((cc=prdptr[i][1]-NTBASE) >= 0)
  2086. /* cc has a goto on c */
  2087. if(temp1[cc] != 0) {
  2088. /* thus, the left side of production i does too */
  2089. cc = *prdptr[i]-NTBASE;
  2090. if(temp1[cc] == 0) {
  2091. work = 1;
  2092. temp1[cc] = 1;
  2093. }
  2094. }
  2095. }
  2096. /* now, we have temp1[c] = 1 if a goto on c in closure of cc */
  2097. if(g2debug && foutput != 0) {
  2098. Bprint(foutput, "%s: gotos on ", nontrst[c].name);
  2099. NTLOOP(i)
  2100. if(temp1[i])
  2101. Bprint(foutput, "%s ", nontrst[i].name);
  2102. Bprint(foutput, "\n");
  2103. }
  2104. /* now, go through and put gotos into tystate */
  2105. aryfil(tystate, nstate, 0);
  2106. SLOOP(i)
  2107. ITMLOOP(i, p, q)
  2108. if((cc = *p->pitem) >= NTBASE)
  2109. /* goto on c is possible */
  2110. if(temp1[cc-NTBASE]) {
  2111. tystate[i] = amem[indgo[i]+c];
  2112. break;
  2113. }
  2114. }
  2115. /*
  2116. * decide a shift/reduce conflict by precedence.
  2117. * r is a rule number, t a token number
  2118. * the conflict is in state s
  2119. * temp1[t] is changed to reflect the action
  2120. */
  2121. void
  2122. precftn(int r, int t, int s)
  2123. {
  2124. int lp, lt, action;
  2125. lp = levprd[r];
  2126. lt = toklev[t];
  2127. if(PLEVEL(lt) == 0 || PLEVEL(lp) == 0) {
  2128. /* conflict */
  2129. if(foutput != 0)
  2130. Bprint(foutput,
  2131. "\n%d: shift/reduce conflict (shift %d(%d), red'n %d(%d)) on %s",
  2132. s, temp1[t], PLEVEL(lt), r, PLEVEL(lp), symnam(t));
  2133. zzsrconf++;
  2134. return;
  2135. }
  2136. if(PLEVEL(lt) == PLEVEL(lp))
  2137. action = ASSOC(lt);
  2138. else
  2139. if(PLEVEL(lt) > PLEVEL(lp))
  2140. action = RASC; /* shift */
  2141. else
  2142. action = LASC; /* reduce */
  2143. switch(action) {
  2144. case BASC: /* error action */
  2145. temp1[t] = ERRCODE;
  2146. break;
  2147. case LASC: /* reduce */
  2148. temp1[t] = -r;
  2149. break;
  2150. }
  2151. }
  2152. /*
  2153. * output state i
  2154. * temp1 has the actions, lastred the default
  2155. */
  2156. void
  2157. wract(int i)
  2158. {
  2159. int p, p0, p1, ntimes, tred, count, j, flag;
  2160. /* find the best choice for lastred */
  2161. lastred = 0;
  2162. ntimes = 0;
  2163. TLOOP(j) {
  2164. if(temp1[j] >= 0)
  2165. continue;
  2166. if(temp1[j]+lastred == 0)
  2167. continue;
  2168. /* count the number of appearances of temp1[j] */
  2169. count = 0;
  2170. tred = -temp1[j];
  2171. levprd[tred] |= REDFLAG;
  2172. TLOOP(p)
  2173. if(temp1[p]+tred == 0)
  2174. count++;
  2175. if(count > ntimes) {
  2176. lastred = tred;
  2177. ntimes = count;
  2178. }
  2179. }
  2180. /*
  2181. * for error recovery, arrange that, if there is a shift on the
  2182. * error recovery token, `error', that the default be the error action
  2183. */
  2184. if(temp1[2] > 0)
  2185. lastred = 0;
  2186. /* clear out entries in temp1 which equal lastred */
  2187. TLOOP(p)
  2188. if(temp1[p]+lastred == 0)
  2189. temp1[p] = 0;
  2190. wrstate(i);
  2191. defact[i] = lastred;
  2192. flag = 0;
  2193. TLOOP(p0)
  2194. if((p1=temp1[p0]) != 0) {
  2195. if(p1 < 0) {
  2196. p1 = -p1;
  2197. goto exc;
  2198. }
  2199. if(p1 == ACCEPTCODE) {
  2200. p1 = -1;
  2201. goto exc;
  2202. }
  2203. if(p1 == ERRCODE) {
  2204. p1 = 0;
  2205. exc:
  2206. if(flag++ == 0)
  2207. Bprint(ftable, "-1, %d,\n", i);
  2208. Bprint(ftable, "\t%d, %d,\n", p0, p1);
  2209. zzexcp++;
  2210. continue;
  2211. }
  2212. Bprint(ftemp, "%d,%d,", p0, p1);
  2213. zzacent++;
  2214. }
  2215. if(flag) {
  2216. defact[i] = -2;
  2217. Bprint(ftable, "\t-2, %d,\n", lastred);
  2218. }
  2219. Bprint(ftemp, "\n");
  2220. }
  2221. /*
  2222. * writes state i
  2223. */
  2224. void
  2225. wrstate(int i)
  2226. {
  2227. int j0, j1;
  2228. Item *pp, *qq;
  2229. Wset *u;
  2230. if(fdebug) {
  2231. if(lastred) {
  2232. Bprint(fdebug, " 0, /*%d*/\n", i);
  2233. } else {
  2234. Bprint(fdebug, " \"");
  2235. ITMLOOP(i, pp, qq)
  2236. Bprint(fdebug, "%s\\n", writem(pp->pitem));
  2237. if(tystate[i] == MUSTLOOKAHEAD)
  2238. WSLOOP(wsets + (pstate[i+1] - pstate[i]), u)
  2239. if(*u->pitem < 0)
  2240. Bprint(fdebug, "%s\\n", writem(u->pitem));
  2241. Bprint(fdebug, "\", /*%d*/\n", i);
  2242. }
  2243. }
  2244. if(foutput == 0)
  2245. return;
  2246. Bprint(foutput, "\nstate %d\n", i);
  2247. ITMLOOP(i, pp, qq)
  2248. Bprint(foutput, "\t%s\n", writem(pp->pitem));
  2249. if(tystate[i] == MUSTLOOKAHEAD)
  2250. /* print out empty productions in closure */
  2251. WSLOOP(wsets+(pstate[i+1]-pstate[i]), u)
  2252. if(*u->pitem < 0)
  2253. Bprint(foutput, "\t%s\n", writem(u->pitem));
  2254. /* check for state equal to another */
  2255. TLOOP(j0)
  2256. if((j1=temp1[j0]) != 0) {
  2257. Bprint(foutput, "\n\t%s ", symnam(j0));
  2258. /* shift, error, or accept */
  2259. if(j1 > 0) {
  2260. if(j1 == ACCEPTCODE)
  2261. Bprint(foutput, "accept");
  2262. else
  2263. if(j1 == ERRCODE)
  2264. Bprint(foutput, "error");
  2265. else
  2266. Bprint(foutput, "shift %d", j1);
  2267. } else
  2268. Bprint(foutput, "reduce %d (src line %d)", -j1, rlines[-j1]);
  2269. }
  2270. /* output the final production */
  2271. if(lastred)
  2272. Bprint(foutput, "\n\t. reduce %d (src line %d)\n\n",
  2273. lastred, rlines[lastred]);
  2274. else
  2275. Bprint(foutput, "\n\t. error\n\n");
  2276. /* now, output nonterminal actions */
  2277. j1 = ntokens;
  2278. for(j0 = 1; j0 <= nnonter; j0++) {
  2279. j1++;
  2280. if(temp1[j1])
  2281. Bprint(foutput, "\t%s goto %d\n", symnam(j0+NTBASE), temp1[j1]);
  2282. }
  2283. }
  2284. void
  2285. warray(char *s, int *v, int n)
  2286. {
  2287. int i;
  2288. Bprint(ftable, "short %s[] =\n{", s);
  2289. for(i=0;;) {
  2290. if(i%10 == 0)
  2291. Bprint(ftable, "\n");
  2292. Bprint(ftable, "%4d", v[i]);
  2293. i++;
  2294. if(i >= n) {
  2295. Bprint(ftable, "\n};\n");
  2296. break;
  2297. }
  2298. Bprint(ftable, ",");
  2299. }
  2300. }
  2301. /*
  2302. * in order to free up the mem and amem arrays for the optimizer,
  2303. * and still be able to output yyr1, etc., after the sizes of
  2304. * the action array is known, we hide the nonterminals
  2305. * derived by productions in levprd.
  2306. */
  2307. void
  2308. hideprod(void)
  2309. {
  2310. int i, j;
  2311. j = 0;
  2312. levprd[0] = 0;
  2313. PLOOP(1, i) {
  2314. if(!(levprd[i] & REDFLAG)) {
  2315. j++;
  2316. if(foutput != 0)
  2317. Bprint(foutput, "Rule not reduced: %s\n", writem(prdptr[i]));
  2318. }
  2319. levprd[i] = *prdptr[i] - NTBASE;
  2320. }
  2321. if(j)
  2322. print("%d rules never reduced\n", j);
  2323. }
  2324. void
  2325. callopt(void)
  2326. {
  2327. int i, *p, j, k, *q;
  2328. /* read the arrays from tempfile and set parameters */
  2329. finput = Bopen(tempname, OREAD);
  2330. if(finput == 0)
  2331. error("optimizer cannot open tempfile");
  2332. pgo[0] = 0;
  2333. temp1[0] = 0;
  2334. nstate = 0;
  2335. nnonter = 0;
  2336. for(;;) {
  2337. switch(gtnm()) {
  2338. case '\n':
  2339. nstate++;
  2340. pmem--;
  2341. temp1[nstate] = pmem - mem0;
  2342. case ',':
  2343. continue;
  2344. case '$':
  2345. break;
  2346. default:
  2347. error("bad tempfile");
  2348. }
  2349. break;
  2350. }
  2351. pmem--;
  2352. temp1[nstate] = yypgo[0] = pmem - mem0;
  2353. for(;;) {
  2354. switch(gtnm()) {
  2355. case '\n':
  2356. nnonter++;
  2357. yypgo[nnonter] = pmem-mem0;
  2358. case ',':
  2359. continue;
  2360. case -1:
  2361. break;
  2362. default:
  2363. error("bad tempfile");
  2364. }
  2365. break;
  2366. }
  2367. pmem--;
  2368. yypgo[nnonter--] = pmem - mem0;
  2369. for(i = 0; i < nstate; i++) {
  2370. k = 32000;
  2371. j = 0;
  2372. q = mem0 + temp1[i+1];
  2373. for(p = mem0 + temp1[i]; p < q ; p += 2) {
  2374. if(*p > j)
  2375. j = *p;
  2376. if(*p < k)
  2377. k = *p;
  2378. }
  2379. /* nontrivial situation */
  2380. if(k <= j) {
  2381. /* j is now the range */
  2382. /* j -= k; /* call scj */
  2383. if(k > maxoff)
  2384. maxoff = k;
  2385. }
  2386. tystate[i] = (temp1[i+1]-temp1[i]) + 2*j;
  2387. if(j > maxspr)
  2388. maxspr = j;
  2389. }
  2390. /* initialize ggreed table */
  2391. for(i = 1; i <= nnonter; i++) {
  2392. ggreed[i] = 1;
  2393. j = 0;
  2394. /* minimum entry index is always 0 */
  2395. q = mem0 + yypgo[i+1] - 1;
  2396. for(p = mem0+yypgo[i]; p < q ; p += 2) {
  2397. ggreed[i] += 2;
  2398. if(*p > j)
  2399. j = *p;
  2400. }
  2401. ggreed[i] = ggreed[i] + 2*j;
  2402. if(j > maxoff)
  2403. maxoff = j;
  2404. }
  2405. /* now, prepare to put the shift actions into the amem array */
  2406. for(i = 0; i < ACTSIZE; i++)
  2407. amem[i] = 0;
  2408. maxa = amem;
  2409. for(i = 0; i < nstate; i++) {
  2410. if(tystate[i] == 0 && adb > 1)
  2411. Bprint(ftable, "State %d: null\n", i);
  2412. indgo[i] = YYFLAG1;
  2413. }
  2414. while((i = nxti()) != NOMORE)
  2415. if(i >= 0)
  2416. stin(i);
  2417. else
  2418. gin(-i);
  2419. /* print amem array */
  2420. if(adb > 2 )
  2421. for(p = amem; p <= maxa; p += 10) {
  2422. Bprint(ftable, "%4d ", (int)(p-amem));
  2423. for(i = 0; i < 10; ++i)
  2424. Bprint(ftable, "%4d ", p[i]);
  2425. Bprint(ftable, "\n");
  2426. }
  2427. /* write out the output appropriate to the language */
  2428. aoutput();
  2429. osummary();
  2430. ZAPFILE(tempname);
  2431. }
  2432. void
  2433. gin(int i)
  2434. {
  2435. int *p, *r, *s, *q1, *q2;
  2436. /* enter gotos on nonterminal i into array amem */
  2437. ggreed[i] = 0;
  2438. q2 = mem0+ yypgo[i+1] - 1;
  2439. q1 = mem0 + yypgo[i];
  2440. /* now, find amem place for it */
  2441. for(p = amem; p < &amem[ACTSIZE]; p++) {
  2442. if(*p)
  2443. continue;
  2444. for(r = q1; r < q2; r += 2) {
  2445. s = p + *r + 1;
  2446. if(*s)
  2447. goto nextgp;
  2448. if(s > maxa)
  2449. if((maxa = s) > &amem[ACTSIZE])
  2450. error("a array overflow");
  2451. }
  2452. /* we have found amem spot */
  2453. *p = *q2;
  2454. if(p > maxa)
  2455. if((maxa = p) > &amem[ACTSIZE])
  2456. error("a array overflow");
  2457. for(r = q1; r < q2; r += 2) {
  2458. s = p + *r + 1;
  2459. *s = r[1];
  2460. }
  2461. pgo[i] = p-amem;
  2462. if(adb > 1)
  2463. Bprint(ftable, "Nonterminal %d, entry at %d\n", i, pgo[i]);
  2464. return;
  2465. nextgp:;
  2466. }
  2467. error("cannot place goto %d\n", i);
  2468. }
  2469. void
  2470. stin(int i)
  2471. {
  2472. int *r, *s, n, flag, j, *q1, *q2;
  2473. tystate[i] = 0;
  2474. /* enter state i into the amem array */
  2475. q2 = mem0+temp1[i+1];
  2476. q1 = mem0+temp1[i];
  2477. /* find an acceptable place */
  2478. for(n = -maxoff; n < ACTSIZE; n++) {
  2479. flag = 0;
  2480. for(r = q1; r < q2; r += 2) {
  2481. if((s = *r + n + amem) < amem)
  2482. goto nextn;
  2483. if(*s == 0)
  2484. flag++;
  2485. else
  2486. if(*s != r[1])
  2487. goto nextn;
  2488. }
  2489. /* check that the position equals another only if the states are identical */
  2490. for(j=0; j<nstate; j++) {
  2491. if(indgo[j] == n) {
  2492. /* we have some disagreement */
  2493. if(flag)
  2494. goto nextn;
  2495. if(temp1[j+1]+temp1[i] == temp1[j]+temp1[i+1]) {
  2496. /* states are equal */
  2497. indgo[i] = n;
  2498. if(adb > 1)
  2499. Bprint(ftable,
  2500. "State %d: entry at %d equals state %d\n",
  2501. i, n, j);
  2502. return;
  2503. }
  2504. /* we have some disagreement */
  2505. goto nextn;
  2506. }
  2507. }
  2508. for(r = q1; r < q2; r += 2) {
  2509. if((s = *r+n+amem) >= &amem[ACTSIZE])
  2510. error("out of space in optimizer a array");
  2511. if(s > maxa)
  2512. maxa = s;
  2513. if(*s != 0 && *s != r[1])
  2514. error("clobber of a array, pos'n %d, by %d", s-amem, r[1]);
  2515. *s = r[1];
  2516. }
  2517. indgo[i] = n;
  2518. if(adb > 1)
  2519. Bprint(ftable, "State %d: entry at %d\n", i, indgo[i]);
  2520. return;
  2521. nextn:;
  2522. }
  2523. error("Error; failure to place state %d\n", i);
  2524. }
  2525. /*
  2526. * finds the next i
  2527. */
  2528. int
  2529. nxti(void)
  2530. {
  2531. int i, max, maxi;
  2532. max = 0;
  2533. maxi = 0;
  2534. for(i = 1; i <= nnonter; i++)
  2535. if(ggreed[i] >= max) {
  2536. max = ggreed[i];
  2537. maxi = -i;
  2538. }
  2539. for(i = 0; i < nstate; ++i)
  2540. if(tystate[i] >= max) {
  2541. max = tystate[i];
  2542. maxi = i;
  2543. }
  2544. if(nxdb)
  2545. Bprint(ftable, "nxti = %d, max = %d\n", maxi, max);
  2546. if(max == 0)
  2547. return NOMORE;
  2548. return maxi;
  2549. }
  2550. /*
  2551. * write summary
  2552. */
  2553. void
  2554. osummary(void)
  2555. {
  2556. int i, *p;
  2557. if(foutput == 0)
  2558. return;
  2559. i = 0;
  2560. for(p = maxa; p >= amem; p--)
  2561. if(*p == 0)
  2562. i++;
  2563. Bprint(foutput, "Optimizer space used: input %d/%d, output %d/%d\n",
  2564. (int)(pmem-mem0+1), MEMSIZE, (int)(maxa-amem+1), ACTSIZE);
  2565. Bprint(foutput, "%d table entries, %d zero\n", (int)(maxa-amem+1), i);
  2566. Bprint(foutput, "maximum spread: %d, maximum offset: %d\n", maxspr, maxoff);
  2567. }
  2568. /*
  2569. * this version is for C
  2570. * write out the optimized parser
  2571. */
  2572. void
  2573. aoutput(void)
  2574. {
  2575. Bprint(ftable, "#define\tYYLAST\t%d\n", (int)(maxa-amem+1));
  2576. arout("yyact", amem, (maxa-amem)+1);
  2577. arout("yypact", indgo, nstate);
  2578. arout("yypgo", pgo, nnonter+1);
  2579. }
  2580. void
  2581. arout(char *s, int *v, int n)
  2582. {
  2583. int i;
  2584. Bprint(ftable, "short %s[] =\n{", s);
  2585. for(i = 0; i < n;) {
  2586. if(i%10 == 0)
  2587. Bprint(ftable, "\n");
  2588. Bprint(ftable, "%4d", v[i]);
  2589. i++;
  2590. if(i == n)
  2591. Bprint(ftable, "\n};\n");
  2592. else
  2593. Bprint(ftable, ",");
  2594. }
  2595. }
  2596. /*
  2597. * read and convert an integer from the standard input
  2598. * return the terminating character
  2599. * blanks, tabs, and newlines are ignored
  2600. */
  2601. int
  2602. gtnm(void)
  2603. {
  2604. int sign, val, c;
  2605. sign = 0;
  2606. val = 0;
  2607. while((c=Bgetrune(finput)) != Beof) {
  2608. if(isdigit(c)) {
  2609. val = val*10 + c-'0';
  2610. continue;
  2611. }
  2612. if(c == '-') {
  2613. sign = 1;
  2614. continue;
  2615. }
  2616. break;
  2617. }
  2618. if(sign)
  2619. val = -val;
  2620. *pmem++ = val;
  2621. if(pmem >= &mem0[MEMSIZE])
  2622. error("out of space");
  2623. return c;
  2624. }