Skip to content

Commit f9ab5e9

Browse files
authored
Bison bugante na hora do make
1 parent e26a0a6 commit f9ab5e9

File tree

11 files changed

+571
-126
lines changed

11 files changed

+571
-126
lines changed

Makefile

+7-28
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,18 @@
11
LOADLIBES= -lfl -lreadline
22
CFLAGS=-Wall -g
33

4-
all: jogo
5-
64
%.c: %.y
75
bison $<
86
mv $*.tab.c $*.c
97

10-
jogo: jogo.o avent.o aventl.o traduzindo.o acoes.o salas.o hash.o lista.o elemento.o
11-
gcc -o jogo jogo.o avent.o aventl.o traduzindo.o acoes.o salas.o hash.o lista.o elemento.o
12-
13-
jogo.o: jogo.c traduzindo.h
14-
gcc -o jogo.o -c jogo.c -Wall
15-
16-
avent.c : avent.y
17-
18-
aventl.o: aventl.l avent.c
19-
20-
traduzindo.o: traduzindo.c acoes.h
21-
gcc -o traduzindo.o -c traduzindo.c -Wall
8+
jogo: main.o jogo.o jogol.o traduzindo.o acoes.o salas.o hash.o lista.o elemento.o
229

23-
acoes.o: acoes.c salas.h
24-
gcc -o acoes.o -c acoes.c -Wall
10+
jogo.c : jogoy.y
2511

26-
salas.o: salas.c hash.h
27-
gcc -o salas.o -c salas.c -Wall
28-
29-
hash.o: hash.c lista.h
30-
gcc -o hash.o -c hash.c -Wall
31-
32-
lista.o: lista.c elemento.h
33-
gcc -o lista.o -c lista.c -Wall
34-
35-
elemento.o: elemento.c
36-
gcc -o elemento.o -c elemento.c -Wall
12+
jogol.o: jogol.l jogo.c
3713

3814
clean:
39-
rm -rm *.o
15+
rm -f avent *tab* *.o *~ jogo.c jogo.tgz
16+
17+
dist:
18+
tar czf jogo.tgz jogol.l jogoy.y traduzindo.c traduzindo.h main.c acoes.c acoes.h salas.c salas.h hash.c hash.h lista.c lista.h elemento.c elemento.h Makefile

acoes.c

