-
Notifications
You must be signed in to change notification settings - Fork 0
/
sintatico.y
75 lines (62 loc) · 1.38 KB
/
sintatico.y
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int yylineno;
%}
%start program
%token LET INTEGER IN
%token SKIP IF THEN ELSE FI END WHILE DO READ WRITE
%token ASSGNOP
%token NUMBER
%token IDENTIFIER
%left '<' '>' '='
%left '-' '+'
%left '*' '/'
%right '^'
%left UMINUS
%define parse.error detailed
%%
program: LET declarations IN commands END { printf ("Programa sintaticamente correto!\n"); } ;
declarations: /* empty */
| INTEGER id_seq IDENTIFIER '.'
;
id_seq: /* empty */
| id_seq IDENTIFIER ','
;
commands: /* empty */
| commands command ';'
;
command: SKIP
| READ IDENTIFIER
| WRITE exp
| IDENTIFIER ASSGNOP exp
| IF exp THEN commands ELSE commands FI
| WHILE exp DO commands END
;
exp: NUMBER
| IDENTIFIER
| exp '<' exp
| exp '=' exp
| exp '>' exp
| exp '+' exp
| exp '-' exp
| exp '*' exp
| exp '/' exp
| exp '^' exp
| '-' exp %prec UMINUS
| '(' exp ')'
;
%%
int main(int argc, char *argv[]) {
extern FILE *yyin;
++argv; --argc;
yyin = fopen(argv[0], "r");
//yydebug = 1;
printf("Analisando arquivo '%s'...\n", argv[0]);
yyparse();
return 0;
}
yyerror(char* s) {
fprintf(stderr, "Erro encontrado na analise sintatica\n Linha do erro: %d\n Descricao do erro: %s\n", yylineno, s);
}