[antlr-interest] Can I get rid of these warnings?
Kjell Nilsson
kjell at oops.se
Wed May 29 05:49:10 PDT 2002
I have a grammar that generates some sort of a calulator. There are some
functions in the calculator that i have java functions to resolve.
The function sum(foo, ../bar) is resolved like this
line 273-275
| #(SUM s1:expr s2:expr) {r =
calculateSum(s1.getText(),s2.getText());}
| KEYIDSTRING {r =
lookupKeyString(expr_AST_in.getText());}
| RELATIVEKEYIDSTRING {r =
lookupRelativeKeyString(expr_AST_in.getText());}
but here I get this warning
ParserGenerator/AntCalc.g:273: warning:Rule 'expr' returns a value
ParserGenerator/AntCalc.g:273: warning:Rule 'expr' returns a value
The generated code works fine so I think I can be safe to use it but is
there an easy way to
avoid them.
Thanks for any hints
--kjell
The grammar looks like this
header {
package se.oops.o2.infospace.data;
import java.math.*;
import java.io.*;
import com.webobjects.foundation.*;
import se.oops.o2.infospace.util.*;
}
class CalcParser extends Parser;
options {
codeGenMakeSwitchThreshold = 3;
codeGenBitsetTestThreshold = 4;
buildAST=true;
ASTLabelType = "antlr.CommonAST";
}
calculate: assignExpr SEMI! ;
assignExpr: multExpr (
pm: (PLUS^ | MINUS^)
me: multExpr
exception catch [ RecognitionException ex ] {
System.out.println("Caught error in assignExpr");
reportError(ex.toString());
} )* ;
multExpr: postfixExpr ( ( MULT^ | DIV^ | MOD^ | UP^) postfixExpr )* ;
postfixExpr: absFunc | acosFunc | asinFunc | atanFunc | cosFunc | expFunc
| invFunc | logFunc | sinFunc | sqrtFunc | tanFunc | powFunc
| sqrFunc | cubeFunc | maxFunc | minFunc | decFunc |
calculateSum | atom ;
absFunc: ABS^ LPAREN! assignExpr RPAREN! ;
acosFunc: ACOS^ LPAREN! assignExpr RPAREN! ;
asinFunc: ASIN^ LPAREN! assignExpr RPAREN! ;
atanFunc: ATAN^ LPAREN! assignExpr RPAREN! ;
cosFunc: COS^ LPAREN! assignExpr RPAREN! ;
expFunc: EXP^ LPAREN! assignExpr RPAREN! ;
invFunc: INV^ LPAREN! assignExpr RPAREN! ;
logFunc: LOG^ LPAREN! assignExpr RPAREN! ;
sinFunc: SIN^ LPAREN! assignExpr RPAREN! ;
sqrtFunc: SQRT^ LPAREN! assignExpr RPAREN! ;
tanFunc: TAN^ LPAREN! assignExpr RPAREN! ;
sqrFunc: SQR^ LPAREN! assignExpr RPAREN! ;
cubeFunc: CUBE^ LPAREN! assignExpr RPAREN! ;
powFunc: POW^ LPAREN! assignExpr COMMA! assignExpr RPAREN! ;
maxFunc: MAX^ LPAREN! assignExpr COMMA! assignExpr RPAREN! ;
minFunc: MIN^ LPAREN! assignExpr COMMA! assignExpr RPAREN! ;
decFunc: DEC^ LPAREN! assignExpr COMMA! unsignedInteger COMMA!
unsignedInteger RPAREN! ;
calculateSum: SUM^ LPAREN! KEYIDSTRING COMMA! RELATIVEKEYIDSTRING
RPAREN! ;
atom: signedNumber | ID | EEE | PI | LPAREN! assignExpr RPAREN! |
variable;
variable: KEYIDSTRING | RELATIVEKEYIDSTRING;
signedNumber: unsignedNumber | MINUS^ unsignedNumber;
unsignedNumber: unsignedInteger | unsignedReal ;
unsignedInteger: NUM_INT ;
unsignedReal: NUM_REAL ;
dummy: DOT | DOTDOT ;
//////////////////////////////////////////////////////////////////////////
/
class CalcLexer extends Lexer;
options {
k=2;
caseSensitive = true;
caseSensitiveLiterals = true;
}
tokens {
SUM = "sum";
VAR = "var" ;
ABS = "abs" ;
ACOS = "acos" ;
ASIN = "asin" ;
ATAN = "atan" ;
COS = "cos" ;
EXP = "exp" ;
INV = "inv" ;
LOG = "log" ;
SIN = "sin" ;
SQRT = "sqrt" ;
TAN = "tan" ;
SQR = "sqr" ;
CUBE = "cube" ;
POW = "pow";
MAX = "max";
MIN = "min";
DEC = "dec";
EEE = "E" ;
PI = "pi" ;
}
WS: (' ' |'\t' | '\n' | '\r') { _ttype = Token.SKIP; } ;
ID options { testLiterals = true; }
: ('a'..'z'|'A'..'Z'|'_') (ID_CHAR)* ;
protected
DIGIT : '0'..'9';
protected
ID_CHAR : 'a'..'z' | 'A'..'Z' |'_' | DIGIT ;
protected
ID_PART : (ID_CHAR)+ ( '.' (ID_CHAR)+)* '@' (DIGIT)+ ;
KEYIDSTRING : '#' ID_PART ('/' ID_PART)* ;
RELATIVEKEYIDSTRING : '.' ('.')? '/' ID_PART ('/' ID_PART)* ;
NUM_INT
{boolean isDecimal=false;}
: ( '0' {isDecimal = true;} // special case for just '0'
| ('1'..'9') ('0'..'9')* {isDecimal=true;} // non-zero decimal
)
// only check to see if it's a float if looks like decimal so far
( { LA(2)!='.' && LA(3)!='.' && isDecimal}?
( '.' ('0'..'9')* (EXPONENT)?
| EXPONENT
)
{ _ttype = NUM_REAL; }
)?
;
LPAREN: '(' ;
RPAREN: ')' ;
MINUS: '-' ;
PLUS: '+' ;
MULT: '*' ;
DIV: '/' ;
MOD: '%' ;
UP: '^' ;
COMMA: ',' ;
SEMI: ';' ;
// a couple protected methods to assist in matching floating point
numbers
protected
EXPONENT
: ('e') ('+'|'-')? ('0'..'9')+
;
//////////////////////////////////////////////////////////////////////////
/
class CalcTreeWalker extends TreeParser;
{
////////// Start special
functions ///////////////////////////////////////////
public String answer;
public Object delegate;
public String answer() {
return answer;
}
public void setAnswer(String value) {
answer = value;
if (delegate != null) {
Object[] keys = { "Sender" };
Object[] objects = { this };
NSDictionary dict = new NSDictionary(objects, keys);
try {
//Object result =
DelegateUtility.performMethodWithDelegate("parserDidAnswer", delegate,
dict);
} catch (Exception ex) {
}
}
}
public Object delegate() {
return delegate;
}
public void setDelegate(Object value) {
delegate = value;
}
public double lookupKeyString(String string) {
String result = "0";
System.out.println("lookupKeyString: " + string);
if(true) return 8.0;
if (delegate() != null) {
Object[] keys = { "Sender" , "KeyString" };
Object[] objects = { this , string };
NSDictionary dict = new NSDictionary(objects, keys);
try {
//result = (String)
DelegateUtility.performMethodWithDelegate("parserWantsValueForKeyString",
delegate, dict);
} catch (Exception ex) {
}
}
return new BigDecimal(result).doubleValue();
}
public double lookupRelativeKeyString(String string) {
String result = "0";
System.out.println("lookupRelativeKeyString: " + string);
if(true) return 7.0;
if (delegate() != null) {
Object[] keys = { "Sender" , "KeyString" };
Object[] objects = { this , string };
NSDictionary dict = new NSDictionary(objects, keys);
try {
//result = (String)
DelegateUtility.performMethodWithDelegate("parserWantsValueForRelativeKeyString"
, delegate, dict);
} catch (Exception ex) {
}
}
return new BigDecimal(result).doubleValue();
}
public double calculateSum(String repetitionKeyString, String
relativeKeyString) {
String result = "0";
System.out.println("rep: \"" + repetitionKeyString + "\" rel: \"" +
relativeKeyString + "\"");
if(true) return 47.0;
if (delegate() != null) {
Object[] keys = { "Sender", "RepetitionKeyString",
"RelativeKeyString" };
Object[] objects = { this, repetitionKeyString, relativeKeyString };
NSDictionary dict = new NSDictionary(objects, keys);
try {
//result = (String)
DelegateUtility.performMethodWithDelegate("parserWantsCalculatedSum",
delegate, dict);
} catch (Exception ex) {
}
}
return new BigDecimal(result).doubleValue();
}
public double functionDec(double value, double decimals, double
rounding) {
BigDecimal result = new BigDecimal(value);
if (rounding < 0 || rounding > 6) rounding =
BigDecimal.ROUND_HALF_DOWN;
if (decimals < 0) decimals = 0;
result = result.setScale((int)decimals, (int)rounding);
return result.doubleValue();
}
////////// End special
functions ///////////////////////////////////////////
}
expr returns [double r]
{
double a,b,c;
r=0;
}
: #(PLUS a=expr b=expr) {r = a+b;}
| (#(MINUS expr expr))=> #(MINUS a=expr b=expr) {r = a-b;} |
#(MINUS a=expr) {r = -a;}
| #(MULT a=expr b=expr) {r = a*b;}
| #(DIV a=expr b=expr) {r = a/b;}
| #(MOD a=expr b=expr) {r = a%b;}
| #(UP a=expr b=expr) {r = Math.pow(a,b);}
| #(ABS a=expr) {r = Math.abs(a);}
| #(ACOS a=expr) {r = Math.acos(a);}
| #(ASIN a=expr) {r = Math.asin(a);}
| #(ATAN a=expr) {r = Math.atan(a);}
| #(COS a=expr) {r = Math.cos(a);}
| #(EXP a=expr) {r = Math.exp(a);}
| #(INV a=expr) {r = 1/a;}
| #(LOG a=expr) {r = Math.log(a);}
| #(SIN a=expr) {r = Math.sin(a);}
| #(SQRT a=expr) {r = Math.sqrt(a);}
| #(TAN a=expr) {r = Math.tan(a);}
| #(SQR a=expr) {r = a*a;}
| #(CUBE a=expr) {r = a*a*a;}
| #(POW a=expr b=expr) {r = Math.pow(a,b);}
| #(MAX a=expr b=expr) {r = Math.max(a,b);}
| #(MIN a=expr b=expr) {r = Math.min(a,b);}
| #(DEC a=expr b=expr c=expr) {r = functionDec(a,b,c);}
| #(SUM s1:expr s2:expr) {r =
calculateSum(s1.getText(),s2.getText());}
| KEYIDSTRING {r =
lookupKeyString(expr_AST_in.getText());}
| RELATIVEKEYIDSTRING {r =
lookupRelativeKeyString(expr_AST_in.getText());}
| EEE {r = Math.E;}
| PI {r = Math.PI;}
| ni: NUM_INT {r = (double)Double.parseDouble(ni.getText());}
| nr: NUM_REAL {r = (double)Double.parseDouble(nr.getText());}
;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 9231 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20020529/40cd6b40/attachment.bin
More information about the antlr-interest
mailing list