/* Mini Calculator */ /* calc.y */ %{ #include #include #include "heading.h" int yyerror(char *s); int yylex(void); %} %union{ int int_val; char* str_val; int int_list[4]; } %start input %token INTEGER_LITERAL %token MNEMONIC %token SYMBOL %type operand %type opseq %% input: /* empty */ | codebl { printf("input-codebl\n") } ; codebl: statem { printf("codebl--statem\n") } | codebl '\n' statem ; statem: instruction { printf("statem-instruction\n"); } ; instruction: SYMBOL opseq { printf("instruction-SYMBOL-opseq\n"); } ; opseq: /* empty */ { printf("opseq-empty\n"); } | opseq1 { printf("opseq-opseq1\n"); } ; opseq1: operand { printf("opseq1-operand\n"); } | opseq ',' operand { printf("opseq1-wcomma\n"); } ; operand: MNEMONIC { $$ = $1; } | INTEGER_LITERAL { printf("int-lit\n"); } | offaddr { printf("operand-offaddr\n"); } ; offaddr: INTEGER_LITERAL '(' operand ')' { printf("offaddr\n"); } ; %% int yyerror(char *s) { extern int yylineno; extern char *yytext; fprintf(stderr, "error: %s at symbol %s on line %d.\n", s, yytext,yylineno); exit(1); }