aboutsummaryrefslogtreecommitdiff
path: root/07/main.y
blob: 26a2007cd9aa6ca14685ba52c5f5b1f96a182a86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
int yyerror(char *);
%}

%token NUM ADD SUB MUL DIV LPAR RPAR
%left ADD SUB MUL DIV
%right NEG

%%

S:
 E { printf("\n"); };
E:
 E ADD E   { printf("+ "); }
 | E MUL E { printf("* "); }
 | E SUB E { printf("- "); }
 | E DIV E { printf("/ "); }
 | LPAR E RPAR
 | SUB E %prec NEG { printf("- "); }
 | NUM { printf("%d ", yylval); }
 ;
%%

int main() {
    yyparse();
}

int yyerror(char *s) {
    printf("error: %s", s);
    exit(0);
}