home *** CD-ROM | disk | FTP | other *** search
Lex Description | 1990-08-02 | 7.3 KB | 383 lines |
- %{
-
- /***************/
- /* Gloal stuff */
- /***************/
-
- extern "C" {
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <io.h>
- }
-
- #define STRMAX 255 /* Max length of strings */
-
- #include "comgram.h" /* Get YACC token equates */
-
- extern YYSTYPE yylval;
- extern FILE * comIn;
- extern FILE * comOut;
-
- /* gets input and stuffs it into "buf". number of characters read, or YY_NULL,
- * is returned in "result".
- *
- * We redefine Flex's routine so we can supply the text from a file.
- */
- #undef YY_INPUT
- #define YY_INPUT(buf,result,max_size) \
- if ( (result = read( fileno(comIn), buf, max_size )) < 0 ) \
- YY_FATAL_ERROR( "read() in flex scanner failed" );
-
- /* We don't want the default action to ECHO, so define it as nothing */
- #undef YY_DEFAULT_ACTION
- #define YY_DEFAULT_ACTION printf("Default action: %i\n",*yytext)
-
- /*
- * Local variables
- */
-
- char *ptr; /* String pointer */
- char *cp; /* Roving string pointer */
- int scount; /* String length */
- extern YYSTYPE yylval; /* For returning token values */
- extern int lineCount; /* Our line counter */
- int q; /* Work variable */
-
- #define BYTEMASK 0x7F
-
- /***********************/
- /* Function prototypes */
- /***********************/
-
- int otoi(char *);
- int htoi(char *);
- char myesc(char *);
-
- %}
-
- %x QUOTE C_COMMENT RECOVER
-
- WS [ \t]+
-
- OPTWS [ \t]*
-
- NAME [A-Za-z_][A-Za-z_0-9]*
-
- LETTERS [A-Za-z]
-
- ESCSEQ \\([^^\n]|"^".|0[0-9]{1,3}|0x[0-9A-Fa-f][0-9A-Fa-f])
-
- DIGIT [0-9]
-
- HDIGIT [0-9A-Fa-f]
-
- DIGITS [0-9]+
-
- HDIGITS [0-9A-Fa-f]+
- %%
-
- /************/
- /* Comments */
- /************/
-
- "/*" BEGIN(C_COMMENT); /* Old style comments. */
-
- <C_COMMENT>"*/" BEGIN(0); /* Sub-scanner to handle comments. */
- <C_COMMENT>[^*\n]+ ;
- <C_COMMENT>"*" ;
- <C_COMMENT>\n {
- lineCount++;
- }
-
- "//".*\n { /* New style comments */
- lineCount++;
- }
-
- /************/
- /* Commands */
- /************/
-
- "ydebug" return(YDEBUG);
-
- "send" return(SEND);
-
- "slow" return(SLOW);
-
- "wait" return(WAIT);
-
- "for" return(FOR);
-
- "eol" return(EOL);
-
- "anything" return(ANYTHING);
-
- "else" return(ELSE);
-
- "end" return(END);
-
- "on" return(ON);
-
- "off" return(OFF);
-
- "notify" return(NOTIFY);
-
- "close" return(CLOSE);
-
- "window" return(WINDOW);
-
- "display comment" return(DISPLAYCOMMENT);
-
- "erase comment" return(ERASECOMMENT);
-
- /* Byte Literal constant */
- ['].['] {
- yylval.ival= *(yytext+1);
- return(BYTE_LITCONST);
- }
-
- /* Long Literal constant */
- [']{LETTERS}{LETTERS}{LETTERS}{LETTERS}['] {
- ptr=yytext+1;
- yylval.lval=(*ptr);
- yylval.lval<<=8;
- yylval.lval+=(*(ptr+1));
- yylval.lval<<=8;
- yylval.lval+=(*(ptr+2));
- yylval.lval<<=8;
- yylval.lval+=(*(ptr+3));
- return(LITCONST);
- }
-
-
- /* Hex constant */
- "0"[xX]{HDIGITS} {
- yylval.ival=0;
- for (q=2; q<yyleng; q++) {
- if (yytext[q]<'A') {
- yylval.ival=(yylval.ival*16) + (yytext[q]-'0');
- } else {
- if (yytext[q]<'a')
- yylval.ival=(yylval.ival*16)+(yytext[q]-'A'+10);
- else
- yylval.ival=(yylval.ival*16)+(yytext[q]-'a'+10);
- }
- }
- return(CONSTANT);
- }
-
- /* Integer constant */
- {DIGITS} {
- yylval.ival=0;
- for (q=0; q<yyleng; q++) {
- yylval.ival=(yylval.ival*10) + (yytext[q]-'0');
- }
- return(CONSTANT);
- }
-
- /* String */
- ["] {
- /* We need to accumulate all characters in the string */
- /* To do this, we use a sub-analyzer. Let's get set! */
- ptr=(char*)calloc(1,STRMAX+1); /* Get memory for the string */
- cp=ptr; /* Make a roving pointer */
- scount=0;
- BEGIN(QUOTE); /* Start the analyzer */
- }
-
- /* Sub-analyzer to accumulate the contents of a string */
- <QUOTE>{ESCSEQ} {
- *cp++=(myesc(yytext) & BYTEMASK);
- scount++;
- if (scount>=STRMAX-1) {
- printf("String too long!\n");
- free(ptr); /* Give back our memory */
- BEGIN(0);
- }
- }
-
- <QUOTE>[^"\n] {
- *cp++=(yytext[0] & BYTEMASK); /* Still in string */
- scount++;
- if (scount>=STRMAX-1) {
- printf("String too long!\n");
- free(ptr); /* Give back our memory */
- BEGIN(0);
- }
- }
-
- <QUOTE>["] {
- /* End of the string */
- *cp=0; /* Terminate the string */
- scount++;
- cp=(char*)realloc(ptr,scount); /* Give a bit of memory back */
- if (cp)
- yylval.ptr=cp; /* Pass pointer to string */
- else
- yylval.ptr=ptr; /* Realloc failed! (impossible) */
- BEGIN(0); /* Turn off string processing */
- return(STRING); /* Return our string token */
- }
-
- <QUOTE>\n {
- printf( "Missing quote\n" );
- free(ptr); /* Give back our memory */
- BEGIN(0);
- }
-
-
- /* Operators */
-
- "<<" return(SHIFT_LEFT);
- ">>" return(SHIFT_RIGHT);
- "||" return(LOGICAL_OR);
- "&&" return(LOGICAL_AND);
- "!=" return(NOT_EQ);
- "==" return(EQ);
- ">=" return(GT_EQ);
- "=>" return(GT_EQ);
- "<=" return(LT_EQ);
- "=<" return(LT_EQ);
-
- /* Single character stuff */
-
- "{" return('{');
- "}" return('}');
- "," return(',');
- "(" return('(');
- ")" return(')');
- "-" return('-');
- "~" return('~');
- "!" return('!');
- "^" return('^');
- "|" return('|');
- "&" return('&');
- ">" return('>');
- "<" return('<');
- "+" return('+');
- "*" return('*');
- "/" return('/');
- "%" return('%');
- ";" return(';');
- ":" return(':');
- "=" return('=');
-
- {WS} ; /* We don't care about whitespace, eat it. */
-
- \012 ; /* Ignore CR */
- \015 {
- lineCount++;
- }
-
- . {
- printf( "Illegal character '%c' (%i)\n", yytext[0],yytext[0]);
- BEGIN(RECOVER);
- }
-
- <RECOVER>.*\n {
- lineCount++;
- BEGIN(0);
- }
- %%
-
- /* myesc - return character corresponding to escape sequence
- *
- * synopsis
- * char array[], c, myesc();
- * c = myesc( array );
- *
- */
-
- char
- myesc(array)
- char *array;
- {
- char c, esc_char;
- register int sptr = 2;
-
- switch (array[1]) {
- case 'n': return ('\n');
- case 't': return ('\t');
- case 'f': return ('\f');
- case 'r': return ('\r');
- case 'b': return ('\b');
-
- case '0':
- if(isdigit(array[2])) { /* \0<octal> */
-
- /* don't increment inside loop control because the
- * macro will expand it to two increments! (Not a
- * problem with the C version of the macro)
- */
- while(isdigit(array[sptr]))
- ++sptr;
-
- c = array[sptr];
- array[sptr] = '\0';
-
- esc_char = otoi(array + 2);
- array[sptr] = c;
-
- return(esc_char);
-
- } else if((array[2] == 'x') || (array[2] == 'X')) { /* hex */
- /* do hex stuff. Format is ALWAYS 0x## */
- sptr+=3;
-
- c = array[sptr];
- array[sptr] = '\0';
-
- esc_char = htoi(array + 3);
- array[sptr] = c;
-
- return(esc_char);
-
- } else {
- return (0);
- }
- break;
-
- case '^':
- if (array[2]>='a')
- return (array[2]-'a'+1);
- else
- return (array[2]-'A'+1);
-
- break;
- }
- return(array[1]);
- }
-
- int
- otoi(char *a)
- {
-
- int x=0;
-
- while(*a) {
- x <<= 3;
- x += *a++ - '0';
- }
- return(x);
- }
-
- int
- htoi(char *a)
- {
- int x=0;
-
- while (*a) {
- if (*a<'A') {
- x=(x<<4) + (*a-'0');
- } else {
- if (*a<'a')
- x=(x<<4)+(*a-'A'+10);
- else
- x=(x<<4)+(*a-'a'+10);
- }
- a++;
- }
- return(x);
- }
-