+10-7
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ int Mover(Elemento* e1, int direcao){
4242

4343

4444
int Examinar(Elemento* e1, Elemento* e2){
45-
if(e1->visivel)
45+
if(e1->visivel){
4646
if(e1->conhecido) puts(e1->curta);
4747
else{
4848
puts(e1->longa);
4949
e1->conhecido = True;
5050
}
51+
}
5152
return e1->visivel;
5253
}
5354

@@ -146,9 +147,10 @@ int Estourar(Elemento* e1, Elemento* e2){
146147
}
147148

148149
int Tocar(Elemento* e1, Elemento* e2){
149-
if(Tbusca(e1->conteudo,e2->nome)==e2)
150+
if(Tbusca(e1->conteudo,e2->nome)==e2){
150151
printf("O disco já está no gramofone\n");
151152
return 0;
153+
}
152154
return (Tinsere(e1->conteudo, e2));
153155
}
154156

@@ -244,10 +246,10 @@ int Comer(Elemento* e1, Elemento* e2){
244246
}
245247

246248
int Deitar(Elemento* e1, Elemento* e2){
247-
if(e1->nome == "cama" && e1->def.objeto.lista[deitado].val == 1){
249+
if(stringsIguais(e1->nome, "cama") && e1->def.objeto.lista[deitado].val == 1){
248250
printf("Você já está deitado.\n"); return 0;
249251
}
250-
else if(e1->nome == "cama"){
252+
else if(stringsIguais(e1->nome, "cama")){
251253
printf("Você se deita na cama e sente como se seu corpo tivesse sido transportado aos céus. Todos seus músculos relaxam, e sua mente fica leve. 'Poderia ficar aqui para sempre', pensa.\n");
252254
e1->def.objeto.lista[deitado].val = 1;
253255
return 1;
@@ -256,12 +258,12 @@ int Deitar(Elemento* e1, Elemento* e2){
256258
}
257259

258260
int Levantar(Elemento* e1, Elemento* e2){
259-
if(e1->nome == "cama" && e1->def.objeto.lista[deitado].val == 1){
261+
if(stringsIguais(e1->nome, "cama") && e1->def.objeto.lista[deitado].val == 1){
260262
printf("Com muito esforço, você cria a determinação para levantar da cama. Seu corpo se sente pesado.\n");
261263
e1->def.objeto.lista[deitado].val = 0;
262264
return 1;
263265
}
264-
else if(e1->nome == "cama" && e1->def.objeto.lista[deitado].val == 0){
266+
else if(stringsIguais(e1->nome, "cama") && e1->def.objeto.lista[deitado].val == 0){
265267
printf("Você já está levantado.\n");
266268
return 0;
267269
}
@@ -288,10 +290,11 @@ int Soltar(Elemento* e1, Elemento* e2){
288290
}
289291

290292
int Quebrar(Elemento* e1, Elemento* e2){
291-
if(e1->ativo)
293+
if(e1->ativo){
292294
e1->ativo = False;
293295
printf("%s quebrado com sucesso", e1->nome);
294296
return 1;
297+
}
295298
printf("Não dá pra quebrar o que já está quebrado...");
296299
return 0;
297300
}

hash.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ void Tdestroi(TabSim t){
3636
return;
3737
}
3838

39-
int Tvazia(TabSim t){
40-
Elo* crawler = t.listas[hash_val].cabec;
41-
if(crawler == NULL) return 1;
42-
return 0;
39+
int Tvazia(TabSim t, int tamanho){
40+
for(int i = 0; i < tamanho; i++){
41+
Elo* crawler = t.listas[i].cabec;
42+
if(crawler != NULL) return 0;
43+
}
44+
return 1;
4345
}
4446

4547
int Tinsere(TabSim t, Elemento* val){

jogol.l

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
%{
2+
#include "acoes.h"
3+
#include "jogo.tab.h"
4+
5+
/* Para readline */
6+
#include <readline/readline.h>
7+
#include <readline/history.h>
8+
9+
/* Redefinição da entrada do Flex para usar o readline */
10+
#define YY_INPUT(buf,result,max_size) result = mygetinput(buf, max_size);
11+
12+
13+
/* Função que substitui a entrada */
14+
static int mygetinput(char *buf, int size) {
15+
char *line;
16+
/* final de arquivo */
17+
if (feof(yyin)) return YY_NULL;
18+
19+
/* Lê uma linha, com o prompt "> " */
20+
line = readline("> ");
21+
if(!line) return YY_NULL;
22+
23+
/* segurança */
24+
if(strlen(line) > size-2){
25+
fprintf(stderr,"input line too long\n");
26+
return YY_NULL;
27+
}
28+
29+
/* copia para o buffer de enrtada */
30+
sprintf(buf,"%s\n",line);
31+
/* adiciona ao histórico */
32+
add_history(line);
33+
34+
/* libera memória */
35+
free(line);
36+
return strlen(buf);
37+
}
38+
%}
39+
40+
/* Não importa se maiúsculas ou minúsculas */
41+
%option caseless
42+
43+
44+
/* em unicode, á são dois caracteres, veja as regras para INVENT e VAPARA */
45+
46+
%%
47+
48+
(fim|Chega) { return FIM;}
49+
50+
<<EOF>> { puts("Bye bye..."); return FIM; }
51+
52+
53+
i(nvent((a|á|Á)rio)?)? { return INVENT;}
54+
55+
v(á|Á|a)[[:space:]]+para {
56+
return VAPARA;}
57+
58+
(N|Norte|NORTE) { return NORTE;}
59+
(S|Sul|SUL) { return SUL;}
60+
(L|Leste|LESTE) { return LESTE;}
61+
(W|Oeste|OESTE) { return OESTE; /* W no lugar de O para evitar confusão com o artigo 'o'*/ }
62+
63+
64+
[ao]|uma?
65+
66+
d[aoe]|em|n[ao]
67+
68+
[[:alpha:]]+ {
69+
/* identificador */
70+
int tipo;
71+
if ((tipo = LBuscaTipoGlobal(sym_table, yytext)) != -1) {
72+
yylval.tptr = LBuscaGlobal(sym_table, yytext);
73+
74+
return tipo;
75+
}
76+
else {
77+
yylval.str = yytext;
78+
return DESC;
79+
}
80+
}
81+
82+
[ \t]+
83+
[\n;] {/* ';' também termina linhas */return EOL;}
84+
85+
86+
. {yylval.str = yytext; return DESC;}

jogoy.y

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
%{
2+
#include <stdio.h>
3+
//#include "lista.h"
4+
//#include "salas.h"
5+
#include "acoes.h"
6+
//#include "elemento.h"
7+
8+
int yylex();
9+
int yyerror(char *);
10+
11+
%}
12+
13+
14+
/* Declaracoes */
15+
%union {
16+
void *tptr;
17+
char *str;
18+
int direc;
19+
}
20+
21+
%token <tptr> VERBO OBJ LUGAR
22+
23+
/* DESC representa uma palavra desconhecida */
24+
%token <str> DESC
25+
%token NORTE SUL LESTE OESTE VAPARA EOL FIM INVENT
26+
27+
%type <direc> dir
28+
%type <tptr> obj
29+
30+
%defines
31+
32+
%%
33+
/* Gramatica */
34+
35+
36+
input: EOL { printf("Zzzz...\n"); }
37+
| cmd
38+
| VAPARA {
39+
/* movimentação */
40+
printf("Seguindo para ");
41+
}
42+
dir {
43+
if ($3 >= 0 && atual->def.lugar.saidas[$3]) {
44+
atual = atual->def.lugar.saidas[$3];
45+
Examinar(NULL,NULL);
46+
}
47+
else puts("Não há passagem....");
48+
} eol
49+
| dir {
50+
/* movimentação */
51+
if ($1 >= 0 && atual->def.lugar.saidas[$1]) {
52+
atual = atual->def.lugar.saidas[$1];
53+
printf("Você foi para %s\n", atual->nome);
54+
Examinar(atual,NULL);
55+
}
56+
else puts("Não há passagem....");
57+
} eol
58+
59+
| INVENT {
60+
/* listagem do inventário */
61+
if (Tvazia(personagem.conteudo))
62+
puts("Você está sem nada no momento...");
63+
else {
64+
puts("Você tem:");
65+
imprimeConteudo(personagem, 4);
66+
}
67+
68+
} eol
69+
| FIM { return 0;}
70+
| DESC { puts("Nada do que você diz está fazendo sentido.");}
71+
| error eol;
72+
;
73+
74+
cmd: VERBO {
75+
/* Intransitivo */
76+
//Busca o verbo na lista e realiza a acao;
77+
fptr acao = (fptr) LBuscaGlobal(sym_table, $1);
78+
acao(NULL, NULL);
79+
} eol
80+
| VERBO obj {
81+
/* Transitivo direto */
82+
//Busca o verbo e o objeto
83+
fptr acao = (fptr) LBuscaGlobal(sym_table, $1);
84+
Elemento *e = (Elemento*) LBuscaGlobal(sym_table, $2);
85+
acao(e, NULL);
86+
} eol
87+
| VERBO obj obj {
88+
/* Bitransitivo */
89+
//Busca o verbo e os dois objetos
90+
fptr acao = (fptr) LBuscaGlobal(sym_table, $1);
91+
Elemento *e = (Elemento*) LBuscaGlobal(sym_table, $2);
92+
Elemento *e2 = (Elemento*) LBuscaGlobal(sym_table, $3);
93+
acao(e, e2);
94+
} eol
95+
| VERBO DESC {
96+
printf("%s??\n", $2);
97+
} eol
98+
| VERBO obj DESC {
99+
printf("não sei o que é isso: %s\n", $3);
100+
} eol
101+
| VERBO DESC DESC {
102+
printf("Pare de jogar e vá descansar um pouco\n"
103+
"Fazer isso com %s e %s, que coisa\n", $2,$3
104+
);
105+
} eol
106+
;
107+
108+
obj: OBJ { $$ = $1;}
109+
| LUGAR { $$ = $1;}
110+
111+
dir: NORTE { puts("norte"); $$=0;}
112+
| SUL { puts("sul"); $$=1;}
113+
| LESTE { puts("leste"); $$=2;}
114+
| OESTE { puts("oeste"); $$=3;}
115+
| DESC { puts("... (onde é isso?)"); $$=-1;}
116+
;
117+
118+
eol: EOL {return 1;}
119+
%%
120+
121+
int yyerror(char *s) {
122+
puts("Não entendi...");
123+
return 0;
124+
}

0 commit comments

Comments
 (0)