[antlr-interest] where can put the rule of transfer between two languages

John B. Brodie jbb at acm.org
Thu Apr 15 16:58:44 PDT 2010


Greetings!

On Thu, 2010-04-15 at 22:08 +0000, Gouasmi Thouraya wrote:
> Please can someone help me,I want make a transfer between two languages and I want to use ANTLR .   
> Please
> can you tell me where I must put the rules of transformation between
> the two grammars? For the second grammar, do I have to put it? If yes,
> where I must to put it?  

I am sorry but I am not sure I understand your question.

Are you trying to translate an input, in source language A, into an
output, in target language B. For example, are trying to do something
similar to translating C++ into C; or another example translating OCAML
to Scheme?

If so, maybe the below may help. If not, this message will be useless.

So if you want to translate input given in source language A and produce
output in target language B using ANTLR to orchestrate, you *REALLY*
need to read one or both of Dr. Parr's books (I believe the
www.antlr.org website has links to these publications, ask if you are
unable to locate them).

I believe the basic outline of the translation process is as follows:

1) create a grammar to parse (and lex) the source language A. this
grammar should produce an appropriate Abstract Syntax Tree (AST) in
order to support subsequent processing.

2) if your input is not certified as valid for language A, you will need
to develop appropriate tree walking phase(s) to validate the source AST.
E.G. if necessary, perform appropriate semantic analysis of the AST to
ensure that the input truly conforms to source language A.

3) develop a family of StringTemplates that, given an individual AST
node, will produce the desired text of the target language B (probably
by appropriately gluing together other StringTemplate results passed as
parameters).

4) write a tree walker grammar that will orchestrate calls upon the
StringTemplate library from step 3) above. each rule in this tree walker
passes the requisite intermediate strings to the proper template and
accumulates the resultant strings into a single String comprising the
entire resultant target language B program.

5) output the String resultant from step 4) to a file. 


and you really should read one of Dr. Parr's books in order to get a
better answer.

Hope this helps....
   -jbb




More information about the antlr-interest mailing list