[antlr-interest] newbie lookahead question

Lucien Stals lstals at swin.edu.au
Sun Apr 16 21:30:02 PDT 2006


Hi,

I've been learning ANTLR for about two weeks and want to be able to 
parse (then transform into XML) an input file in a specific markup 
language (ASAP2). I have not worked with parsers before and I feel like 
I'm in a little over my head. It's sink or swim time for me.

I have some basic stuff working, but I'm getting lots of warnings about 
ambiguity.

Part of a sample input file might look like:

...
/begin CALIBRATION_METHOD
	"Slewing"
	1
	/begin CALIBRATION_HANDLE
		0x1ffbf8
		0x400
		0
		AllSlews
	/end CALIBRATION_HANDLE
/end CALIBRATION_METHOD
...


My question is about look ahead.
In my parser, I have rules like:


calibrationMethod:BEGIN CAL_METH
			(calibrationHandle)*
			END CAL_METH;
			
calibrationHandle:BEGIN CAL_HAND
			END CAL_HAND;

Where my lexer rules are:

protected
SLASH		:'/';

BEGIN		:SLASH "begin";

END		:SLASH "end";

CAL_METH	:"CALIBRATION_METHOD";

CAL_HAND	:"CALIBRATION_HANDLE";


(I'm just dealing with the tag structure first. Parsing the actual data 
is my next step. I have filter=true for now so I can ignore what I can't 
parse yet)

And I'm getting ambiguity warnings. Should I set my lookahead to 
something silly like 13 just to look past "CALIBRATION_"? (I read that 
bigger lookaheads are performance killers) Or is there a smarter way to 
do this? Should I use predicates like:

calibrationMethod:BEGIN CAL_METH {this.inCalMeth=true;}
			(calibrationHandle)*
			END CAL_METH {this.inCalMeth=false;};
			
calibrationHandle:{this.inCalMeth}?
		BEGIN CAL_HAND
		END CAL_HAND;

Perhaps I'm completely off base. If it looks like I'm really missing 
something, you might be right. Feel free to let me know.

This is only one of the problems I'm having, but I'll just keep it to 
one question per post ;)

BTW, if anyone is aware of a grammar that is similar which I can get 
inspiration from, can you let me know?

Thanks,

Lucien.


More information about the antlr-interest mailing list