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

Micheal J open.zone at virgin.net
Wed Feb 22 10:24:50 PST 2006


Robert,

> 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>
>        ;

This should do the trick:

formal!
    :	dt:dataType id:ID<AST=MiniJava.Identifier>
	{
		## = #( #[MJ_FORMAL, "MJ_FORMAL", "MiniJava.Formal"], #dt,
#id );
	}

I'm assuming that MJ_FORMAL is a valid token type for your grammar. If not,
define it in the tokens sections.

=========================

This is simpler and might/should also work actually (at least for the C#
you're using):

tokens 
{
   ....
   MJ_FORMAL<AST=MiniJava.Formal>;
   ....
}

formal!
    :	dt:dataType id:ID<AST=MiniJava.Identifier>
	{
		## = #( #[MINI_JAVA_FORMAL, "MINI_JAVA_FORMAL"], #dt, #id );
	}


Cheers,

Micheal
ANTLR/C#



More information about the antlr-interest mailing list