[antlr-interest] Pattern for returning errors from ANTLR in data structures, not STDERR

Arthur Goldberg goldberg at cbio.mskcc.org
Wed Dec 1 16:05:14 PST 2010


Thanks Ter

However, as you might expect, I don't have time to integrate all this 
code into my parser. I'll do something simpler.

BR
A

On 11/30/10 11:28 PM, Terence Parr wrote:
> Let me be more specific.
>
> In the v4 prerelease stuff, Tool.java has
>
> // http://antlr.org/depot/antlr4/main/tool/src/org/antlr/v4/Tool.java
> public void error(Message msg) {
> if ( listeners.size()==0 ) {
> defaultListener.error(msg);
> return;
> }
> for (ANTLRToolListener l : listeners) l.error(msg);
> }
>
> using
>
> http://antlr.org/depot/antlr4/main/tool/src/org/antlr/v4/tool/ANTLRToolListener.java
>
> then in
>
> http://antlr.org/depot/antlr4/main/tool/src/org/antlr/v4/parse/ToolANTLRParser.java
>
> I do:
>
> public void displayRecognitionError(String[] tokenNames,
> RecognitionException e)
> {
> String msg = getParserErrorMessage(this, e);
> if ( paraphrases.size()>0 ) {
> String paraphrase = (String)paraphrases.peek();
> msg = msg+" while "+paraphrase;
> }
> //List stack = getRuleInvocationStack(e, this.getClass().getName());
> //msg += ", rule stack = "+stack;
> tool.errMgr.syntaxError(ErrorType.SYNTAX_ERROR, getSourceName(), 
> e.token, e, msg);
> }
>
> which forces all msgs from antlr to shunt to Tool.error(), which 
> announces to listeners.
>
> For testing, i have unit tests use this
>
> http://antlr.org/depot/antlr4/main/tool/test/org/antlr/v4/test/ErrorQueue.java
>
> as a listener to the tool like so:
>
> Tool antlr = newTool(optionsA);
> antlr.addListener(equeue);
>
> Ter
>
> On Nov 30, 2010, at 5:20 PM, Terence Parr wrote:
>
>> Check ANTLRToolListener in antlr source or whatever. it has a similar 
>> problem to solve.
>> Ter
>> On Nov 30, 2010, at 5:13 PM, Arthur Goldberg wrote:
>>
>>> Hello All
>>>
>>> My ANTLR parser is embedded in other code. I've decided that reporting
>>> errors out of the parser would be better handled as a list of errors,
>>> not exceptions, so that the parse is allowed to recover from errors and
>>> complete, if possible.
>>> Does anyone have a working pattern for returning errors from ANTLR in a
>>> list, not STDERR?
>>> In more detail, I'd run it in two modes:
>>> 1) testing: call some rule other than the root
>>> In this case, I will let the rule report an error via an exception.
>>>
>>> 2) production: call the root rule
>>> In this case, a newReportError() in
>>>
>>>    catch (RecognitionException re) {
>>>        newReportError(re);
>>>        recover(input,re);
>>>    }
>>>
>>> would catch exceptions thrown by other rules, and store them in a list.
>>> When the root rule returns, it returns the list of errors, along with
>>> the results of the parse.
>>>
>>> If someone can help me with this, I'll make it worth your while. I'm
>>> running out of time for messing with ANTLR.
>>>
>>> Thanks
>>> Arthur
>>>
>>>
>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>> Unsubscribe: 
>>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: 
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>

-- 
Senior Research Scientist
Computational Biology
Memorial Sloan-Kettering Cancer Center



More information about the antlr-interest mailing list