[antlr-interest] Using Own classes to recolect or parse the data withing the grammar

Bart Kiers bkiers at gmail.com
Wed Aug 11 23:35:37 PDT 2010


On Thu, Aug 12, 2010 at 7:10 AM, Victor Giordano
<power_giordo at yahoo.com.ar>wrote:

> ...
>
> The question is: How i can do this, without definig the class inside the
> @menbers section. That class, in fact, is one of my program.. so i only
> want to reuse that. ...
>

Hi Victor,

Define a class LinearExpr like this:

package foo;

public class LinearExpr {

    // ... the rest of your class...
}


and in your grammar, import that class like this:

grammar LinearExpression;

@header {
    import foo.LinearExpression;
}

// ... the rest of your grammar ...


@header will automatically put everything you define in there at the start
of your parser class. It does the same as:

@parser::header {
    import foo.LinearExpression;
}


// ... the rest of your grammar ...


If you want your lexer and parser to be in the same package, say foo.parse for
example, do something like this:

grammar LinearExpression;

@parser::header {
    package foo.parse;
    import foo.LinearExpression;
}

@lexer::header {
    package foo.parse;
    import foo.LinearExpression;
}

// ... the rest of your grammar ...


Regards,

Bart.


More information about the antlr-interest mailing list