[antlr-interest] The most import questions for me about antlr?

Nigel Sheridan-Smith nbsherid at secsme.org.au
Mon May 9 21:23:17 PDT 2005



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of liigo
> Sent: Tuesday, 10 May 2005 12:51 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] The most import questions for me about antlr?
> 
> Hi, all
> I'm a newbie of antrl!
> 
> 1.
>      I want to make my new language..or call it a script, such as
> python,ruby,lua,etc.
> and use the new language write a "program" and "run" it.
> please note, Not olny "parse" the program, but also "RUN" it!
> 
> Can antlr help me?  I do think it can help me, but I do not know How to
> do.
> Would you like to tell me how to start? or give me a sample.
> 

Hi Liigo,

A grammar file defines the context-free rules of your language, and any
actions that you take when you encounter the rule. You will most likely need
both rules and actions! The grammar you define allows ANTLR to generate a
Lexer and Parser (and maybe Tree Parser) for you to match the elements
contained within the input file (e.g. the script files written by your
users). Once it matches those elements though, you must use then use actions
to do the things you want, or use tree parser rules to structure your
Abstract Syntax Tree (AST) nodes in the order you want. 

In a real compiler or interpreter, you might need to do various semantic
checks first (context-sensitive, rather than context-free). Phase one is
checking identifiers or types (if they are relevant to your language), etc.
You can do this with both the actions of ANTLR (either parser or tree
parser), or by calling methods on the objects used in the target language
you tell ANTLR to use (Java, C#, Python, C++). 

Then you will probably want to convert the parsed expressions/statements of
your language into some other form (i.e. compilation to byte codes or
assembly language of chosen target architecture), or execute the statements
using an interpreter. Currently, I am using the second method for my
language (for now!) by calling functions recursively on the Java objects I
have defined. 

You *could* use tree parsers for some of this stuff, but I am yet to come
across a good example of an interpreter or compiler using tree parsers.
Hopefully, someone else here could suggest an example or sample that others
can find and examine in a more complete compiler scenario.

Nigel

--
Nigel Sheridan-Smith
PhD research student

Faculty of Engineering
University of Technology, Sydney
Phone: 02 9514 7946
Fax: 02 9514 2435



More information about the antlr-interest mailing list