[antlr-interest] Confused about backtracking

Terence Parr parrt at cs.usfca.edu
Mon Nov 28 08:55:37 PST 2011


> expr	:	ID suffix;

will try to match the sep '.'.  backtrack takes first alt that wins and the decision for suffix needs to backtrack due to your program rule. therefore it attempts '.' expr and fails.

allowing ;;;;…;;;;… as a program is not a good idea ;)

T
On Nov 26, 2011, at 4:21 AM, franck102 wrote:

> I think I am missing something fundamental about backtracking. The grammar
> below won't parse input such as
> 
> a=b.
> c=d;
> 
> ... even though I would expect it to backtrack after realizing that a=b.c
> leads to a dead-end. What am I missing?
> 
> Thanks!
> 
> PS: I am not looking for refactoring options, I have the issue in a complex
> grammar that can't easily be refactored.
> 
> =====================================
> grammar Test;
> 
> options {
> 	output=AST;
> 	backtrack=true;
> }
> 
> program
> 	: statement* EOF
> 	;
> 	
> statement
> 	:	ID '=' expr
> 	|	sep
> 	;
> 
> expr	:	ID suffix;
> 
> suffix	:	( '.' expr )*
> 	;
> 
> sep	:	';' | '.';
> 
> // LEXER
> // ==========================================
> 
> ID
> 	:	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
> 	;
> 
> 
> WS  :   ( ' '
>        | '\t'
>        | '\r'
>        | '\n'
>        ) {$channel=HIDDEN;}
>    ;
> 
> 
> --
> View this message in context: http://antlr.1301665.n2.nabble.com/Confused-about-backtracking-tp7033712p7033712.html
> Sent from the ANTLR mailing list archive at Nabble.com.
> 
> 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