[antlr-interest] Modifying tree based on semantic content

David Jameson dhjdhj at gmail.com
Fri May 15 04:01:48 PDT 2009


Tilman, thanks for the suggestion. Sadly, that approach will only work  
if a and b are literals. It will not work if the non-terminals a and/ 
or b represent identifiers whose type can only be determined by  
checking the symbol table.



Cheers,
D

On May 15, 2009, at 4:43 AM, Tilman Bender wrote:

> Hi David,
>
> I think this is possible with rewrite rules, but it depends on how  
> you structure you grammar:
>
> e.g. Have lexer rules for STRING, INT, FLOAT so you could do things  
> like
>
> expr:
> 	a=STRING '+' b=STRING -> ^(CONCATENATE a b)
> |	a=number '+' b=number -> ^(PLUS a b)
> ;
>
> number:
> 	INT
> |	FLOAT
> ;
>
> This is just a quick brain-dump but might give you hints in the  
> right direction.
>
> Tilman Bender
> Student des Software Engineering
> Hochschule Heilbronn
> tbender at stud.hs-heilbronn.de
>
>
>
> Am 15.05.2009 um 05:38 schrieb David Jameson:
>
>> Is there any way to control the built-in tree generation (from an
>> initial parse phase) based on semantics of what is being parsed?
>>
>> As a simple example,    if   I see the expression
>>        a + b
>>
>> then I want to produce
>>     (PLUS a b)
>> or
>>    (CONCATENATE a b)
>>
>> depending on whether a and b are numeric or string.
>>
>>
>> How can I do this with rewrite rules (for example)?   Or do I have to
>> construct my own trees?
>>
>>
>> Thanks,
>> D
>>
>> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090515/825dda39/attachment.html 


More information about the antlr-interest mailing list