[antlr-interest] tree recognition during multiple passes
Monty Zukowski
monty at codetransform.com
Mon May 10 09:06:19 PDT 2004
There are usually a few places where languages converge. In my AREV to
VB translator it was expressions & statements. A typical tree rule for
expressions would be:
expr:
#(PLUS expr expr)
| #(MINUS expr expr)
| #(MULT expr expr)
| #(DIVIDE expr expr)
...
| IDENTIFIER
;
Let's say your target AST has other expression types, maybe it can
format strings. Break your expressions into rules for the two
languages:
expr:
aExpr
| bExpr
;
aExpr:
#(PLUS expr expr)
| #(MINUS expr expr)
| #(MULT expr expr)
| #(DIVIDE expr expr)
...
| IDENTIFIER
;
bExpr:
#(STRFORMAT FORMAT (expr)+ //format string & variable number of args
...
| B_IDENTIFIER //identifier name-mangled to fit into language B
Similarly you can combine statements from the different languages. The
key is that the node types must not overlap. In essence your tree
grammar will be for both languages joined together at the abstractions
they share such as identifiers, types, expressions, etc.
By the way what's your project?
Monty
On May 10, 2004, at 4:11 AM, excel_robot wrote:
> Hi Guys,
> I'm using Antlr to develop a source to source translator.
> I have read a good bit of the doc and articles and they all seem to
> suggest the following
> 1)lex your input into tokens
> 2)recognize the tokens in your grammar and build a source AST
> 3)recognize and morph your source AST to a target AST using multiple
> passes
> 4)generate your output from the target AST
>
> My problem lies with recognizing the entire AST during the passes, as
> it is neither a pure Source AST or pure Target AST.
> In the following FAQ
> http://www.jguru.com/faq/view.jsp?EID=759279
> Mr Zukowski mentions that you can combine 2 languages into the same
> tree.
>
> My Question is how do I recognize a tree during the passes?
> A solution would be that for each pass I convert a sub tree entirely
> from Source AST to Target AST. I could then verify the sub tree
> against the Target AST recognizer.Then this sub tree would not be
> modified again by future passes.
>
> What I could not do is half convert a sub tree in one pass and then
> finish it off in another pass. The reason being I could not recognize
> this sub tree after the first pass.
>
> Am I on the right track? Or are trees recognizable when they are
> neither pure Source AST or pure Target AST.
>
> Thanks for your time.
> Dermot.
>
> PS: I have worked with javacc for a few years and this will be my
> first Antlr project.
> So far I'm kicking myself I didn't jump to Antlr sooner. This forum
> ,docs, articles and examples are Top.
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
Monty Zukowski
ANTLR & Java Consultant -- http://www.codetransform.com
ANSI C/GCC transformation toolkit --
http://www.codetransform.com/gcc.html
Embrace the Decay -- http://www.codetransform.com/EmbraceDecay.html
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