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