%{
#include <ctype.h>
#include <stdio.h>
int a[26];
%}
%token NUMBER
%%
id : 'a'
| 'b'
| 'c'
| 'd'
| 'e'
| 'f'
| 'g'
| 'h'
| 'i'
| 'j'
| 'k'
| 'l'
| 'm'
| 'n'
| 'p'
| 'q'
| 'r'
| 's'
| 't'
| 'u'
| 'v'
| 'w'
| 'x'
| 'y'
| 'z'
;
print : "print" id { printf("%i\n", a[$2-'a']);}
statement : assignment
| cond
| loop
| print
;
assignment : id ":=" expr { a[$1-'a'] = $3; }
;
cond : "if" boolexpr "then" statement "fi" { if ($2) { $4; }}
| "if" boolexpr "then" statement "else" statement "fi" { if ($2) { $4; } else { $6; }}
;
loop : "while" boolexpr "do" statement "od" { while ($2) {$3;}}
;
expr : numexpr
| boolexpr
;
boolexpr : numexpr '<' numexpr { $1 < $3; }
;
lines : lines statement '\n'
| lines '\n'
;
numexpr : numexpr '+' term { $$ = $1 + $3; }
| numexpr '-' term { $$ = $1 - $3; }
| term
;
term : term '*' factor { $$ = $1 * $3; }
| term '/' factor { $$ = $1 * $3; }
| factor
;
factor : '(' numexpr ')' { $$ = $2; }
| NUMBER
;
%%
int yylex() {
int c;
c = getchar();
if (isdigit(c)) {
ungetc(c, stdin);
scanf("%d", &yylval);
return NUMBER;
}
return c;
}