Skip to content

Commit b93b44c

Browse files
Update parser.y
Sprint 3 — Suporte Sintático Essencial
1 parent 23be9fb commit b93b44c

File tree

1 file changed

+123
-42
lines changed

1 file changed

+123
-42
lines changed

src/parser.y

Lines changed: 123 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,158 @@
55

66
int yylex(void);
77
void yyerror(const char *s);
8-
98
%}
109

10+
/* ---------- União de valores ---------- */
1111
%union {
1212
int intValue;
1313
float floatValue;
14-
char *strValue; // Manipula strings corretamente
1514
char *id;
1615
}
1716

18-
%type <intValue> expr
17+
/* ---------- Tokens ---------- */
1918
%token <intValue> NUM
2019
%token <floatValue> FLOAT
2120
%token <id> IDENT
22-
%token SEMI
21+
22+
%token INT FLOAT_TYPE BOOL CHAR
2323
%token TRUE FALSE
24-
%token PLUS MINUS TIMES DIVIDE MOD
24+
%token IF ELSE WHILE FOR RETURN PRINT
2525
%token EQ NEQ LT LEQ GT GEQ
2626
%token AND OR NOT
27-
%token IF ELSE WHILE DO RETURN PRINT
28-
%token LPAREN RPAREN
29-
%token END
27+
%token ASSIGN
28+
%token PLUS MINUS TIMES DIVIDE MOD
29+
%token LBRACE RBRACE LPAREN RPAREN
30+
%token SEMICOLON COMMA
3031

32+
/* ---------- Precedência ---------- */
33+
%left OR
34+
%left AND
35+
%left EQ NEQ
36+
%left LT LEQ GT GEQ
3137
%left PLUS MINUS
3238
%left TIMES DIVIDE MOD
33-
%left POW
34-
%left AND OR
3539
%right NOT
40+
%right UMINUS
3641

37-
%start input
42+
/* ---------- Tipos ---------- */
43+
%type <intValue> expr
44+
%type <intValue> stmt
3845

3946
%%
4047

41-
input:
42-
/* vazio */
43-
| expr SEMI { /* Trata a expressão seguida de ponto e vírgula */ }
44-
| PRINT expr SEMI { /* Trata o comando print */ }
45-
| IF LPAREN expr RPAREN input ELSE input { /* Condicional if/else */ }
46-
| WHILE LPAREN expr RPAREN DO input END { /* Loop while com do...end */ }
47-
| RETURN expr SEMI { /* Comando return */ }
48-
| error SEMI { yyerror("Erro sintático: Esperado expressão após ';'"); }
48+
/* ---------- Programa e estrutura ---------- */
49+
program:
50+
stmt_list
51+
;
52+
53+
/* Lista de comandos */
54+
stmt_list:
55+
stmt_list stmt
56+
| /* vazio */
57+
;
58+
59+
/* ---------- Comandos principais ---------- */
60+
stmt:
61+
decl_stmt
62+
| assign_stmt
63+
| if_stmt
64+
| while_stmt
65+
| for_stmt
66+
| print_stmt
67+
| block
68+
| RETURN expr SEMICOLON
69+
;
70+
71+
/* Declarações */
72+
decl_stmt:
73+
type IDENT ASSIGN expr SEMICOLON
74+
{ printf("Declaração: %s = %d\n", $2, $4); }
75+
| type IDENT SEMICOLON
76+
{ printf("Declaração: %s\n", $2); }
77+
;
78+
79+
/* Atribuição */
80+
assign_stmt:
81+
IDENT ASSIGN expr SEMICOLON
82+
{ printf("Atribuição: %s = %d\n", $1, $3); }
4983
;
5084

