[antlr-interest] How can I find the line number and column for asemantic error

Sam Harwell sharwell at pixelminegames.com
Fri Apr 10 12:45:26 PDT 2009


$a.tree or $a.token are possibilities. :)

 

Sam

 

From: dhjdhj [mailto:dhjdhj at gmail.com] 
Sent: Friday, April 10, 2009 2:24 PM
To: Sam Harwell
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] How can I find the line number and column
for asemantic error

 

Capturing errors is not the issue, I've already got such a framework
setup --- the problem I'm running into is finding that "token" object
with the line and col information in it so that I can call a method such
as your AddError in the first place.

 

 

In other words, using your example,  suppose I have a grammar rule

 

foo:    a=expr b=expr

            {

                if $a.type != $b.type

                   //then

                       AddError("Types are incompatible",    ????????,
severity);

            }

 

What goes in the field marked by ?????????

 

 

D

 

 

On Apr 10, 2009, at 12:41 PM, Sam Harwell wrote:

	 

	public void AddError( string message, CommonToken token,
Severity severity )

	{

	    if ( string.IsNullOrEmpty( message ) || token == null ||
ErrorHandler == null )

	        return;

	 

	    int line = token.Line;

	    int col = token.CharPositionInLine;

	    int length = token.StopIndex - token.StartIndex + 1;

	    // these errors aren't forcing the parser into recovery mode
so they

	    // shouldn't have the same devastating performance impact

	    bool canCancel = false;

	 

	    ErrorHandler.AddError( message, line, col, length, severity,
canCancel );

	}

	 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090410/3529d8c8/attachment.html 


More information about the antlr-interest mailing list