%{ my $INT = '(int)\b'; my $ID = '([a-zA-Z_][a-zA-Z_0-9]*)'; my $NUM = '(\d+)'; %} %token NUM = /$NUM/ %token INT = /$INT/ %token ID = /$ID/ %right '=' %left '+' %% decl: %name DECLARATOR INT declarator ';' | %name DECLARATORINIT INT declarator '=' expr ';' ; expr: %name ID:EXP ID | %name NUM NUM | %name TYPECAST INT '(' expr ')' /* typecast */ | %name PLUS expr '+' expr | %name ASSIGN expr '=' expr ; declarator: %name ID:DEC ID | '(' declarator ')' ; %% #################################################### =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 in C =item * L =item * L =item * L =item * L =item * Edward Willink's "Meta-Compilation for C++" PhD thesis at L =back =cut