123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- %{
- #include <ctype.h>
- int regs[26];
- int base;
- int val;
- int printflag;
- %}
- %start stmt
- %token DIGIT LETTER EXIT
- %left '|'
- %left '&'
- %left '+' '-'
- %left '*' '/' '%'
- %left UMINUS
- %%
- stmt : expr
- { val = $1; printflag = 1; }
- | LETTER '=' expr
- { regs[$1] = $3; }
- | EXIT
- { exit(0); }
- ;
- expr : '(' expr ')'
- { $$ = $2; }
- | expr '+' expr
- { if ($1 == 69)
- {
- $$ = 2;
- printf ("$1 became %d\n", $1);
- $1 = 69;
- }
- $$ = $1 + $3; }
- | expr '-' expr
- { $$ = $1 - $3; }
- | expr '*' expr
- { $$ = $1 * $3; }
- | expr '/' expr
- { $$ = $1 / $3; }
- | expr '%' expr
- { $$ = $1 % $3; }
- | expr '|' expr
- { $$ = $1 | $3; }
- | expr '&' expr
- { $$ = $1 & $3; }
- | '-' expr %prec UMINUS
- { $$ = - $2; }
- | LETTER
- { $$ = regs[$1]; }
- | number
- /* | '?'
- { yydebug = !yydebug; }
- */ ;
- number : DIGIT
- { $$ = $1; base = ($1 == 0) ? 8 : 10; }
- | number DIGIT
- { $$ = base * $1 + $2; }
- ;
- %%
- static int eol;
- int
- yylex()
- {
- int c;
- while ( (c=getchar()) == ' ') {}
- if (c == '\n')
- { eol = 1;
- return 0; }
- if (c == 'Q' || c == EOF)
- return(EXIT);
- if (islower(c))
- {
- yylval = c - 'a';
- return (LETTER);
- }
- if (isdigit(c))
- {
- yylval = c - '0';
- return (DIGIT);
- }
- return (c);
- }
- yyerror(s)
- char *s;
- {
- printf("%s\n", s);
- }
- main()
- {
- for (;;)
- {
- eol = 0;
- printflag = 0;
- if (yyparse()) printflag = 0;
- if (printflag) printf("%d\n", val);
- while (!eol)
- if (yylex() == EOF)
- exit(0);
- }
- }
|