rendered paste body/* Mini Calculator */
/* calc.y */
%{
#include <stdlib.h>
#include <string.h>
#include "heading.h"
int yyerror(char *s);
int yylex(void);
%}
%union{
int int_val;
char* str_val;
int int_list[4];
}
%start input
%token <int_val> INTEGER_LITERAL
%token <int_val> MNEMONIC
%token <str_val> SYMBOL
%type <int_val> operand
%type <int_list> 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);
}