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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
%% -*-Erlang-*-
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1999-2009. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%% Core Erlang YECC parser grammar
%% Have explicit productions for annotated phrases named anno_XXX.
%% This just does an XXX and adds the annotation.
Expect 1.
Nonterminals
module_definition module_export module_attribute module_defs
exported_names exported_name
attribute_list attribute
function_definition function_definitions
constant constants atomic_constant tuple_constant cons_constant tail_constant
other_pattern atomic_pattern tuple_pattern cons_pattern tail_pattern
binary_pattern segment_patterns segment_pattern
expression single_expression
literal literals atomic_literal tuple_literal cons_literal tail_literal
nil tuple cons tail
binary segments segment
let_expr let_vars letrec_expr case_expr fun_expr
function_name
application_expr call_expr primop_expr arg_list
receive_expr timeout try_expr
sequence catch_expr
variable clause clause_pattern
map_expr map_pairs map_pair map_pair_assoc map_pair_exact
map_pattern map_pair_patterns map_pair_pattern
annotation anno_fun anno_expression anno_expressions
anno_variable anno_variables anno_pattern anno_patterns
anno_function_name
anno_clause anno_clauses.
Terminals
%% Separators
'(' ')' '{' '}' '[' ']' '|' ',' '->' '=' '/' '<' '>' ':' '-|' '#' '~' '::'
%% Keywords (atoms are assumed to always be single-quoted).
'module' 'attributes' 'do' 'let' 'in' 'letrec'
'apply' 'call' 'primop'
'case' 'of' 'end' 'when' 'fun' 'try' 'catch' 'receive' 'after'
%% Literal tokens (provided by the tokeniser):
char integer float atom string var.
%% Literal tokens NOT provided by the tokenise:
nil -> '[' ']' : {nil,tok_line('$1')}.
%% Declare the start rule for parsing
Rootsymbol module_definition.
%% Grammar
module_definition ->
'module' atom module_export module_attribute module_defs 'end' :
#c_module{name=#c_literal{val=tok_val('$2')},exports='$3',
attrs='$4',defs='$5'}.
module_definition ->
'(' 'module' atom module_export module_attribute module_defs 'end'
'-|' annotation ')' :
#c_module{anno='$9',name=tok_val('$3'),exports='$4',
attrs='$5',defs='$6'}.
module_export -> '[' ']' : [].
module_export -> '[' exported_names ']' : '$2'.
exported_names -> exported_name ',' exported_names : ['$1' | '$3'].
exported_names -> exported_name : ['$1'].
exported_name -> function_name : '$1'.
module_attribute -> 'attributes' '[' ']' : [].
module_attribute -> 'attributes' '[' attribute_list ']' : '$3'.
attribute_list -> attribute ',' attribute_list : ['$1' | '$3'].
attribute_list -> attribute : ['$1'].
attribute -> atom '=' literal :
{#c_literal{val=tok_val('$1')},'$3'}.
module_defs -> function_definitions : '$1'.
annotation -> '[' ']' : [].
annotation -> '[' constants ']' : '$2'.
function_definitions ->
function_definition function_definitions : ['$1' | '$2'].
function_definitions ->
'$empty' : [].
function_definition ->
anno_function_name '=' anno_fun :
{'$1','$3'}.
anno_fun -> '(' fun_expr '-|' annotation ')' :
core_lib:set_anno('$2', '$4').
anno_fun -> fun_expr : '$1'.
%% Constant terms for annotations and attributes.
%% These are represented by straight unabstract Erlang.
constant -> atomic_constant : '$1'.
constant -> tuple_constant : '$1'.
constant -> cons_constant : '$1'.
constants -> constant ',' constants : ['$1' | '$3'].
constants -> constant : ['$1'].
atomic_constant -> char : tok_val('$1').
atomic_constant -> integer : tok_val('$1').
atomic_constant -> float : tok_val('$1').
atomic_constant -> atom : tok_val('$1').
atomic_constant -> string : tok_val('$1').
atomic_constant -> nil : [].
tuple_constant -> '{' '}' : {}.
tuple_constant -> '{' constants '}' : list_to_tuple('$2').
cons_constant -> '[' constant tail_constant : ['$2'|'$3'].
tail_constant -> ']' : [].
tail_constant -> '|' constant ']' : '$2'.
tail_constant -> ',' constant tail_constant : ['$2'|'$3'].
%% Patterns
%% We have to be a little sneaky here as we would like to be able to
%% do:
%% V = {a}
%% ( V = {a} -| <anno> )
%% ( V -| <anno> ) = {a}
%% V = ( {a} -| <anno> )
%% ( ( V -| <anno> ) = ( {a} -| <anno> ) -| <anno> )
anno_pattern -> '(' other_pattern '-|' annotation ')' :
core_lib:set_anno('$2', '$4').
anno_pattern -> other_pattern : '$1'.
anno_pattern -> anno_variable : '$1'.
anno_patterns -> anno_pattern ',' anno_patterns : ['$1' | '$3'].
anno_patterns -> anno_pattern : ['$1'].
other_pattern -> atomic_pattern : '$1'.
other_pattern -> tuple_pattern : '$1'.
other_pattern -> map_pattern : '$1'.
other_pattern -> cons_pattern : '$1'.
other_pattern -> binary_pattern : '$1'.
other_pattern -> anno_variable '=' anno_pattern :
#c_alias{var='$1',pat='$3'}.
atomic_pattern -> atomic_literal : '$1'.
tuple_pattern -> '{' '}' : c_tuple([]).
tuple_pattern -> '{' anno_patterns '}' : c_tuple('$2').
map_pattern -> '~' '{' '}' '~' : #c_map{es=[]}.
map_pattern -> '~' '{' map_pair_patterns '}' '~' :
#c_map{es=lists:sort('$3')}.
map_pair_patterns -> map_pair_pattern : ['$1'].
map_pair_patterns -> map_pair_pattern ',' map_pair_patterns : ['$1' | '$3'].
map_pair_pattern -> '~' '<' anno_pattern ',' anno_pattern '>' :
#c_map_pair{op=#c_literal{val=exact},key='$3',val='$5'}.
cons_pattern -> '[' anno_pattern tail_pattern :
#c_cons{hd='$2',tl='$3'}.
tail_pattern -> ']' : #c_literal{val=[]}.
tail_pattern -> '|' anno_pattern ']' : '$2'.
tail_pattern -> ',' anno_pattern tail_pattern :
#c_cons{hd='$2',tl='$3'}.
binary_pattern -> '#' '{' '}' '#' : #c_binary{segments=[]}.
binary_pattern -> '#' '{' segment_patterns '}' '#' : #c_binary{segments='$3'}.
segment_patterns -> segment_pattern ',' segment_patterns : ['$1' | '$3'].
segment_patterns -> segment_pattern : ['$1'].
segment_pattern -> '#' '<' anno_pattern '>' '(' anno_patterns ')':
case '$6' of
[S,U,T,Fs] ->
#c_bitstr{val='$3',size=S,unit=U,type=T,flags=Fs};
true ->
return_error(tok_line('$1'),
"expected 4 arguments in binary segment")
end.
variable -> var : #c_var{name=tok_val('$1')}.
anno_variables -> anno_variable ',' anno_variables : ['$1' | '$3'].
anno_variables -> anno_variable : ['$1'].
anno_variable -> variable : '$1'.
anno_variable -> '(' variable '-|' annotation ')' :
core_lib:set_anno('$2', '$4').
%% Expressions
%% Must split expressions into two levels as nested value expressions
%% are illegal.
anno_expression -> expression : '$1'.
anno_expression -> '(' expression '-|' annotation ')' :
core_lib:set_anno('$2', '$4').
anno_expressions -> anno_expression ',' anno_expressions : ['$1' | '$3'].
anno_expressions -> anno_expression : ['$1'].
expression -> '<' '>' : #c_values{es=[]}.
expression -> '<' anno_expressions '>' : #c_values{es='$2'}.
expression -> single_expression : '$1'.
single_expression -> atomic_literal : '$1'.
single_expression -> tuple : '$1'.
single_expression -> cons : '$1'.
single_expression -> binary : '$1'.
single_expression -> variable : '$1'.
single_expression -> function_name : '$1'.
single_expression -> fun_expr : '$1'.
single_expression -> let_expr : '$1'.
single_expression -> letrec_expr : '$1'.
single_expression -> case_expr : '$1'.
single_expression -> receive_expr : '$1'.
single_expression -> application_expr : '$1'.
single_expression -> call_expr : '$1'.
single_expression -> primop_expr : '$1'.
single_expression -> try_expr : '$1'.
single_expression -> sequence : '$1'.
single_expression -> catch_expr : '$1'.
single_expression -> map_expr : '$1'.
literal -> atomic_literal : '$1'.
literal -> tuple_literal : '$1'.
literal -> cons_literal : '$1'.
literals -> literal ',' literals : ['$1' | '$3'].
literals -> literal : ['$1'].
atomic_literal -> char : #c_literal{val=tok_val('$1')}.
atomic_literal -> integer : #c_literal{val=tok_val('$1')}.
atomic_literal -> float : #c_literal{val=tok_val('$1')}.
atomic_literal -> atom : #c_literal{val=tok_val('$1')}.
atomic_literal -> string : #c_literal{val=tok_val('$1')}.
atomic_literal -> nil : #c_literal{val=[]}.
tuple_literal -> '{' '}' : c_tuple([]).
tuple_literal -> '{' literals '}' : c_tuple('$2').
cons_literal -> '[' literal tail_literal : c_cons('$2', '$3').
tail_literal -> ']' : #c_literal{val=[]}.
tail_literal -> '|' literal ']' : '$2'.
tail_literal -> ',' literal tail_literal : #c_cons{hd='$2',tl='$3'}.
tuple -> '{' '}' : c_tuple([]).
tuple -> '{' anno_expressions '}' : c_tuple('$2').
map_expr -> '~' '{' '}' '~' : #c_map{es=[]}.
map_expr -> '~' '{' map_pairs '}' '~' : #c_map{es='$3'}.
map_expr -> variable '~' '{' '}' '~' : #c_map{var='$1',es=[]}.
map_expr -> variable '~' '{' map_pairs '}' '~' : #c_map{var='$1',es='$4'}.
map_pairs -> map_pair : ['$1'].
map_pairs -> map_pair ',' map_pairs : ['$1' | '$3'].
map_pair -> map_pair_assoc : '$1'.
map_pair -> map_pair_exact : '$1'.
map_pair_assoc -> '::' '<' anno_expression ',' anno_expression'>' :
#c_map_pair{op=#c_literal{val=assoc},key='$3',val='$5'}.
map_pair_exact -> '~' '<' anno_expression ',' anno_expression'>' :
#c_map_pair{op=#c_literal{val=exact},key='$3',val='$5'}.
cons -> '[' anno_expression tail : c_cons('$2', '$3').
tail -> ']' : #c_literal{val=[]}.
tail -> '|' anno_expression ']' : '$2'.
tail -> ',' anno_expression tail : c_cons('$2', '$3').
binary -> '#' '{' '}' '#' : #c_literal{val = <<>>}.
binary -> '#' '{' segments '}' '#' : #c_binary{segments='$3'}.
segments -> segment ',' segments : ['$1' | '$3'].
segments -> segment : ['$1'].
segment -> '#' '<' anno_expression '>' '(' anno_expressions ')':
case '$6' of
[S,U,T,Fs] ->
#c_bitstr{val='$3',size=S,unit=U,type=T,flags=Fs};
true ->
return_error(tok_line('$1'),
"expected 4 arguments in binary segment")
end.
function_name -> atom '/' integer :
#c_var{name={tok_val('$1'),tok_val('$3')}}.
anno_function_name -> function_name : '$1'.
anno_function_name -> '(' function_name '-|' annotation ')' :
core_lib:set_anno('$2', '$4').
let_vars -> anno_variable : ['$1'].
let_vars -> '<' '>' : [].
let_vars -> '<' anno_variables '>' : '$2'.
sequence -> 'do' anno_expression anno_expression :
#c_seq{arg='$2',body='$3'}.
fun_expr -> 'fun' '(' ')' '->' anno_expression :
#c_fun{vars=[],body='$5'}.
fun_expr -> 'fun' '(' anno_variables ')' '->' anno_expression :
#c_fun{vars='$3',body='$6'}.
let_expr -> 'let' let_vars '=' anno_expression 'in' anno_expression :
#c_let{vars='$2',arg='$4',body='$6'}.
letrec_expr -> 'letrec' function_definitions 'in' anno_expression :
#c_letrec{defs='$2',body='$4'}.
case_expr -> 'case' anno_expression 'of' anno_clauses 'end' :
#c_case{arg='$2',clauses='$4'}.
anno_clauses -> anno_clause anno_clauses : ['$1' | '$2'].
anno_clauses -> anno_clause : ['$1'].
anno_clause -> clause : '$1'.
anno_clause -> '(' clause '-|' annotation ')' :
core_lib:set_anno('$2', '$4').
clause -> clause_pattern 'when' anno_expression '->' anno_expression :
#c_clause{pats='$1',guard='$3',body='$5'}.
clause_pattern -> anno_pattern : ['$1'].
clause_pattern -> '<' '>' : [].
clause_pattern -> '<' anno_patterns '>' : '$2'.
application_expr -> 'apply' anno_expression arg_list :
#c_apply{op='$2',args='$3'}.
call_expr ->
'call' anno_expression ':' anno_expression arg_list :
#c_call{module='$2',name='$4',args='$5'}.
primop_expr -> 'primop' anno_expression arg_list :
#c_primop{name='$2',args='$3'}.
arg_list -> '(' ')' : [].
arg_list -> '(' anno_expressions ')' : '$2'.
try_expr ->
'try' anno_expression 'of' let_vars '->' anno_expression
'catch' let_vars '->' anno_expression :
Len = length('$8'),
if Len =:= 2; Len =:= 3 ->
#c_try{arg='$2',vars='$4',body='$6',evars='$8',handler='$10'};
true ->
return_error(tok_line('$7'),
"expected 2 or 3 exception variables in 'try'")
end.
catch_expr -> 'catch' anno_expression : #c_catch{body='$2'}.
receive_expr -> 'receive' timeout :
{T,A} = '$2',
#c_receive{clauses=[],timeout=T,action=A}.
receive_expr -> 'receive' anno_clauses timeout :
{T,A} = '$3',
#c_receive{clauses='$2',timeout=T,action=A}.
timeout ->
'after' anno_expression '->' anno_expression : {'$2','$4'}.
%% ====================================================================== %%
Erlang code.
%% The following directive is needed for (significantly) faster compilation
%% of the generated .erl file by the HiPE compiler. Please do not remove.
-compile([{hipe,[{regalloc,linear_scan}]}]).
-include("core_parse.hrl").
-import(cerl, [c_cons/2,c_tuple/1]).
tok_val(T) -> element(3, T).
tok_line(T) -> element(2, T).
%% vim: syntax=erlang
|