aboutsummaryrefslogtreecommitdiff
path: root/10/main.y
blob: f45e23f454ddd9277cf631382d5a3b5b0be3c477 (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
%{
#include <stdio.h>
#include <stdlib.h>
int yylex();
int yyerror(char *);
%}

%token ADD SUB MUL DIV NUM
%left ADD SUB MUL DIV

%%
R: E {
    printf("result: %d", $$);
    return 0;
 }
E: E ADD T { $$ = $1 + $3; }
 | E SUB T { $$ = $1 - $3; }
 | T
 ;
T: T MUL NUM { $$ = $1 * $3; }
 | T DIV NUM { $$ = $1 / $3; }
 | NUM { $$ = $1; }
 ;
%%

int main() {
    yyparse();
}

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