Listing 2. lex Processing of Algebraic Statements

1: %
2: /* This routine is the matching flex routine
    to the yacc parser that handles vector/scalar
3:  operations. Written February, 1994
4:  by D. A. Provins
5:  $Header: /home/provinsd/LAS/MOTIF_LAS/RCS/flex.main,v 1.8
 1994/06/13 01:23:46 provinsd Exp $
6: */
7: #include <stdlib.h>
8: #include "y.tab.h"
9: extern int DeBuG;    /* do_arithmetic sets
            if "las_debug" valued */
10: int numlines=1;
11: static int ivalue = 0;
12: float value = 0.0;
13: #undef YY_INPUT
14: #define YY_INPUT(b,r,ms) (r=my_yyinput (b,ms))
15: #define min(a,b) (a<b?a:b)
16: %}
17: NEWLINE \\n
18: EQUAL  \\=
19: PLUS  \\+
20: MINUS  \\-
21: DIGIT  [0-9]
22: INT   {DIGIT}+
23: FLOAT  {DIGIT}*"."{DIGIT}*
24: DOUBLE {DIGIT}*"."?{DIGIT}*([eEdD]
   ({PLUS}|{MINUS})?{DIGIT}{DIGIT}*)?
25: VAR   [a-zA-Z][a-zA-Z0-9]*
26: DIVIDE \\/
27: TIMES  \\*
28: RAISE  \\^
29: LPAREN \\(
30: RPAREN \\)
31: WS   [ \\t]
32: %%
33: {NEWLINE} numlines++; return -1;
34: {EQUAL} {if (DeBuG)
35:      printf ("Equal sign\\n");
36:      return EQUAL;  }
38: {VAR}  {if (DeBuG)
39:    printf ("Variable <%s>\\n", yytext);
40:      return VARIABLE;  }
42: {VAR}/{EQUAL} {if (DeBuG)
43:        printf ("LHS of equation <%s>\\n", yytext);
44:        return LHS;  }
46: {INT}  {if (DeBuG)
47:      printf ("Int <%s>\\n", yytext);
48:      ivalue= atoi (yytext);
49:      value= ivalue;
50:      return NUMBER;  }
52: {FLOAT} {if (DeBuG)
53:      printf ("Float <%s>\\n", yytext);
54:      value= atof (yytext);
55:      return NUMBER;  }
57: {DOUBLE} {if (DeBuG)
58:      printf ("Double <%s>\\n", yytext);
59:      value= (float) atof (yytext);
60:      return NUMBER;  }
62: {PLUS} {if (DeBuG)
63:     printf ("Sign <%s>\\n", yytext);
64:     return PLUS;  }
66: {MINUS} {if (DeBuG)
67:      printf ("Sign <%s>\\n", yytext);
68:     return MINUS;  }
70: {TIMES} {if (DeBuG)
71:     printf ("Times <%s>\\n", yytext);
72:     return TIMES;  }
 74: {DIVIDE} {if (DeBuG)
 75:      printf ("Divide <%s>\\n", yytext);
 76:      return DIVIDE;  }
 78: {RAISE} {if (DeBuG)
 79:     printf ("Raise <%s>\\n", yytext);
 80:     return RAISE;  }
 82: {LPAREN} {if (DeBuG)
 83:      printf ("Left paren\\n");
 84:      return LPAREN;  }
 86: {RPAREN} {if (DeBuG)
 87:      printf ("Right paren\\n");
 88:      return RPAREN;  }
 90: {WS}   /* eat whitespace */
 91: %%
 92: /********************************************
 93:   This routine is replaces the Flex
       "internal" YY_INPUT macro
 94:   that it uses to get input
 95: *********************************************
 96: int
 97: my_yyinput (
 98: #ifdef _NO_PROTO
 99: buf, max_size)
100: char * buf;
101: int max_size;
102: #else
103: char * buf, int max_size)
104: #endif
105: {
106: /* Elsewhere, "my_arithmetic_string"
        must be assigned a value. At that time,
107:    "current_pointer" should point to the
108:    first character in "my_arithmetic_string",
109:    and "end_of_string_pointer" should
        be set to the trailing NULL
110: */
111:  extern char * my_arithmetic_string,
112:        * current_pointer,
113:        * end_of_string_pointer;
114: int n = min (max_size, end_of_string_pointer\
   - current_pointer);
115:  if (n > 0) {
116:   memcpy (buf, current_pointer, n);
117:   current_pointer += n;  }
119:  return n;
120: }
121: restart_lex ()
122: {
123:  *yy_c_buf_p = (YY_CHAR *) 0;
124:  yy_init = 1;   /* whether to initialize */
125:  yy_start = 0;  /* start state number */