[antlr-interest] Creation of a AST for my grammar
Matt Benson
gudnabrsam at yahoo.com
Fri Mar 21 09:02:07 PST 2003
You're in luck! Terence Parr, creator of Antlr, is
currently teaching a course on Programming Languages,
I believe at USF (wherever that is :) ). In the
interest of the community at large, Ter is making his
lectures, reading material, and assignments for the
course available on the web. The site is
http://www.cs.usfca.edu/~parrt/course/652/index.html
and has helped my understanding of ANTLR and general
language processing immensely.
-Matt
--- queengiraffe <cow_jumped_moon at hotmail.com> wrote:
> Hiya, I'm really new to ANTLR, in fact I'm really
> new to any type of
> compiler design. I'm trying to create the front end
> of a compiler for
> my own langauge, so far I have:
>
>
>
> options {
> language="Cpp";
> }
>
//////////////////////////////////////////////////////////////
> class CompilerParser extends Parser;
> options {
> buildAST = true; // uses CommonAST by default
> k=2; //lookahead =2.
> }
>
> block :BEGIN (statement)+ END
> ;
>
> statement :arthExpr SEMI
> |block
> ;
>
> arthExpr :(term EQUALS)? plusExpr
> ;
>
> plusExpr :divExpr((PLUS divExpr)|(MINUS divExpr))*
> ;
>
> divExpr :atom((STAR atom)|(BSLASH atom))*
> ;
>
> atom :term
> |LPAREN plusExpr RPAREN
> ;
>
> term :ID
> |INT
> ;
>
/////////////////////////////////////////////////////////////////
>
//---------------------------------------------------------------
> // The scanner
>
//---------------------------------------------------------------
>
/////////////////////////////////////////////////////////////////
> class CompilerLexer extends Lexer;
> options {
> k=2; //lookahead =2.
> }
>
> // ------------
> // keywords
> // ------------
> tokens {
> BEGIN = "begin" ;
> END = "end" ;
> }
>
> // Whitespace -- ignored
> WS : (' '
> | '\t'
> | '\n'
> | '\r')
> { _ttype = ANTLR_USE_NAMESPACE(antlr)Token::SKIP;
> }
>
> ;
> // Single-line comments
> SL_COMMENT
> : "//"
> (~('\n'|'\r'))* ('\n'|'\r'('\n')?)
> {$setType(ANTLR_USE_NAMESPACE(antlr)Token::SKIP);
> newline();}
> ;
>
> // ----------------
> // operators
> // ----------------
>
> LPAREN: '('
> ;
>
> RPAREN: ')'
> ;
>
> STAR: '*'
> ;
>
> BSLASH: '/'
> ;
>
> MINUS: '-'
> ;
>
> PLUS: '+'
> ;
>
> SEMI: ';'
> ;
>
> EQUALS: '='
> ;
>
> protected
> DIGIT: '0'..'9'
> ;
> protected
> LETTER: 'a'..'z'
> ;
> protected
> CAPLET: 'A'..'Z'
> ;
>
>
> ID : (LETTER|CAPLET)(LETTER|CAPLET|DIGIT)*
> ;
>
> INT : (DIGIT)+
> ;
>
>
> The question I have is how do I create a AST for
> this grammar.
>
> Great thanks in advance to any one that can help or
> point me to some
> (simple) documentation/site that may be helpful.
>
> Regards
> A very confused newbie
>
>
>
>
> Your use of Yahoo! Groups is subject to
> http://docs.yahoo.com/info/terms/
>
>
__________________________________________________
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!
http://platinum.yahoo.com
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list