Posted By:
John_Riendeau
Posted On:
Friday, November 15, 2002 11:25 AM
Hi all, I'm using ANTLR to parse very basic C++ files, and I'd like to get informative error messages when we hit a syntax error in the C++ code. As it stands now, however, C++ code such as: int main() { int foo return 0; } results in an "Unexpected token foo" error message from the lexer. I've included parts of the grammar I'm using below; is there a way for me to add/remove/change things in my variable declaration grammar to have the lexer give an error message relating to the missing semicolon rather than pointing at "foo" as the source of the problem? Any help would be greatly appriciated. Thanks!
More>>
Hi all,
I'm using ANTLR to parse very basic C++ files, and I'd like to get informative error messages when we hit a syntax error in the C++ code. As it stands now, however, C++ code such as:
int main()
{
int foo
return 0;
}
results in an "Unexpected token foo" error message from the lexer. I've included parts of the grammar I'm using below; is there a way for me to add/remove/change things in my variable declaration grammar to have the lexer give an error message relating to the missing semicolon rather than pointing at "foo" as the source of the problem?
Any help would be greatly appriciated. Thanks!
declaration
: blockDeclaration SEMI!
| (declarationSpecifier)+ initDeclaratorList SEMI!
blockDeclaration
: usingDirective
;
usingDirective
: USING! NAMESPACE! nestedNameSpecifier
;
nestedNameSpecifier
: (IDENTIFIER)*
;
declarationSpecifier
: (typeModifiers)? typeSpecifier (ptrOperator)?
;
typeSpecifier
: TYPENAME
| structSpecifier
| enumSpecifier
;
initDeclaratorList
: (initDeclarator (COMMA! initDeclarator)*)?
;
initDeclarator
: d:declarator (ASSIGN! initializer)?
;
declarator
: (IDENTIFIER LPAREN) => IDENTIFIER LPAREN! parameterList RPAREN!
| IDENTIFIER
| IDENTIFIER (arrayDesignator)+
;
parameterList
: parameterDeclarations
;
parameterDeclarations
: parameterDeclaration (COMMA! parameterDeclaration)*
;
parameterDeclaration
: (declarationSpecifier)+ (declarator)?
;
<<Less