[antlr-interest] How an alternative rule can emit code conditionally on the type of the parent node?

Aggelos Biboudis biboudis at gmail.com
Thu Sep 27 10:26:29 PDT 2012


Thank you. I managed to save some information in a global scope and then 
querying it accordingly.

Aggelos

On 09/26/2012 07:41 PM, Douglas Godfrey wrote:
> Pass the type of the parent node as a parameter to the rule and
> 1)	use syntactic or semantic predicates to change the parse based on the
> parent type
> or
> 2)	use the parent type to change the actions that generate code or symbol
> table entries
> or
> 3)	use the parent type to change the emitted text via string templates
>
>
>
> On 9/26/12 9:05 AM, "Aggelos Biboudis" <biboudis at gmail.com> wrote:
>
>> It seems dynamic scoping is the solution to this.
>>
>> Thx,
>> Aggelos B.
>>
>> On Wed, Sep 26, 2012 at 2:12 PM, Aggelos Biboudis
>> <biboudis at gmail.com>wrote:
>>
>>> Dear all,
>>>
>>> I am using the C grammar with rewrite option on. I would like to know if
>>> it is possible for my alternative rule (after the "->"), to emit code
>>> depending on the type of the parent node. I want to rewrite the token
>>> buffer in-place (following the book for source-to-source translations
>>> when
>>> performing slight modifications/instrumentation) only in compound
>>> statements. My intention is to emit a preamble after the first "{" only
>>> if
>>> the compound statement originates from a selection, iteration or jump
>>> statement. For example: I would like to rewrite the if (expr) {
>>> statement1;} to if (expr) { my_statement; statement1;}. Is that
>>> conditional
>>> rewriting possible, following the token-buffer edit way?
>>>
>>> I am using the usual ANSI C
>>> grammar<http://www.antlr.org/grammar/1153358328744/C.g>and following the
>>> definitive ANTLR reference.
>>>
>>> Any help appreciated.
>>> Aggelos B.
>>>
>> 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