[antlr-interest] Confused about backtracking
franck102
franck102 at yahoo.com
Tue Nov 29 12:55:44 PST 2011
I could really use any ideas on this, what I am beginning to understand is
that backtracking can give up on an alternative to try the next alternative
in the same rule, but if a rule at the bottom of the call chain picks an
alternative, and the invoking rule at the top subsequently can't find a
continuation, backtracking won't be able to question the earlier choice of
the bottom-most rule.
Refactoring would basically imply rewriting the entire grammar and doing two
complete passes to end up with a meaningful AST, I am hoping this is a
common problem and maybe there are patterns that can help (e.g. using
context, scopes, ...)?
Here is a toy grammar that exhibits the problem, it is unable to parse input
such as a=b. c=d;
I would like the parser, after matching a=b.c as an expr, to realize that
"=d;" can't be matched and to go back to match a=b instead...
Any suggestion greatly appreciated!
================================
grammar Test;
options {
output=AST;
backtrack=true;
}
program
: statement* EOF
;
statement
: objectExpr '=' objectExpr
| sep
;
varDeclaration
: ident 'is' 'a' ident;
objectExpr
: indexExpr
| objectExpr 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-tp7033712p7044541.html
Sent from the ANTLR mailing list archive at Nabble.com.
More information about the antlr-interest
mailing list