home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / labels / labels.y < prev    next >
Encoding:
Lex Description  |  1988-01-31  |  5.4 KB  |  262 lines

  1. %{
  2. /*                   -- LABELS.Y --
  3.  
  4. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  5. @@                                                                           @@
  6. @@                       Mailing List Label Formatter                        @@
  7. @@                     Syntax Analyzer for Form Parsing                      @@
  8. @@                      (C) Copyright 1987 by Joe Chen                       @@
  9. @@                           All Rights Reserved                             @@
  10. @@                                                                           @@
  11. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  12.  
  13. You may freely distribute this software to others.  But there are
  14. few rules you must follow:
  15.  
  16. 1. You don't profit from it.  You may however ask for minimal fees to
  17.    cover shipping and handling.
  18.  
  19. 2. This program is copyrighted, meaning you may not modify or enhance
  20.    this software and market it.  You may make changes to suit your
  21.    local needs.  Any enhancements are welcomed.
  22.  
  23. 3. Please honor the author by not removing or replacing his name from
  24.    the source codes.
  25.  
  26.  
  27. Feel free to contact me if you have any questions.
  28.  
  29.                     Joe Chen
  30.  
  31. ---------------------------------------------------------------------------
  32. Phones at work: (213) 743-5363, (213) 743-5935; at home: (818) 571-5304
  33. University Computing Services, University of Southern California
  34. UUCP: {sdcrdcf, uscvax}!oberon!wasat!joec
  35. ARPA: joec@wasat.usc.edu, joec@ecla.usc.edu
  36. ---------------------------------------------------------------------------
  37.  
  38. Program created by Joe Chen - Jul 24, 1987
  39.  
  40. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  41.  
  42. */
  43. #include <stdio.h>
  44.  
  45. extern char *malloc(), *realloc();
  46.  
  47. extern int value, line_no;
  48. extern char yytext[], **fmt_arry, *l_istring;
  49. extern int l_width, l_hgap, l_vgap, l_columns, l_mlines, l_offset, l_cross;
  50. extern int l_ifill, l_foffs, l_poffs, l_lrows;
  51. extern FILE *form;
  52. %}
  53.  
  54. %union {
  55.   int vars;
  56. };
  57.  
  58. %token _INITIAL
  59. %token _KSTRING
  60. %token _FORM
  61. %token _PAGE
  62. %token _FILL
  63. %token _WIDTH
  64. %token _HGAP
  65. %token _VGAP
  66. %token _COLUMNS
  67. %token _LINES
  68. %token _ROWS
  69. %token _OFFSET
  70. %token _FORMAT
  71. %token _OF
  72. %token _LINE
  73. %token _IS
  74. %token _ASSIGN
  75. %token _DELIM
  76. %token _VALUE
  77. %token _STRING
  78. %token _WORD
  79.  
  80. %type <vars> variable
  81.  
  82. %%
  83.  
  84. cmd        : stmts
  85.         ;
  86.  
  87. stmts        : stmts delim stmt
  88.         | stmt
  89.         ;
  90.  
  91. delim        : _DELIM delim
  92.         |
  93.         ;
  94.  
  95. stmt        : param_stmt_val
  96.         | fmt_stmt
  97.         | init_stmt
  98.         ;
  99.  
  100. param_stmt_val    : variable is _VALUE
  101.         {
  102.           switch($1) {
  103.           case _WIDTH:
  104.             l_width = value;
  105.             break;
  106.           case _HGAP:
  107.             l_hgap = value;
  108.             break;
  109.           case _VGAP:
  110.             l_vgap = value;
  111.             break;
  112.           case _COLUMNS:
  113.             l_cross = value;
  114.             break;
  115.           case _LINES: {
  116.             if (l_mlines < value) {
  117.               int i;        /* need to adjust format buffer */
  118.               fmt_arry=(char **)realloc(fmt_arry,value*sizeof(char *));
  119.               if (fmt_arry == (char **)NULL)
  120.             goto e;
  121.               /* add new format lines */
  122.               for (i=l_mlines; i < value; i++) {
  123.             fmt_arry[i] = malloc(l_width+1);
  124.             if (fmt_arry[i] == (char *)NULL)
  125.               goto e;
  126.             (void) strcpy(fmt_arry[i], "%s");
  127.               }
  128.             } /* adjust the format buffer */
  129.             l_mlines = value;
  130.             break;
  131.           e:
  132.             fprintf(stderr,"Fatal Error: Insufficient Memory\n");
  133.             exit(-1);
  134.           }
  135.           case _OFFSET:
  136.             /* label column offset */
  137.             l_offset = value;
  138.             break;
  139.           case _FORM:
  140.             /* form offset value */
  141.             l_foffs = value;
  142.             break;
  143.           case _PAGE:
  144.             /* page offset value */
  145.             l_poffs = value;
  146.             break;
  147.           case _ROWS:
  148.             /* rows per label */
  149.             l_lrows = value;
  150.             break;
  151.           } /* set various variables */
  152.         }
  153.         ;
  154.  
  155. variable    : _WIDTH
  156.         {
  157.           $$=_WIDTH;
  158.         }
  159.         | _HGAP
  160.         {
  161.           $$=_HGAP;
  162.         }
  163.         | _VGAP
  164.         {
  165.           $$=_VGAP;
  166.         }
  167.         | _COLUMNS
  168.         {
  169.           $$=_COLUMNS;
  170.         }
  171.         | _LINES
  172.         {
  173.           $$=_LINES;
  174.         }
  175.         | _LINE
  176.         {
  177.           $$=_LINES;
  178.         }
  179.         | _ROWS
  180.         {
  181.           $$=_ROWS;
  182.         }
  183.         | _OFFSET
  184.         {
  185.           $$=_OFFSET;
  186.         }
  187.         | _FORM _OFFSET
  188.         {
  189.           $$=_FORM;
  190.         }
  191.         | _PAGE _OFFSET
  192.         {
  193.           $$=_PAGE;
  194.         }
  195.         | _WORD
  196.         {
  197.           $$=0;
  198.           fprintf(stderr,"Line %d in %s: Unknown variable: %s\n",
  199.               line_no, form, yytext);
  200.         }
  201.         ;
  202.  
  203. fmt_stmt    : _FORMAT _OF line _VALUE is _STRING
  204.         {
  205.           if (value > l_mlines) {
  206.             fprintf(stderr,"Line %d in %s: Line Number Execeeded ",
  207.                 line_no, form);
  208.             fprintf(stderr,"Maximum (%d); Format Ignored\n",l_mlines);
  209.           }
  210.           else {
  211.             /* supercede format */
  212.             (void) strcpy(fmt_arry[value-1], &yytext[1]);
  213.             /* remove quotes */
  214.             fmt_arry[value-1][strlen(fmt_arry[value-1])-1] = '\0';
  215.           }
  216.         }
  217.         ;
  218.  
  219. init_stmt    : _INITIAL _KSTRING is _STRING
  220.         {
  221.           if ((l_istring = malloc(strlen(yytext))) == (char *)NULL) {
  222.             fprintf(stderr,"Fatal Error: Insufficient Memory\n");
  223.             exit(-1);
  224.           }
  225.           /* remove the quotations */
  226.           (void) strcpy(l_istring, &yytext[1]);
  227.           l_istring[strlen(l_istring)-1] = '\0';
  228.         }
  229.         | _INITIAL _FILL is _VALUE
  230.         {
  231.           /* initial fill value */
  232.           l_ifill = value;
  233.         }
  234.         ;
  235.  
  236. line        : _LINE
  237.         |
  238.         ;
  239.  
  240. is        : _IS
  241.         | _ASSIGN
  242.         |
  243.         ;
  244.  
  245. %%
  246.  
  247. /****************************************************************************
  248.   yyerror() [Public function] - Yacc error trap function
  249.  
  250.   parameters:    s        Error message from yacc's LR driver
  251.  
  252.   exit flags:    none
  253. ****************************************************************************/
  254.  
  255. yyerror(s)
  256.      char *s;
  257. {
  258.   if (strcmp(s, "syntax error"))
  259.     fprintf(stderr,"Line %d in %s: %s\n",line_no,form,s);
  260. } /* yyerror() */
  261.  
  262.