-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgramatica.y
More file actions
47 lines (37 loc) · 766 Bytes
/
gramatica.y
File metadata and controls
47 lines (37 loc) · 766 Bytes
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
35
36
37
38
39
40
41
42
43
44
45
46
47
%{
#include <stdio.h>
void yyerror(char *);
int yylex(void);
%}
%token NUMAR
%token ADAUGA SCADE INM IMP POZ CAT
%token TERMINAL
%token STPAR DRPAR PARS PARD
%left '-'
%%
cal: /* empty */
| cal expresie TERMINAL { printf("%d\n", $2);}
;
expresie: factor
| expresie ADAUGA factor { $$ = $1 + $3; }
| expresie SCADE factor { $$ = $1 - $3; }
;
factor: term
| factor INM term { $$ = $1 * $3; }
| factor IMP term { $$ = $1 / $3; }
| factor CAT term { $$ = $1 % $3; }
;
term: NUMAR
| POZ term { $$ = $2 >= 0? $2 : - $2; }
| STPAR expresie DRPAR { $$ = $2; }
| PARS expresie PARD { $$ = $2; }
;
%%
void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
}
int main(void)
{
yyparse();
return 0;
}