85+
/* If / Else */
86+
if_stmt:
87+
IF LPAREN expr RPAREN stmt
88+
{ printf("If condicional simples\n"); }
89+
| IF LPAREN expr RPAREN stmt ELSE stmt
90+
{ printf("If/Else condicional\n"); }
91+
;
92+
93+
/* While */
94+
while_stmt:
95+
WHILE LPAREN expr RPAREN stmt
96+
{ printf("Loop While\n"); }
97+
;
98+
99+
/* For */
100+
for_stmt:
101+
FOR LPAREN assign_stmt expr SEMICOLON assign_stmt RPAREN stmt
102+
{ printf("Loop For\n"); }
103+
;
104+
105+
/* Print */
106+
print_stmt:
107+
PRINT LPAREN expr RPAREN SEMICOLON
108+
{ printf("Print encontrado\n"); }
109+
;
110+
111+
/* Blocos */
112+
block:
113+
LBRACE stmt_list RBRACE
114+
{ printf("Bloco de comandos\n"); }
115+
;
116+
117+
/* Tipos */
118+
type:
119+
INT
120+
| FLOAT_TYPE
121+
| BOOL
122+
| CHAR
123+
;
124+
125+
/* ---------- Expressões ---------- */
51126
expr:
52-
expr PLUS expr { $$ = $1 + $3; }
53-
| expr MINUS expr { $$ = $1 - $3; }
54-
| expr TIMES expr { $$ = $1 * $3; }
55-
| expr DIVIDE expr {
56-
if ($3 == 0) {
57-
yyerror("Erro semântico: Divisão por zero");
58-
}
59-
$$ = $1 / $3;
60-
}
61-
| expr MOD expr { $$ = $1 % $3; }
62-
| LPAREN expr RPAREN { $$ = $2; }
63-
| NUM { $$ = $1; }
64-
| FLOAT { $$ = $1; }
65-
| TRUE { $$ = 1; }
66-
| FALSE { $$ = 0; }
67-
| IDENT { $$ = 0; }
68-
| NOT expr { $$ = !$2; }
69-
| expr EQ expr { $$ = $1 == $3; }
70-
| expr NEQ expr { $$ = $1 != $3; }
71-
| expr LT expr { $$ = $1 < $3; }
72-
| expr LEQ expr { $$ = $1 <= $3; }
73-
| expr GT expr { $$ = $1 > $3; }
74-
| expr GEQ expr { $$ = $1 >= $3; }
127+
expr PLUS expr { $$ = $1 + $3; }
128+
| expr MINUS expr { $$ = $1 - $3; }
129+
| expr TIMES expr { $$ = $1 * $3; }
130+
| expr DIVIDE expr { $$ = $1 / $3; }
131+
| expr MOD expr { $$ = $1 % $3; }
132+
| expr EQ expr { $$ = ($1 == $3); }
133+
| expr NEQ expr { $$ = ($1 != $3); }
134+
| expr LT expr { $$ = ($1 < $3); }
135+
| expr LEQ expr { $$ = ($1 <= $3); }
136+
| expr GT expr { $$ = ($1 > $3); }
137+
| expr GEQ expr { $$ = ($1 >= $3); }
138+
| expr AND expr { $$ = ($1 && $3); }
139+
| expr OR expr { $$ = ($1 || $3); }
140+
| NOT expr { $$ = !$2; }
141+
| MINUS expr %prec UMINUS { $$ = -$2; }
142+
| LPAREN expr RPAREN { $$ = $2; }
143+
| NUM { $$ = $1; }
144+
| FLOAT { $$ = (int)$1; }
145+
| TRUE { $$ = 1; }
146+
| FALSE { $$ = 0; }
147+
| IDENT { printf("Uso de variável: %s\n", $1); $$ = 0; }
75148
;
76149

77150
%%
78151

152+
/* ---------- Funções auxiliares ---------- */
153+
int main(void) {
154+
printf("Iniciando análise sintática...\n");
155+
yyparse();
156+
printf("Análise concluída.\n");
157+
return 0;
158+
}
159+
79160
void yyerror(const char *s) {
80161
fprintf(stderr, "Erro sintático: %s\n", s);
81162
}

0 commit comments

Comments
 (0)