/***************** cal.l start **********************/
%{
#include \"y.tab.h\"
#include <stdlib.h>
#define token(x) x
extern int yylval;
%}
%%
\"-\" { return(YBBGI);}
\"*\" { return(YGOPHAGI);}
\"+\" { return(YDUHAGI);}
\"/\" { return(YNANUGI);}
[\\t\\n]+ { return(YNEWLINE); }
[ ]+ { ; }
[0-9][0-9]* { yylval = atoi(yytext); return(YCONSTENT);}
\"(\" { return(YLEFT);}
\")\" { return(YRIGHT);}
%%
/***************** cal.l end **********************/
/***************** cal.y start **********************/
%{
#include <stdio.h>
extern void yyerror(char *);
%}
%token YDUHAGI
%token YBBGI
%token YGOPHAGI
%token YNANUGI
%token YCONSTENT
%token YLEFT
%token YRIGHT
%token YNEWLINE
%left YDUHAGI YBBGI
%left YGOPHAGI YNANUGI
%start calculator
%%
calculator
: calculator expression YNEWLINE {printf(\"\\n결과 : %d\\n\", $2);}
|
;
expression
: expression YDUHAGI expression { $$ = $1 + $3; }
| expression YBBGI expression { $$ = $1 - $3; }
| expression YNANUGI expression { $$ = $1 / $3; }
| expression YGOPHAGI expression { $$ = $1 * $3; }
| YLEFT expression YRIGHT { $$ = $2; }
| YCONSTENT
;
%%
extern FILE *yyin;
main()
{
// while(!feof(yyin)){
yyparse();
// }
}
void yyerror(s)
char *s;
{
fprintf(stderr, \"%s\\n\", s);
}
/***************** cal.y end **********************/
사용방법은
간단해요 ^^
이게먼가연