[antlr-interest] I wish a dummy token is accepted by antlr

=?gb2312?q?=CB=EF=BC=CD=B8=D5=20Jigang=20(Robert)=20Sun?= sunjigang1965 at yahoo.com.cn
Wed Feb 22 06:52:05 PST 2006


I want make a tree with root node type as
<AST=MiniJava.Formal>; dataType as first child; ID, to
be constructed as <AST=MiniJava.Identifier>,as
nextSibling. The following grammar is not recognised:

formal
	:
	dataType ID^ <AST=MiniJava.Identifier>
<AST=MiniJava.Formal>
       ;

So I added actions:

formal
	:
	dataType
	{	
	    MiniJava.Identifier id_AST = null;
            id_AST =
(MiniJava.Identifier)astFactory.create(LT(1),
"MiniJava.Identifier");
            astFactory.addASTChild(ref currentAST,
(AST)id_AST);

	} ID^ <AST=MiniJava.Formal>
	;


Got the following code:

		returnAST = null;
		ASTPair currentAST = new ASTPair();
		AST formal_AST = null;
		
		try {      // for error handling
			dataType();
			astFactory.addASTChild(ref currentAST, returnAST);
				
			MiniJava.Identifier id_AST = null;
			id_AST =
(MiniJava.Identifier)astFactory.create(LT(1),
"MiniJava.Identifier");
			astFactory.addASTChild(ref currentAST,
(AST)id_AST);
			
					
			MiniJava.Formal tmp2_AST = null;
			tmp2_AST = (MiniJava.Formal)
astFactory.create(LT(1), "MiniJava.Formal");
			astFactory.makeASTRoot(ref currentAST,
(AST)tmp2_AST);
			match(ID);
			formal_AST = currentAST.root;
		}
		catch (RecognitionException ex)
		{
			reportError(ex);
			recover(ex,tokenSet_1_);
		}
		returnAST = formal_AST;



I wish a dummy token is accepted by antlr, then I
could use 

formal
	:
	dataType ID <AST=MiniJava.Identifier> DUMMY^ 
<AST=MiniJava.Formal>
       ;

to get my tree.

Does anyone have better solutions?




Reference:

dataType
	:
	 "int"! LSQUARE! RSQUARE <AST=MiniJava.IntArrayType>
	| "int" <AST=MiniJava.IntegerType >
	| "boolean" <AST=MiniJava.BooleanType>
	|  ID <AST=MiniJava.IdentifierType>
	;



__________________________________________________
¸Ï¿ì×¢²áÑÅ»¢³¬´óÈÝÁ¿Ãâ·ÑÓÊÏä?
http://cn.mail.yahoo.com


More information about the antlr-interest mailing list