1 #include <stdio.h>
2 #include <string.h>
3 #include "defs.h"
4 
5 #define JAVA_PACKAGE    "@JAVA_PACKAGE@"
6 #define JAVA_CLASS_DECL "@JAVA_CLASS_DECL@"
7 #define JAVA_RUN        "@JAVA_RUN@"
8 #define JAVA_CONSTRUCT  "@JAVA_CONSTRUCT@"
9 #define JAVA_STACK      "@JAVA_STACK@"
10 
11 /*  The banner used here should be replaced with an #ident directive	*/
12 /*  if the target C compiler supports #ident directives.		*/
13 /*									*/
14 /*  If the skeleton is changed, the banner should be changed so that	*/
15 /*  the altered version can easily be distinguished from the original.	*/
16 
17 char *banner[] =
18 {
19     "#ifndef lint",
20     "static char yysccsid[] = \"@(#)yaccpar	1.8 (Berkeley) 01/20/90\";",
21     "#endif",
22     "#define YYBYACC 1",
23     0
24 };
25 
26 char *jbanner[] =
27 {
28     "//### This file created by BYACC 1.8(/Java extension  1.15)",
29     "//### Java capabilities added 7 Jan 97, Bob Jamison",
30     "//### Updated : 27 Nov 97  -- Bob Jamison, Joe Nieten",
31     "//###           01 Jan 98  -- Bob Jamison -- fixed generic semantic constructor",
32     "//###           01 Jun 99  -- Bob Jamison -- added Runnable support",
33     "//###           06 Aug 00  -- Bob Jamison -- made state variables class-global",
34     "//###           03 Jan 01  -- Bob Jamison -- improved flags, tracing",
35     "//###           16 May 01  -- Bob Jamison -- added custom stack sizing",
36     "//###           04 Mar 02  -- Yuval Oren  -- improved java performance, added options",
37     "//###           14 Mar 02  -- Tomas Hurka -- -d support, static initializer workaround",
38     "//### Please send bug reports to tom@hukatronic.cz",
39     "//### static char yysccsid[] = \"@(#)yaccpar	1.8 (Berkeley) 01/20/90\";",
40     "\n\n",
41     JAVA_PACKAGE,
42     "\n\n",
43     0
44 };
45 
46 
47 char *tables[] =
48 {
49     "extern short yylhs[];",
50     "extern short yylen[];",
51     "extern short yydefred[];",
52     "extern short yydgoto[];",
53     "extern short yysindex[];",
54     "extern short yyrindex[];",
55     "extern short yygindex[];",
56     "extern short yytable[];",
57     "extern short yycheck[];",
58     "#if YYDEBUG",
59     "extern char *yyname[];",
60     "extern char *yyrule[];",
61     "#endif",
62     0
63 };
64 
65 char *jtables[] =
66 {
67     "extern short yylhs[];",
68     0
69 };
70 
71 
72 char *header[] =
73 {
74     "#define yyclearin (yychar=(-1))",
75     "#define yyerrok (yyerrflag=0)",
76     "#ifdef YYSTACKSIZE",
77     "#ifndef YYMAXDEPTH",
78     "#define YYMAXDEPTH YYSTACKSIZE",
79     "#endif",
80     "#else",
81     "#ifdef YYMAXDEPTH",
82     "#define YYSTACKSIZE YYMAXDEPTH",
83     "#else",
84     "#define YYSTACKSIZE 500",
85     "#define YYMAXDEPTH 500",
86     "#endif",
87     "#endif",
88     "int yydebug;",
89     "int yynerrs;",
90     "int yyerrflag;",
91     "int yychar;",
92     "short *yyssp;",
93     "YYSTYPE *yyvsp;",
94     "YYSTYPE yyval;",
95     "YYSTYPE yylval;",
96     "short yyss[YYSTACKSIZE];",
97     "YYSTYPE yyvs[YYSTACKSIZE];",
98     "#define yystacksize YYSTACKSIZE",
99     0
100 };
101 
102 /* yio 20011121: eliminated a bit of unnecessary code for better performance */
103 char *jheader[] =
104 {
105   "\n\n\n",
106   JAVA_CLASS_DECL,
107   "{\n",
108   "boolean yydebug;        //do I want debug output?",
109   "int yynerrs;            //number of errors so far",
110   "int yyerrflag;          //was there an error?",
111   "int yychar;             //the current working character",
112   "\n//########## MESSAGES ##########",
113   "//###############################################################",
114   "// method: debug",
115   "//###############################################################",
116   "void debug(String msg)",
117   "{",
118   "  if (yydebug)",
119   "    System.out.println(msg);",
120   "}",
121   "\n//########## STATE STACK ##########",
122   JAVA_STACK,
123   "\nint statestk[] = new int[YYSTACKSIZE]; //state stack",
124   "int stateptr;",
125   "int stateptrmax;                     //highest index of stackptr",
126   "int statemax;                        //state when highest index reached",
127   "//###############################################################",
128   "// methods: state stack push,pop,drop,peek",
129   "//###############################################################",
130   "final void state_push(int state)",
131   "{",
132   "  try {",
133   "		stateptr++;",
134   "		statestk[stateptr]=state;",
135   "	 }",
136   "	 catch (ArrayIndexOutOfBoundsException e) {",
137   "     int oldsize = statestk.length;",
138   "     int newsize = oldsize * 2;",
139   "     int[] newstack = new int[newsize];",
140   "     System.arraycopy(statestk,0,newstack,0,oldsize);",
141   "     statestk = newstack;",
142   "     statestk[stateptr]=state;",
143   "  }",
144   "}",
145   "final int state_pop()",
146   "{",
147   "  return statestk[stateptr--];",
148   "}",
149   "final void state_drop(int cnt)",
150   "{",
151   "  stateptr -= cnt; ",
152   "}",
153   "final int state_peek(int relative)",
154   "{",
155   "  return statestk[stateptr-relative];",
156   "}",
157   "//###############################################################",
158   "// method: init_stacks : allocate and prepare stacks",
159   "//###############################################################",
160   "final boolean init_stacks()",
161   "{",
162   "  stateptr = -1;",
163   "  val_init();",
164   "  return true;",
165   "}",
166   "//###############################################################",
167   "// method: dump_stacks : show n levels of the stacks",
168   "//###############################################################",
169   "void dump_stacks(int count)",
170   "{",
171   "int i;",
172   "  System.out.println(\"=index==state====value=     s:\"+stateptr+\"  v:\"+valptr);",
173   "  for (i=0;i<count;i++)",
174   "    System.out.println(\" \"+i+\"    \"+statestk[i]+\"      \"+valstk[i]);",
175   "  System.out.println(\"======================\");",
176   "}",
177   0
178 };
179 
180 
181 char *body[] =
182 {
183     "#define YYABORT goto yyabort",
184     "#define YYACCEPT goto yyaccept",
185     "#define YYERROR goto yyerrlab",
186     "int",
187     "yyparse()",
188     "{",
189     "    register int yym, yyn, yystate;",
190     "#if YYDEBUG",
191     "    register char *yys;",
192     "    extern char *getenv();",
193     "",
194     "    if (yys = getenv(\"YYDEBUG\"))",
195     "    {",
196     "        yyn = *yys;",
197     "        if (yyn >= '0' && yyn <= '9')",
198     "            yydebug = yyn - '0';",
199     "    }",
200     "#endif",
201     "",
202     "    yynerrs = 0;",
203     "    yyerrflag = 0;",
204     "    yychar = (-1);",
205     "",
206     "    yyssp = yyss;",
207     "    yyvsp = yyvs;",
208     "    *yyssp = yystate = 0;",
209     "",
210     "yyloop:",
211     "    if (yyn = yydefred[yystate]) goto yyreduce;",
212     "    if (yychar < 0)",
213     "    {",
214     "        if ((yychar = yylex()) < 0) yychar = 0;",
215     "#if YYDEBUG",
216     "        if (yydebug)",
217     "        {",
218     "            yys = 0;",
219     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
220     "            if (!yys) yys = \"illegal-symbol\";",
221     "            printf(\"yydebug: state %d, reading %d (%s)\\n\", yystate,",
222     "                    yychar, yys);",
223     "        }",
224     "#endif",
225     "    }",
226     "    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&",
227     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
228     "    {",
229     "#if YYDEBUG",
230     "        if (yydebug)",
231     "            printf(\"yydebug: state %d, shifting to state %d (%s)\\n\",",
232     "                    yystate, yytable[yyn],yyrule[yyn]);",
233     "#endif",
234     "        if (yyssp >= yyss + yystacksize - 1)",
235     "        {",
236     "            goto yyoverflow;",
237     "        }",
238     "        *++yyssp = yystate = yytable[yyn];",
239     "        *++yyvsp = yylval;",
240     "        yychar = (-1);",
241     "        if (yyerrflag > 0)  --yyerrflag;",
242     "        goto yyloop;",
243     "    }",
244     "    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&",
245     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
246     "    {",
247     "        yyn = yytable[yyn];",
248     "        goto yyreduce;",
249     "    }",
250     "    if (yyerrflag) goto yyinrecovery;",
251     "#ifdef lint",
252     "    goto yynewerror;",
253     "#endif",
254     "yynewerror:",
255     "    yyerror(\"syntax error\");",
256     "#ifdef lint",
257     "    goto yyerrlab;",
258     "#endif",
259     "yyerrlab:",
260     "    ++yynerrs;",
261     "yyinrecovery:",
262     "    if (yyerrflag < 3)",
263     "    {",
264     "        yyerrflag = 3;",
265     "        for (;;)",
266     "        {",
267     "            if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&",
268     "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
269     "            {",
270     "#if YYDEBUG",
271     "                if (yydebug)",
272     "                    printf(\"yydebug: state %d, error recovery shifting\\",
273     " to state %d\\n\", *yyssp, yytable[yyn]);",
274     "#endif",
275     "                if (yyssp >= yyss + yystacksize - 1)",
276     "                {",
277     "                    goto yyoverflow;",
278     "                }",
279     "                *++yyssp = yystate = yytable[yyn];",
280     "                *++yyvsp = yylval;",
281     "                goto yyloop;",
282     "            }",
283     "            else",
284     "            {",
285     "#if YYDEBUG",
286     "                if (yydebug)",
287     "                    printf(\"yydebug: error recovery discarding state %d\
288 \\n\",",
289     "                            *yyssp);",
290     "#endif",
291     "                if (yyssp <= yyss) goto yyabort;",
292     "                --yyssp;",
293     "                --yyvsp;",
294     "            }",
295     "        }",
296     "    }",
297     "    else",
298     "    {",
299     "        if (yychar == 0) goto yyabort;",
300     "#if YYDEBUG",
301     "        if (yydebug)",
302     "        {",
303     "            yys = 0;",
304     "            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
305     "            if (!yys) yys = \"illegal-symbol\";",
306     "            printf(\"yydebug: state %d, error recovery discards token %d\
307  (%s)\\n\",",
308     "                    yystate, yychar, yys);",
309     "        }",
310     "#endif",
311     "        yychar = (-1);",
312     "        goto yyloop;",
313     "    }",
314     "yyreduce:",
315     "#if YYDEBUG",
316     "    if (yydebug)",
317     "        printf(\"yydebug: state %d, reducing by rule %d (%s)\\n\",",
318     "                yystate, yyn, yyrule[yyn]);",
319     "#endif",
320     "    yym = yylen[yyn];",
321     "    yyval = yyvsp[1-yym];",
322     "    switch (yyn)",
323     "    {",
324     0
325 };
326 
327 /* yio 20011121: eliminated a bit of unnecessary code for better performance */
328 char *jbody_a[] =
329 {
330     "//###############################################################",
331     "// method: yylexdebug : check lexer state",
332     "//###############################################################",
333     "void yylexdebug(int state,int ch)",
334     "{",
335     "String s=null;",
336     "  if (ch < 0) ch=0;",
337     "  if (ch <= YYMAXTOKEN) //check index bounds",
338     "     s = yyname[ch];    //now get it",
339     "  if (s==null)",
340     "    s = \"illegal-symbol\";",
341     "  debug(\"state \"+state+\", reading \"+ch+\" (\"+s+\")\");",
342     "}\n\n\n",
343     "\n",
344     "//The following are now global, to aid in error reporting",
345     "int yyn;       //next next thing to do",
346     "int yym;       //",
347     "int yystate;   //current parsing state from state table",
348     "String yys;    //current token string",
349     "\n",
350     "//###############################################################",
351     "// method: yyparse : parse input and execute indicated items",
352     "//###############################################################",
353 	"int yyparse()",
354 	0
355 };
356 
357 /* yio 20020304: thrown exceptions can be inserted between body sections a and b */
358 
359 char *jbody_b[] =
360 {
361     "{",
362     "boolean doaction;",
363     "  init_stacks();",
364     "  yynerrs = 0;",
365     "  yyerrflag = 0;",
366     "  yychar = -1;          //impossible char forces a read",
367     "  yystate=0;            //initial state",
368     "  state_push(yystate);  //save it",
369     "  val_push(yylval);     //save empty value",
370     "  while (true) //until parsing is done, either correctly, or w/error",
371     "    {",
372     "    doaction=true;",
373     "    if (yydebug) debug(\"loop\"); ",
374     "    //#### NEXT ACTION (from reduction table)",
375     "    for (yyn=yydefred[yystate];yyn==0;yyn=yydefred[yystate])",
376     "      {",
377     "      if (yydebug) debug(\"yyn:\"+yyn+\"  state:\"+yystate+\"  yychar:\"+yychar);",
378     "      if (yychar < 0)      //we want a char?",
379     "        {",
380     "        yychar = yylex();  //get next token",
381     "        if (yydebug) debug(\" next yychar:\"+yychar);",
382     "        //#### ERROR CHECK ####",
383     "        if (yychar < 0)    //it it didn't work/error",
384     "          {",
385     "          yychar = 0;      //change it to default string (no -1!)",
386     "          if (yydebug)",
387     "            yylexdebug(yystate,yychar);",
388     "          }",
389     "        }//yychar<0",
390     "      yyn = yysindex[yystate];  //get amount to shift by (shift index)",
391     "      if ((yyn != 0) && (yyn += yychar) >= 0 &&",
392     "          yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
393     "        {",
394     "        if (yydebug)",
395     "          debug(\"state \"+yystate+\", shifting to state \"+yytable[yyn]);",
396     "        //#### NEXT STATE ####",
397     "        yystate = yytable[yyn];//we are in a new state",
398     "        state_push(yystate);   //save it",
399     "        val_push(yylval);      //push our lval as the input for next rule",
400     "        yychar = -1;           //since we have 'eaten' a token, say we need another",
401     "        if (yyerrflag > 0)     //have we recovered an error?",
402     "           --yyerrflag;        //give ourselves credit",
403     "        doaction=false;        //but don't process yet",
404     "        break;   //quit the yyn=0 loop",
405     "        }",
406     "",
407     "    yyn = yyrindex[yystate];  //reduce",
408     "    if ((yyn !=0 ) && (yyn += yychar) >= 0 &&",
409     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
410     "      {   //we reduced!",
411     "      if (yydebug) debug(\"reduce\");",
412     "      yyn = yytable[yyn];",
413     "      doaction=true; //get ready to execute",
414     "      break;         //drop down to actions",
415     "      }",
416     "    else //ERROR RECOVERY",
417     "      {",
418     "      if (yyerrflag==0)",
419     "        {",
420     "        yyerror(\"syntax error\");",
421     "        yynerrs++;",
422     "        }",
423     "      if (yyerrflag < 3) //low error count?",
424     "        {",
425     "        yyerrflag = 3;",
426     "        while (true)   //do until break",
427     "          {",
428     "          if (stateptr<0)   //check for under & overflow here",
429     "            {",
430     "            yyerror(\"stack underflow. aborting...\");  //note lower case 's'",
431     "            return 1;",
432     "            }",
433     "          yyn = yysindex[state_peek(0)];",
434     "          if ((yyn != 0) && (yyn += YYERRCODE) >= 0 &&",
435     "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
436     "            {",
437     "            if (yydebug)",
438     "              debug(\"state \"+state_peek(0)+\", error recovery shifting to state \"+yytable[yyn]+\" \");",
439     "            yystate = yytable[yyn];",
440     "            state_push(yystate);",
441     "            val_push(yylval);",
442     "            doaction=false;",
443     "            break;",
444     "            }",
445     "          else",
446     "            {",
447     "            if (yydebug)",
448     "              debug(\"error recovery discarding state \"+state_peek(0)+\" \");",
449     "            if (stateptr<0)   //check for under & overflow here",
450     "              {",
451     "              yyerror(\"Stack underflow. aborting...\");  //capital 'S'",
452     "              return 1;",
453     "              }",
454     "            state_pop();",
455     "            val_pop();",
456     "            }",
457     "          }",
458     "        }",
459     "      else            //discard this token",
460     "        {",
461     "        if (yychar == 0)",
462     "          return 1; //yyabort",
463     "        if (yydebug)",
464     "          {",
465     "          yys = null;",
466     "          if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
467     "          if (yys == null) yys = \"illegal-symbol\";",
468     "          debug(\"state \"+yystate+\", error recovery discards token \"+yychar+\" (\"+yys+\")\");",
469     "          }",
470     "        yychar = -1;  //read another",
471     "        }",
472     "      }//end error recovery",
473     "    }//yyn=0 loop",
474     "    if (!doaction)   //any reason not to proceed?",
475     "      continue;      //skip action",
476     "    yym = yylen[yyn];          //get count of terminals on rhs",
477     "    if (yydebug)",
478     "      debug(\"state \"+yystate+\", reducing \"+yym+\" by rule \"+yyn+\" (\"+yyrule[yyn]+\")\");",
479     "    if (yym>0)                 //if count of rhs not 'nil'",
480     "      yyval = val_peek(yym-1); //get current semantic value",
481     "    yyval = dup_yyval(yyval); //duplicate yyval if ParserVal is used as semantic value",
482     "    switch(yyn)",
483     "      {",
484     "//########## USER-SUPPLIED ACTIONS ##########",
485     0
486 };
487 
488 
489 /* yio 20011121: this version never checks debug flag for better performance */
490 char *jbody_nodebug_a[] =
491 {
492     "//###############################################################",
493     "// method: yylexdebug : check lexer state",
494     "//###############################################################",
495     "void yylexdebug(int state,int ch)",
496     "{",
497     "String s=null;",
498     "  if (ch < 0) ch=0;",
499     "  if (ch <= YYMAXTOKEN) //check index bounds",
500     "     s = yyname[ch];    //now get it",
501     "  if (s==null)",
502     "    s = \"illegal-symbol\";",
503     "  debug(\"state \"+state+\", reading \"+ch+\" (\"+s+\")\");",
504     "}\n\n\n",
505     "\n",
506     "//The following are now global, to aid in error reporting",
507     "int yyn;       //next next thing to do",
508     "int yym;       //",
509     "int yystate;   //current parsing state from state table",
510     "String yys;    //current token string",
511     "\n",
512     "//###############################################################",
513     "// method: yyparse : parse input and execute indicated items",
514     "//###############################################################",
515     "int yyparse()",
516 	0
517 };
518 
519 /* yio 20020304: thrown exceptions can be inserted between body sections a and b */
520 
521 char *jbody_nodebug_b[] =
522 {
523     "{",
524     "boolean doaction;",
525     "  init_stacks();",
526     "  yynerrs = 0;",
527     "  yyerrflag = 0;",
528     "  yychar = -1;          //impossible char forces a read",
529     "  yystate=0;            //initial state",
530     "  state_push(yystate);  //save it",
531     "  val_push(yylval);     //save empty value",
532     "  while (true) //until parsing is done, either correctly, or w/error",
533     "    {",
534     "    doaction=true;",
535     "    //if (yydebug) debug(\"loop\"); ",
536     "    //#### NEXT ACTION (from reduction table)",
537     "    for (yyn=yydefred[yystate];yyn==0;yyn=yydefred[yystate])",
538     "      {",
539     "      //if (yydebug) debug(\"yyn:\"+yyn+\"  state:\"+yystate+\"  yychar:\"+yychar);",
540     "      if (yychar < 0)      //we want a char?",
541     "        {",
542     "        yychar = yylex();  //get next token",
543     "        //if (yydebug) debug(\" next yychar:\"+yychar);",
544     "        //#### ERROR CHECK ####",
545     "        if (yychar < 0)    //it it didn't work/error",
546     "          {",
547     "          yychar = 0;      //change it to default string (no -1!)",
548     "          //if (yydebug)",
549     "          //  yylexdebug(yystate,yychar);",
550     "          }",
551     "        }//yychar<0",
552     "      yyn = yysindex[yystate];  //get amount to shift by (shift index)",
553     "      if ((yyn != 0) && (yyn += yychar) >= 0 &&",
554     "          yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
555     "        {",
556     "        //if (yydebug)",
557     "          //debug(\"state \"+yystate+\", shifting to state \"+yytable[yyn]);",
558     "        //#### NEXT STATE ####",
559     "        yystate = yytable[yyn];//we are in a new state",
560     "        state_push(yystate);   //save it",
561     "        val_push(yylval);      //push our lval as the input for next rule",
562     "        yychar = -1;           //since we have 'eaten' a token, say we need another",
563     "        if (yyerrflag > 0)     //have we recovered an error?",
564     "           --yyerrflag;        //give ourselves credit",
565     "        doaction=false;        //but don't process yet",
566     "        break;   //quit the yyn=0 loop",
567     "        }",
568     "",
569     "    yyn = yyrindex[yystate];  //reduce",
570     "    if ((yyn !=0 ) && (yyn += yychar) >= 0 &&",
571     "            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)",
572     "      {   //we reduced!",
573     "      //if (yydebug) debug(\"reduce\");",
574     "      yyn = yytable[yyn];",
575     "      doaction=true; //get ready to execute",
576     "      break;         //drop down to actions",
577     "      }",
578     "    else //ERROR RECOVERY",
579     "      {",
580     "      if (yyerrflag==0)",
581     "        {",
582     "        yyerror(\"syntax error\");",
583     "        yynerrs++;",
584     "        }",
585     "      if (yyerrflag < 3) //low error count?",
586     "        {",
587     "        yyerrflag = 3;",
588     "        while (true)   //do until break",
589     "          {",
590     "          if (stateptr<0)   //check for under & overflow here",
591     "            {",
592     "            yyerror(\"stack underflow. aborting...\");  //note lower case 's'",
593     "            return 1;",
594     "            }",
595     "          yyn = yysindex[state_peek(0)];",
596     "          if ((yyn != 0) && (yyn += YYERRCODE) >= 0 &&",
597     "                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)",
598     "            {",
599     "            //if (yydebug)",
600     "              //debug(\"state \"+state_peek(0)+\", error recovery shifting to state \"+yytable[yyn]+\" \");",
601     "            yystate = yytable[yyn];",
602     "            state_push(yystate);",
603     "            val_push(yylval);",
604     "            doaction=false;",
605     "            break;",
606     "            }",
607     "          else",
608     "            {",
609     "            //if (yydebug)",
610     "              //debug(\"error recovery discarding state \"+state_peek(0)+\" \");",
611     "            if (stateptr<0)   //check for under & overflow here",
612     "              {",
613     "              yyerror(\"Stack underflow. aborting...\");  //capital 'S'",
614     "              return 1;",
615     "              }",
616     "            state_pop();",
617     "            val_pop();",
618     "            }",
619     "          }",
620     "        }",
621     "      else            //discard this token",
622     "        {",
623     "        if (yychar == 0)",
624     "          return 1; //yyabort",
625     "        //if (yydebug)",
626     "          //{",
627     "          //yys = null;",
628     "          //if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
629     "          //if (yys == null) yys = \"illegal-symbol\";",
630     "          //debug(\"state \"+yystate+\", error recovery discards token \"+yychar+\" (\"+yys+\")\");",
631     "          //}",
632     "        yychar = -1;  //read another",
633     "        }",
634     "      }//end error recovery",
635     "    }//yyn=0 loop",
636     "    if (!doaction)   //any reason not to proceed?",
637     "      continue;      //skip action",
638     "    yym = yylen[yyn];          //get count of terminals on rhs",
639     "    //if (yydebug)",
640     "      //debug(\"state \"+yystate+\", reducing \"+yym+\" by rule \"+yyn+\" (\"+yyrule[yyn]+\")\");",
641     "    if (yym>0)                 //if count of rhs not 'nil'",
642     "      yyval = val_peek(yym-1); //get current semantic value",
643     "    yyval = dup_yyval(yyval); //duplicate yyval if ParserVal is used as semantic value",
644     "    switch(yyn)",
645     "      {",
646     "//########## USER-SUPPLIED ACTIONS ##########",
647     0
648 };
649 
650 
651 
652 
653 
654 
655 
656 
657 
658 
659 char *trailer[] =
660 {
661     "    }",
662     "    yyssp -= yym;",
663     "    yystate = *yyssp;",
664     "    yyvsp -= yym;",
665     "    yym = yylhs[yyn];",
666     "    if (yystate == 0 && yym == 0)",
667     "    {",
668     "#if YYDEBUG",
669     "        if (yydebug)",
670     "            printf(\"yydebug: after reduction, shifting from state 0 to\\",
671     " state %d\\n\", YYFINAL);",
672     "#endif",
673     "        yystate = YYFINAL;",
674     "        *++yyssp = YYFINAL;",
675     "        *++yyvsp = yyval;",
676     "        if (yychar < 0)",
677     "        {",
678     "            if ((yychar = yylex()) < 0) yychar = 0;",
679     "#if YYDEBUG",
680     "            if (yydebug)",
681     "            {",
682     "                yys = 0;",
683     "                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];",
684     "                if (!yys) yys = \"illegal-symbol\";",
685     "                printf(\"yydebug: state %d, reading %d (%s)\\n\",",
686     "                        YYFINAL, yychar, yys);",
687     "            }",
688     "#endif",
689     "        }",
690     "        if (yychar == 0) goto yyaccept;",
691     "        goto yyloop;",
692     "    }",
693     "    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&",
694     "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
695     "        yystate = yytable[yyn];",
696     "    else",
697     "        yystate = yydgoto[yym];",
698     "#if YYDEBUG",
699     "    if (yydebug)",
700     "        printf(\"yydebug: after reduction, shifting from state %d \\",
701     "to state %d\\n\", *yyssp, yystate);",
702     "#endif",
703     "    if (yyssp >= yyss + yystacksize - 1)",
704     "    {",
705     "        goto yyoverflow;",
706     "    }",
707     "    *++yyssp = yystate;",
708     "    *++yyvsp = yyval;",
709     "    goto yyloop;",
710     "yyoverflow:",
711     "    yyerror(\"yacc stack overflow\");",
712     "yyabort:",
713     "    return (1);",
714     "yyaccept:",
715     "    return (0);",
716     "}",
717     0
718 };
719 
720 char *jtrailer[] =
721 {
722     "//########## END OF USER-SUPPLIED ACTIONS ##########",
723     "    }//switch",
724     "    //#### Now let's reduce... ####",
725     "    if (yydebug) debug(\"reduce\");",
726     "    state_drop(yym);             //we just reduced yylen states",
727     "    yystate = state_peek(0);     //get new state",
728     "    val_drop(yym);               //corresponding value drop",
729     "    yym = yylhs[yyn];            //select next TERMINAL(on lhs)",
730     "    if (yystate == 0 && yym == 0)//done? 'rest' state and at first TERMINAL",
731     "      {",
732     "      if (yydebug) debug(\"After reduction, shifting from state 0 to state \"+YYFINAL+\"\");",
733     "      yystate = YYFINAL;         //explicitly say we're done",
734     "      state_push(YYFINAL);       //and save it",
735     "      val_push(yyval);           //also save the semantic value of parsing",
736     "      if (yychar < 0)            //we want another character?",
737     "        {",
738     "        yychar = yylex();        //get next character",
739     "        if (yychar<0) yychar=0;  //clean, if necessary",
740     "        if (yydebug)",
741     "          yylexdebug(yystate,yychar);",
742     "        }",
743     "      if (yychar == 0)          //Good exit (if lex returns 0 ;-)",
744     "         break;                 //quit the loop--all DONE",
745     "      }//if yystate",
746     "    else                        //else not done yet",
747     "      {                         //get next state and push, for next yydefred[]",
748     "      yyn = yygindex[yym];      //find out where to go",
749     "      if ((yyn != 0) && (yyn += yystate) >= 0 &&",
750     "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
751     "        yystate = yytable[yyn]; //get new state",
752     "      else",
753     "        yystate = yydgoto[yym]; //else go to new defred",
754     "      if (yydebug) debug(\"after reduction, shifting from state \"+state_peek(0)+\" to state \"+yystate+\"\");",
755     "      state_push(yystate);     //going again, so push state & val...",
756     "      val_push(yyval);         //for next action",
757     "      }",
758     "    }//main loop",
759     "  return 0;//yyaccept!!",
760     "}",
761     "//## end of method parse() ######################################",
762     "\n\n",
763     "//## run() --- for Thread #######################################",
764     JAVA_RUN,
765     "//## end of method run() ########################################",
766     "\n\n",
767     "//## Constructors ###############################################",
768     JAVA_CONSTRUCT,
769     "//###############################################################",
770     "\n\n",
771     "}",
772     "//################### END OF CLASS ##############################",
773     0
774 };
775 
776 /* yio 20011121: this version never checks debug flag for better performance */
777 char *jtrailer_nodebug[] =
778 {
779     "//########## END OF USER-SUPPLIED ACTIONS ##########",
780     "    }//switch",
781     "    //#### Now let's reduce... ####",
782     "    //if (yydebug) debug(\"reduce\");",
783     "    state_drop(yym);             //we just reduced yylen states",
784     "    yystate = state_peek(0);     //get new state",
785     "    val_drop(yym);               //corresponding value drop",
786     "    yym = yylhs[yyn];            //select next TERMINAL(on lhs)",
787     "    if (yystate == 0 && yym == 0)//done? 'rest' state and at first TERMINAL",
788     "      {",
789     "      //if (yydebug) debug(\"After reduction, shifting from state 0 to state \"+YYFINAL+\"\");",
790     "      yystate = YYFINAL;         //explicitly say we're done",
791     "      state_push(YYFINAL);       //and save it",
792     "      val_push(yyval);           //also save the semantic value of parsing",
793     "      if (yychar < 0)            //we want another character?",
794     "        {",
795     "        yychar = yylex();        //get next character",
796     "        if (yychar<0) yychar=0;  //clean, if necessary",
797     "        //if (yydebug)",
798     "          //yylexdebug(yystate,yychar);",
799     "        }",
800     "      if (yychar == 0)          //Good exit (if lex returns 0 ;-)",
801     "         break;                 //quit the loop--all DONE",
802     "      }//if yystate",
803     "    else                        //else not done yet",
804     "      {                         //get next state and push, for next yydefred[]",
805     "      yyn = yygindex[yym];      //find out where to go",
806     "      if ((yyn != 0) && (yyn += yystate) >= 0 &&",
807     "            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)",
808     "        yystate = yytable[yyn]; //get new state",
809     "      else",
810     "        yystate = yydgoto[yym]; //else go to new defred",
811     "      //if (yydebug) debug(\"after reduction, shifting from state \"+state_peek(0)+\" to state \"+yystate+\"\");",
812     "      state_push(yystate);     //going again, so push state & val...",
813     "      val_push(yyval);         //for next action",
814     "      }",
815     "    }//main loop",
816     "  return 0;//yyaccept!!",
817     "}",
818     "//## end of method parse() ######################################",
819     "\n\n",
820     "//## run() --- for Thread #######################################",
821     JAVA_RUN,
822     "//## end of method run() ########################################",
823     "\n\n",
824     "//## Constructors ###############################################",
825     JAVA_CONSTRUCT,
826     "//###############################################################",
827     "\n\n",
828     "}",
829     "//################### END OF CLASS ##############################",
830     0
831 };
832 
833 
834 
835 
836 
837 
838 
839 
write_section(char ** section)840 void write_section(char **section)
841 {
842 int i;
843 FILE *fp;
844   fp = code_file;
845   for (i = 0; section[i]; ++i)
846     {
847     ++outline;
848     if (strcmp(section[i],JAVA_PACKAGE)==0)  /*Java package name, if any*/
849       {
850       if (jpackage_name && strlen(jpackage_name)>0)
851         fprintf(fp,"package %s;\n",jpackage_name);
852       }
853     else if (strcmp(section[i],JAVA_CLASS_DECL)==0)
854       {
855       if (jclass_name && strlen(jclass_name)>0)
856         fprintf(fp,"public class %s\n",jclass_name);
857       else
858         fprintf(fp,"public class Parser\n");
859       if (jextend_name && strlen(jextend_name)>0)
860         fprintf(fp,"             extends %s\n",jextend_name);
861       if (jimplement_name && strlen(jimplement_name)>0)
862         fprintf(fp,"             implements %s\n",jimplement_name);
863       }
864     else if (strcmp(section[i],JAVA_RUN)==0)
865       {
866       if (jrun)
867         {
868         fprintf(fp,"/**\n");
869         fprintf(fp," * A default run method, used for operating this parser\n");
870         fprintf(fp," * object in the background.  It is intended for extending Thread\n");
871         fprintf(fp," * or implementing Runnable.  Turn off with -Jnorun .\n");
872         fprintf(fp," */\n");
873         fprintf(fp,"public void run()\n");
874         fprintf(fp,"{\n");
875         fprintf(fp,"  yyparse();\n");
876         fprintf(fp,"}\n");
877         }
878       else
879         {
880         fprintf(fp,"//## The -Jnorun option was used ##\n");
881         }
882       }
883     else if (strcmp(section[i],JAVA_CONSTRUCT)==0)
884       {
885       if (jconstruct)
886         {
887         fprintf(fp,"/**\n");
888         fprintf(fp," * Default constructor.  Turn off with -Jnoconstruct .\n\n");
889         fprintf(fp," */\n");
890         fprintf(fp,"public %s()\n",jclass_name);
891         fprintf(fp,"{\n");
892         fprintf(fp,"  //nothing to do\n");
893         fprintf(fp,"}\n");
894         fprintf(fp,"\n\n");
895         fprintf(fp,"/**\n");
896         fprintf(fp," * Create a parser, setting the debug to true or false.\n");
897         fprintf(fp," * @param debugMe true for debugging, false for no debug.\n");
898         fprintf(fp," */\n");
899          fprintf(fp,"public %s(boolean debugMe)\n",jclass_name);
900         fprintf(fp,"{\n");
901         fprintf(fp,"  yydebug=debugMe;\n");
902         fprintf(fp,"}\n");
903         }
904       else
905         {
906         fprintf(fp,"//## The -Jnoconstruct option was used ##\n");
907         }
908       }
909     else if (strcmp(section[i],JAVA_STACK)==0)
910       {
911       fprintf(fp,
912        "final static int YYSTACKSIZE = %d;  //maximum stack size",
913        jstack_size);
914       }
915     else
916       fprintf(fp, "%s\n", section[i]);
917     }
918 }
919 
920 
921 
922