[antlr-interest] notation algorithm translation

Christian chwchw at gmx.de
Thu Dec 8 01:04:06 PST 2011


Hello,

which examples did you try and at which point had you stopped following?
Here is an example from the official website:
http://www.antlr.org/wiki/display/ST/Language+Translation+Using+ANTLR+and+StringTemplate

In your procedure rule, you have to add action code with ST code inside.
You must define a template that represents a void function with
parameters for the function's name, its formal parameters and
corresponding values, and its body.

Regards,
Christian

Am 08.12.2011 03:36, schrieb wijanarto wijanarto:
> Hi,
> Thank you for you last email, but i still not understand, how to
> translate 'Procedure' to 'void' using ST inside ANTLR. I have try a
> lot examples to
>  do that, but i got wrong result. Can you give me simple example to
> translate 'Procedure' to 'void' using st and antrl, please.Thank You.
> Regards,
> Wijanarto
> ------------------------------------------------------------------------
>   *From:* Christian <chwchw at gmx.de>
> *To:* wijanarto wijanarto <wijanarto.nagan at yahoo.com>
> *Cc:* "antlr-interest at antlr.org" <antlr-interest at antlr.org>
> *Sent:* Monday, November 28, 2011 8:48 PM
> *Subject:* Re: [antlr-interest] notation algorithm translation
>
> Hello,
>> is ST and/or Antlr can solve that problem?
> Sure, it can! But a pattern matching framework could be a better
> alternative because it is more straight forward in mapping input to
> output. However, if you at all costs want to use ANTLR in combination
> with StringTemplate, you need to do the things I wrote to you.
>
> But for the sake of clarification, I name the steps again:
>
>  1. Define a lexer and a parser grammar for your input notation
>  2. Insert actions into your parser grammar (here you can use ST to
>     define your output in C)
>  3. Generate the corresponding lexer and parser in your favorite
>     programming language
>  4. Execute the lexer and parser on a text/file that contains an
>     instance of your notation (for example a particular algorithm)
>  5. Read the result of the parser
>      1. If you use ST, you can get the whole output from the result
>      2. If you, however, did not use ST, you have to programmatically
>         map the input tokens (such as 'Procedure' and 'Integer') to
>         your willing output tokens ('void' and 'int', respectively)
>  6. Save the result/mapping result to a file, for example
>     'converted_example_algorithm.c'
>
>
> Regards,
> Christian
>
> Am 28.11.2011 14:25, schrieb wijanarto wijanarto:
>> Hi...Sorry about my poor english, formally, i never learn english. I
>> hope in this example below can make it clear.
>> Input Notation:Procedure Bla(var input X:Integer;var output
>> Y:Integer) {//algorithm}
>> should be translate become (in C)
>> Output Language: void Bla (int X, int *Y){//algorithm}
>> is ST and/or Antlr can solve that problem?
>>
>>
>>
>>
>> ------------------------------------------------------------------------
>> *From:* Christian <chwchw at gmx.de> <mailto:chwchw at gmx.de>
>> *To:* wijanarto wijanarto <wijanarto.nagan at yahoo.com>
>> <mailto:wijanarto.nagan at yahoo.com>
>> *Sent:* Friday, November 25, 2011 5:31 AM
>> *Subject:* Re: [antlr-interest] notation algorithm translation
>>
>> Hello,
>>
>> unfortunately, I have some problems in understanding your english and
>> following your thoughts. So, I try to interpret:
>>
>>> so i have to create language for my notation (used ST-4.0.4) 
>> I think, you're right - only your phrasing is confusing. You have to
>> define an ANTLR grammar that represents your notation. You can then
>> insert actions to the grammar that utilize StringTemples (ST-4.0.4)
>> to directly generate your corresponding programming language fragment.
>>
>> I, however, recommend a separation of the pure grammar from output
>> generation for the sake of overview and adaptability. That means, do
>> not use actions and string templates in the grammar.
>>
>> Either write a program that maps the grammar parts (you get them from
>> the automatically generated ANTLR parser from your notation grammar)
>> to the corresponding programming language code (e.g., Java, C, or C#)
>> or do not use ANTLR but a pattern matching framework. The latter
>> should be much easier for language translation purposes (see, again,
>> the link that I have given to you in my first answer).
>>
>> Regards,
>> Christian
>>
>> Am 24.11.2011 14:38, schrieb wijanarto wijanarto:
>>> Hi Chris, thank you for fast reply,  ok i get it. I just want to
>>> help student to test algorithm that they wrote in notation as a
>>> function or procedure, and then compare it with actual code which is
>>> tested. It just for academic learning, that is my idea.
>>> so i have to create language for my notation (used ST-4.0.4) and
>>> then translate them into formal language (java/c, used ANTLR) to get
>>> source code, finally  compile and execute. Am i right ?????
>>>
>>> ------------------------------------------------------------------------
>>> *From:* Christian <chwchw at gmx.de> <mailto:chwchw at gmx.de>
>>> *To:* antlr-interest at antlr.org <mailto:antlr-interest at antlr.org>
>>> *Sent:* Tuesday, November 22, 2011 5:30 PM
>>> *Subject:* Re: [antlr-interest] notation algorithm translation
>>>
>>> Hi and Wellcome!
>>>
>>> First of all, ANTLR is a parser generator. Thus, ANTLR generates a
>>> parser in several programming languages, such as Java and C#, from an
>>> ANTLR-grammar. In order to read/parse your algorithm notation, you need
>>> to have or define an ANTLR grammar for this language. Then, ANTLR can
>>> generate a parser that is able to read your algorithm notation.
>>>
>>> If you further want to translate it your notation into source code, you
>>> have to write a mapping between your notation and the resulting source
>>> code. But you unfortunately gave less information about what you exactly
>>> want to do.
>>>
>>> For language translation, you should also have a look at this article,
>>> especially at chapter "Walking the AST":
>>> http://www.antlr.org/article/1136917339929/stringTemplate.html
>>>
>>> Regards,
>>> Christian
>>>
>>> Am 22.11.2011 04:23, schrieb wijanarto wijanarto:
>>> > Hi, I am totally newbie in DSL with antlr. It it posibble to
>>> create translation from algorithm notation (natural language) to
>>> generate specific source code (formal language) (eg. C/java)
>>> >
>>> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>> > Unsubscribe:
>>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>> >
>>>
>>>
>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>> Unsubscribe:
>>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>>
>>>
>>
>>
>>
>
>
>



More information about the antlr-interest mailing list