[antlr-interest] Templates in rules

Arnulf Heller aheller at gmx.at
Fri Apr 28 10:37:16 PDT 2006


Hi,

depending on the direction of flow of information (top-down, 
bottom-up), you can do the following:

class A : public base;
class B : public base;

Foo [vector<base*>& v]
:
         ( X {v.push_back(new A);} | Y {v.push_back(new B);} )
;

alternatively, you can return different vectors

class AnyVector { /* empty */ };
class Avector : public AnyVector { std::vector<A> imp; };
class Bvector : public AnyVector { std::vector<B> imp; };

Foo returns [AnyVector* v]
:
         ( X {v = new AVector;} | Y {v = new BVector;} )
;

or

X returns [AVector* v]
Y returns [BVector* v]
Foo returns [AnyVector* v] : (X|Y);

don't take the code above literally, I don't know the exact ANTLR 
syntax by heart.

hope that helps,
arnulf

At 08:21 27.04.2006, you wrote:
>Hi all,
>
>The software I'm currently writing (in C++) uses a lot of template classes.
>I'm also writing a frontend in ANTLR that has to interact with those
>template classes.
>What I found myself doing was writing wrappers for those template classes so
>that I could pass the as a variable from one rule to another but this is a
>very inconvenient way.
>
>So let's say I have;
>std::vector<A>
>std::vector<B>
>
>I have written the following wrapper classes; class AnyVector {
>         ...
>         std::vector<A> imp;
>};
>class Avector : public AnyVector
>{
>         ...
>         std::vector<A> imp;
>};
>
>class Bvector : public AnyVector
>{
>         ...
>         std::vector<B> imp;
>};
>
>So that I can have the following rule
>
>Foo [AnyVector& theVector]
>:
>         ( X | Y )
>;
>
>What I would like to have is something like this;
>
>Foo <Element>[std::vector<Element>& theVector]
>:
>         ( X | Y )
>;
>
>Is there any way that I can parametrize an antlr rule to achieve that
>effect?
>
>TIA,
>
>Corno



More information about the antlr-interest mailing list