home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / watcher / part02 / control.y next >
Encoding:
Text File  |  1987-09-27  |  6.7 KB  |  344 lines

  1. /*
  2.    the grammer describing the control file for watcher.
  3.  
  4.    Kenneth Ingham
  5.  
  6.    Copyright (C) 1987 The University of New Mexico
  7. */
  8.  
  9. %token PIPELINE NUMBER STRING
  10. %start command
  11.  
  12. %{
  13. #include "defs.h"
  14. #define SMAXMIN    struct max_min_st
  15. #define SOUTFMT    struct out_fmt_st
  16. #define SCOLOUT    struct col_out_st
  17. #define SRELOUT    struct rel_out_st
  18. #define SCMD    struct cmd_st
  19. #define SCH    struct change_fmt_st
  20. extern char *strval, ostrval[], pipeline[];
  21. extern int intval, ointval;
  22. extern struct cmd_st *clist;
  23. int out_type;
  24. char alias[MAX_STR];
  25. union out_fmt_u key;
  26. %}
  27.  
  28. %%
  29. /*
  30.    THE PARSER DESCRIPTION (read aloud to the beginning of
  31.    _Also_sprach_Zarathustra_)
  32.  
  33.    As things are discovered, they are put into the linked list structure
  34.    detailed in defs.h.  Some error checking is done, but the messages
  35.    leave a bit to be desired.  Yacc and I didn't get along too well in
  36.    figuring out how to handle errors.
  37.  
  38.    Assuming that pointers will fit into YYSTYPE (int on 4.3 vax).
  39.    Should use yacc's union facility.
  40. */
  41.    
  42. command        : one_command
  43.             {
  44.                 if (clist == NULL)
  45.                     clist = (SCMD *) $1;
  46.             }
  47.         | command one_command
  48.             { 
  49.                 SCMD *p;
  50.  
  51.                 if (clist != NULL) {
  52.                     for (p=clist; p->next!=NULL; p=p->next)
  53.                         ;
  54.                     p->next = (SCMD *)$2;
  55.                 }
  56.                 else
  57.                     clist = (SCMD *)$1;
  58.             }
  59.         | error '.'
  60.             {
  61.             fprintf(stderr, "Command error near ");
  62.             fprintf(stderr, "'%s'\n", pipeline);
  63.             fprintf(stderr,"Last string read was '%s'\n", strval);
  64.             }
  65.         ;
  66.  
  67. one_command    : PIPELINE alias out_fmt ':' change_fmt '.'
  68.             {
  69.                 SCMD *p;
  70.  
  71.                 p = (SCMD *) malloc(sizeof(SCMD));
  72.                 if (p == NULL) {
  73.                     fprintf(stderr,"malloc error\n");
  74.                     exit(1);
  75.                 }
  76.                 p->pipeline = malloc((unsigned)strlen(pipeline)+1);
  77.                 if (p->pipeline == NULL) {
  78.                     fprintf(stderr,"malloc error\n");
  79.                     exit(1);
  80.                 }
  81.                 (void) strcpy(p->pipeline, pipeline);
  82.  
  83.                 if (alias[0]) {
  84.                     p->alias = malloc((unsigned)strlen(alias)+1);
  85.                     if (p->alias == NULL) {
  86.                         fprintf(stderr,"malloc error\n");
  87.                         exit(1);
  88.                     }
  89.                     (void) strcpy(p->alias, alias);
  90.                 }
  91.                 else
  92.                     p->alias = NULL;
  93.  
  94.                 p->change_fmt = (SCH *)$5;
  95.                 p->next = NULL;
  96.                 p->out_type = out_type;
  97.                 if (out_type == RELATIVE) {
  98.                     p->out_fmt.rel_fmt = (SRELOUT *)$3;
  99.                     p->key.rel_fmt = key.rel_fmt;
  100.                 }
  101.                 else { /* it better be column */
  102.                     p->out_fmt.col_fmt = (SCOLOUT *)$3;
  103.                     p->key.col_fmt = key.col_fmt;
  104.                 }
  105.  
  106.                 key.rel_fmt = NULL;
  107.                 $$ = (int)p;
  108.             }
  109.         ;
  110.  
  111. out_fmt        : rel_out_fmt
  112.             { 
  113.                 out_type = RELATIVE;
  114.                 $$ = $1;
  115.             }
  116.         | col_out_fmt
  117.             {
  118.                 out_type = COLUMN;
  119.                 $$ = $1;
  120.             }
  121.         | error '.'
  122.             {
  123.                 fprintf(stderr,"Output format error on ");
  124.                 fprintf(stderr,"'%s'\n", pipeline);
  125.                 fprintf(stderr,"Last string read was '%s'\n",
  126.                     strval);
  127.             }
  128.         ;
  129.  
  130. change_fmt    : one_change_fmt
  131.         | change_fmt ';' one_change_fmt
  132.             {
  133.                 SCH *p;
  134.  
  135.                 for (p=(SCH *)$1; p->next != NULL; p = p->next)
  136.                     ;
  137.                 
  138.                 p->next = (SCH *)$3;
  139.  
  140.                 $$ = (int)$1;
  141.             }
  142.         ;
  143.  
  144. one_change_fmt    : pct_change_fmt
  145.             { $$ = $1; } /* Is this default? */
  146.         | abs_change_fmt
  147.             { $$ = $1; }
  148.         | max_min_fmt
  149.             { $$ = $1; }
  150.         | str_change_fmt
  151.             { $$ = $1; }
  152.         | error '.'
  153.             {
  154.             fprintf(stderr,"Unknown change format type ");
  155.             fprintf(stderr,"on '%s'\n", pipeline);
  156.             fprintf(stderr,"Last string read '%s'\n", strval);
  157.             }
  158.         ;
  159.  
  160. rel_out_fmt    : one_rel_fmt
  161.         | rel_out_fmt  one_rel_fmt
  162.             {
  163.                 SRELOUT *p;
  164.  
  165.                 for (p=(SRELOUT *)$1;p->next != NULL; p=p->next)
  166.                     ;
  167.                 p->next = (SRELOUT *)$2;
  168.                 $$ = (int)$1;
  169.             }
  170.         ;
  171.  
  172. col_out_fmt    : one_col_fmt
  173.         | col_out_fmt  one_col_fmt
  174.             {
  175.                 SCOLOUT *p;
  176.  
  177.                 for (p=(SCOLOUT *)$1;p->next != NULL; p=p->next)
  178.                     ;
  179.                 p->next = (SCOLOUT *)$2;
  180.                 $$ = (int)$1;
  181.             }
  182.         /*
  183.         | error '.'
  184.             {
  185.             fprintf(stderr,"Column output format error\n");
  186.             fprintf(stderr,"Last string read '%s'\n", strval);
  187.             }
  188.         */
  189.         ;
  190.  
  191. one_rel_fmt    : NUMBER STRING '%' STRING
  192.             {
  193.             extern int parse_error;
  194.             SRELOUT *p;
  195.  
  196.             p = (SRELOUT *) malloc(sizeof(SRELOUT));
  197.             p->name = malloc((unsigned)strlen(ostrval)+1);
  198.             (void) strcpy(p->name, ostrval);
  199.             p->field = intval;
  200.             p->next = NULL;
  201.  
  202.             if (strval[1] != '\0') {
  203.                 fprintf(stderr,"%s: Invalid type specifier '%s'.\n",
  204.                     NAME, strval);
  205.                 parse_error = True;
  206.                 $$ = (int)p;
  207.             }
  208.  
  209.             switch (*strval) {
  210.                 case 'd':
  211.                 case 'f':
  212.                     p->type = NUM;
  213.                     break;
  214.                 case 's':
  215.                     p->type = STRING;
  216.                     break;
  217.                 case 'k':
  218.                     p->type = KEY;
  219.                     key.rel_fmt = p;
  220.                     break;
  221.                 default:
  222.                     fprintf(stderr,"%s: Invalid type specifier '%s'.\n",
  223.                         NAME, strval);
  224.                     parse_error = True;
  225.             }
  226.  
  227.             $$ = (int) p;
  228.             }
  229.         ;
  230.  
  231. one_col_fmt    : NUMBER '-' NUMBER STRING '%' STRING
  232.             {
  233.                 extern int parse_error;
  234.                 SCOLOUT *p;
  235.  
  236.                 p = (SCOLOUT *) malloc(sizeof(SCOLOUT));
  237.                 p->name = malloc((unsigned)strlen(ostrval)+1);
  238.                 (void) strcpy(p->name, ostrval);
  239.                 p->start = ointval;
  240.                 p->end = intval;
  241.                 p->next = NULL;
  242.  
  243.                 if (ointval >= intval) {
  244.                     fprintf(stderr,"%s: start %d larger than end %d!\n", NAME, ointval, intval);
  245.                     parse_error = True;
  246.                     $$ = (int)p;
  247.                 }
  248.  
  249.                 if (strval[1] != '\0') {
  250.                     fprintf(stderr,"%s: Invalid %s '%s'.\n",
  251.                         NAME, "type specifier", strval);
  252.                     parse_error = True;
  253.                     $$ = (int)p;
  254.                 }
  255.  
  256.                 switch (*strval) {
  257.                     case 'd':
  258.                     case 'f':
  259.                         p->type = NUM;
  260.                         break;
  261.                     case 's':
  262.                         p->type = STRING;
  263.                         break;
  264.                     case 'k':
  265.                         p->type = KEY;
  266.                         key.col_fmt = p;
  267.                         break;
  268.                     default:
  269.                         fprintf(stderr,"%s: %s '%s'.\n",
  270.                             NAME,
  271.                             "Bad type specifier",
  272.                             strval);
  273.                         parse_error = True;
  274.                 }
  275.                 $$ = (int) p;
  276.             }
  277.         ;
  278.  
  279. pct_change_fmt    : STRING NUMBER '%'
  280.             {
  281.                 SCH *p;
  282.  
  283.                 p = (SCH *) malloc(sizeof(SCH));
  284.                 p->name = malloc((unsigned)strlen(strval)+1);
  285.                 (void) strcpy(p->name, strval);
  286.                 p->fmt.percent = (float)intval / 100;
  287.                 p->type = PERCENT;
  288.  
  289.                 $$ = (int) p;
  290.             }
  291.         ;
  292.  
  293. abs_change_fmt    : STRING NUMBER
  294.             {
  295.               SCH *p;
  296.  
  297.               p = (SCH *) malloc(sizeof(SCH));
  298.               p->name = malloc((unsigned) strlen(strval)+1);
  299.               (void) strcpy(p->name, strval);
  300.               p->fmt.abs_amount = intval;
  301.               p->type = ABSOLUTE;
  302.  
  303.               $$ = (int) p;
  304.             }
  305.         ;
  306.  
  307. max_min_fmt    : STRING NUMBER NUMBER
  308.             {
  309.               SCH *p;
  310.  
  311.               p = (SCH *) malloc(sizeof(SCH));
  312.               p->name = malloc((unsigned)strlen(strval)+1);
  313.               (void) strcpy(p->name, strval);
  314.               p->fmt.max_min.max = intval;
  315.               p->fmt.max_min.min = ointval;
  316.               p->type = MAX_MIN;
  317.  
  318.               $$ = (int) p;
  319.             }
  320.         ;
  321.  
  322. str_change_fmt    : STRING '"' STRING '"'
  323.             { 
  324.               SCH *p;
  325.  
  326.               p = (SCH *) malloc(sizeof(SCH));
  327.               p->name = malloc((unsigned)strlen(ostrval)+1);
  328.               (void) strcpy(p->name, ostrval);
  329.               p->fmt.str_value = malloc((unsigned)strlen(strval)+1);
  330.               (void) strcpy(p->fmt.str_value, strval);
  331.               p->type = STRING;
  332.  
  333.               $$ = (int) p;
  334.             }
  335.         ;
  336.  
  337. alias        : '{' STRING '}'
  338.             { (void) strcpy(alias, strval); }
  339.         | empty 
  340.             { alias[0] = '\0'; }
  341.         ;
  342.  
  343. empty        : ;
  344.