Skip to content

Commit d81776f

Browse files
Create simplecalc.y
1 parent da789fc commit d81776f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
%{
2+
#include <stdio.h>
3+
int yylex();
4+
void yyerror (char const *s) {
5+
fprintf (stderr, "%s\n", s);
6+
}
7+
%}
8+
9+
10+
%token NAME NUMBER
11+
%%
12+
13+
statement: NAME '=' expression {printf("%c = %d\n", $1,$3);}
14+
| expression {printf("= %d\n", $1);}
15+
;
16+
expression: expression '+' NUMBER { $$ = $1 + $3;}
17+
| expression '-' NUMBER { $$ = $1 - $3;}
18+
| expression '*' NUMBER { $$ = $1 * $3;}
19+
| '(' expression ')' {$$ = $2;}
20+
| NUMBER {$$ = $1;}
21+
;
22+
23+
%%
24+
25+
int main(){
26+
yyparse();
27+
return 0;
28+
}

0 commit comments

Comments
 (0)