[antlr-interest] 3.0 multiple language support
Thomas Dudziak
tomdz at first.gmd.de
Wed Aug 4 06:10:47 PDT 2004
Sebastian Kaliszewski wrote:
>Tiller, Michael (M.M.) wrote:
>
>
>>>How about allowing different language versions of the same action in
>>>the same file? Something like:
>>>
>>>my_prod
>>> : my_subpod %C{ /* here C action comes */ }
>>> %J{ /* here java action goes */ }
>>> %P{ /* here python action goes */ }
>>>
>>>
>>Of the options I have seen so far, I prefer the one where the grammar
>>just has macros and the actions themselves are specified outside the
>>grammar, e.g.
>>
>>some_rule
>> : a:A "," b:B << process_some_rule(a,b); >>
>>
>>And then you could weave together an action file with a grammar file.
>>So you might have a C++ action file:
>>
>>void process_some_rule(A *a, B* b)
>>{
>> /* Do something very general in this language */
>>}
>>
>>
>>
>
>I really don't like this. Actions (esp. predicates) are part of the grammar.
>Separating them makes code much harder to read, analyse and maitain, as one
>has to jump around the code text.
>Macros might be a nice idea, but they should be intermixable with the
>parser/lexer definition in the same file.
>
>
I find Michaels solution much easier to maintain. Imagine the Java
grammar with embedded actions for all supported languages - it will
surely be not readable anymore. Defining labels, e.g.
"process_some_rule" is far better IMO. You can simply add all
information as parameters that is required by the action. The net result
is that the grammars themselves are target-language-independent
(remember the newbie asking about a Java version for the SQL grammar ?)
and you have the action in a concise form, perhaps in a single class. Or
Antlr generates abstract method declarations (for C++/C#/Java) in the
parser that the developer has to override in a concrete parser subclass
(template and hook).
There are a few situations where this might seem like overkill (e.g.
maintaining and using a counter for a ()* subrule that shall be
traversed like 20 times), but there might be better ways to handle these
situations (e.g. defining syntax for it - say, ()[20] - and letting
Antlr work out the details).
Tom
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list