header.c 3.7 KB

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