header.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # include "ldefs.h"
  2. extern int nine;
  3. void
  4. phead1(void)
  5. {
  6. Bprint(&fout,"typedef unsigned char Uchar;\n");
  7. if (nine) {
  8. Bprint(&fout,"# include <u.h>\n");
  9. Bprint(&fout,"# include <libc.h>\n");
  10. }
  11. Bprint(&fout,"# include <stdio.h>\n");
  12. Bprint(&fout, "# define U(x) x\n");
  13. Bprint(&fout, "# define NLSTATE yyprevious=YYNEWLINE\n");
  14. Bprint(&fout,"# define BEGIN yybgin = yysvec + 1 +\n");
  15. Bprint(&fout,"# define INITIAL 0\n");
  16. Bprint(&fout,"# define YYLERR yysvec\n");
  17. Bprint(&fout,"# define YYSTATE (yyestate-yysvec-1)\n");
  18. Bprint(&fout,"# define YYOPTIM 1\n");
  19. # ifdef DEBUG
  20. Bprint(&fout,"# define LEXDEBUG 1\n");
  21. # endif
  22. Bprint(&fout,"# define YYLMAX 200\n");
  23. Bprint(&fout,
  24. "# define unput(c) {yytchar= (c);if(yytchar=='\\n')yylineno--;*yysptr++=yytchar;}\n");
  25. Bprint(&fout,"# define yymore() (yymorfg=1)\n");
  26. Bprint(&fout,"# define ECHO fprintf(yyout, \"%%s\",yytext)\n");
  27. Bprint(&fout,"# define REJECT { nstr = yyreject(); goto yyfussy;}\n");
  28. Bprint(&fout,"int yyleng; extern char yytext[];\n");
  29. Bprint(&fout,"int yymorfg;\n");
  30. Bprint(&fout,"extern Uchar *yysptr, yysbuf[];\n");
  31. Bprint(&fout,"int yytchar;\n");
  32. Bprint(&fout,"FILE *yyin = {stdin}, *yyout = {stdout};\n");
  33. Bprint(&fout,"extern int yylineno;\n");
  34. Bprint(&fout,"struct yysvf { \n");
  35. Bprint(&fout,"\tstruct yywork *yystoff;\n");
  36. Bprint(&fout,"\tstruct yysvf *yyother;\n");
  37. Bprint(&fout,"\tint *yystops;};\n");
  38. Bprint(&fout,"struct yysvf *yyestate;\n");
  39. Bprint(&fout,"extern struct yysvf yysvec[], *yybgin;\n");
  40. Bprint(&fout,"int yylook(void), yywrap(void), yyback(int *, int);\n");
  41. if(nine) {
  42. Bprint(&fout,
  43. "int infd, outfd;\n"
  44. "\n"
  45. "void\n"
  46. "output(char c)\n"
  47. "{\n"
  48. " int rv;\n"
  49. " if ((rv = write(outfd, &c, 1)) < 0)\n"
  50. " sysfatal(\"output: %%r\");\n"
  51. " if (rv == 0)\n"
  52. " sysfatal(\"output: EOF?\");\n"
  53. "}\n"
  54. "\n"
  55. "int\n"
  56. "input(void)\n"
  57. "{\n"
  58. " if(yysptr > yysbuf)\n"
  59. " yytchar = U(*--yysptr);\n"
  60. " else {\n"
  61. " int rv;\n"
  62. " if ((rv = read(infd, &yytchar, 1)) < 0)\n"
  63. " sysfatal(\"input: %%r\");\n"
  64. " if (rv == 0)\n"
  65. " return 0;\n"
  66. " }\n"
  67. " if (yytchar == '\\n')\n"
  68. " yylineno++;\n"
  69. " return yytchar;\n"
  70. "}\n");
  71. }
  72. else {
  73. Bprint(&fout,"# define output(c) putc(c,yyout)\n");
  74. Bprint(&fout, "%s%d%s\n",
  75. "# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==",
  76. '\n',
  77. "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
  78. }
  79. }
  80. void
  81. phead2(void)
  82. {
  83. Bprint(&fout,"while((nstr = yylook()) >= 0)\n");
  84. Bprint(&fout,"yyfussy: switch(nstr){\n");
  85. Bprint(&fout,"case 0:\n");
  86. Bprint(&fout,"if(yywrap()) return(0); break;\n");
  87. }
  88. void
  89. ptail(void)
  90. {
  91. if(!pflag){
  92. Bprint(&fout,"case -1:\nbreak;\n"); /* for reject */
  93. Bprint(&fout,"default:\n");
  94. Bprint(&fout,"fprintf(yyout,\"bad switch yylook %%d\",nstr);\n");
  95. Bprint(&fout,"} return(0); }\n");
  96. Bprint(&fout,"/* end of yylex */\n");
  97. }
  98. pflag = 1;
  99. }
  100. void
  101. statistics(void)
  102. {
  103. fprint(errorf,"%d/%d nodes(%%e), %d/%d positions(%%p), %d/%d (%%n), %ld transitions\n",
  104. tptr, treesize, (int)(nxtpos-positions), maxpos, stnum+1, nstates, rcount);
  105. fprint(errorf, ", %d/%d packed char classes(%%k)", (int)(pcptr-pchar), pchlen);
  106. fprint(errorf,", %d/%d packed transitions(%%a)",nptr, ntrans);
  107. fprint(errorf, ", %d/%d output slots(%%o)", yytop, outsize);
  108. fprint(errorf,"\n");
  109. }