home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 4.ddi / COM.ZIP / COM.Y < prev    next >
Encoding:
Text File  |  1990-08-09  |  7.6 KB  |  343 lines

  1. /**********************************************/
  2. /*                                              */
  3. /* YACC source file for the telecom compiler  */
  4. /*                                              */
  5. /**********************************************/
  6. /*
  7.     Written by Tim Capps, 4/11/90
  8.     Copyright 1990, QED Software, all rights reserved.
  9. */
  10.  
  11. %{
  12. /* Make the generated module separately compilable */
  13.  
  14. #ifdef BORLAND
  15.  #pragma warn -sus                /* No suspicious pointer messages */
  16. #endif
  17.  
  18. #define YYDEBUG 1
  19. #define CUSTOM_SIZE 200            /* Initial allocation for custom resources */
  20.  
  21. extern "C" {
  22. #include    <String.H>
  23. #include    <CType.H>
  24. #include    <stdlib.h>
  25. #include    <stdio.h>
  26. #include    <io.h>
  27. }
  28.             
  29. #include    "pcodes.h"        /* P-codes understood by the com engine        */
  30.  
  31. extern    short  yystate;
  32. extern    FILE * comOut;
  33.         
  34. char    *p;                    /* General purpose pointer                    */
  35. char    *pbase;             /* Pointer to base of generic structure        */
  36. long    psize;                /* Size of data pointed to by p                */
  37. char    *junk;                /* Pointer to someting about to be trashed  */
  38. int        *pi;                /* Integer pointer                            */
  39. short   *ps;                /* Short pointer                            */
  40. int        x;                    /* Work variable                            */
  41.         
  42. int     yyerror(char *);
  43. int     yylex();
  44.         
  45. %}
  46.  
  47. %start com
  48.        
  49. %union
  50. {
  51.     int    ival;                /* Integer value                            */
  52.     long lval;                /* Long value                                */
  53.     char * ptr;                /* General pointer                            */
  54. }
  55.  
  56.  
  57. /************/
  58. /*  Tokens  */
  59. /************/
  60.     
  61. %token    <ival>    YDEBUG NOYDEBUG
  62. %token    <ival>    SHIFT_RIGHT SHIFT_LEFT LOGICAL_OR LOGICAL_AND
  63. %token    <ival>    NOT_EQ EQ GT_EQ LT_EQ BYTE_LITCONST
  64.         
  65. %token    <lval>    CONSTANT LITCONST
  66.  
  67. %token    <ptr>    STRING
  68.  
  69. /* Commands */
  70. %token    <ival>    SEND WAIT FOR EOL ANYTHING ELSE END BELL NOTIFY ON OFF
  71. %token    <ival>    SLOW CLOSE WINDOW DISPLAYCOMMENT ERASECOMMENT
  72.         
  73. /***********************/
  74. /*  Nonterminal types  */
  75. /***********************/
  76.  
  77. %type    <ival>    byte_litconst
  78.  
  79. %type    <lval>    long_constant expression literal_constant
  80.  
  81. /*************************/
  82. /*  Operator Precedence  */
  83. /*************************/
  84.  
  85. /* This is the same as 'C' operator precedence */
  86. %left LOGICAL_OR
  87. %left LOGICAL_AND
  88. %left '|'
  89. %left '^'
  90. %left '&'
  91. %left EQ NOT_EQ
  92. %left '<' LT_EQ '>' GT_EQ
  93. %left SHIFT_RIGHT SHIFT_LEFT
  94. %left '+' '-'
  95. %left '*' '/' '%'
  96. %right '!' '~'
  97. %left ','
  98.       
  99. %%
  100.  
  101. /*******************************************/
  102. /*                                         */
  103. /*  Communication Script Compiler Grammar  */
  104. /*                                         */
  105. /*******************************************/
  106.     
  107. com:                /* nothing */
  108.                     | com statement
  109.                     | com test
  110.                     | com error
  111.                     ;
  112.  
  113. statement:            end
  114.                     | send
  115.                     | waitfor
  116.                     | notify
  117.                     | bell
  118.                     | wait
  119.                     | closewindow
  120.                     | dispcom                /* Display text in comment area */
  121.                     | erasecom                /* Erase comment area            */
  122.                     | toggle_debug
  123.                     ;
  124.                     
  125. test:                '=' expression ';'
  126.                     {
  127.                         printf ("Result=%i\n", $2);
  128.                     }
  129.  
  130. /* Display text in the comment area */
  131. dispcom:            DISPLAYCOMMENT STRING
  132.                     {
  133.                         fputc(PC_DISPCOM, comOut);    // Display comment text
  134.                         /* Length is +2 to include length byte + terminator */
  135.                         fputc(strlen($2)+2, comOut);// Write the length
  136.                         fputs($2, comOut);            // Write string    w/o null
  137.                         fputc(0, comOut);            // Termnate it
  138.                     }
  139.                     
  140. /* Erase the comment area */
  141. erasecom:            ERASECOMMENT
  142.                     {
  143.                         fputc(PC_ERASECOM, comOut);
  144.                     }
  145.                     
  146. /* Close the current window */
  147. closewindow:        CLOSE WINDOW
  148.                     {
  149.                         fputc(PC_CLOSEWINDOW, comOut);
  150.                     }
  151.                     
  152. /* Wait a specified number of seconds */
  153. /* Wait P-code is followed by int count */
  154. wait:                WAIT expression
  155.                     {
  156.                         fputc(PC_WAIT, comOut);
  157.                         x=$2;                        // Make it into an INT
  158.                         (void) fwrite(&x, sizeof(int), 1, comOut);
  159.                     }
  160.                     
  161. /* End of com program */
  162. end:                END
  163.                     {
  164.                         fputc(PC_END, comOut);    /* Write the end command */
  165.                     }
  166.  
  167. /* Send a string to the remote host */
  168. send:                SEND STRING
  169.                     {
  170.                         fputc(PC_SEND, comOut);        // Start a send command
  171.                         /* Length is +1 to include the length byte */
  172.                         fputc(strlen($2)+1, comOut);// Write the length
  173.                         fputs($2, comOut);            // Write string    w/o null
  174.                         /* NOTE: WE DON'T TERMINATE THE STRING! */
  175.                     }
  176.                     | SEND SLOW STRING
  177.                     {
  178.                         fputc(PC_SENDSLOW, comOut);    // Start a send command
  179.                         /* Length is +1 to include the length byte */
  180.                         fputc(strlen($3)+1, comOut);// Write the length
  181.                         fputs($3, comOut);            // Write string    w/o null
  182.                         /* NOTE: WE DON'T TERMINATE THE STRING! */
  183.                     }
  184.                     
  185.                     
  186. /* Wait for a string from the remote host */
  187. waitfor:            WAIT FOR STRING
  188.                     {
  189.                         fputc(PC_WAITFOR, comOut);    // Wait for a string
  190.                         /* Length is +2 to include length byte + terminator */
  191.                         fputc(strlen($3)+2, comOut);// Write the length
  192.                         fputs($3, comOut);            // Write string    w/o null
  193.                         fputc(0, comOut);            // Termnate it
  194.                     }
  195.                     
  196. /* Turn BISON debugging on/off */
  197. toggle_debug:        YDEBUG ON
  198.                     {
  199.                         yydebug=1;
  200.                     }
  201.                     | YDEBUG OFF
  202.                     {
  203.                         yydebug=0;
  204.                     }
  205.                     ;
  206.                     
  207. /* Beep the terminal */
  208. bell:                BELL
  209.                     {
  210.                         fputc(PC_BELL, comOut);            // Beep!
  211.                     }
  212.  
  213. /* Put up a 'notify' box */
  214. notify:                NOTIFY STRING
  215.                     {
  216.                         fputc(PC_NOTIFY, comOut);
  217.                         /* Length is +2 because we have a null terminator     */
  218.                         /*  and we include the length byte.                    */
  219.                         fputc(strlen($2)+2, comOut); // Write the length
  220.                         fputs($2, comOut);             // Write string w/o null
  221.                         fputc(0, comOut);             // Termnate it w/null
  222.                     }
  223.                     | NOTIFY OFF
  224.                     {
  225.                         fputc(PC_NOTIFYOFF, comOut);    // Remove notify box
  226.                     }
  227.                     
  228. /* Expressions */
  229.  
  230. expression:            long_constant
  231.                     | byte_litconst
  232.                     {
  233.                         $$=$1;            /* Avoid type mismatch warning */
  234.                     }
  235.                     | literal_constant
  236.                     | '-' expression
  237.                     {
  238.                         $$=(-$2);
  239.                     }   
  240.                     | '~' expression
  241.                     {
  242.                         $$=(~$2);
  243.                     }
  244.                     | '!' expression
  245.                     {
  246.                         $$=(!$2);
  247.                     }
  248.                     | '(' expression ')'
  249.                     {
  250.                         $$=$2;
  251.                     }
  252.                     | expression SHIFT_RIGHT expression
  253.                     {
  254.                         $$=($1 >> $3);
  255.                     }
  256.                     | expression SHIFT_LEFT expression
  257.                     {
  258.                         $$=($1 << $3);
  259.                     }
  260.                     | expression '^' expression
  261.                     {
  262.                         $$=($1 ^ $3);
  263.                     }
  264.                     | expression LOGICAL_OR expression
  265.                     {
  266.                         $$=($1 || $3);
  267.                     }
  268.                     | expression LOGICAL_AND expression
  269.                     {
  270.                         $$=($1 && $3);
  271.                     }
  272.                     | expression '|' expression
  273.                     {
  274.                         $$=($1 | $3);
  275.                     }
  276.                     | expression '&' expression
  277.                     {
  278.                         $$=($1 & $3);
  279.                     }
  280.                     | expression NOT_EQ expression
  281.                     {
  282.                         $$=($1 != $3);
  283.                     }
  284.                     | expression EQ expression
  285.                     {
  286.                         $$=($1 == $3);
  287.                     }
  288.                     | expression GT_EQ expression
  289.                     {
  290.                         $$=($1 >= $3);
  291.                     }
  292.                     | expression LT_EQ expression
  293.                     {
  294.                         $$=($1 <= $3);
  295.                     }
  296.                     | expression '>' expression
  297.                     {
  298.                         $$=($1 > $3);
  299.                     }
  300.                     | expression '<' expression
  301.                     {
  302.                         $$=($1 < $3);
  303.                     }
  304.                     | expression '-' expression
  305.                     {
  306.                         $$=($1 - $3);
  307.                     }
  308.                     | expression '+' expression
  309.                     {
  310.                         $$=($1 + $3);
  311.                     }
  312.                     | expression '*' expression
  313.                     {
  314.                         $$=($1 * $3);
  315.                     }
  316.                     | expression '/' expression
  317.                     {
  318.                         if ($3!=0) {
  319.                             $$=($1 / $3);
  320.                         } else {
  321.                             /* This error should be handled properly!   */
  322.                             /* At the moment, I don't know how - sorry. */
  323.                             yyerror("Attempt to divide by zero");
  324.                             break;
  325.                         }
  326.                     }
  327.                     | expression '%' expression
  328.                     {
  329.                         $$=($1 % $3);
  330.                     }
  331.                     ;
  332.  
  333. /* Numbers */
  334.  
  335. long_constant:        CONSTANT
  336.                     ;
  337.                     
  338. literal_constant:    LITCONST
  339.                     ;
  340.  
  341. byte_litconst:        BYTE_LITCONST
  342.                     ;
  343.