home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 4.ddi / COM.ZIP / COM.L < prev    next >
Encoding:
Lex Description  |  1990-08-02  |  7.3 KB  |  383 lines

  1. %{
  2.  
  3. /***************/
  4. /* Gloal stuff */
  5. /***************/
  6.  
  7. extern "C" {
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #include <io.h>
  13. }
  14.  
  15. #define STRMAX 255                    /* Max length of strings            */
  16.  
  17. #include "comgram.h"                /* Get YACC token equates            */
  18.  
  19. extern YYSTYPE yylval;
  20. extern FILE * comIn;
  21. extern FILE * comOut;
  22.  
  23. /* gets input and stuffs it into "buf". number of characters read, or YY_NULL,
  24.  * is returned in "result".
  25.  *
  26.  * We redefine Flex's routine so we can supply the text from a file.
  27.  */
  28. #undef YY_INPUT
  29. #define YY_INPUT(buf,result,max_size) \
  30.         if ( (result = read( fileno(comIn), buf, max_size )) < 0 ) \
  31.             YY_FATAL_ERROR( "read() in flex scanner failed" );
  32.  
  33. /* We don't want the default action to ECHO, so define it as nothing */
  34. #undef YY_DEFAULT_ACTION
  35. #define YY_DEFAULT_ACTION printf("Default action: %i\n",*yytext)
  36.  
  37. /*
  38.  *    Local variables
  39.  */
  40.  
  41. char *ptr;                            /* String pointer                      */
  42. char *cp;                            /* Roving string pointer            */
  43. int scount;                            /* String length                    */
  44. extern YYSTYPE yylval;                /* For returning token values        */
  45. extern int lineCount;                /* Our line counter                    */
  46. int q;                                /* Work variable                     */
  47.  
  48. #define BYTEMASK     0x7F
  49.  
  50. /***********************/
  51. /* Function prototypes */
  52. /***********************/
  53.  
  54. int otoi(char *);
  55. int htoi(char *);
  56. char myesc(char *);
  57.  
  58. %}
  59.  
  60. %x QUOTE C_COMMENT RECOVER
  61.  
  62. WS            [ \t]+
  63.  
  64. OPTWS        [ \t]*
  65.  
  66. NAME        [A-Za-z_][A-Za-z_0-9]*
  67.  
  68. LETTERS        [A-Za-z]
  69.  
  70. ESCSEQ        \\([^^\n]|"^".|0[0-9]{1,3}|0x[0-9A-Fa-f][0-9A-Fa-f])
  71.  
  72. DIGIT        [0-9]
  73.  
  74. HDIGIT        [0-9A-Fa-f]
  75.  
  76. DIGITS      [0-9]+
  77.  
  78. HDIGITS     [0-9A-Fa-f]+
  79. %%
  80.  
  81. /************/
  82. /* Comments */
  83. /************/
  84.  
  85. "/*"            BEGIN(C_COMMENT);        /* Old style comments.              */
  86.  
  87. <C_COMMENT>"*/"        BEGIN(0);            /* Sub-scanner to handle comments.    */
  88. <C_COMMENT>[^*\n]+    ;
  89. <C_COMMENT>"*"        ;
  90. <C_COMMENT>\n        {
  91.                         lineCount++;
  92.                     }
  93.  
  94. "//".*\n            {                        /* New style comments */
  95.                         lineCount++;
  96.                     }
  97.  
  98. /************/
  99. /* Commands */
  100. /************/
  101.  
  102. "ydebug"        return(YDEBUG);
  103.  
  104. "send"             return(SEND);
  105.  
  106. "slow"            return(SLOW);
  107.  
  108. "wait"            return(WAIT);
  109.  
  110. "for"            return(FOR);
  111.  
  112. "eol"            return(EOL);
  113.  
  114. "anything"        return(ANYTHING);
  115.  
  116. "else"            return(ELSE);
  117.  
  118. "end"            return(END); 
  119.  
  120. "on"            return(ON);
  121.  
  122. "off"            return(OFF);
  123.  
  124. "notify"        return(NOTIFY);
  125.  
  126. "close"            return(CLOSE);
  127.  
  128. "window"        return(WINDOW);
  129.  
  130. "display comment" return(DISPLAYCOMMENT);
  131.  
  132. "erase comment" return(ERASECOMMENT);
  133.  
  134. /* Byte Literal constant */
  135. ['].[']            {
  136.                     yylval.ival= *(yytext+1);
  137.                     return(BYTE_LITCONST);
  138.                 }
  139.  
  140. /* Long Literal constant */
  141. [']{LETTERS}{LETTERS}{LETTERS}{LETTERS}[']    {
  142.                     ptr=yytext+1;
  143.                     yylval.lval=(*ptr);
  144.                     yylval.lval<<=8;
  145.                     yylval.lval+=(*(ptr+1));
  146.                     yylval.lval<<=8;
  147.                     yylval.lval+=(*(ptr+2));
  148.                     yylval.lval<<=8;
  149.                     yylval.lval+=(*(ptr+3));
  150.                     return(LITCONST);
  151.                 }
  152.  
  153.  
  154. /* Hex constant */
  155. "0"[xX]{HDIGITS}    {
  156.                     yylval.ival=0;
  157.                     for (q=2; q<yyleng; q++) {
  158.                         if (yytext[q]<'A') {
  159.                             yylval.ival=(yylval.ival*16) + (yytext[q]-'0');
  160.                         } else {
  161.                             if (yytext[q]<'a')
  162.                               yylval.ival=(yylval.ival*16)+(yytext[q]-'A'+10);
  163.                             else
  164.                               yylval.ival=(yylval.ival*16)+(yytext[q]-'a'+10);
  165.                         }
  166.                     }
  167.                     return(CONSTANT);
  168.                 }
  169.  
  170. /* Integer constant */
  171. {DIGITS}    {
  172.                 yylval.ival=0;
  173.                 for (q=0; q<yyleng; q++) {
  174.                     yylval.ival=(yylval.ival*10) + (yytext[q]-'0');
  175.                 }
  176.                 return(CONSTANT);
  177.             }
  178.  
  179. /* String */
  180. ["]         {
  181.                 /* We need to accumulate all characters in the string     */
  182.                 /* To do this, we use a sub-analyzer.  Let's get set!    */
  183.                 ptr=(char*)calloc(1,STRMAX+1); /* Get memory for the string    */
  184.                 cp=ptr;                    /* Make a roving pointer        */
  185.                 scount=0;
  186.                 BEGIN(QUOTE);            /* Start the analyzer             */
  187.             }
  188.  
  189. /* Sub-analyzer to accumulate the contents of a string */
  190. <QUOTE>{ESCSEQ} {
  191.                 *cp++=(myesc(yytext) & BYTEMASK);
  192.                 scount++;
  193.                 if (scount>=STRMAX-1) {
  194.                     printf("String too long!\n");
  195.                     free(ptr);                /* Give back our memory */
  196.                     BEGIN(0);
  197.                 }
  198.             }
  199.  
  200. <QUOTE>[^"\n] {
  201.                 *cp++=(yytext[0] & BYTEMASK); /* Still in string */
  202.                 scount++;
  203.                 if (scount>=STRMAX-1) {
  204.                     printf("String too long!\n");
  205.                     free(ptr);                /* Give back our memory */
  206.                     BEGIN(0);
  207.                 }
  208.             }
  209.  
  210. <QUOTE>["]  {
  211.                 /* End of the string */
  212.                 *cp=0;                    /* Terminate the string            */
  213.                 scount++;
  214.                 cp=(char*)realloc(ptr,scount); /* Give a bit of memory back */
  215.                 if (cp)
  216.                     yylval.ptr=cp;        /* Pass pointer to string        */
  217.                 else
  218.                     yylval.ptr=ptr;        /* Realloc failed! (impossible) */
  219.                 BEGIN(0);                /* Turn off string processing    */
  220.                 return(STRING);            /* Return our string token        */
  221.             }
  222.  
  223. <QUOTE>\n    {
  224.                 printf( "Missing quote\n" );
  225.                 free(ptr);                /* Give back our memory */
  226.                 BEGIN(0);
  227.             }
  228.  
  229.  
  230. /* Operators */
  231.  
  232. "<<"        return(SHIFT_LEFT);
  233. ">>"        return(SHIFT_RIGHT);
  234. "||"        return(LOGICAL_OR);
  235. "&&"        return(LOGICAL_AND);
  236. "!="        return(NOT_EQ);
  237. "=="        return(EQ);
  238. ">="        return(GT_EQ);
  239. "=>"        return(GT_EQ);
  240. "<="        return(LT_EQ);
  241. "=<"        return(LT_EQ);
  242.  
  243. /* Single character stuff */
  244.  
  245. "{"         return('{');
  246. "}"         return('}');
  247. ","            return(',');
  248. "("            return('(');
  249. ")"            return(')');
  250. "-"            return('-');
  251. "~"            return('~');
  252. "!"            return('!');
  253. "^"         return('^');
  254. "|"            return('|');
  255. "&"            return('&');
  256. ">"            return('>');
  257. "<"            return('<');
  258. "+"            return('+');
  259. "*"            return('*');
  260. "/"            return('/');
  261. "%"            return('%');
  262. ";"            return(';');
  263. ":"            return(':');
  264. "="            return('=');
  265.  
  266. {WS}        ;                    /* We don't care about whitespace, eat it. */
  267.  
  268. \012        ;                    /* Ignore CR */
  269. \015        {
  270.                 lineCount++;
  271.             }
  272.  
  273. .            {
  274.                 printf( "Illegal character '%c' (%i)\n", yytext[0],yytext[0]);
  275.                 BEGIN(RECOVER);
  276.             }
  277.  
  278. <RECOVER>.*\n {
  279.                 lineCount++;
  280.                  BEGIN(0);
  281.               }
  282. %%
  283.  
  284. /* myesc - return character corresponding to escape sequence
  285.  *
  286.  * synopsis
  287.  *    char array[], c, myesc();
  288.  *    c = myesc( array );
  289.  *
  290.  */
  291.  
  292. char
  293. myesc(array)
  294. char *array;
  295. {
  296.     char c, esc_char;
  297.     register int sptr = 2;
  298.  
  299.     switch (array[1]) {
  300.         case 'n': return ('\n');
  301.         case 't': return ('\t');
  302.         case 'f': return ('\f');
  303.         case 'r': return ('\r');
  304.         case 'b': return ('\b');
  305.     
  306.         case '0':
  307.             if(isdigit(array[2])) { /* \0<octal> */
  308.         
  309.                 /* don't increment inside loop control because the
  310.                  * macro will expand it to two increments!  (Not a
  311.                  * problem with the C version of the macro)
  312.                  */
  313.                 while(isdigit(array[sptr]))
  314.                     ++sptr;
  315.         
  316.                 c = array[sptr];
  317.                 array[sptr] = '\0';
  318.         
  319.                 esc_char = otoi(array + 2);
  320.                 array[sptr] = c;
  321.         
  322.                 return(esc_char);
  323.  
  324.             } else if((array[2] == 'x') || (array[2] == 'X')) { /* hex */
  325.                 /* do hex stuff. Format is ALWAYS 0x## */
  326.                 sptr+=3;
  327.         
  328.                 c = array[sptr];
  329.                 array[sptr] = '\0';
  330.         
  331.                 esc_char = htoi(array + 3);
  332.                 array[sptr] = c;
  333.         
  334.                 return(esc_char);
  335.  
  336.             } else {
  337.                 return (0);
  338.             }
  339.             break;
  340.  
  341.         case '^':
  342.             if (array[2]>='a')
  343.                 return (array[2]-'a'+1);
  344.             else
  345.                 return (array[2]-'A'+1);
  346.  
  347.             break;
  348.     }
  349.     return(array[1]);
  350. }
  351.  
  352. int
  353. otoi(char *a)
  354. {
  355.  
  356.     int        x=0;
  357.  
  358.     while(*a) {
  359.         x <<= 3;
  360.         x += *a++ - '0';
  361.     }
  362.     return(x);
  363. }
  364.  
  365. int
  366. htoi(char *a)
  367. {
  368.     int        x=0;
  369.  
  370.     while (*a) {
  371.         if (*a<'A') {
  372.             x=(x<<4) + (*a-'0');
  373.         } else {
  374.             if (*a<'a')
  375.               x=(x<<4)+(*a-'A'+10);
  376.             else
  377.               x=(x<<4)+(*a-'a'+10);
  378.         }
  379.         a++;
  380.     }
  381.     return(x);
  382. }
  383.