1: %{
2: extern long evalval;
3: #define YYSTYPE long
4: %}
5:
6: %term DIGITS
7: %left '|'
8: %left '&'
9: %right '!'
10: %nonassoc GT GE LT LE NE EQ
11: %left '+' '-'
12: %left '*' '/' '%'
13: %right POWER
14: %right UMINUS
15: %%
16:
17: s : e ={ evalval = $1; }
18: | ={ evalval = 0; }
19: ;
20:
21: e : e '|' e ={ $$ = ($1!=0 || $3!=0) ? 1 : 0; }
22: | e '&' e ={ $$ = ($1!=0 && $3!=0) ? 1 : 0; }
23: | '!' e ={ $$ = $2 == 0; }
24: | e EQ e ={ $$ = $1 == $3; }
25: | e NE e ={ $$ = $1 != $3; }
26: | e GT e ={ $$ = $1 > $3; }
27: | e GE e ={ $$ = $1 >= $3; }
28: | e LT e ={ $$ = $1 < $3; }
29: | e LE e ={ $$ = $1 <= $3; }
30: | e '+' e ={ $$ = ($1+$3); }
31: | e '-' e ={ $$ = ($1-$3); }
32: | e '*' e ={ $$ = ($1*$3); }
33: | e '/' e ={ $$ = ($1/$3); }
34: | e '%' e ={ $$ = ($1%$3); }
35: | '(' e ')' ={ $$ = ($2); }
36: | e POWER e ={ for ($$=1; $3-->0; $$ *= $1); }
37: | '-' e %prec UMINUS ={ $$ = $2-1; $$ = -$2; }
38: | '+' e %prec UMINUS ={ $$ = $2-1; $$ = $2; }
39: | DIGITS ={ $$ = evalval; }
40: ;
41:
42: %%
43:
44: yylex() {
45: extern char *pe;
46:
47: while (*pe==' ' || *pe=='\t' || *pe=='\n')
48: pe++;
49: switch(*pe) {
50: case '\0':
51: case '+':
52: case '-':
53: case '/':
54: case '%':
55: case '(':
56: case ')':
57: return(*pe++);
58: case '^':
59: pe++;
60: return(POWER);
61: case '*':
62: return(peek('*', POWER, '*'));
63: case '>':
64: return(peek('=', GE, GT));
65: case '<':
66: return(peek('=', LE, LT));
67: case '=':
68: return(peek('=', EQ, EQ));
69: case '|':
70: return(peek('|', '|', '|'));
71: case '&':
72: return(peek('&', '&', '&'));
73: case '!':
74: return(peek('=', NE, '!'));
75: default:
76: evalval = 0;
77: while (*pe >= '0' && *pe <= '9')
78: evalval = evalval*10 + *pe++ - '0';
79: return(DIGITS);
80: }
81: }
82:
83: peek(c, r1, r2)
84: {
85: if (*++pe != c)
86: return(r2);
87: ++pe;
88: return(r1);
89: }
90:
91: yyerror(s)
92: char *s;
93: {
94: }
Defined functions
_peek
defined in line
83; used 7 times
Defined macros