[antlr-interest] ambiguous predicates

Stéphane Pion PION_Stephane at stna.dgac.fr
Fri Feb 22 04:06:18 PST 2002


hi,
I am using antlr to parse a binary format

The first byte indicate the presence of following other fields:
eg, if first byte is 11000000, then, the message contains two fields 
(defined by the bit positions in the first byte).

One of my fields is construct as a byte representing the number of 
following bytes.
One other is only a byte.
So, i write the following lexer:
AsterixLexer.g:

options {

    language="Cpp";
    mangleLiteralPrefix = "TK_";
}

class AsterixLexer extends Lexer;
options {
    charVocabulary = '\u0000'..'\u00ff';
    exportVocab=AsterixLexer;
    k=1;
}
{
    public:
    unsigned int count;
}
BYTE
    :
        val:.
    ;

And the associated parser:
AsterixParser.g:
options {    
    language="Cpp";
    mangleLiteralPrefix = "TK_";
}

class AsterixParser extends Parser;
options{
    importVocab=AsterixLexer;
    k=1;
}

eng
    :        
        fetch_01
        fetch_02
    ;

protected        
fetch_01
    :
        {asterix->01()}?
        (
            {
                unsigned char count = 0;
            }
            fr:BYTE
            ( // Ambiguous!!! (line 29)
                { count++ <  (fr->getText())[0]}?
                (
                    BYTE                    
                )                
            )+            
        )
        |
    ;
exception
catch[ANTLR_USE_NAMESPACE(antlr)SemanticException ex]{        
        }

protected        
fetch_02
    :
        {asterix->02()}?
        (
            BYTE            
        )
        |
    ;

On fetch_*, the first predicate is present to validate the presence of the field (according to the first field)
the first parsed byte indicate the field length. So, when enough bytes are read, a Semantic exception is thrown and the rule is aborted.
Then the second field parsing can begin. But:

/usr/bin/java antlr.Tool AsterixParser.g
ANTLR Parser Generator   Version 2.7.1   1989-2000 jGuru.com
AsterixParser.g:29: warning: nondeterminism upon
AsterixParser.g:29: 	k==1:BYTE
AsterixParser.g:29: 	between alt 1 and exit branch of block


I remark if i commented out fetch_02 generation work fine.
Any solution?

I have an additionnal question:
Wy antlr throw an error if i want create two parsing rules with the same name but with different signatures?

Thanks for the help

Stéphane



 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list