[antlr-interest] passing more than one parameter to a rule
Mark Volkmann
r.mark.volkmann at gmail.com
Tue Dec 11 12:46:24 PST 2007
On Dec 11, 2007 2:02 PM, Terence Parr <parrt at cs.usfca.edu> wrote:
>
> On Dec 11, 2007, at 11:58 AM, Mark Volkmann wrote:
>
> > On Dec 11, 2007 1:30 PM, Terence Parr <parrt at cs.usfca.edu> wrote:
> >> Hi Mark. Are you using the latest build? This sounds familiar.
> >
> > I'm using version 3.0.1.
>
> Can u try:
>
> http://www.antlr.org/download/build/antlr-2007-12-10.19.tar.gz
I got the same error with that version. Here's the output. I use Ant
to run this.
gen:
[java] ANTLR Parser Generator Version 3.1b1 (??) 1989-2007
[java] ANTLR Parser Generator Version 3.1b1 (??) 1989-2007
[move] Moving 3 files to
/Users/Mark/Documents/Programming/ANTLR/Math/gen/com/ociweb/math
compile:
[javac] Compiling 8 source files to
/Users/Mark/Documents/Programming/ANTLR/Math/build/classes
[javac] /Users/Mark/Documents/Programming/ANTLR/Math/gen/com/ociweb/math/MathParser.java:372:
illegal start of expression
[javac]
polynomial11=polynomial((fn!=null?input.toString(fn.start,fn.stop):null),
, (fv!=null?input.toString(fv.start,fv.stop):null));
[javac]
^
[javac] /Users/Mark/Documents/Programming/ANTLR/Math/gen/com/ociweb/math/MathParser.java:372:
')' expected
[javac]
polynomial11=polynomial((fn!=null?input.toString(fn.start,fn.stop):null),
, (fv!=null?input.toString(fv.start,fv.stop):null));
[javac]
^
[javac] 2 errors
In case you want to try it, here's my full lexer/parser grammar ...
fairly small.
grammar Math;
options {
output = AST;
}
tokens {
ASSIGN;
DEFINE;
DERIVATIVE;
POLYNOMIAL;
PRINT;
SCRIPT;
TERM;
}
@header {
package com.ociweb.math;
}
@lexer::header {
package com.ociweb.math;
}
coefficient: NUMBER;
exponent: '^' NUMBER -> NUMBER;
functionName: NAME;
assign: variable '=' value -> ^(ASSIGN variable value);
define
: fn=functionName '(' fv=variable ')' '=' polynomial[$fn.text, $fv.text]
-> ^(DEFINE $fn $fv polynomial);
derivative: functionName '\'' '(' variable ')' -> ^(DERIVATIVE functionName);
// fnt = function name text; fvt = function variable text
polynomial[String fnt, String fvt]
: term[$fvt] (SIGN term[$fvt])* -> ^(POLYNOMIAL term (SIGN term)*);
print
: 'print' functionName '(' value ')' -> ^(PRINT functionName value)
| 'print' functionName '(' variable ')' -> ^(PRINT functionName variable)
| 'print' variable -> ^(PRINT variable)
;
// This is the "start rule".
script: (statement TERMINATOR)* EOF -> ^(SCRIPT statement*);
statement: assign | define | derivative | print;
// fnt = function name text; fvt = function variable text
term[String fnt, String fvt]
// tv = term variable
: c=coefficient? tv=variable? e=exponent?
{ tv == null ? true : ($tv.text).equals($fvt) }? // a validating
semantic predicate
-> ^(TERM $c? $tv? $e?)
;
catch [FailedPredicateException fpe] {
String tvt = input.toString(tv.start, tv.stop);
System.err.println("In function " + fnt + " the term variable " + tvt +
" doesn't match function variable " + fvt);
}
value: NUMBER;
variable: NAME;
COMMENT: '#' (~'\n')* { $channel = HIDDEN; };
CONTINUATION: '\\' NEWLINE { $channel = HIDDEN; };
NAME: 'a'..'z';
// Windows uses \r\n. UNIX and Mac OS X use \n.
NEWLINE: '\r'? '\n' { $channel = HIDDEN; };
NUMBER: INTEGER | FLOAT;
fragment FLOAT: INTEGER '.' NATURAL_NUMBER;
fragment INTEGER: SIGN? NATURAL_NUMBER;
fragment NATURAL_NUMBER: '0' | '1'..'9' '0'..'9'*;
SIGN: '+' | '-';
TERMINATOR: ';'; // | NEWLINE;
WHITESPACE: (' '|'\t')+ { $channel = HIDDEN; };
Here's some sample input.
f(x) = 2x^4 - 5x + 7 - x^3 + x;
print f(-2);
--
R. Mark Volkmann
Object Computing, Inc.
More information about the antlr-interest
mailing list