[antlr-interest] Confirming my approach
Malte Stien
malte.stien at web.de
Fri Sep 21 07:12:50 PDT 2007
Hello everyone,
I have been using ANTLR for a few days now, and I think I am getting
used to it. I also bought the book and rest the best part of it,
however, I would just like to run something past you all to see whether
I have taken the right approach or whether I am on the utterly wrong
path. I have to match an input language that looks a bit like this:
domain foo
has class a
has attribute ax
has attribute ay
end class
has class b
has attribute bx
has attribute by
end class
end domain
The example is a bit 'dumbed' down to get my point across a little
easier. So I have my grammar-file and I can match the language and
everything is well. Now, as the next step, I would like to create an
instance of some Java-class for every domain, every class and every
attribute and I would like to link the class to my domain and the
attributes to my classes. I am using a tree grammar with the following
two rules (amongst others):
class_declaration[ArrayList<ModelClass> modelClasses] returns
[ModelClass modelClass]
:
^(CLASS name=IDENTIFIER)
{
modelClass = new modelClass($name.text);
modelClasses.add(modelClass);
}
;
domain_declaration[]
:
{
ArrayList<ModelClass> modelClasses = new
ArrayList<ModelClass>();
}
^(DOMAIN IDENTIFIER class_declaration[
modelClasses]*)
{
ModelDomain modelDomain = new ModelDomain($IDENTIFIER.text);
for (ModelClass modelClass : modelClasses) {
modelClass.setModelDomain(modelDomain);
}
}
;
Basically, I am creating an ArrayList just before my domain_declaration
is matched, then pass that ArrayList to my class_declaration-rule so
that all my classes can add themselves to the list and then after the
domain_declaration-rule has matched, it creates a ModelDomain and adds
all the ModelClasses to it (by modelClass.setModelDomain(modelDomain)).
Is that the way to go? It appears a little long-winded to me.
I looked at two other options, neither of which seem to work:
- Having the created modelDomain available to all rules beneath using
the @scope attributes described in Chapter 4.5 of the book. However,
that does not seem to work as the modelDomain instance does not actually
come into existence until after the domain_declaration has completely
matched and that includes unfortunately the class_declarations.
- Having ANTLR create the ArrayList for me by using the += operator as
described in Chapter 6.1 of the book. Then ANTLR would throw those
ModelClasses into that ArrayList automatically and the
domain_declaration could proceed similarly to what it does now. As far
as I can see, however, that does not work either as one has to have an
output-option specified to use the += operator and one cannot specify
output options for tree grammars.
I thought, I may be able to use a variation on the first option, where I
don't make the ModelDomain-instance available via the @scope attribute,
but rather just the name of the domain. That would mean, though, that in
the model_declaration-rule, the instance would need to be retrieved just
from the name from some lookup-table, hashmap or similar. That however,
would not work either as again that ModelDomain-instance would not have
been created, yet, when in the class_declaration-rule.
So, what I have done above is really the only thing I could come up
with, and, as I said earlier, it does look a little long-winded to me.
If any of you more experienced guys have any opinion on this, I would
greatly appreciated that.
Thank you.
Malte.
More information about the antlr-interest
mailing list