[antlr-interest] Error recovery problem?

Edson Tirelli tirelli at post.com
Tue Jan 23 06:58:23 PST 2007


   Hello All,

   I'm perceiving a strange behavior IMO on antlr parser when trying to 
recover from an error, and I don't know if it was supposed to behave 
like that or not.

   I'm implementing test cases for parsing "incomplete files", i.e., we 
have an IDE that must parser code while the user is typing and handle it 
accordingly. So, my unit test is this:

        input =
            "rule MyRule \n" +
            "    when \n" +
            "        Class ( name:";
        rule = parseRuleString(input);
        assertEquals(1, rule.getLhs().getDescrs().size());
        column = (ColumnDescr) rule.getLhs().getDescrs().get(0);
        assertEquals("Class", column.getObjectType());
        assertEquals(-1, column.getEndCharacter());

    The grammar rule that is behaving strangely is this:

fact returns [BaseDescr d]
    @init {
        d=null;
    }
     :    id=dotted_name
         {
             d = new ColumnDescr( id );
         }
         loc=LEFT_PAREN {
                 d.setLocation( offset(loc.getLine()), 
loc.getCharPositionInLine() );
                     d.setStartCharacter( 
((CommonToken)loc).getStartIndex() );
             }
         (    c=constraints
             {
                 for ( Iterator cIter = c.iterator() ; cIter.hasNext() ; ) {
                     ((ColumnDescr)d).addDescr( (BaseDescr) cIter.next() );
                 }
             }
          )?
         endLoc=RIGHT_PAREN
        {
            d.setEndLocation( offset(endLoc.getLine()), 
endLoc.getCharPositionInLine() );   
            d.setEndCharacter( ((CommonToken)endLoc).getStopIndex() );
         }
     ;

     So, what I expect is that the last semantic action (after it finds 
the RIGHT_PAREN) to be executed only when the right parentesis ')' 
character is found. But what IS happening is that as the input string 
finishes without a ')', it is executing the last semantic action anyway.
     As the input string finishes unexpectedly, I would expect it to 
raise an exception and stop executing semantic actions, as it is not 
possible to recover from this error.

     Am I wrong in my assumption? Is this the correct behavior? BTW, I'm 
using antlr 3.0b5.

     I can easily add an if() within the last semantic action, but that 
would mean I would have to do the same throughout my entire grammar in 
almost all semantic actions... what I think it is not the intent... :(

     I appreciate any advice.

     []s
     Edson

-- 
 Edson Tirelli
 Software Engineer - JBoss Rules Core Developer
 Office: +55 11 3124-6000
 Mobile: +55 11 9218-4151
 JBoss, a division of Red Hat @ www.jboss.com




More information about the antlr-interest mailing list