Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
50 lines (39 sloc) 1.25 KB
%{
#include <stdio.h>
int vars[26];
#define YY_MEMOIZATION
%}
Stmt = - e:Expr EOL { printf("%d\n", e); }
| ( !EOL . )* EOL { printf("error\n"); }
Expr = i:ID ASSIGN s:Sum { $$= vars[i]= s; }
| s:Sum { $$= s; }
Sum = l:Product
( PLUS r:Product { l += r; }
| MINUS r:Product { l -= r; }
)* { $$= l; }
Product = l:Value
( TIMES r:Value { l *= r; }
| DIVIDE r:Value { l /= r; }
)* { $$= l; }
Value = i:NUMBER { $$= atoi(yytext); }
| i:ID !ASSIGN { $$= vars[i]; }
| OPEN i:Expr CLOSE { $$= i; }
NUMBER = < [0-9]+ > - { $$= atoi(yytext); }
ID = < [a-z] > - { $$= yytext[0] - 'a'; }
ASSIGN = '=' -
PLUS = '+' -
MINUS = '-' -
TIMES = '*' -
DIVIDE = '/' -
OPEN = '(' -
CLOSE = ')' -
- = [ \t]*
EOL = '\n' | '\r\n' | '\r' | ';'
%%
int main()
{
GREG g;
memset(&g, 0, sizeof(GREG));
while (yyparse(&g));
return 0;
}
Something went wrong with that request. Please try again.