[antlr-interest] syntax predicates

richardhensley99 richard.hensley at mckesson.com
Thu Sep 12 16:09:08 PDT 2002


I have a question about syntax predicates because I think I found a 
bug.

I have the following grammar snippet

startRule
:   (   "create" (("or" "replace" "force" "view") => createView)?
    |   skipTokens
    )* EOF
;
    
createView
    :   (ID)* "view"
    ;
    
skipTokens
    :   ~("create")
    ;

And it generates the following code in the startRule method

case LITERAL_create :
    {
        AST tmp1_AST = null;
        tmp1_AST = (AST) astFactory.create(LT(1));
        astFactory.addASTChild(
            currentAST,
            tmp1_AST);
        match(LITERAL_create);
        {
            boolean synPredMatched2949 = false;
            if (((LA(1) == LITERAL_view
                || LA(1) == ID)
                && (_tokenSet_0.member(LA(2)))
                && (_tokenSet_0.member(LA(3)))
                && (_tokenSet_0.member(LA(4))))) {
                int _m2949 = mark();
                synPredMatched2949 = true;
                inputState.guessing++;
                try {
                    {
                        match(LITERAL_or);
                        match(LITERAL_replace);
                        match(LITERAL_force);
                        match(LITERAL_view);
                    }
                }
                catch (RecognitionException pe) {
                    synPredMatched2949 = false;
                }
                rewind(_m2949);
                inputState.guessing--;
            }

The problem seems to be the following line of code:

if (((LA(1) == LITERAL_view
    || LA(1) == ID)

Which is generated because of the syntax predicate, however the 
syntax predicate is for the literal "or", not an ID or the 
literal "view". I'm not sure what gives, but this causes problems 
when the testLiterals options is true for an ID token because the 
token is kicked out of the lexicial analyzer as a LITERAL_or instead 
of an ID.

Has anybody else encountered this problem?

I've worked around it by just using my createView rule as a predicate 
to createView, but I thought I would ask.

Richard



 

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



More information about the antlr-interest mailing list