How do I add constructor parameters to the class definition?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jennifer_Bevan
Posted On:   Monday, May 6, 2002 11:52 AM

Hi, I would like to be able to specify constructor parameters within my class definition. I tried to extend the rule parameter method, by using: class JavaTreeParser extends TreeParser [MyType myarg]; Unfortunately, antlr choked on this, expecting a semicolon after the TreeParser but getting the parameter list: expecting SEMI, found '[MyType myarg] ...(lots of follow-on errors)... rule classDef trapped: java.importdg.g:14: unexpected token MyType myarg I haven't had any luck using the jguru search for this question, or for finding examples of how others may have already done this. Waaah. Any   More>>

Hi,
I would like to be able to specify constructor parameters
within my class definition. I tried to extend the rule
parameter method, by using:

			
class JavaTreeParser extends TreeParser [MyType myarg];


Unfortunately, antlr choked on this, expecting a
semicolon after the TreeParser but getting the parameter
list:

			
expecting SEMI, found '[MyType myarg]
...(lots of follow-on errors)...
rule classDef trapped: java.importdg.g:14: unexpected token
MyType myarg


I haven't had any luck using the jguru search for this
question, or for finding examples of how others may have
already done this. Waaah. Anyway, all help is, of
course, greatly appreciated...


Jennifer Bevan

   <<Less

Re: How do I add constructor parameters to the class definition?

Posted By:   Monty_Zukowski  
Posted On:   Tuesday, May 7, 2002 01:08 PM

The only way you can do what you want is to put them as methods in the class:

class MyParser extends Parser;
options{}
{
MyParser(TokenStream lexer, MyObject whatever){
super(lexer);
...
}
}
rule1:...;

There are a few differenct constructors generated and you may want to extend them with your parameters. Look at the generated code.
About | Sitemap | Contact