1 %token-table 2 3 %{ 4 # include <stdio.h> 5 # include <ctype.h> 6 7 int regs[26]; 8 int base; 9 10 #ifdef YYBISON 11 int yylex(void); 12 static void yyerror(const char *s); 13 #endif 14 15 %} 16 17 %start list 18 19 %token DIGIT LETTER 20 21 %left '|' 22 %left '&' 23 %left '+' '-' 24 %left '*' '/' '%' 25 %left UMINUS /* supplies precedence for unary minus */ 26 27 %% /* beginning of rules section */ 28 29 list : /* empty */ 30 | list stat '\n' 31 | list error '\n' 32 { yyerrok ; } 33 ; 34 35 stat : expr 36 { printf("%d\n",$1);} 37 | LETTER '=' expr 38 { regs[$1] = $3; } 39 ; 40 41 expr : '(' expr ')' 42 { $$ = $2; } 43 | expr '+' expr 44 { $$ = $1 + $3; } 45 | expr '-' expr 46 { $$ = $1 - $3; } 47 | expr '*' expr 48 { $$ = $1 * $3; } 49 | expr '/' expr 50 { $$ = $1 / $3; } 51 | expr '%' expr 52 { $$ = $1 % $3; } 53 | expr '&' expr 54 { $$ = $1 & $3; } 55 | expr '|' expr 56 { $$ = $1 | $3; } 57 | '-' expr %prec UMINUS 58 { $$ = - $2; } 59 | LETTER 60 { $$ = regs[$1]; } 61 | number 62 ; 63 64 number: DIGIT 65 { $$ = $1; base = ($1==0) ? 8 : 10; } 66 | number DIGIT 67 { $$ = base * $1 + $2; } 68 ; 69 70 %% /* start of programs */ 71 72 #ifdef YYBYACC 73 extern int YYLEX_DECL(); 74 #endif 75 76 int 77 main (void) 78 { 79 while(!feof(stdin)) { 80 yyparse(); 81 } 82 return 0; 83 } 84 85 static void 86 yyerror(const char *s) 87 { 88 fprintf(stderr, "%s\n", s); 89 } 90 91 int 92 yylex(void) 93 { 94 /* lexical analysis routine */ 95 /* returns LETTER for a lower case letter, yylval = 0 through 25 */ 96 /* return DIGIT for a digit, yylval = 0 through 9 */ 97 /* all other characters are returned immediately */ 98 99 int c; 100 101 while( (c=getchar()) == ' ' ) { /* skip blanks */ } 102 103 /* c is now nonblank */ 104 105 if( islower( c )) { 106 yylval = c - 'a'; 107 return ( LETTER ); 108 } 109 if( isdigit( c )) { 110 yylval = c - '0'; 111 return ( DIGIT ); 112 } 113 return( c ); 114 } 115