-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer2.erl
106 lines (92 loc) · 4.07 KB
/
lexer2.erl
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
-module(lexer2).
-export([iniciol2/1]).
-define(IN_RANGE(X, Start, End), (((Start) =< (X)) and ((X) =< (End)))).
-define(IS_DIGIT(X), ?IN_RANGE(X, $0, $9)).
-define(IS_LOWER(X), ?IN_RANGE(X, $a, $z)).
-define(IS_UPPER(X), ?IN_RANGE(X, $A, $Z)).
-define(IS_LETTER(X), (?IS_LOWER(X) or ?IS_UPPER(X))).
-define(IS_SPACE(X), ((X) == $ )).
-define(IS_SYMBOL(X), ((X) == $+)or ((X) == $-) or ((X) == $*) or ((X) == $/) or ((X) == $=) or
((X) == $%) or ((X) == $() or ((X) == $))or ((X) == $<)or ((X) == $>) or ((X) == $[) or
((X) == $]) or ((X) == $,) or ((X) == ${) or ((X) == $})).
iniciol2({_,Ren,Col,[_,_|T]})->
io:format("stateA~p~n~n", [T]),
stateA(T,Ren,Col+2,[]).
%% Escoge entre los diferentes caractéres posibles
stateA([H|T],Ren,Col,Result)->
if
(H == $}) and (hd(T) == $}) -> stateK( [H|T],Result);
?IS_SPACE(H) -> stateA(T, Ren, Col + 1, Result);
(H == $') -> stateB(T, Ren, Col + 1, Result, [H], Ren, Col);
(H == $") -> stateC(T, Ren, Col + 1, Result, [H], Ren, Col);
?IS_DIGIT(H) -> stateD(T, Ren, Col + 1, Result, [H], Ren, Col);
(H == $.) -> stateE(T, Ren, Col + 1, Result, [H], Ren, Col);
?IS_SYMBOL(H) -> stateF([H|T], Ren, Col, Result, Ren, Col);
?IS_LOWER(H) -> stateH(T, Ren, Col + 1, Result, [H], Ren, Col);
true -> throw(invalidsyntax)
end.
%% Revisa una cadena de string con comilla sencilla
stateB([H|T],Ren,Col,Result, Partial,R,C)->
if
(H == $') -> stateI(T, Ren, Col + 1, Result, [H|Partial], R, C, cadena);
(H == 10) -> stateB(T, Ren+1, 1, Result, [H|Partial], R, C);
true -> stateB(T, Ren, Col + 1, Result, [H|Partial], R, C)
end.
%% Revisa una cadena de string con comilla dolbe
stateC([H|T],Ren,Col,Result, Partial,R,C)->
if
(H == $") -> stateI(T, Ren, Col+ 1, Result, [H|Partial], R, C, cadena);
(H == 10) -> stateC(T, Ren+1, 1, Result, [H|Partial], R, C);
true -> stateC(T, Ren, Col + 1, Result, [H|Partial], R, C)
end.
%% Reivsa un número entero y un foltante
stateD([H|T],Ren,Col,Result, Partial,R,C)->
if
?IS_DIGIT(H) -> stateD(T, Ren, Col+1, Result, [H|Partial], R, C);
(H == $.) -> stateE(T, Ren, Col+1, Result, [H|Partial], R, C);
?IS_SYMBOL(H) or ?IS_SPACE(H)
or (H == $}) -> stateI([H|T], Ren, Col, Result, Partial, R, C, entero);
true -> throw(invalidsyntax)
end.
%% Concatena un número flotante
stateE([H|T],Ren,Col,Result, Partial,R,C)->
if
?IS_DIGIT(H) -> stateE(T, Ren, Col+1, Result, [H|Partial], R, C);
(H == $}) or
?IS_SYMBOL(H) or ?IS_SPACE(H) -> stateI([H|T], Ren, Col, Result, Partial, R, C, float);
true -> throw(invalidsyntax)
end.
%% Revisa caractéres
stateF([H|T],Ren,Col,Result,R,C)->
if
%(H == 40) -> stateI(T, Ren, Col+ 1, Result, [H], R, C, simbolo);
%(H == 41) -> stateI(T, Ren, Col+ 1, Result, [H], R, C, simbolo);
true -> stateI(T, Ren, Col+ 1, Result, [H], R, C, simbolo)
end.
%% Concatena identificadores
stateH([H|T],Ren,Col,Result, Partial,R,C)->
if
?IS_DIGIT(H) or ?IS_LETTER(H) or (H ==$_) -> stateH(T, Ren, Col + 1, Result, [H|Partial], R, C);
?IS_SPACE(H) or ?IS_SYMBOL(H) or
(H == $}) -> stateI([H|T], Ren, Col, Result, Partial, R, C, identificador);
true -> throw(invalidsyntax)
end.
%% Tolo de que esté es partial lo concatena a el resultado final
stateI([H|T], Ren, Col, Result, Partial, R, C, Type)->
stateA([H|T], Ren, Col, [{Type, R, C, lists:reverse(Partial)}|Result]).
%stateJ([H|T], Ren, Col, Result, Partial, R , C) ->
% if
% H == $n -> stateC(T, Ren + 1, Col, Result, [H|Partial], R, C);
% true -> stateC(T, Ren, Col + 1, Result, [H|Partial], R, C)
% end.
%% Estado que revisa el cierre final de la expresion y que termine en (}})
stateK([H|T],Result)->
if
(H == $}) and (hd(T) == $}) ->lists:reverse(Result);
true -> throw(invalidsyntax)
end.
%stateL([H|T], Ren, Col, Result, Partial, R, C ,Type) ->
% if
%H == $" -> stateI(T, Ren, Col + 1, Result, [H|Partial], R, C, Type);
%true ->stateJ(T, Ren, Col + 1, Result, [H|Partial], R, C, Type)
% end.