나 대학교 2학년때 만든 코드..
y.tab.h ---------------------
#define YDUHAGI 257
#define YBBGI 258
#define YGOPHAGI 259
#define YNANUGI 260
#define YCONSTENT 261
#define YLEFT 262
#define YRIGHT 263
#define YNEWLINE 264
-------------------------------
cal.l ------------------------------
%{
#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.y -------------------------------
%{
#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);
}
-----------------------------------------------------
댓글 0