[antlr-interest] Problem With CLASSPATH During Generate When Using ANTLRWorks
Michael Duffy
duffymo at gmail.com
Fri Oct 17 07:49:58 PDT 2008
I'm using the latest ANTLRWorks plug-in with IntelliJ 7.0.4 and ANTLR 3.1.1.
I'm working my way through "Definitive ANTLR Reference" to climb the
learning curve. So far, so good. Brilliantly done - kudos to Terence Parr
and everyone who contributes.
I've hit a snag in section 3.2 when I start adding actions to the expression
grammar. The code in the book works fine, but I tried adding a wrinkle. I
added Apache Commons Logging JARs to my CLASSPATH, created a Log as a member
using their LogFactory, and logged to info instead of printing to
System.out. ANTLRWorks writes failure messages to the console when I
generate the lexer/parser, saying that package org.apache.commons.logging
does not exist.
I can see the library in my IntelliJ project browser. It includes the
package, classes, and methods that I'm invoking. I know how to add 3rd
party JARs to IntelliJ well enough to make ANTLR work fine without the
logging. Did I miss something important here? I've included the modified
grammar. Any advice would be most welcome. Thanks.
grammar Eval;
@header
{
import java.util.Map;
import java.util.HashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
}
@members
{
Map memory = new HashMap();
Log logger = LogFactory.getLog(EvalParser.class);
}
program : statement+;
statement: expr { logger.info($expr.value); }
| ID EQUALS expr NEWLINE { memory.put($ID.text, new
Integer($expr.value)); }
| NEWLINE;
expr returns [int value]
: e=multExpr {$value = $e.value; }
( '+' e=multExpr { $value += $e.value; }
| '-' e=multExpr { $value -= $e.value; }
)*;
multExpr returns [int value]
: e=atom {$value = $e.value; } ('*' e=atom { $value =
$e.value; })*;
atom returns [int value]
: INT { $value = Integer.parseInt($INT.text); }
| ID
{
Integer v = (Integer)memory.get($ID.text);
if (v != null)
{
$value = v.intValue();
}
else
{
logger.error("undefined variable " + $ID.text);
}
}
| LPAREN expr RPAREN { $value = $expr.value; };
ID : ('a'..'z'|'A'..'Z'|'_')+ | ('"' (~'"')* '"');
INT : '0'..'9'+;
NEWLINE : '\n'|'\r'('\n')? { skip(); };
COMMENT : '#' ( ~('\n'|'\r') )* NEWLINE? { skip(); };
LBRACE : '{';
RBRACE : '}';
LPAREN : '(';
RPAREN : ')';
EQUALS : '=';
SEMI : ';';
SPACE : ' ';
TAB : '\t';
WS : (SPACE
| NEWLINE
| TAB
)+ { skip(); };
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081017/c998225b/attachment.html
More information about the antlr-interest
mailing list