[antlr-interest] How does one handle variable number of function
parameters?
Rob Greene
robgreene at gmail.com
Tue Nov 29 10:59:55 PST 2005
I'm sorry, but I'm still totally lost. I've chopped out everything
except numbers and the function calls trying to simplify, but no
luck... Here's the code that doesn't work, I'm hoping somebody can
straighten me out. Anything with more than two "#" characters is
simply copy & pasted. :-)
-Rob
class FunctionParser extends Parser;
options {
buildAST = true;
k = 1;
exportVocab = Function; // name of the vocabulary
}
imaginaryTokenDefinitions
: ARGLIST
;
expression
: NUMBER
| IDENTIFIER LPAREN^ argList RPAREN!
| LPAREN! expression RPAREN!
;
argList
: expression (COMMA! expression)* { ## = #( #[ARGLIST,"ARGLIST"], ## ); }
;
class FunctionLexer extends Lexer;
options {
caseSensitive=false;
k = 2;
exportVocab = Function; // name of the vocabulary
}
IDENTIFIER
options { testLiterals = true; }
: 'a'..'z' ( 'a'..'z' | '0'..'9' )*
;
NUMBER
: ('0'..'9')+ ( '.' ('0'..'9')+ )?
;
// Whitespace -- ignored
WS : ( ' '
| '\t'
| '\f'
| ( "\r\n" // Evil DOS
| '\r' // Macintosh
| '\n' // Unix (the right way)
)
)
{ $setType(Token.SKIP); }
;
LPAREN
: "("
;
RPAREN
: ")"
;
COMMA
: ","
;
class FunctionTreeParser extends TreeParser;
expression returns [double value]
{
double a,b;
value = 0;
}
: n:NUMBER { value = Double.parseDouble(n.getText());
System.out.println("number = " + value); }
| i:IDENTIFIER
// ( arg=expression { System.out.println("Adding argument " + arg); })*
{ System.out.println("Executing function " + i.getText()); }
;
argList
: #( ARGLIST
(
d:expression { System.out.println("expression = " + d); }
(
e:expression { System.out.println("expression = " + e); }
)*
)?
)
;
More information about the antlr-interest
mailing list