Consulta Analizador Léxico
Publicado por Jeremias (3 intervenciones) el 20/07/2013 04:00:50
Necesitaria que me revisen el siguiente codigo, sobre un analizador lexico, y me digan donde esta el error, ya que no me hace lo que tiene que hacer, es decir compila y todo pero siempre nos devuelve vacio como compolex y no ejecuta las demas funciones. La unit declaraciones que usamos en el analizador se las agrego tambien al comienzo. El analizador sintactico lo tengo hecho aparte, pero funciona correctamente. Muchas gracias.
---------------------------------------------------------------------------------------------------------------------------------------
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
unit declaraciones;
interface
uses crt;
type
archivo=file of char;
simbolos=(comienzo,fin,id,mientras,si,entero,entonces,leer,mostrar,pa,pc,hacer,es,sino,coma,puntocoma,mas,menos,por,divi,pot,constante,may,men,ig,dist,mayig,menig,andd,orr,nott,punto,epsilon,vacio,peso,PROGRAMA,LISTASENT,SENT,N,P,LISTASAL,M,EXPARIT,J,W,Z,X,T2,EXPLOG,A,D,EXPREL,OPREL,Y);
terminales=comienzo..peso;
variables=PROGRAMA..Y;
tdatol=simbolos;
tpunterol=^tnodo;
tnodo=record
info:simbolos;
sig:tpunterol;
end;
tlista=record
cab:tpunterol;
tam:cardinal;
end;
tas=array[variables,terminales] of tlista;
tdatop=simbolos;
tpunterop=^tnodop;
tnodop= record
info:tdatop;
sig: tpunterop;
end;
tpila= record
tope:tpunterop;
tam:cardinal;
end;
tdatotabla=simbolos;
tpunterotabla=^tnodotabla;
tnodotabla=record
lex:string;
comp:tdatotabla;
sig:tpunterotabla;
end;
ttabla= record
cab:tpunterotabla;
tam:cardinal;
end;
var
pila:tpila;
procedure crearlista (var l:tlista);
procedure insertarl(var l:tlista;x:tdatol);
function tamaniolista(l:tlista):cardinal;
procedure crearp(var p:tpila);
procedure insertarp(var p:tpila; x:tdatop);
procedure eliminarp(var p:tpila;var x:tdatop);
procedure apilarproducciones(var p:tpila;producciones:tlista);
procedure ruta(var l:string; var r:string);
procedure creartabla(var l:ttabla);
procedure agregartabla(var l:ttabla; lexema:string; comp:terminales);
procedure busqentabla(var l:ttabla; lexema:string;var comp:terminales; var aux:byte);
implementation
procedure crearlista (var l:tlista);
begin
l.cab:=nil;
l.tam:=0;
end;
procedure insertarl(var l:tlista;x:tdatol);
var aux,ant,act:tpunterol;
begin
inc(l.tam);
new(aux);
aux^.info:=x;
if (l.cab=nil) or (x<l.cab^.info) then
begin
aux^.sig:=l.cab;
l.cab:=aux;
end
else
begin
ant:=l.cab;
act:=l.cab^.sig;
while (act<>nil) and (act^.info <= x) do
begin
ant:=act;
act:=act^.sig;
end;
aux^.sig:=act;
ant^.sig:=aux;
end;
end;
function tamaniolista(l:tlista):cardinal;
begin
tamaniolista:=l.tam;
end;
procedure crearp(var p:tpila);
begin
p.tope:=nil;
p.tam:=0;
end;
procedure insertarp(var p:tpila; x:tdatop);
var aux:tpunterop;
begin
new(aux);
aux^.info:=x;
aux^.sig:=p.tope;
p.tope:=aux;
inc(p.tam);
end;
procedure eliminarp(var p:tpila;var x:tdatop);
var aux:tpunterop;
begin
x:=p.tope^.info;
aux:=p.tope;
p.tope:=aux^.sig;
dispose(aux);
dec(p.tam);
end;
procedure apilarproducciones(var p:tpila;producciones:tlista);
type
tvector=array[1..30] of simbolos;
var
i,t:byte;
vecaaux:tvector;
procedure barridol(l:tlista;var vector:tvector);
var act:tpunterol;
begin
act:=l.cab;
i:=1;
while (act <> nil) do
begin
vector[i]:=act^.info;
act:=act^.sig;
inc(i);
end;
end;
begin
barridol(producciones,vecaaux);
t:=tamaniolista(producciones);
for i:=t downto 1 do
if vecaaux[i]<>epsilon then
insertarp(p,vecaaux[i]);
end;
procedure ruta(var l:string; var r:string);
Var aux1,aux2:string;
begin
aux1:='C:\Users\MATIAS\Desktop\Proyecto\';
aux2:='.txt';
gotoxy(33,4);
readln(l);
gotoxy(18,7);
readln(r);
r:=aux1+r+aux2;
end;
procedure creartabla(var l:ttabla);
begin
l.cab:=nil;
l.tam:=0;
end;
procedure busqentabla(var l:ttabla; lexema:string;var comp:terminales; var aux:byte);
var act:tpunterotabla;
begin
aux:=1;
act:=l.cab;
while (act <> nil) do
begin
if act^.lex=lexema then
comp:=act^.comp
else
act:=act^.sig;
end;
if act=nil then
aux:=0
end;
procedure agregartabla(var l:ttabla; lexema:string;comp:terminales);
var aux,ant,act:tpunterotabla;
begin
inc(l.tam);
new(aux);
aux^.lex:=lexema;
aux^.comp:=comp;
if (l.cab=nil) or (lexema<l.cab^.lex) then
begin
aux^.sig:=l.cab;
l.cab:=aux;
end
else
begin
ant:=l.cab;
act:=l.cab^.sig;
while (act<>nil) and (act^.lex <=lexema) do
begin
ant:=act;
act:=act^.sig;
end;
aux^.sig:=act;
ant^.sig:=aux;
end;
end;
END.
---------------------------------------------------------------------------------------------------------------------------------------
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
unit analex;
interface
uses declaraciones;
procedure obtenersigcomplex(var fuente:archivo; var control:longint; var compolex:terminales; var lexema:string; ts:ttabla);
implementation
procedure esid(var fuente:archivo;var control:longint;var lexema:string;var compolex:terminales; var ts:ttabla;var a:byte);
Const
q0=0;
Type
Sigma=(Letra, Digito, Otro);
Q=0..3;
TipoDelta=Array[Q,Sigma] of Q;
Var
EstadoActual: Q;
Delta: TipoDelta;
b:byte;
caracter:char;
function CarASimb(Car:char):sigma;
Begin
Case car of
'a'..'z', 'A'..'Z':CarASimb:=Letra;
'0'..'9':CarASimb:=Digito;
else
CarASimb:=Otro
End;
End;
Begin
Delta[0,Letra]:=1;
Delta[0,Digito]:=2;
Delta[0,Otro]:=2;
Delta[1,letra]:=1;
Delta[1,digito]:=1;
Delta[1,otro]:=3;
Delta[2,letra]:=2;
Delta[2,digito]:=2;
Delta[2,otro]:=2;
lexema:='';
EstadoActual:=q0;
while (EstadoActual<>2) and (EstadoActual<>3) and not eof(fuente) do
begin
read(fuente,caracter);
EstadoActual:=Delta[EstadoActual, CarAsimb(Lexema[Control])];
inc(control);
seek(fuente,control);lexema:=lexema+caracter;inc(control);
end;
if estadoactual<>3 then
a:=0
else
begin
a:=1;
delete(lexema,length(lexema),1);
control:=filepos(fuente)-1;
busqentabla(ts,lexema,compolex,b);
if b=0 then
begin
compolex:=id;
agregartabla(ts,lexema,compolex);
end
end;
End;
function esentero(var fuente:archivo;var control:longint;var lexema:string):byte;
Const
q0=0;
Type
Sigma=(signo, Digito, Otro);
Q=0..4;
TipoDelta=Array[Q,Sigma] of Q;
Var
EstadoActual: Q;
Delta: TipoDelta;
caracter:char;
function CarASimb(Car:char):sigma;
Begin
Case car of
'0'..'9':CarASimb:=Digito;
'-': CarAsimb:=Signo
else
CarASimb:=Otro
End;
End;
Begin
Delta[0,Digito]:=1;
Delta[0,Signo]:=2;
Delta[0,Otro]:=3;
Delta[1,Digito]:=1;
Delta[1,Signo]:=3;
Delta[1,otro]:=4;
Delta[2,Digito]:=1;
Delta[2,otro]:=3;
Delta[2,signo]:=3;
Delta[3,digito]:=3;
Delta[3,signo]:=3;
Delta[3,otro]:=3;
EstadoActual:=q0;
lexema:='';
while (EstadoActual<>3) and (EstadoActual<>4)and not eof(fuente) do
begin
seek(fuente,control);
read(fuente,caracter);
lexema:=lexema+caracter;
EstadoActual:=Delta[EstadoActual, CarAsimb(Lexema[Control])];
inc(control);
end;
if EstadoActual=3 then
esentero:=0
else
begin
delete(lexema,length(lexema),1);
esentero:=1;
control:=filepos(fuente)-1;
end;
End;
procedure obtenersigcomplex(var fuente:archivo; var control:longint; var compolex:terminales; var lexema:string; ts:ttabla);
var
caracter:char;
c:byte;
begin
seek(fuente,control);
if eof(fuente) then
compolex:=peso
else
begin
read(fuente,caracter);
while (caracter in [#0..#32]) and not eof(fuente) do
begin
inc(control);
read(fuente,caracter);
end;
if eof(fuente) then
compolex:=peso
else
begin
if caracter in ['=',',','.','(',')',';','>','<','+','-','*','/','^','&','~','|'] then
case caracter of
'=': begin
compolex:=ig;
inc(control);
end;
';': begin
compolex:=puntocoma;
inc(control);
end;
',': begin
compolex:=coma;
inc(control);
end;
'.': begin
compolex:=punto;
inc(control);
end;
'(': begin
compolex:=pa;
inc(control);
end;
')': begin
compolex:= pc;
inc(control);
end;
'>': begin
compolex:= may;
inc(control);
end;
'<': begin
compolex:= men;
inc(control);
end;
'+': begin
compolex:= mas;
inc(control);
end;
'-': begin
compolex:= menos;
inc(control);
end;
'*': begin
compolex:=por;
inc(control);
end;
'/': begin
compolex:=divi;
inc(control);
end;
'^': begin
compolex:=pot;
inc(control);
end;
'&': begin
compolex:=andd;
inc(control);
end;
'~': begin
compolex:=nott;
inc(control);
end;
'|': begin
compolex:=orr;
inc(control);
end;
end
else
begin
esid(fuente,control,lexema,compolex,ts,c);
if c=1 then
compolex:=compolex
else
begin
control:=control-1;
if esentero(fuente,control,lexema)=1 then
begin
lexema:=lexema;
compolex:=constante;
end;
end;
end;
end;
end;
end;
END.
Valora esta pregunta


0