[antlr-interest] Optional Tree path not followed
Anakreon
anakreonmejdi at yahoo.gr
Sat Jan 22 04:47:06 PST 2005
Hello everyone.
I have a problem with a TreeParser rule.
The antlr code for the rule is:
sub_call
:
<tsub> #(SUB_CALL sub_name:expression <sub_call>
(argListValues)?
<sub_call_end>)
;
where the tokens <name> is some action code.
and the argListValues definition is:
argListValues
:
!#(ARGLIST_VALUES <arg_val> (arg:expression <arg_val_end>)+)
;
The problem is that when a argListValues could be matched from the sub_call
rule, it isn't.
I checked the code produced when the sub_call defines the match of rule argListValues
as mandatory.
Here is the code:
case SUB_CALL:
{
AST __t27 = _t;
AST tmp18_AST = null;
AST tmp18_AST_in = null;
tmp18_AST = astFactory.create((AST)_t);
tmp18_AST_in = (AST)_t;
ASTPair __currentAST27 = currentAST.copy();
currentAST.root = currentAST.child;
currentAST.child = null;
match(_t,SUB_CALL);
_t = _t.getFirstChild();
sub_name = _t==ASTNULL ? null : (AST)_t;
expression(_t);
_t = _retTree;
sub_name_AST = (AST)returnAST;
lastStackSize = argStack.size();
boolean isArray = sub_name_AST != null
&& sub_name_AST.getType() == IDENTIFIER
&& getIdentifierType(sub_name_AST.getText()) == ARRAY;
//this added by me in the generated code
log.debug(Boolean.toString(_t == null));
argListValues(_t);
_t = _retTree;
statement_AST = (AST)currentAST.root;
if (isArray) {
statement_AST = translateArray(sub_name_AST, lastStackSize);
} else {
statement_AST = handleFunctionSubEnd(sub_name_AST, lastStackSize);
}
if (statement_AST != null) {
statement_AST = (AST)astFactory.make( (new ASTArray(2)).add(astFactory.create(EXPR,"EXPR")).add(statement_AST));
}
currentAST.root = statement_AST;
currentAST.child = statement_AST!=null &&statement_AST.getFirstChild()!=null ?
statement_AST.getFirstChild() : statement_AST;
currentAST.advanceChildToEnd();
currentAST = __currentAST27;
_t = __t27;
_t = _t.getNextSibling();
break;
}
The diff when the argListValues rule is optional is:
.................................
{
if (_t==null) _t=ASTNULL;
switch ( _t.getType()) {
case ARGLIST_VALUES:
{
argListValues(_t);
_t = _retTree;
break;
}
case 3:
{
break;
}
default:
{
throw new NoViableAltException(_t);
}
}
}
............................
The interesting thing is that in both cases the statement
log.debug(Boolean.toString(_t == null));
prints true. When the match of argListValues is mandatory and
and the tree analyzed can be matched by argListValues _t is null
but argListValues does not fail. It seems to me that _t is allways null
no matter if argListValues can be matched or not.
antlr version: 1.7.4
What should I do to investigate if this happens because of action code or
it is a bug in the code generated from antlr?
Anakreon
More information about the antlr-interest
mailing list