[antlr-interest] Newbie question on lexing '{' and '{action();}'

Stephen Parker Stephen.Parker at pi-shurlok.com
Fri Jul 13 06:32:32 PDT 2007


This seems to be some kind of FAQ, but I haven't found an answer.  
I want to parse a language like this:
 
	machine G {
	 /* xxx. */
	 id1 {action1();}
	 id2 {action2();}
	}

And I have a grammar like this:
 
	grammar Test;
 
	prog : 'machine' ID OPEN state+ CLOSE;
	state : case+ ;
	case : ID ACTION ;
 
	OPEN :  '%{';  // Would like '{'.
	CLOSE : '%}';  // Would like '}'.

	ACTION :   '{' ( ACTION | ~('}'|'{') )* '}' ;
	ID  :   ('a'..'z'|'A'..'Z')+ ;
	COMMENT : '/*' ( options {greedy=false;} : . )* '*/' {skip();};
 
Notice that the braces '{' '}' are both ordinary tokens, and can
represent an
(ANTLRish) action.  I can't work out how to write the lexer rules for
this
(which is why I have '%{' at the moment).

Is it possible to get what I want here?

Many thanks,
stephen


More information about the antlr-interest mailing list