[antlr-interest] Confused about backtracking

franck102 franck102 at yahoo.com
Tue Nov 29 14:00:30 PST 2011


Ok, I am really new to parsing technology but isn't there room for
improvement here? The grammar below successfully parses "a=b. c=d;", but it
fails if the first two alternatives of objectExpr are swapped. And it also
fails if the rule is written naturally as objectExpr : indexExpr ( DOT
indexExpr )*.

Those changes look like syntactic details that shouldn't affect the result
of the parse (I do understand that the first alternative is attempted first,
but with backtrack=true one would expect that to have nothing but a
performance impact).
Is it theoretically hard to backtrack across rules? And to consider the
option to grab one instance of a ()* construct as an alternative that can be
backtracked?

Franck

-----------------------------------

grammar Test;

options {
        output=AST;
        backtrack=true;
}

program
        : ( objectExpr '=' objectExpr sep )* EOF
        ;
   
objectExpr
	:	indexExpr
	|	indexExpr DOT indexExpr
	|	indexExpr DOT indexExpr DOT indexExpr
	;
	
indexExpr
	:	ident ( '[' objectExpr ']' | '(' objectExpr ')' )*
	;
	
sep : ';' | DOT;

ident	:	ID | UNIT;

// LEXER
// ==========================================

UNIT	:	'second' 's'?
	|	'minute' 's'?
	;
	
DOT	:	'.';

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-tp7033712p7044771.html
Sent from the ANTLR mailing list archive at Nabble.com.


More information about the antlr-interest mailing list