/
ulang_yecc.yrl
177 lines (148 loc) · 3.73 KB
/
ulang_yecc.yrl
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
Nonterminals program module_exp functions function args arg while_exp let_exp exp exps if_exp test_exp true_exp false_exp op_exp call_exp remote_call_exp stmt list_exp tail_exp export_exp fundecs fundec tuple_exp tuple_build.
Terminals '.' '(' ')' '->' '<-' '{' '}' '[' ']' '<' '>' '<=' '>=' ',' 'fn' 'let' 'if' 'then' 'else' 'module' ';' 'end' 'export' int atom char string var op.
Rootsymbol program.
program ->
module_exp: [ '$1' ].
program ->
module_exp exps: [ '$1' | '$2' ].
program ->
module_exp export_exp exps: [ '$1', '$2' | '$3' ].
program ->
exps: '$1'.
module_exp ->
'module' var:
{attribute, ?line('$2'),module, element(3,'$2')}.
export_exp ->
'export' '[' fundecs ']' :
{attribute,?line('$1'),export, '$3' }.
fundecs ->
fundec : [ '$1' ].
fundecs ->
fundec ',' fundecs :
[ '$1' | '$3' ].
fundec ->
'(' var ',' int ')' :
{element(3,'$2'), string_to_integer(element(3,'$4'))}.
functions ->
function functions:
[ '$1' | '$2' ].
functions ->
function : ['$1'].
function ->
'fn' var '(' args ')' '->' '{' exps '}' :
{function,?line('$1'),element(3,'$2'), length('$4'),
[{clause,?line('$1'),'$4',[],
'$8'
}]
}.
function ->
'fn' var '(' ')' '->' '{' exps '}' :
{function,?line('$1'),element(3,'$2'), 0,
[{clause,?line('$1'),[],[],
'$7'
}]
}.
args ->
arg ',' args :
[ '$1' | '$3' ].
args ->
arg : [ '$1' ].
arg ->
var : '$1'.
exps ->
exp :
[ '$1' ].
exps ->
exp ';' exps :
[ '$1' | '$3' ].
stmt ->
'{' exps '}'.
exp ->
function : '$1'.
exp ->
let_exp : '$1'.
exp ->
if_exp : '$1'.
exp ->
op_exp : '$1'.
exp ->
list_exp : '$1'.
exp ->
call_exp : '$1'.
exp ->
remote_call_exp : '$1'.
exp ->
tuple_exp : '$1'.
exp ->
int : {integer, ?line('$1'), string_to_integer(element(3,'$1'))}.
exp ->
string : '$1'.
exp ->
var : '$1'.
let_exp ->
'let' var '<-' exp :
{'match', ?line('$1'), '$2', '$4'}.
if_exp ->
'if' test_exp 'then' true_exp 'else' false_exp 'end':
{'case', ?line('$1'),
'$2',
[{'clause', ?line('$3'), [{atom, ?line('$3'),'true'}],
[],
'$4'},
{'clause', ?line('$5'), [{atom, ?line('$5'),'false'}],
[],
'$6'}]}.
test_exp ->
exp : '$1' .
true_exp ->
exps : '$1' .
false_exp ->
exps : '$1' .
op_exp ->
exp op exp :
{op, ?line('$1'), element(3,'$2'), '$1', '$3' }.
call_exp ->
var '(' ')':
{call, ?line('$1'),var_to_atom('$1'),nil}.
call_exp ->
var '(' exps ')' :
{call, ?line('$1'),var_to_atom('$1'),'$3'}.
remote_call_exp ->
var '.' var '(' ')' :
{call, ?line('$1'), {remote, ?line('$1'), var_to_atom('$1'), var_to_atom('$3')}, nil}.
remote_call_exp ->
var '.' var '(' exps ')' :
{call, ?line('$1'), {remote, ?line('$1'), var_to_atom('$1'), var_to_atom('$3')}, '$5'}.
list_exp ->
'[' ']': {nil, ?line('$1')}.
list_exp ->
'[' exp tail_exp:
{cons, ?line('$2'), '$2', '$3'}.
tail_exp ->
',' exp tail_exp:
{cons, ?line('$1'), '$2', '$3'}.
tail_exp ->
']':
{nil, ?line('$1')}.
tuple_exp ->
'(' ')' :
{tuple, ?line('$1'),[]}.
tuple_exp ->
'(' tuple_build ')' :
{tuple, ?line('$1'), '$2'}.
tuple_build ->
exp ',' tuple_build :
[ '$1' | '$3' ].
tuple_build ->
exp :
[ '$1' ].
Erlang code.
-define(line(Tup), element(2, Tup)).
line(Tup)-> element(2, Tup).
string_to_integer(Str) ->
case string:to_integer(Str) of
{Code,_} ->
Code
end.
var_to_atom({var,Line,V}) ->
{atom,Line,V}.