1234949Sbapt%{
2234949Sbapt# include <stdio.h>
3234949Sbapt# include <ctype.h>
4234949Sbapt
5234949Sbaptint regs[26];
6234949Sbaptint base;
7234949Sbapt
8234949Sbaptextern int yylex(void);
9234949Sbaptstatic void yyerror(const char *s);
10234949Sbapt
11234949Sbapt%}
12234949Sbapt
13234949Sbapt%start list
14234949Sbapt
15234949Sbapt%token DIGIT LETTER
16234949Sbapt
17234949Sbapt%left '|'
18234949Sbapt%left '&'
19234949Sbapt%left '+' '-'
20234949Sbapt%left '*' '/' '%'
21234949Sbapt%left UMINUS   /* supplies precedence for unary minus */
22234949Sbapt
23234949Sbapt%% /* beginning of rules section */
24234949Sbapt
25234949Sbaptlist  :  /* empty */
26234949Sbapt      |  list stat '\n'
27234949Sbapt      |  list error '\n'
28234949Sbapt            {  yyerrok ; }
29234949Sbapt      ;
30234949Sbapt
31234949Sbaptstat  :  expr
32234949Sbapt            {  printf("%d\n",$1);}
33234949Sbapt      |  LETTER '=' expr
34234949Sbapt            {  regs[$1] = $3; }
35234949Sbapt      ;
36234949Sbapt
37234949Sbaptexpr  :  '(' expr ')'
38234949Sbapt            {  $$ = $2; }
39234949Sbapt      |  expr '+' expr
40234949Sbapt            {  $$ = $1 + $3; }
41234949Sbapt      |  expr '-' expr
42234949Sbapt            {  $$ = $1 - $3; }
43234949Sbapt      |  expr '*' expr
44234949Sbapt            {  $$ = $1 * $3; }
45234949Sbapt      |  expr '/' expr
46234949Sbapt            {  $$ = $1 / $3; }
47234949Sbapt      |  expr '%' expr
48234949Sbapt            {  $$ = $1 % $3; }
49234949Sbapt      |  expr '&' expr
50234949Sbapt            {  $$ = $1 & $3; }
51234949Sbapt      |  expr '|' expr
52234949Sbapt            {  $$ = $1 | $3; }
53234949Sbapt      |  '-' expr %prec UMINUS
54234949Sbapt            {  $$ = - $2; }
55234949Sbapt      |  LETTER
56234949Sbapt            {  $$ = regs[$1]; }
57234949Sbapt      |  number
58234949Sbapt      ;
59234949Sbapt
60234949Sbaptnumber:  DIGIT
61234949Sbapt         {  $$ = $1; base = ($1==0) ? 8 : 10; }
62234949Sbapt      |  number DIGIT
63234949Sbapt         {  $$ = base * $1 + $2; }
64234949Sbapt      ;
65234949Sbapt
66234949Sbapt%% /* start of programs */
67234949Sbapt
68234949Sbaptint
69234949Sbaptmain (void)
70234949Sbapt{
71234949Sbapt    while(!feof(stdin)) {
72234949Sbapt	yyparse();
73234949Sbapt    }
74234949Sbapt    return 0;
75234949Sbapt}
76234949Sbapt
77234949Sbaptstatic void
78234949Sbaptyyerror(const char *s)
79234949Sbapt{
80234949Sbapt    fprintf(stderr, "%s\n", s);
81234949Sbapt}
82234949Sbapt
83234949Sbaptint
84234949Sbaptyylex(void)
85234949Sbapt{
86234949Sbapt	/* lexical analysis routine */
87234949Sbapt	/* returns LETTER for a lower case letter, yylval = 0 through 25 */
88234949Sbapt	/* return DIGIT for a digit, yylval = 0 through 9 */
89234949Sbapt	/* all other characters are returned immediately */
90234949Sbapt
91234949Sbapt    int c;
92234949Sbapt
93234949Sbapt    while( (c=getchar()) == ' ' )   { /* skip blanks */ }
94234949Sbapt
95234949Sbapt    /* c is now nonblank */
96234949Sbapt
97234949Sbapt    if( islower( c )) {
98234949Sbapt	yylval = c - 'a';
99234949Sbapt	return ( LETTER );
100234949Sbapt    }
101234949Sbapt    if( isdigit( c )) {
102234949Sbapt	yylval = c - '0';
103234949Sbapt	return ( DIGIT );
104234949Sbapt    }
105234949Sbapt    return( c );
106234949Sbapt}
107