[antlr-interest] newbie lookahead question

Martin Probst mail at martin-probst.com
Mon Apr 17 01:28:07 PDT 2006


Hi,

you can either increase your lookahead (which is not advisable in  
this case), or rather do it manually:

CALIBRATION_THINGY:
   "CALIBRATION_" ( "METHOD" { $setType(CAL_METHOD);} |  
"HANDLE" { $setType(CAL_HANDLE);} );

This parses the CALIBRATION_ part and then decides what kind of token  
type this is later. You'll may want to add "CAL_METHOD" and  
"CAL_HANDLE" to the tokens section of your grammar because they  
aren't declared automatically if used like this - you can use that to  
give them a proper help message later on.

Martin


Am 17.04.2006 um 06:30 schrieb Lucien Stals:

> 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