home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume6 / yaccref / yyref-grammer < prev    next >
Encoding:
Text File  |  1986-11-30  |  4.3 KB  |  287 lines

  1. %{
  2.  
  3. # include <ctype.h>
  4. # include <stdio.h>
  5.  
  6. %}
  7.  
  8.  
  9.     /*******************************************************\
  10.     *                            *
  11.     *    X_reference program for YACC files        *
  12.     *    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        *
  13.     *                            *
  14.     *    Cathy Taylor,                    *
  15.     *    c/o Department of Computing,            *
  16.     *    University of Lancaster,            *
  17.     *    Bailrigg, Lancaster, England.            *
  18.     *    Date : Fri Jul  4 00:50:04 BST 1986        *
  19.     *                            *
  20.     \*******************************************************/
  21.  
  22.  
  23.     /***********************************************\
  24.     *                        *
  25.     *    Yacc Input Syntax            *
  26.     *    ~~~~~~~~~~~~~~~~~            *
  27.     *                        *
  28.     *    Adapted from the document        *
  29.     *    'YACC - Yet Another Compiler Compiler'    *
  30.     *        by                *
  31.     *       S. C. Johnson            *
  32.     *                        *
  33.     *    Date: Tue Jul  1 02:40:18 BST 1986    *
  34.     *                        *
  35.     \***********************************************/
  36.  
  37.  
  38. %token    IDENTIFIER CHARACTER NUMBER
  39. %token    LEFT RIGHT NONASSOC TOKEN PREC TYPE START UNION
  40. %token    PER PERCURL ACT
  41. %token    COLON SEMICOLON COMMA OR LESS GREATER
  42.  
  43. %start    spec
  44.  
  45. %%
  46.  
  47. spec
  48.     :    defs PER rules tail
  49.             {
  50.                 printf("\n\n");
  51.                 yyclearin;
  52.                 return(0);
  53.             }
  54.     ;
  55.  
  56. tail
  57.     :    /* empty */
  58.     |    PER 
  59.     ;
  60.  
  61. defs
  62.     :    /* empty */
  63.     |    def_bk
  64.     ;
  65.  
  66. def_bk
  67.     :    def
  68.     |    def_bk def
  69.     ;
  70.  
  71. def
  72.     :    START IDENTIFIER
  73.     |    UNION
  74.     |    PERCURL
  75.     |    rword tag nlist
  76.     ;
  77.  
  78. rword
  79.     :    TOKEN
  80.     |    LEFT
  81.     |    RIGHT
  82.     |    NONASSOC
  83.     |    TYPE
  84.     ;
  85.  
  86. tag
  87.     :    /* empty */
  88.     |    LESS IDENTIFIER GREATER
  89.     ;
  90.  
  91. nlist
  92.     :    nmno
  93.     |    nlist opt_comma nmno
  94.     ;
  95.  
  96. opt_comma
  97.     :    /* empty */
  98.     |    COMMA
  99.     ;
  100.  
  101. nmno
  102.     :    IDENTIFIER opt_num
  103.     ;
  104.  
  105. opt_num
  106.     :    /* empty */
  107.     |    NUMBER
  108.     ;
  109. rules
  110.     :    rule
  111.     |    rules rule
  112.     ;
  113.  
  114. rule
  115.     :    IDENTIFIER
  116.         {
  117.             yyaction(ON_C_IDENT,line);
  118.         }
  119.         COLON body SEMICOLON
  120.     ;
  121.  
  122. body
  123.     :    body_block
  124.     |    body OR body_block
  125.     ;
  126.  
  127. body_block
  128.     :    /* empty */
  129.     |    body_block body_entity
  130.     ;
  131.  
  132. body_entity
  133.     :    opt_prec id_ent
  134.     |    ACT
  135.     ;
  136.  
  137. id_ent
  138.     :    IDENTIFIER
  139.         {
  140.             yyaction(ON_IDENT,line);
  141.         }
  142.     |    CHARACTER
  143.     ;
  144.  
  145. opt_prec
  146.     :    /* empty */
  147.     |    PREC
  148.     ;
  149.  
  150.  
  151. %%
  152.  
  153. # include    <stdio.h>
  154. # include    "lex.yy.c"
  155. # include    "yyref-line.h"
  156.  
  157. #define    ON_C_IDENT    000
  158. #define    ON_IDENT    001
  159.  
  160. #define    MAXIDENTS    1000
  161. #define    MAXCHARS    100
  162. #define    MAXDEFS        20
  163. #define    MAXOCCS        1000
  164.  
  165. struct    IREC {
  166.         char    ident[MAXCHARS];
  167.         int    desc[MAXDEFS];
  168.         int    nextdesc;
  169.         int    occ[MAXOCCS];
  170.         int    nextocc;
  171.         } table[MAXIDENTS];
  172.  
  173.  
  174. yyaction (action,ln)
  175. int    action;
  176. int    ln;
  177. {
  178.     int    id;
  179.     
  180.     id = 0;
  181.     while (    strcmp(table[id].ident,yytext) != 0 && strcmp(table[id].ident,"") != 0 )
  182.         id++;
  183.  
  184.     if ( strcmp(table[id].ident, yytext) != 0 )
  185.     {
  186.  
  187.     /*******************************************************\
  188.     *                            *
  189.     *    New non-terminal to be stored.            *
  190.     *    No distinction is made here between tokens    *
  191.     *    and (non) terminals.                *
  192.     *                            *
  193.     \*******************************************************/
  194.  
  195.         strcpy(table[id].ident,yytext);
  196.         table[id].nextdesc = 0;
  197.         table[id].nextocc = 0;
  198.     } /* fi */
  199.  
  200.     switch (action) {
  201.     case ON_C_IDENT:
  202.  
  203.     /*******************************************************\
  204.     *                            *
  205.     *    Add to list of definition lines.        *
  206.     *                            *
  207.     \*******************************************************/
  208.  
  209.         table[id].desc[table[id].nextdesc++] = ln;
  210.         break;
  211.  
  212.     case ON_IDENT:
  213.                 
  214.     /*******************************************************\
  215.     *                            *
  216.     *    Add to list of occurance lines.            *
  217.     *                            *
  218.     \*******************************************************/
  219.  
  220.         table[id].occ[table[id].nextocc++] = ln;
  221.         break;
  222.  
  223.     default        :
  224.         fprintf (stdout, "yyaction: invalid action\n");
  225.         return (-1);
  226.         } /* hctiws */
  227.     return (0);
  228. } /* corp */
  229.  
  230. nline(ln)
  231. int    ln;
  232. {
  233.     printf("%4d :\t",ln);
  234. }
  235.  
  236.  
  237.     char    declared_at_mark[] = "*";
  238.     char    occurs_at_mark[] = "";
  239.     char    token_maybe[] = "is not declared - token??";
  240.     char    start_maybe[] = "never occurs on rhs of rule - start rule?";
  241.     
  242. /*
  243. *    Strings for output
  244. */
  245.  
  246. main ()
  247. {
  248.     int    ind,id;
  249.  
  250.     strcpy(table[0].ident,"");
  251.  
  252.     line = 0;
  253.     nline(++line);
  254.  
  255.     yyparse ();
  256.  
  257.     id = 0;
  258.     while( strcmp(table[id].ident,"") != 0 )
  259.     {
  260.         printf("\n'%s' -\n~~~~~~~\t\t",table[id].ident);
  261.         if (table[id].nextdesc == 0 )
  262.             printf("%s",token_maybe);
  263.         else
  264.         {
  265.             ind = 0;
  266.             printf("*%d ",table[id].desc[ind++]);
  267.             for ( ind=1; ind < table[id].nextdesc ; ind++)
  268.             printf(", %s%d",declared_at_mark,table[id].desc[ind]);
  269.         }
  270.         if (table[id].occ[0] == 0)
  271.             printf(", %s",start_maybe);
  272.         else
  273.         {
  274.             for ( ind = 0; ind < table[id].nextocc ; ind++ )
  275.             printf(", %s%d",occurs_at_mark,table[id].occ[ind]);
  276.         }
  277.         id++;
  278.     }
  279.     printf("\n\n\tEnd of X-ref\n\t~~~~~~~~~~~~\n");
  280. } /* niam */
  281.  
  282. yyerror(mess)
  283. char    *mess;
  284. {
  285.     printf("\n\t%s\n",mess);
  286. } /* corp */
  287.