[antlr-interest] Creation of a AST for my grammar

queengiraffe cow_jumped_moon at hotmail.com
Fri Mar 21 08:49:48 PST 2003


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/ 



More information about the antlr-interest mailing list