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