[antlr-interest] Problems using the command-line grammar interpreter

Brian DeVries contingencyplan at gmail.com
Sat Oct 14 05:42:36 PDT 2006


I'm trying to use the org.antlr.tool.Interp program to test my grammar
against some basic sample output. I also have a very basic main( ) to
do the same task (though I'd prefer the command-line if possible).

I am using the following grammar (stripped down from the full version):

grammar Pseudocode;
options
{
	language = Java;
	backtrack = true;
	memoize = true;
	output = AST;
} // end options

@header
{
	package parsing;
}

@lexer::header
{
	package parsing;
} // end header

/**
 * An assignment
 */
assign
	: variable ASSIGN expression;

/**
 * A variable - something that can be on the left-hand side of an assignment
 */
variable
	: ID
	;

/**
 * A mathematical expression. Here, we only have INTs, but we'll add more
 * stuff after it's working.
 */
expression
	:	INT
	|	variable
	;

// Basic building blocks
fragment
LETTER
	:	'A'..'Z'
	|	'a'..'z'
	;

fragment
DIGIT
	:	'0'..'9'
	;

// Basic types - IDs, numbers, and strings
ID: (LETTER | '_') (LETTER | '_' | DIGIT)* ;

INT
	: DIGIT+
	;

/* Whitespace */
WS	: ( ' '
	  | '\n'
	  | '\r'
	  | '\t'
	  )
	  {channel = 99;}	// I wish it was something other than a "magic number"
	  ;

ASSIGN
	:	':='
	;

Here is the code for my main ( ) (again stripped down)

String filename = "test.slpc";
CharStream cs = new ANTLRFileStream (filename);
PseudocodeLexer lexer = new PseudocodeLexer (cs);
CommonTokenStream tokens = new CommonTokenStream ();
tokens.setTokenSource(lexer);
Pseudocode parser = new Pseudocode (tokens);
parser.assign();
System.out.println ("It parsed!");

This code runs and produces the desired result (printing "It
parsed!"). No errors are printed nor exceptions thrown.

However, when I run the command

% java -cp ${HOME}/classpath/antlr-2.7.6.jar:${HOME}/classpath/antlr_mine.jar:${HOME}/classpath/stringtemplate-2.3b9.jar
org.antlr.tool.Interp Pseudocode2.g WS assign ../test.slpc


I get the output

(<grammar Pseudocode> (assign (variable [@0,0:0='x',<5>,1:0])
[@2,2:3=':=',<4>,1:2] (expression
FailedPredicateException(expression,{synpred1}?))))


Any ideas why it's throwing an exception here, but not in the regular Java code?

Thanks!
~Brian DeVries


More information about the antlr-interest mailing list