[antlr-interest] Parser Generator Error

Gardner, Michael T mtgardn at sandia.gov
Mon Jul 27 08:29:14 PDT 2009


In looking at the Tree Interpreter grammar as described in chapter 9 of "Language Design Patterns", I note that AntlrWorks warns of an if-then-else ambiguity when checking the grammar file (Pie.g).  Being new to ANTLR, I thought I would try resolving the ambiguity so that the warning goes away.  I referred to "The Definitive ANTLR Reference", chap 12, p305.  I implemented a modified if statement in the Pie.g grammar file as follows:

statement
	:	structDefinition
	|	qid '=' expr NL				-> ^('=' qid expr)
	|	'return' expr NL  			-> ^('return' expr)
	|	'print' expr NL	 			-> ^('print' expr)
	|	ifstat					-> ifstat
	|	'while' expr slist			-> ^('while' expr slist)
	|	call NL					-> call
	|	NL						->
	;


ifstat
	options 
	{ backtrack = true;}
	:	'if' expr c=slist 'else' el=slist 	-> ^('if' expr $c $el)
	|	'if' expr d=slist  			-> ^('if' expr $d )
	;

Using AntlrWorks, the grammar now checks fine and yields no warnings on code generation, BUT, the ANTLR-generated PieParser code will not compile!  Here's the error message:

Compiling 20 source files to D:\Projects\Sandbox\ParserGenerators\Tools\Antlr\ReferenceBooks\LanguageDesignPatterns\Projects\TreeInterpX\build\classes
D:\Projects\Sandbox\ParserGenerators\Tools\Antlr\ReferenceBooks\LanguageDesignPatterns\Projects\TreeInterpX\src\PieParser.java:493: cannot find symbol
symbol  : variable fs
location: class PieParser
              currentScope = new LocalScope(fs);
D:\Projects\Sandbox\ParserGenerators\Tools\Antlr\ReferenceBooks\LanguageDesignPatterns\Projects\TreeInterpX\src\PieParser.java:503: cannot find symbol
symbol  : variable fs
location: class PieParser
                              fs.blockAST = (slist18!=null?((PieAST)slist18.tree):null);
                              ^
Note: D:\Projects\Sandbox\ParserGenerators\Tools\Antlr\ReferenceBooks\LanguageDesignPatterns\Projects\TreeInterpX\src\Interpreter.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Indeed, there is no declaration of an fs (FunctionSymbol) in scope.  Has this error been identified previously?  Is there a patch to fix this in ANTLR?  Are there any problems or side effects with just ignoring the error as you suggested in the book?  Does anyone have a working definition of the if-then-else statement that does not generate AntlrWorks warnings, compiles cleanly (in Java), and works?

Michael T. Gardner



More information about the antlr-interest mailing list