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