home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / OS2_LEX.ZIP / OUT1.C < prev    next >
C/C++ Source or Header  |  1989-10-25  |  12KB  |  338 lines

  1. /*
  2.  * Copyright (c) 1978 Charles H. Forsyth
  3.  *
  4.  * Modified 02-Dec-80 Bob Denny -- Conditionalize debug code for smaller size
  5.  *                           01 -- Removed ending() function code from here
  6.  *                                  to lex.c, so ytab.c code could share the
  7.  *                                  same overlay region as this module.
  8.  *                           02 -- Removed nfaprint(), llactr(), newcase(),
  9.  *                                  cclprint(), chprint() and setline(),
  10.  *                                  the rest of this can share an overlay.
  11.  *                                  They're in 'out2.c'. This is now 'out1.c'.
  12.  *          29-May-81 Bob Denny -- More extern hacking for RSX overlaying.
  13.  *          19-Mar-82 Bob Denny -- New compiler and library
  14.  *          03-May-82 Bob Denny -- Final touches, remove unreferenced autos
  15.  *          28-Aug-82 Bob Denny -- Put "=" into table initializers to make
  16.  *                                  new compiler happy. Add "-s" code to
  17.  *                                  supress "#include <stdio.h>" in output.
  18.  *                                  Tables output 8 values/line instead of
  19.  *                                  16.  Overran R.H. edge on 3 digit octals.
  20.  *                                  Change output format for readability.
  21.  *          31-Aug-82 Bob Denny -- Add lexswitch( ...) to llstin so table
  22.  *                                  name selected by -t switch is automatically
  23.  *                                  switched-to at yylex() startup time.  Removed
  24.  *                                  hard reference to "lextab" from yylex();
  25.  *                                  This module generates extern declaration
  26.  *                                  for forward reference.
  27.  *          14-Apr-83 Bob Denny -- Add VAX11C support.  Remove usage of remote
  28.  *                                  formats (damn!) not supported on VAX-11 C.
  29.  *                                 Use "short int" for 16-bit table items under
  30.  *                                  VAX-11 C to save size.
  31.  *                                 Contitional out flaky debug code.  Probably
  32.  *                                  related to non-functional minimization.
  33.  *            20-Nov-83 Scott Guthery -- Adapt for IBM PC & DeSmet C
  34.  *            26-Dec-83 Scott Guthery -- Removed "extern FILE lexin" from llstin
  35.  *                                       code and put it lex.h.
  36.  */
  37.  
  38. /*
  39.  * lex -- output human- and machine-readable tables
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include "lexlex.h"
  45. #include "lextern.h"
  46.  
  47. extern char *ignore;
  48. extern char *illeg;
  49. extern char *breakc;
  50. extern int nlook;
  51.  
  52. void dospccl(char *, char *, char *);
  53. void refccl(char *, char *, char *);
  54. void vstart(char *, char *);
  55. void vend(void);
  56. void vel(char *, int);
  57.  
  58. char strdec1[] = "\nstruct lextab %s =\t{\n";
  59. char strdec2[] = "\t\t\t%d,\t\t/* Highest state */\n";
  60. char strdec3[] = "\t\t\t_D%s\t/* --> \"Default state\" table */\n";
  61. char strdec4[] = "\t\t\t_N%s\t/* --> \"Next state\" table */\n";
  62. char strdec5[] = "\t\t\t_C%s\t/* --> \"Check value\" table */\n";
  63. char strdec6[] = "\t\t\t_B%s\t/* --> \"Base\" table */\n";
  64. char strdec7[] = "\t\t\t%d,\t\t/* Index of last entry in \"next\" */\n";
  65. char strdec8[] = "\t\t\t%s,\t\t/* --> Byte-int move routine */\n";
  66. char strdec9[] = "\t\t\t_F%s\t/* --> \"Final state\" table */\n";
  67. char strdec10[] = "\t\t\t_A%s\t/* --> Action routine */\n";
  68. char strdec11[] = "\n\t\t\t%s%s\t/* Look-ahead vector */\n";
  69.  
  70. char ptabnam[] = { "         " };
  71.  
  72. /*
  73.  * Print the minimised DFA, and at the same time, construct the vector which
  74.  * indicates final states by associating them with their translation index.
  75.  * (DFA printout supressed ifndef DEBUG.)
  76.  */
  77. void dfaprint(void)
  78. {
  79.         register struct move *dp;
  80.         register struct dfa *st;
  81.         register i;
  82.         int fi, k, l;
  83.  
  84.         vstart("LL16BIT _F%s", tabname);
  85. #ifdef DEBUG
  86.         fprintf(lexlog, "\nMinimised DFA for complete syntax\n");
  87. #endif
  88.         for (i = 0; i < ndfa; i++) {
  89. #ifdef DEBUG
  90.                 fprintf(lexlog, "\nstate %d", i);
  91. #endif
  92.                 st = &dfa[i];
  93.                 k = -1;
  94.                 if (fi = st->df_name->s_final) {
  95.                         k = nfa[fi].n_trans - trans;
  96. #ifdef DEBUG
  97.                         fprintf(lexlog, " (final %d[%d])", fi, k);
  98. #endif
  99.                         if (nfa[fi].n_flag&FLOOK) {
  100.                                 k |= (nfa[fi].n_look+1)<<11;
  101. #ifdef DEBUG
  102.                                 fprintf(lexlog, " restore %d", nfa[fi].n_look);
  103. #endif
  104.                         }
  105.                 }
  106.                 if (l = st->df_name->s_look)
  107. #ifdef DEBUG
  108.                         fprintf(lexlog, " look-ahead %o", l);
  109. #else
  110.                         ;
  111. #endif
  112.                 vel(" %d,", k);
  113. #ifdef MINIM_OK
  114.                 k = st->df_name->s_group->s_els[0];
  115.                 if (k!=i) {
  116.                         fprintf(lexlog, " deleted\n");
  117.                         continue;
  118.                 }
  119.  
  120.                 fprintf(lexlog, "\n");
  121.                 for (dp = st->df_base; dp < st->df_max; dp = range(st, dp))
  122.                 if (st->df_default)
  123.                       fprintf(lexlog, "\t.\tsame as %d\n", st->df_default-dfa);
  124. #endif
  125.         }
  126.         vel(" %d,", -1);        /* blocking state */
  127.         vend();
  128. }
  129.  
  130. #ifdef MINIM_OK
  131.  
  132. range(st, dp)
  133. register struct dfa *st;
  134. register struct move *dp;
  135. {
  136.         int low, high, last;
  137.         struct set *s;
  138.         register a;
  139.  
  140.         while (dp < st->df_max && dp->m_check!=st)
  141.                 dp++;
  142. /***************************************************
  143.  * This always returns given the above statement ! *
  144.  ***************************************************/
  145.         if (dp >= st->df_max)
  146.                 return(dp);
  147.  
  148.         low = dp - st->df_base;
  149. /*
  150.         s = dp->m_next->s_group->s_els[0];
  151. */
  152.         s = dp->m_next;
  153.         for (last = low-1; dp < st->df_max &&
  154.                            dp->m_check==st &&
  155.                            (a = dp - st->df_base)==last+1 &&
  156. /*
  157.                            dp->m_next->s_state->s_els[0]==s; dp++)
  158. */
  159.                            dp->m_next==s; dp++)
  160.                 last = a;
  161.         high = last;
  162.         fprintf(lexlog, "\t");
  163.         if (high==low)
  164.                 chprint(low);
  165.         else {
  166.                 fprintf(lexlog, "[");
  167.                 if (high-low > 4) {
  168.                         chprint(low);
  169.                         fprintf(lexlog, "-");
  170.                         chprint(high);
  171.                 } else {
  172.                         while (low<=high)
  173.                                 chprint(low++);
  174.                 }
  175.                 fprintf(lexlog, "]");
  176.         }
  177.         if (s->s_state==NULL)
  178.                 fprintf(lexlog, "\tNULL\n"); else
  179.                 fprintf(lexlog, "\t%d\n", s->s_state-dfa);
  180.         return(dp);
  181. }
  182. #endif
  183.  
  184. void heading(void)
  185. {
  186.         char *ver;
  187.         fprintf(llout,
  188.          "/*\n * Created by IBM PC LEX from file \"%s\"\n", infile);
  189.         if(sflag == 0)                  /* If "standalone" switch off */
  190.                 {
  191.                 fprintf(llout,
  192.                         " *\t- for use with standard I/O\n */\n");
  193.                 fprintf(llout, "\n#include <stdio.h>\n");
  194.                 }
  195.         else
  196.                 fprintf(llout, " *\t- for use with stand-alone I/O\n */\n");
  197.  
  198.         fprintf(llout, "#include <lex.h>\n");
  199.         fprintf(llout, "#define LL16BIT int\n");
  200.         fprintf(llout, "extern int _lmov%c();\n",
  201.                         (ndfa <= 255) ? 'b' : 'i');
  202.         fprintf(llout, "extern struct lextab *_tabp;\n");
  203.         fprintf(llout, "int lexval, yyline;\n");
  204.         fprintf(llout, "char llbuf[], *llend;\n");
  205. }
  206.  
  207. void dfawrite(void)
  208. {
  209.         register struct move *dp;
  210.         register i, a;
  211.         struct dfa *st, *def;
  212.         struct set *xp;
  213.         char *xcp;
  214.  
  215.         setline();
  216.         fprintf(llout,
  217.                 "\n#define\tLLTYPE1\t%s\n", ndfa<=255? "char": "LL16BIT");
  218.         vstart("LLTYPE1 _N%s", tabname);
  219.         for (i = 0; i <= llnxtmax; i++)
  220.                 if (xp = move[i].m_next)
  221.                         vel(" %d,", xp->s_state-dfa); else
  222.                         vel(" %d,", ndfa);
  223.         vend();
  224.         vstart("LLTYPE1 _C%s", tabname);
  225.         for (i = 0; i <= llnxtmax; i++)
  226.                 if (st = move[i].m_check)
  227.                         vel(" %d,", st-dfa); else
  228.                         vel(" %d,", -1);
  229.         vend();
  230.         vstart("LLTYPE1 _D%s", tabname);
  231.         for (i = 0; i < ndfa; i++)
  232.                 if (def = dfa[i].df_default)
  233.                         vel(" %d,", def-dfa); else
  234.                         vel(" %d,", ndfa); /* refer to blocking state */
  235.         vend();
  236.         vstart("LL16BIT _B%s", tabname);
  237.         for (i = 0; i < ndfa; i++)
  238.                 if (dp = dfa[i].df_base)
  239.                         vel(" %d,", dp-move); else
  240.                         vel(" %d,", 0);
  241.         vel(" %d,", 0); /* for blocking state */
  242.         vend();
  243.         if (nlook) {
  244.                 fprintf(llout, "char    *llsave[%d];\n", nlook);
  245.                 vstart("LL16BIT _L%s", tabname);
  246.                 a = nlook<=NBPC? NCHARS-1: -1;
  247.                 for (i = 0; i < ndfa; i++)
  248.                         vel(" 0%o,", dfa[i].df_name->s_look&a);
  249.                 vel(" %d,", 0);
  250.                 vend();
  251.         }
  252.         dospccl(ignore, "LLIGN", "X");
  253.         dospccl(breakc, "LLBRK", "Y");
  254.         dospccl(illeg, "LLILL", "Z");
  255.  
  256.         strcpy(ptabnam, tabname); strcat(ptabnam,",");
  257.         fprintf(llout, strdec1, tabname);
  258.         fprintf(llout, strdec2, ndfa);
  259.         fprintf(llout, strdec3, ptabnam);
  260.         fprintf(llout, strdec4, ptabnam);
  261.         fprintf(llout, strdec5, ptabnam);
  262.         fprintf(llout, strdec6, ptabnam);
  263.         fprintf(llout, strdec7, llnxtmax);
  264.         fprintf(llout, strdec8, ndfa<=255?"_lmovb":"_lmovi");
  265.         fprintf(llout, strdec9, ptabnam);
  266.         fprintf(llout, strdec10, ptabnam);
  267.         fprintf(llout, strdec11, nlook?"_L":"", nlook?ptabnam:"NULL,   ");
  268.         refccl(ignore, "Ignore", "X");
  269.         refccl(breakc, "Break", "Y");
  270.         refccl(illeg, "Illegal", "Z");
  271.         fprintf(llout, "\t\t\t};\n");
  272.         if(lflag == 0)
  273.             {
  274.             if(sflag == 0)                  /* If stdio flavor */
  275.                 {
  276.                 fprintf(llout, "\n/* Standard I/O selected */\n");
  277.  
  278.                 fprintf(llout, "FILE *lexin;\n\n");
  279.  
  280.                 fprintf(llout, "llstin()\n   {\n   if(lexin == NULL)\n");
  281.                 fprintf(llout, "      lexin = stdin;\n");
  282.                 }
  283.             else                            /* Stand-alone flavor */
  284.                 {
  285.                 fprintf(llout, "\n/* Stand-alone selected */\n");
  286.                 fprintf(llout, "\llstin()\n   {\n");
  287.                 }
  288.             fprintf(llout, "   if(_tabp == NULL)\n");
  289.             fprintf(llout, "      lexswitch(&%s);\n   }\n\n", tabname);
  290.             }
  291.         fclose(llout);
  292. }
  293.  
  294. void dospccl(char *cp, char *s, char *tag)
  295. {
  296.         register n;
  297.         char cclnam[16];
  298.  
  299.         if (cp==0)
  300.                 return;
  301.         fprintf(llout, "#define\t%s\t%s\n", s, tag);
  302.         strcpy(cclnam, tag); strcat(cclnam, tabname);
  303.         vstart("char _%s", cclnam);
  304.         for (n = sizeof(ccls[0]); n--;)
  305.                 vel(" 0%o,", *cp++&0377);
  306.         vend();
  307. }
  308.  
  309. void refccl(char *cp, char *nm, char *tag)
  310. {
  311.         if (cp==0)
  312.                 fprintf(llout, "\t\t\t0,\t\t/* No %s class */\n", nm);
  313.         else
  314.                 fprintf(llout, "\t_%s%s,\t/* %s class */\n", tag, tabname, nm);
  315. }
  316.  
  317. int     vnl;
  318.  
  319. void vstart(char *fmt, char *str)
  320. {
  321.         vnl = 0;
  322.         putc('\n', llout);
  323.         fprintf(llout, fmt, str);
  324.         fprintf(llout, "[] =\n   {\n  ");
  325. }
  326.  
  327. void vend(void)
  328. {
  329.         fprintf(llout, "\n   };\n");
  330. }
  331.  
  332. void vel(char *fmt, int val)
  333. {
  334.         fprintf(llout, fmt, val);
  335.         if ((++vnl&07)==0)
  336.                 fprintf(llout, "\n  ");
  337. }
  338.