# See http://www.gnu.org/software/bison/manual/html_mono/bison.html#GLR-Parsers %strict %token ID INT NUM %right '=' %left '+' %{ our $VERSION = '0.01'; use base q{DebugTail}; %} %tree bypass %% prog: %name EMPTY /* empty */ | %name PROG prog stmt ; stmt: %name EXP expr ';' | %name DECL decl ; expr: %name EXPID ID decexpconflict | %name NUM NUM | %name TYPECAST INT '(' expr ')' /* typecast */ | %name PLUS expr '+' expr | %name ASSIGN expr '=' expr ; decl: %name DECLARATOR INT declarator ';' | %name DECLARATORINIT INT declarator '=' expr ';' ; declarator: %name DECID ID decexpconflict | '(' declarator ')' ; decexpconflict: /* empty. Just for hacking the LALR tables */ { my $self = shift; my $conflictstate = $self->YYNextState(); if (${$self->input} =~ m{^[)\s]*[;=]\s*}) { $self->YYSetLRAction($conflictstate, ')', 'DECID' ); } else { $self->YYSetLRAction($conflictstate, ')', 'EXPID' ); } } ; %% __PACKAGE__->lexer( sub { my $self = shift; for (${$self->input()}) { s{^(\s*)}{}; return ('', undef) unless $_; return ('INT', $1) if s{^(int\b)}{}; return ('ID', $1) if (s{^([a-zA-Z_]\w*)}{}); return ('NUM', $1) if s/^(\d+)//; return ($1, $1) if s/^(.)//; } return ('',undef); } ); unless (caller()) { my $prompt = 'Try first "int (x) = 2;" then "int (x) + 2;" '. '(press to finish): '; __PACKAGE__->main($prompt) } =head1 SYNOPSIS Compile it with eyapp -b '' Cplusplus2.eyp Run it with: ./Cplusplus2.pm -t try with inputs: int (x) = 2; int (x) + 2; the result will be the generated abstract sytax 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++ 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 (hacking). =head1 SEE ALSO =over 2 =item * File C in C =item * L =item * L =item * L =item * L =item * Edward Willink's "Meta-Compilation for C++" PhD thesis at L =back =cut