[antlr-interest] Fundamental question

Jim Idle jimi at temporal-wave.com
Wed Nov 9 09:15:06 PST 2011


First, you need to change the start rule:

public r
 	: variables* EOF
 	;

Otherwise it just stops a strig because that is not predicted.

Next though, you have to decide what it means to have something in the AST
that us put there by a syntax error. In almost all cases it is meaningless
and you have to stop processing after the parsing. However, it can
sometimes be useful to have some kind of error node, which ANTLR will
attempt to do for you, but generally you have to do this manually.

Jim


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Mari Matinlassi
> Sent: Wednesday, November 09, 2011 8:19 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Fundamental question
>
> Hi,
>
> There is something fundamental and important that I have not understood
> with ANTLR grammars.
>
> If I need to parse something like...
>
> 	int test;
> 	strig another;
> 	int variable;
> 	string here;
>
> You notice there is a typing error on the second line ('strig' instead
> of 'string').
>
> How do I make a grammar that will not stop parsing but, continues after
> an error??
>
> Below is an example how I CANNOT make it work the right way (created
> AST contains only 'int test ;')..
>
> grammar List;
>
> options {
> 	language = CSharp3;
> 	TokenLabelType = CommonToken;
> 	output=AST;
> 	ASTLabelType = CommonTree;
> }
>
> @lexer::namespace{ConsoleApplication4}
> @parser::namespace{ConsoleApplication4}
>
>
> public r
> 	: variables*
> 	;
>
> variables
> 	: type ID ';'
> 	;
>
> type
> 	: 'int'
> 	| 'string'
> 	;
>
> ID : 'a'..'z' + ;
>
> WS : (' ' |'\n' |'\r' ) {$channel=Hidden;} ;
>
>
> Many thanks for your time and help,
>
> Mari
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list