[antlr-interest] Re: tree recognition during multiple passes

excel_robot dermot_oneill at hotmail.com
Mon May 10 10:25:40 PDT 2004


Thanks for your fast reply.
I work for Oracle and I'm researching transformations between SQL
dialects.
I was thinking about what things I would do in the passes.
I had the following choices
1) Would each pass handle one SQL statement( one for the select
statement, one for the update statement ...)
or 
2) Would the first pass handle expressions, the second pass handles
all DML ...

If I went the second way with the following simple example.
SELECT col1 + 2 FROM table1

my source AST would look like this
SELECT
|-SELECT_LIST
| |-+
|   |-col1
|   |-2
|
|-FROM
  |-table1
  
  
My first pass would change the expression
SELECT
|-SELECT_LIST
| |-TARGET_AST
|   |-col1
|   |-2
|
|-FROM
  |-table1

Then I couldn't recognize the SELECT_LIST as it was half source AST
and half target AST

Its easy enough for me to create the source AST grammar and the target
AST grammar.
I think what your saying is that I write a new AST grammar that can
recognize both.

select: #(SELECT select_list from)
;

select_list: #(SELECT_LIST 
                 #((operator|TARGET_AST) // choice between source and
target AST's
                      column number) 
;


I see what you mean but I was hoping too keep things as neat as possible. 
If the source or target AST changed then I would have to update this
into my grandaddy of all grammars
I used to think super grammars could combine a source and target AST
for me but after reading a few of your own articles I realized that
this is not what they are for.

Maybe I should just transform entire sub trees to the target and not
worry about having a sub tree which was a combination of both.
Im only working on a simple prototype at the moment but Im trying to
think of some of the big issues and approaches.

Thanks again
Dermot.


--- In antlr-interest at yahoogroups.com, Monty Zukowski <monty at c...> wrote:
> 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