[antlr-interest] Objective-C tree grammars with 3.0.1

Kay Röpke kroepke at classdump.org
Sun Nov 11 08:55:39 PST 2007


Hi Basil!

On Nov 11, 2007, at 8:13 AM, Basil Shkara wrote:

> I ran an example tree grammar and set the language option to ObjC  
> and it generated fine so I guess that answers my previous question  
> about whether ANTLR supports tree grammars for objective-c.
>
> However I still do not understand why my particular grammar is not  
> able to be generated.  I am going through Terence's book (Definitive  
> ANTLR reference) and converting the java examples to objective-c  
> progressively.  So far I have been able to write and generate  
> everything except for the tree grammar.
>
> The grammar I have been trying to generate is below.
>
> Any ideas?


Turns out that I've broken the ASTLabelType option with a typo in the  
code generation templates.
Thanks for catching this!

I've uploaded a fixed version to http://classdump.org/articles/2007/11/11/antlr-3-1-early-access-source

Please note that the copy is straight from the (unreleased) depot  
version and contains stuff that might not work (it also contains  
changes to the Java source that might not be in the next release) so  
proceed with care.

In case you want to fix your existing copy, unpack the antlr3.jar and  
change the file named ObjC.stg. After the replacement you can repack  
the jar, or just adjust your classpath accordingly. Alternatively, you  
can put the modified ObjC.stg into your classpath before the jar.  
(Sounds complicated, but really isn't ;))
You need to replace the string "ANTLRASTLabelType" to "ASTLabelType".  
It's just one occurrence. After that change you grammar goes trough  
fine, though it might not do what you intended because the references  
to a and b won't work:

expr returns [NSString *value]
		:	^('+' a=expr b=expr)	{$value = a+b;}
		|	^('-' a=expr b=expr)	{$value = a-b;}
		|	^('*' a=expr b=expr)	{$value = a*b;}
		|	ID
			{

You need to use $a and $b and make sure that you first convert their  
string values to integers or floats and then do the arithmetic.

HTH,

-k
-- 
Kay Röpke
http://classdump.org/








More information about the antlr-interest mailing list