[antlr-interest] Re: Translators Should Use Tree Grammars

Anakreon anakreonmejdi at yahoo.gr
Tue Nov 23 08:51:43 PST 2004


atripp54321 wrote:
> 
> Anakreon,
> OK, thanks. Now I see.
> So you have, for example, this rule in XML:
>   <!-- Check w3 for header name -->
>   <property name="CacheControl" type="DSTRING">
>     <arg type="DSTRING" />
>     <read>
>       <ast name="METHOD_CALL" text="session_cache_limiter" />
>     </read>
>     <write>
>       <ast name="METHOD_CALL" text="session_cache_limiter">
>           <ast name="ARGLIST_VALUES">
>               <arg index="1" />
>           </ast>
>       </ast>
>     </write>
>   </property>
> 
> ...which says to replace this:
> Response.CacheControl = "X"
> ...with this:
> session_cache_limiter("X");
yes
> 
> 
> I'm still not seeing how you do the more complicated stuff like
> accessing information that's in different parts of the AST.
More information on how xml is used as template for translation
can be found at http://www.antlr.org/workshop/ANTLR2004/proceedings/XMLAsASTTemplates.pdf
> Do you have a symbol table? And can you get a list of all
There is a symbol table populated by the parser.
> the references to a particular variable? In other words,
I do not need this information, if I understand correctly your question
For example, if I find this code in Jscript:
var b = a.length
All I need to know is what is variable "a"
>>From the symbol table, I can find out what kind of object is referenced
by a, and handle the case.    
But I do not need to know what variable "a" was referencing before.
For example:
a = 30; //is an instance of class Number
a = "Hello" //is an instance of class String
var b = a.length //length is a property of String, it used to be a Number
but at this point the TreeParser does not have that information and does
not care.

> the XML files in the object directory specify the simpler
> types of transformations, but where is the code for
> the more complicated cases?
All the code is at the *_php.act files.
Additionally a family of classes hold information about classes
and objects. They are illustrated at the doc I sent to antlr workshop.

I'll use an example:
a = "Hello"
The AST for this is:
(EXR ASSIGN a "Hello)
In the rule for ASSIGN, a check is done if the left operand is an IDENTIFIER
(it is in this case).
If is an IDENTIFIER, the right operand is checked.
At this particular case, the right operand is an instance of class String 
(described at the objects/string.xml file).
The symbol table (a Map in my case) is updated with key equal to "a" and 
value an instance of ASPObjectInstance.
var b = a.lenght
AST: (EXPR ASSIGN b (DOT a length))
First the subtree (DOT a length) is examined.
>>From the symbol table, I retrieve the ASPObjectInstance bounded to this variable.
The ASPObjectInstance contains this info:
class: String
instance: a
member: null
I get the class String, and query for a member called length.
The result is a Property instance.
The ASPObjectInstance is updated to contain the info:
class: String
instance: a
member: length (instance of Property)
The subtree is transformed into a node of type ObjectAST

Now the tree is:
 
(EXPR ASSIGN b ObjectAST) 
The rule for assign is called.
The ASPObjectInstance is taken from the ObjectAST node, and
the return type of the Property is checked.
This is done because if the return type is not a primitive, the symbol table should be
updated.
A property has two methods, a read and a write as it should be obvious from
the  XML snipset you posted.
In this case we are reading from the property.
>>From the ASPObjectInstance the member is retrieved and the method:
AST read(AST instance) is called where the value of the parameter in this
case is the node [IDENTIFIER, "a"].

>>From the description of the class provided in the XML file, and the argument
given to the method the AST ([METHOD_CALL, "strlen] ([ARGLIST_VALUES] a)) is 
created.

The final AST produced from (EXPR ASSIGN b (DOT a length))
is (EXPR (ASSIGN b (strlen (ARGLIST_VALUES a)))

I could describe a more complicated code sample but this
the idea behind the translation.

Anakreon


 
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