Tree grammar cannot find tokens file
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Michael_Duffy
Posted On:   Friday, October 17, 2008 03:43 PM

I'm reading "Definitive ANTLR" and following along using ANTLR 3.1.1, IntelliJ 7.0.4, JDK 6, and the ANTLRWorks plug-in. It's all very nice so far, but I'm having a problem with the expression grammer tree walker. When I generate the Java classes in both ANTLRWorks or on the command line, I get the message "cannot find tokens file Expr.tokens" The Expr.tokens file does exist; it's in my CLASSPATH. How must I change my tree grammar to find it? I've attached the Walker.g for the tree walker and my version of Expr.g. Terence, I hope you're watching. The interest mailing list hasn't done me any good. I've proposed using ANTLR to generate some web services from COBOL code, and I'm   More>>


I'm reading "Definitive ANTLR" and following along using ANTLR 3.1.1, IntelliJ 7.0.4, JDK 6, and the ANTLRWorks plug-in. It's all very nice so far, but I'm having a problem with the expression grammer tree walker. When I generate the Java classes in both ANTLRWorks or on the command line, I get the message "cannot find tokens file Expr.tokens"




The Expr.tokens file does exist; it's in my CLASSPATH. How must I change my tree grammar to find it?



I've attached the Walker.g for the tree walker and my version of Expr.g.

Terence, I hope you're watching. The interest mailing list hasn't done me any good. I've proposed using ANTLR to generate some web services from COBOL code, and I'm under the gun to make it work.

			
tree grammar Walker;

options
{
tokenVocab=Expr;
ASTLabelType=CommonTree;
}

@header
{
import java.util.Map;
import java.util.HashMap;
}

@members
{
Map memory = new HashMap();
}

program : statement+;

statement: expr { System.out.println($expr.value); }
| ^(EQUALS ID expr) { memory.put($ID.text, new Integer($expr.value)); }
;

expr returns [int value]
: ^('+' a=expr b=expr) { $value = a+b; }
| ^('-' a=expr b=expr) { $value = a-b; }
| ^('*' a=expr b=expr) { $value = a*b; }
| ID
{
Integer v = memory.get($ID.text);
if (v != null)
{
$value = v.intValue();
}
else
{
System.err.println("undefined variable " + $ID.text);
}
}
| INT
;



			
grammar Expr;

program : statement+;

statement: expr NEWLINE
| ID EQUALS expr NEWLINE
| NEWLINE;

expr : multExpr (('+'|'-') multExpr)*;

multExpr : atom ('*' atom)*;

atom : INT | ID | (LPAREN expr RPAREN);

ID : ('a'..'z'|'A'..'Z'|'_')+ | ('"' (~'"')* '"');
INT : '0'..'9'+;

NEWLINE : '
'? '
';

COMMENT : '#' ( ~('
'|'
') )* NEWLINE? { skip(); };
LBRACE : '{';
RBRACE : '}';
LPAREN : '(';
RPAREN : ')';
EQUALS : '=';
SEMI : ';';
SPACE : ' ';
TAB : ' ';

WS : (SPACE
| '
'
| '
'
| TAB
)+ { skip(); };

   <<Less
About | Sitemap | Contact