%{ #include #include #define YYDEBUG 1 %} %token DIGIT %% lines : lines line | ; line : expr '\n' { printf("%d\n",$1); } ; expr : expr '+' term { $$ = $1 + $3; } | expr '-' term { $$ = $1 - $3; } | term ; term : term '*' factor { $$ = $1 * $3; } | term '/' factor { $$ = $1 / $3; } | term '%' factor { $$ = $1 % $3; } | factor ; factor : '(' expr ')' { $$ = $2; } | DIGIT ; %% yylex(){ register c; c = getchar(); if (isdigit(c)) { yylval = c-'0'; return DIGIT; } return c; } yyerror(s) char *s; { fprintf(stderr,"%s\n",s); exit(1); } extern int yydebug; main(argc,argv)char *argv[];{ yydebug=(argc>1 ? atoi(argv[1]) : 0); yyparse(); }