[antlr-interest] target language independent action code

Johannes Luber jaluber at gmx.de
Mon Jan 21 14:20:47 PST 2008


Arnulf Heller schrieb:
> Hm. Interesting. Taking a look into CMinus.g, I can understand that 
> action code like
> 
> variable
>     :   type declarator ';'
>         -> {$function.size()>0 && $function::name==null}?
>            globalVariable(type={$type.st},name={$declarator.st})
>         -> variable(type={$type.st},name={$declarator.st})
>     ;
> 
> declarator
>     :   ID -> {new StringTemplate($ID.text)}
>     ;
> 
> will work on most target languages.

At least those based on C. Others like BASIC or Lisp will require 
changes. And CSharp doesn't know "size()" for arrays, but "Count". So 
there is some difference already.

> Thats because Terence already did 
> the work in these cases (and I assume that there are hidden string 
> templates that translate that). ANTLR translates the $variable tokens 
> appropriately.

Ter created the example and someone else than me translated it to 
CSharp. So I don't know how much had to be changed there. In any case, 
the $-variables are parsed by ANTLR itself and don't need StringTemplate 
per se. But the actual output will use ST again.

> But I can not imagine that this works in C#, does it?
> 
> program
> scope {
>   List globals;
>   List functions;
> }
> @init {
>   $program::globals = new ArrayList();
>   $program::functions = new ArrayList();
> }
>     :   declaration+
>         -> 
> program(globals={$program::globals},functions={$program::functions})
>     ;
> 
> If it does then C# is indeed just a reincarnation of Java (or should I 
> say relabel?) :-)

It doesn't compile - you have to change List into IList first! :P 
Seriously, C# has lots of differences compared to Java, but these 
examples don't showcase them. E.g., Java doesn't have a using-statement 
and a bad support for generics and none for closures yet. Please read 
books about C# and Java first, before you go on to compare them.

Johannes


More information about the antlr-interest mailing list