[antlr-interest] Tutorial... actual use? & BSD question

Zachary Palmer zep_antlr at bahj.com
Mon Nov 8 10:13:26 PST 2010


Tenaja,

The option of which you are thinking is "target", not "output".  The 
target option specifies the language for which you would like the parser 
to be generated; the output option specifies the type of information 
produced by a call to one of the functions in the generated code.

I am not familiar with any five minute intro that would give you the 
information you want other than the ANTLR book I mentioned in the 
previous e-mail.  The following might help, but please bear in mind that 
this is all off of the top of my head.  :)

So consider the grammar

     grammar Simple;
     options { target=Java; }
     program: decl*;
     decl: IDENTIFIER '{' statement* '}';
     statement: '+' | '-';
     IDENTIFIER: ('a'..'z')*;
     WS: ('\n' | '\r' | '\t' | ' ')* { skip(); } ;

This grammar would recognize any of the following code:

     foo { + - - + }
     bar { - + }

     stuff { + + + }

but it wouldn't produce any output; that is, the SimpleParser.program() 
method has a void return.  If you want output, you have to specify it in 
the grammar.  Consider the following rewrite:

     grammar Simple;
     options { target=Java; output=AST; }
     tokens { PROG; DECL; }
     program: decl* -> ^(PROG decl*);
     decl: IDENTIFIER '{' statement* '}' -> ^(DECL statement*);
     statement: '+' | '-';
     IDENTIFIER: ('a'..'z')*;
     WS: ('\n' | '\r' | '\t' | ' ')* { skip(); } ;

Now, the SimpleParser.program() method will have a return value of a 
type generated by ANTLR.  That type will have a public member named 
"tree".  By default, tree has type Object but actually contains a 
CommonTree; that CommonTree is a homogenously-typed AST which contains 
the parse of the input.  There are also instructions on the ANTLR site 
for causing the grammar to produce heterogeneously-typed ASTs.

In the project on which I am working, we have to create nodes through a 
factory.  As a result, we went with an alternative approach: manually 
controlling the output of the grammar rules.  Our rules look something 
like this:

     fieldDeclaration returns [FieldDeclarationNode ret]
         :
             fieldModifiers
             type
             variableDeclarators
             ';'
             {
                 $ret = factory.makeFieldDeclarationNode(
                         $fieldModifiers.ret, $type.ret, 
$variableDeclarators.ret);
             }
         ;

Does this answer your question about using the output of the ANTLR parser?

In terms of licensing, I would expect that the generated parser is not 
covered by the BSD license unless the original grammar file is covered 
by the BSD license.  I have this impression for the same reason that the 
exported PNG from a Microsoft Visio document isn't subject to any 
licensing restrictions from the Visio software itself; it's merely 
subject to restrictions which were on the original .vsd file.  But I 
could be wrong about this; you'd have to ask the more lawyer-y people on 
the list to be sure.

Cheers,

Zach
> Zach,
> Thank you for responding. I think maybe my inquiry was unclear. I've read the "five minute intro..." and have used antlr to process several samples, producing code in Java, C, C#, etc,. (It's very simple...) Unless the output=AST is different from those output commands (output = csharp), it is probably not what I'm interested in.
>   
> What I'm looking for is a 5-minute tutorial on using the output; for instance, sending a file to the compiled C# output, and having that C# code process the text file and  generate a result. My end goal is a simple compiler to produce some Intermediate Representation (Three-Address, or PCode, etc).
>   
> ...
> On another topic, regarding the BSD license, since this is the first time I've worked with o/s code generation tools.
> .....The non-C output does not include a copy of it.
> .....The C output includes the BSD notice at the top.
> So, does that mean that I'm free to compile and distribute binary forms of Java&  C# output code without the license, but not C? Does that mean the only requirements for distributing the C in binary form are to put it in an About box, or something equivelant? Why would the C output require it, but not the other languages? I may eventually turn this into a commercial product so I'd like to know how much disclosure is required.
>   
> Thanks!!!
>
> --- On Sun, 11/7/10, Zachary Palmer<zep_antlr at bahj.com>  wrote:
>
>
> From: Zachary Palmer<zep_antlr at bahj.com>
> Subject: Re: [antlr-interest] Tutorial... actuala use?
> To: antlr-interest at antlr.org
> Date: Sunday, November 7, 2010, 5:16 PM
>
>
> Tenaja,
>
> By default, the generated ANTLR grammar is just a recognizer: it will
> throw an exception in the presence of a string not in the grammar and it
> will terminate normally in the presence of a string in the grammar.  To
> get more output from ANTLR (such as an AST), you probably want to set
> "output=..." in the options section of your grammar.  For an informal
> discussion of "output=AST", try
> http://www.antlr.org/wiki/display/ANTLR3/Tree+construction .  For more
> detailed information, there is a relatively inexpensive book ("The
> Definitive ANTLR Reference") that you can buy in electronic form.
>
> For more examples, you might try taking a glance at some of the grammars
> on the ANTLR site (http://www.antlr.org/grammar/list).  There's also a
> somewhat non-standard approach that I've been forced to use (due to some
> strange constraints in my project) which is illustrated by the ANTLR
> grammar used in a branch of the OpenJDK project in which each rule
> explicitly returns the values that it needs (which are then set by
> grammar actions).
>
> Cheers,
>
> Zach
>    
>> I'm familiar with bnf (etc) files, and have written a simple r/d compiler myself. I'm looking at antlr for expanding, and for maintenance reason. As such, I'd like to put together one of my simple bnf languages and view the output. I've seen a few antlr tutorials, but haven't found one that really describes the compiled code output (not the antlr output, but what a compiled exe or java file output produces).
>>     
>> So... can someone point me to a tutorial that shows what to do AFTER you compile the anltr-generated file?
>>     
>> Thanks!
>> Tenaja
>>
>>
>>
>>
>> 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
>
>
>
>
>
> 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