[antlr-interest] (newbie) mismatched error

jbb at acm.org jbb at acm.org
Mon Jul 16 20:59:41 PDT 2007


>  Hi, 

Greetings!
   
>  I apologise for the inconsistencies in the grammar I posted
>  earlier. Here is the more correct one.

Ok. still not precisely correct tho...

>   
>  For the input:
>  system; endsystem;
>
>  I get the following error message: What seems to be wrong? What is set null?
>   
>  line 1:0 mismatched input 'system' expecting set null
>   
>  the grammar is as following:
>  
.....snipped.....


-------------------------
1) I was unable to reproduce your error.

But you have

>  textualSystemDefinition 
>              :packageReferenceClause* ....other stuff...
>        ;

and

>  packageReferenceClause:
>              ;

so your rules are saying that a textualSystemDefinition starts off by
containing 0 or more packageReferenceClause's.

yet your rules also say that a packageReferenceClause contains nothing
(e.g. null).

this is, of course, ambiguous. surely the ANTLR tool warned about this.

-------------------------
2) i did try the grammar you posted in order to try to reproduce the error you
   reported.

but there are missing rules and apparent spelling mistakes.

so i tried to fix the missing rules for characterstring and for handling
whitespace and various spelling errors

and got several ambiguity warnings from ANTLR. the for packageReferenceClause
as mentioned above and for word (as, once again, i am sure the Tool already
informed you about).

after fixing those ambiguity things, i obtained several runtime errors during
tree building. i think i fixed those.

so attached please find a version of your posted grammar that Tool's,
compiles, and executes without any warnings or errors.  but, of course, it may
not be what you originally wanted.

-------------------------
3) when you ask for help, it is best to include the SMALLEST yet COMPLETE
   example of the problem you are asking about. this really aids others (like
   me) in their quest to answer your question(s).

and maybe even actually try to run the grammar snippet you post too....

(i know i have been guilty of not running by example questions in the past,
much to my embarrassment ;-)




Hope This Helps
   -jbb

and here is the code....
----------begin cut here----------
grammar Test;

options {
	output = AST;
	ASTLabelType = CommonTree;
}

tokens { PACKREF; COM; WORD; }

//------------------------------------------------------------------------
// code to be incorporated into the generated parser...

@members {
    private static final String [] x = new String[]{
        "system; endsystem;",
        "system foo_bar; endsystem bar_foo;"
    };

    public static void main(String [] args) {
        for( int i = 0; i < x.length; ++i ) {
            try {
                System.out.println("about to parse:"+x[i]);
                TestLexer lexer = new TestLexer(new ANTLRStringStream(x[i]));
                CommonTokenStream tokens = new CommonTokenStream(lexer);

                TestParser parser = new TestParser(tokens);
                TestParser.start_return p_result = parser.start();

                CommonTree ast = p_result.tree;
                System.out.println("resultant tree:" + ast.toStringTree());
                System.out.println();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
}

//------------------------------------------------------------------------
// next follows the parser...

start : textualSystemDefinition ;

textualSystemDefinition :
        packageReferenceClause*
        'system'    sn1=systemName?  end // added sn1=
        'endsystem' sn2=systemName?  end // added sn2=
        -> ^(PACKREF packageReferenceClause*
            'system'    $sn1? end  // use sn1 to grab the first systemName
            'endsystem' $sn2? end) // use sn2 to grab the second systemName
    ;

packageReferenceClause : 'needsomethinghere' /*to avoid grammar warning*/ ; 

systemName : NAME ; // name is now a lexer rule

end : ( 'comment' characterstring )? ';'
    ->^(';' ( ^(COM 'comment' characterstring) )? )
    ;

// changed name to be a lexer rule.
NAME : Word { $type = WORD; } ( '_' Word { $type = NAME; } )* ;
/* was: name : word ( '_' word )* -> ^(word ( ^(WORD '_' word) )* ) ;*/

// changed word to be a lexer rule, fixed ambiguity along the way.
fragment
Word : '.'* AlphaNumeric ( AlphaNumeric | '.' )* ;
// was word : ( alphaNumeric | '.' )* alphaNumeric ( alphaNumeric | '.' )* ;

fragment // changed to be a lexer fragment (was alphaNumeric : ...)
AlphaNumeric : Uppercase | National | Lowercase | Decimaldigit ;

fragment // changed to be a lexer fragment
Decimaldigit : '0'..'9' ;

fragment // changed to be a lexer fragment
National : '#' | '@' | '"' | '$' | '[' | ']' | '{' | '}' | '^' | '~' ;

fragment // changed to be a lexer fragment
Lowercase : 'a'..'z' ;

fragment // changed to be a lexer fragment
Uppercase :'A'..'Z' ;

// rules that seem to be necessary, but were not given in original posting
characterstring : 'characterstring';
WS : (' ' | '\t' | '\n' | '\r')+ { $channel = HIDDEN; } ;

// will probably need this, for the natural numbers...
// NATURAL : Decimaldigit+ ;
----------end cut here----------


More information about the antlr-interest mailing list