-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCplusplus.pm
executable file
·555 lines (490 loc) · 10.5 KB
/
Cplusplus.pm
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#!/usr/bin/perl
########################################################################################
#
# This file was generated using Parse::Eyapp version 1.166.
#
# (c) Parse::Yapp Copyright 1998-2001 Francois Desarmenien.
# (c) Parse::Eyapp Copyright 2006-2008 Casiano Rodriguez-Leon. Universidad de La Laguna.
# Don't edit this file, use source file 'Cplusplus.eyp' instead.
#
# ANY CHANGE MADE HERE WILL BE LOST !
#
########################################################################################
package Cplusplus;
use strict;
push @Cplusplus::ISA, 'Parse::Eyapp::Driver';
BEGIN {
# This strange way to load the modules is to guarantee compatibility when
# using several standalone and non-standalone Eyapp parsers
require Parse::Eyapp::Driver unless Parse::Eyapp::Driver->can('YYParse');
require Parse::Eyapp::Node unless Parse::Eyapp::Node->can('hnew');
}
sub unexpendedInput { defined($_) ? substr($_, (defined(pos $_) ? pos $_ : 0)) : '' }
#line 1 "Cplusplus.eyp"
our $VERSION = '0.01';
my $WHITES = qr{\G\s*};
my $INT = qr{\Gint\b};
my $ID = qr{\G([a-zA-Z_][a-zA-Z_0-9]*)};
my $NUM = qr{\G(\d+)};
my $PUN = qr{\G([-+*/=();,])};
# If the incoming input looks like this then it is a declaration
my $ISDEC = qr{\G(?=
[)\s]* # closing parenthesis, s.t. like: ') ) )'
[;=]\ # followed by semicolon or '='
)
}x;
#line 19 "Cplusplus.eyp"
__PACKAGE__->YYLexer(
sub { # lexical analyzer
my $self = $_[0];
for (${$self->input()}) { # contextualize
#line 19 "Cplusplus.eyp"
m{$WHITES}gc;
return ('INT', $1) if m{$INT}gc;
return ('ID', $1) if m{$ID}gc;
return ('NUM', $1) if m{$NUM}gc;
return ($1, $1) if m{$PUN}gc;
#line 63 ./Cplusplus.pm
return ('', undef) if ($_ eq '') || (defined(pos($_)) && (pos($_) >= length($_)));
die("Error inside the lexical analyzer. Line: 26. File: Cplusplus.eyp. No regexp matched.\n");
}
} # end lexical analyzer
);
#line 70 ./Cplusplus.pm
my $warnmessage =<< "EOFWARN";
Warning!: Did you changed the \@Cplusplus::ISA variable inside the header section of the eyapp program?
EOFWARN
sub new {
my($class)=shift;
ref($class) and $class=ref($class);
warn $warnmessage unless __PACKAGE__->isa('Parse::Eyapp::Driver');
my($self)=$class->SUPER::new(
yyversion => '1.166',
yyGRAMMAR =>
[
[ '_SUPERSTART' => '$start', [ 'prog', '$end' ], 1 ],
[ 'EMPTY' => 'prog', [ ], 1 ],
[ 'PROG' => 'prog', [ 'prog', 'stmt' ], 1 ],
[ 'EXP' => 'stmt', [ 'expr', ';' ], 1 ],
[ 'DECL' => 'stmt', [ 'decl' ], 1 ],
[ 'ID:EXP' => 'expr', [ 'ID', 'decORexp' ], 1 ],
[ 'NUM' => 'expr', [ 'NUM' ], 1 ],
[ 'TYPECAST' => 'expr', [ 'INT', '(', 'expr', ')' ], 1 ],
[ 'PLUS' => 'expr', [ 'expr', '+', 'expr' ], 1 ],
[ 'ASSIGN' => 'expr', [ 'expr', '=', 'expr' ], 1 ],
[ 'DECLARATOR' => 'decl', [ 'INT', 'declarator', ';' ], 1 ],
[ 'DECLARATORINIT' => 'decl', [ 'INT', 'declarator', '=', 'expr', ';' ], 1 ],
[ 'ID:DEC' => 'declarator', [ 'ID', 'decORexp' ], 1 ],
[ 'declarator_13' => 'declarator', [ '(', 'declarator', ')' ], 1 ],
[ 'decORexp' => 'decORexp', [ ], 1 ],
],
yyTERMS =>
{ '' => { ISSEMANTIC => 0 },
'(' => { ISSEMANTIC => 0 },
')' => { ISSEMANTIC => 0 },
'+' => { ISSEMANTIC => 0 },
';' => { ISSEMANTIC => 0 },
'=' => { ISSEMANTIC => 0 },
ID => { ISSEMANTIC => 1 },
INT => { ISSEMANTIC => 1 },
NUM => { ISSEMANTIC => 1 },
error => { ISSEMANTIC => 0 },
},
yyFILENAME => 'Cplusplus.eyp',
yystates =>
[
{#State 0
DEFAULT => -1,
GOTOS => {
'prog' => 1
}
},
{#State 1
ACTIONS => {
'' => 5,
'ID' => 3,
'NUM' => 2,
'INT' => 8
},
GOTOS => {
'stmt' => 4,
'expr' => 7,
'decl' => 6
}
},
{#State 2
DEFAULT => -6
},
{#State 3
DEFAULT => -14,
GOTOS => {
'decORexp' => 9
}
},
{#State 4
DEFAULT => -2
},
{#State 5
DEFAULT => 0
},
{#State 6
DEFAULT => -4
},
{#State 7
ACTIONS => {
";" => 11,
"+" => 10,
"=" => 12
}
},
{#State 8
ACTIONS => {
'ID' => 13,
"(" => 14
},
GOTOS => {
'declarator' => 15
}
},
{#State 9
DEFAULT => -5
},
{#State 10
ACTIONS => {
'ID' => 3,
'NUM' => 2,
'INT' => 17
},
GOTOS => {
'expr' => 16
}
},
{#State 11
DEFAULT => -3
},
{#State 12
ACTIONS => {
'ID' => 3,
'NUM' => 2,
'INT' => 17
},
GOTOS => {
'expr' => 18
}
},
{#State 13
DEFAULT => -14,
GOTOS => {
'decORexp' => 19
}
},
{#State 14
ACTIONS => {
'ID' => 20,
'NUM' => 2,
"(" => 21,
'INT' => 17
},
GOTOS => {
'expr' => 22,
'declarator' => 23
}
},
{#State 15
ACTIONS => {
";" => 24,
"=" => 25
}
},
{#State 16
DEFAULT => -8
},
{#State 17
ACTIONS => {
"(" => 26
}
},
{#State 18
ACTIONS => {
"+" => 10,
"=" => 12
},
DEFAULT => -9
},
{#State 19
DEFAULT => -12
},
{#State 20
DEFAULT => -14,
GOTOS => {
'decORexp' => 27
}
},
{#State 21
ACTIONS => {
'ID' => 13,
"(" => 21
},
GOTOS => {
'declarator' => 23
}
},
{#State 22
ACTIONS => {
"+" => 10,
")" => 28,
"=" => 12
}
},
{#State 23
ACTIONS => {
")" => 29
}
},
{#State 24
DEFAULT => -10
},
{#State 25
ACTIONS => {
'ID' => 3,
'NUM' => 2,
'INT' => 17
},
GOTOS => {
'expr' => 30
}
},
{#State 26
ACTIONS => {
'ID' => 3,
'NUM' => 2,
'INT' => 17
},
GOTOS => {
'expr' => 22
}
},
{#State 27
DEFAULT => -5
},
{#State 28
DEFAULT => -7
},
{#State 29
DEFAULT => -13
},
{#State 30
ACTIONS => {
";" => 31,
"+" => 10,
"=" => 12
}
},
{#State 31
DEFAULT => -11
}
],
yyrules =>
[
[#Rule _SUPERSTART
'$start', 2, undef
#line 311 ./Cplusplus.pm
],
[#Rule EMPTY
'prog', 0,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 318 ./Cplusplus.pm
],
[#Rule PROG
'prog', 2,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 325 ./Cplusplus.pm
],
[#Rule EXP
'stmt', 2,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 332 ./Cplusplus.pm
],
[#Rule DECL
'stmt', 1,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 339 ./Cplusplus.pm
],
[#Rule ID:EXP
'expr', 2,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 346 ./Cplusplus.pm
],
[#Rule NUM
'expr', 1,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 353 ./Cplusplus.pm
],
[#Rule TYPECAST
'expr', 4,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 360 ./Cplusplus.pm
],
[#Rule PLUS
'expr', 3,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 367 ./Cplusplus.pm
],
[#Rule ASSIGN
'expr', 3,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 374 ./Cplusplus.pm
],
[#Rule DECLARATOR
'decl', 3,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 381 ./Cplusplus.pm
],
[#Rule DECLARATORINIT
'decl', 5,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 388 ./Cplusplus.pm
],
[#Rule ID:DEC
'declarator', 2,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 395 ./Cplusplus.pm
],
[#Rule declarator_13
'declarator', 3,
sub {
#line 42 "Cplusplus.eyp"
goto &Parse::Eyapp::Driver::YYBuildAST }
#line 402 ./Cplusplus.pm
],
[#Rule decORexp
'decORexp', 0,
sub {
#line 85 "Cplusplus.eyp"
my $self = $_[0];
for (${$self->input()}) {
#line 31 "Cplusplus.eyp"
if (m{$ISDEC}) {
$self->YYSetReduce(')', 'ID:DEC' );
}
else {
$self->YYSetReduce(')', 'ID:EXP' );
}
#line 419 ./Cplusplus.pm
}
}
#line 422 ./Cplusplus.pm
]
],
#line 425 ./Cplusplus.pm
yybypass => 1,
yybuildingtree => 1,
yyprefix => '',
yyaccessors => {
},
yyconflicthandlers => {
'decORexp' => {
'production' => {
'-5' => [
1
],
'-12' => [
1
]
},
'line' => 31
}
}
,
@_,
);
bless($self,$class);
$self->make_node_classes('TERMINAL', '_OPTIONAL', '_STAR_LIST', '_PLUS_LIST',
'_SUPERSTART',
'EMPTY',
'PROG',
'EXP',
'DECL',
'ID:EXP',
'NUM',
'TYPECAST',
'PLUS',
'ASSIGN',
'DECLARATOR',
'DECLARATORINIT',
'ID:DEC',
'declarator_13',
'decORexp', );
$self;
}
#line 85 "Cplusplus.eyp"
unless (caller()) {
my $prompt = 'Try first "int (x) = 2;" then "int (x) + 2;" '.
'(press <CR><CTRL-D> to finish): ';
__PACKAGE__->main($prompt)
}
####################################################
=head1 SYNOPSIS
Compile it with
eyapp -b '' Cplusplus
Run it with:
./Cplusplus.pm -t -nos -i
or
./Cplusplus.pm -t -i -c 'int (x) + 2;'
try with inputs:
int (x) = 2;
int (x) + 2;
the output will be a description of the generated abstract syntax tree
=head1 C++ Ambiguities
This grammar models a problematic part of the C++ grammar
the ambiguity between certain
declarations and statements. For example,
int (x) = y+z;
parses as either an expr or a stmt.
Eyapp detects this as a reduce/reduce conflict:
State 17 contains 1 reduce/reduce conflict
State 17:
expr -> ID . (Rule 5)
declarator -> ID . (Rule 11)
')' [reduce using rule 11 (declarator)]
$default reduce using rule 5 (expr)
The C++ disambiguation rule is:
take it as a declaration if it looks as a declaration,
otherwise is an expression.
This Eyapp parser solves the problem by dynamically changing the parser.
=head1 SEE ALSO
=over 2
=item * The file C<Cplusplus2.eyp> in C<examples/debuggintut>
=item * L<http://www.gnu.org/software/bison/manual/html_mono/bison.html#GLR-Parsers>
=item * L<http://en.wikipedia.org/wiki/Significantly_Prettier_and_Easier_C%2B%2B_Syntax>
=item * L<http://www.csse.monash.edu.au/~damian/papers/PS/ModestProposal.ps>
=item * L<http://www.nobugs.org/developer/parsingcpp/>
=item * Edward Willink's "Meta-Compilation for C++" PhD thesis at L<http://www.computing.surrey.ac.uk/Research/CSRG/fog/FogThesis.pdf>
=back
=cut
#line 550 ./Cplusplus.pm
1;