Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include "orig.tab.h"
- #include "lex.yy.c"
- int yylex(void);
- void yyerror(const char *);
- extern FILE *yyin;
- int memori = 0, memori_rsl = 0;
- %}
- %token KAZU
- %token MPLUS MMINUS MPRINT MCLEAR
- %left '-' '+'
- %left '/' '*'
- %precedence VTOKEN
- %%
- source:
- /* empty */
- | source bun
- ;
- bun:
- shiki ';' { printf("%d\n", $1); memori = $1; }
- | memori ';'
- ;
- shiki:
- KAZU
- | tasu
- | hiku
- | waru
- | kakeru
- | '-' shiki %prec VTOKEN { $$ = -$2; }
- | '(' shiki ')' { $$ = $2; }
- ;
- tasu:
- shiki '+' shiki { $$ = $1 + $3; }
- ;
- hiku:
- shiki '-' shiki { $$ = $1 - $3; }
- ;
- waru:
- shiki '/' shiki {
- if ($3) $$ = $1 / $3;
- else {
- $$ = 1;
- fprintf(stderr, "division by zero: \"%d / %d\"\n", $1, $3);
- }
- }
- ;
- kakeru:
- shiki '*' shiki { $$ = $1 * $3; }
- ;
- memori:
- MPLUS { memori_rsl += memori; }
- | MMINUS { memori_rsl -= memori; }
- | MPRINT { printf("memori_rsl = %d\n", memori_rsl); }
- | MCLEAR { memori = memori_rsl = 0; }
- ;
- %%
- void yyerror(const char *str) {
- fprintf(stderr, "error: %s\n", str);
- }
- int main(int argc, char *argv[]) {
- if (argc != 2) return 1;
- yyin = fopen(argv[1], "r");
- return yyparse();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement