38 lines
548 B
Plaintext
38 lines
548 B
Plaintext
%{
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
extern int yylex();
|
|
extern int yyparse();
|
|
extern FILE* yyin;
|
|
|
|
void yyerror(const char* s);
|
|
%}
|
|
|
|
%token T_PLUS T_MINUS T_MULTIPLY T_DIVIDE T_LPAREN T_RPAREN
|
|
%token T_NEWLINE T_QUIT T_INT
|
|
%start translation_unit
|
|
|
|
%%
|
|
|
|
translation_unit: expression {printf("%d\n",$1);}
|
|
| translation_unit expression
|
|
;
|
|
|
|
expression: T_INT {$$=$1;}
|
|
| expression T_PLUS T_INT {$$=$1+$3;}
|
|
;
|
|
|
|
%%
|
|
|
|
int main()
|
|
{
|
|
yyparse();
|
|
}
|
|
|
|
void yyerror(const char*s)
|
|
{
|
|
fprintf(stderr,"error: %s\n",s);
|
|
exit(1);
|
|
}
|