I'm a new to ANTLR and am encountering compiler errors when I compile
the ANTLR generated parser. I'm using a modified Pie.g grammar file
from Terence Par's book, Language Design Patterns, chap 9, Tree
Interpreter.
Here is the grammar for the function definition, along with the
embedded actions.
funcDef
: DEF ID
{
// Create new function symbol with current
scope as parent
FunctionSymbol fs = new FunctionSymbol
($ID.text,currentScope);
// Define function in current scope
currentScope.define(fs);
// Set current scope to function scope
currentScope = fs;
}
LPAREN (vardef (',' vardef)* )? RPAREN
{
// Create new local scope for function and set
as current
// with function scope as parent
currentScope = new LocalScope(fs);
}
statList
{
// Copy statList into function symbol's AST
fs.blockAST = $statList.tree;
// Return from local scope back to function
scope
currentScope = currentScope.getEnclosingScope();
// Return function scope back to original scope
currentScope = currentScope.getEnclosingScope();
}
-> ^(SFDECL)
;
When the code gets generated into the parser for this rule I see the
following code fragment:
{
DEF42=(Token)match(input,DEF,FOLLOW_DEF_in_funcDef1015);
if (state.failed) return retval;
if ( state.backtracking==0 ) stream_DEF.add(DEF42);
ID43=(Token)match(input,ID,FOLLOW_ID_in_funcDef1017); if
(state.failed) return retval;
if ( state.backtracking==0 ) stream_ID.add(ID43);
---> if ( state.backtracking==0 ) {
// Create new function symbol with current scope as
parent
FunctionSymbol fs = new FunctionSymbol((ID43!=null?
ID43.getText():null),currentScope);
// Define function in current scope
currentScope.define(fs);
// Set current scope to function scope
currentScope = fs;
}
In this code fragment, that is generated by ANTLR, there is an "if
(state.backtracking==0) {" line (identified by ---> arrow) that
preceeds the creation of the new function symbol. The braces
associated with the "if block" make the definition of "fs" a locally
scoped value and thus not visible to the entire function. This is the
cause of the compiler error that I've been seeing.
I've looked at the code generated by the unmodified Pie.g grammar from
Terence Par and see that this checking of the state does NOT occur.
So I must have defined something in the grammar that triggers this
check, but have been unsuccessful in figuring out what that is. The
keyword "backtrack" is nowhere found in my test grammar and syntactic
predicates are not defined for this function definition. Is there a
way to turn off this check of the state.backtracking value so that
"fs" is now visible by later statements in the function. Does any
one know what causes this "if" statement to be generated? I'm at a
loss. Any clues and help will be appreciated.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---