Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include <stdlib.h>
- #include "polyn.h"
- #include "symbtab.h"
- extern int yylex();
- extern int yyparse();
- void yyerror(const char* s);
- %}
- %token T_INT
- %token T_IDENT T_POLYN
- %token T_PLUS T_MINUS T_MULTIPLY T_DIVIDE T_LROUND T_RROUND T_EQUAL
- %token T_NEWLINE T_QUIT
- %left T_PLUS T_MINUS
- %left T_MULTIPLY T_DIVIDE
- %start input
- %union {
- int intval;
- char *chstr;
- struct Polyn *p;
- }
- %type <intval> expression T_INT
- %type <chstr> assign T_IDENT
- %type <p> poln T_POLYN
- %%
- input:
- %empty
- | input line
- ;
- line:
- T_NEWLINE
- | expression T_NEWLINE { printf("\tResult: %i\n", $1); }
- | assign T_NEWLINE ;
- | T_QUIT T_NEWLINE { exit(0); }
- ;
- assign:
- T_IDENT T_EQUAL poln {store($1, $3); }
- ;
- poln:
- T_POLYN ;
- | poln T_PLUS poln ;
- | poln T_MINUS poln ;
- ;
- expression:
- T_INT { $$ = $1; }
- | expression T_PLUS expression { $$ = $1 + $3; }
- | expression T_MINUS expression { $$ = $1 - $3; }
- | expression T_MULTIPLY expression { $$ = $1 * $3; }
- | expression T_DIVIDE expression { $$ = $1 / $3; }
- | T_LROUND expression T_RROUND { $$ = $2; }
- ;
- %%
- int main() {
- while (yyparse());
- return 0;
- }
- void yyerror(const char* s) {
- fprintf(stderr, "Parse error: %s\n", s);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement