Discussion:
what means "mismatched input 'xxx' expecting set null"
Pete Siemsen
2007-10-07 04:51:56 UTC
Permalink
I'm trying to implement include file processing with ANTLR 3.0.1. I
stole liberally from the
ANTLR Wiki page that describes it (http://www.antlr.org/wiki/pages/
viewpage.action?pageId=557057).
Now my grammar doesn't recognize my test input file, which contains just

#pragma include ("System\CIM_DiagnosticResult.mof")
#pragma include ("Event\CIM_Indication.mof")
#pragma include ("Core\CIM_ManagedElement.mof")

When I run it, I get

line 1:8 mismatched input 'include' expecting set null
line 2:8 mismatched input 'include' expecting set null
line 3:8 mismatched input 'include' expecting set null

Finally, here's the grammar. I stripped it down to just the relevant
stuff:

grammar cimmof2java;

tokens {
BACKSLASH = '\\' ;
DOUBLEQUOTE = '"' ;
INCLUDE = 'include' ;
LOCALE = 'locale' ;
LPAREN = '(' ;
PRAGMA = '#pragma' ;
RPAREN = ')' ;
SINGLEQUOTE = '\'' ;
}


mofSpecification
: (mofProduction)+
;

mofProduction
: compilerDirective
;

compilerDirective
: PRAGMA (PragmaInclude | PragmaLocale)
;

PragmaInclude
: INCLUDE LPAREN f=StringConstant RPAREN
;

PragmaLocale
: LOCALE LPAREN StringConstant RPAREN
;

WhiteSpace
: ( ' ' | '\t' | '\n' | '\r')+ {$channel=HIDDEN;}
;

fragment
StringCharacter
: ' '..'!' | '#'..'[' | ']'..'~'
;

The above grammar produces the error. What's wrong?

Note that the PragmaInclude rule used to be a parser rule, with a
lowercase "p". Include processing works in the lexer, so I changed
the "p" to a "P". ANTLR accepts it and produces compileable Java,
but when I run it I get the errors.

Perhaps it's relevant that if I change the "P" on "PragmaLocale" to
"p", I get this error instead:

line 1:8 no viable alternative at input 'include'
line 2:8 no viable alternative at input 'include'
line 3:8 no viable alternative at input 'include'

-- Pete
Gavin Lambert
2007-10-07 05:57:13 UTC
Permalink
Post by Pete Siemsen
PragmaInclude
: INCLUDE LPAREN f=StringConstant RPAREN
;
[...]
Post by Pete Siemsen
The above grammar produces the error. What's wrong?
Since PragmaInclude is a lexer rule, it won't discard whitespace
(or to be more specific, it won't generate whitespace tokens and
then either skip them or push them onto a hidden channel). So
anywhere that whitespace may occur within a lexer rule you will
need to explicitly specify it.
Post by Pete Siemsen
Perhaps it's relevant that if I change the "P" on "PragmaLocale"
line 1:8 no viable alternative at input 'include'
line 2:8 no viable alternative at input 'include'
line 3:8 no viable alternative at input 'include'
It's hard to say for this one -- you've specified "StringConstant"
in your rule but haven't actually given a definition for it in the
grammar you posted. If that's actually what you've got (or if
StringConstant is a fragment rule), then that's your
problem. Parser rules can only (usefully) refer to non-fragment
lexer rules that the lexer will actually generate :) (ANTLR won't
complain about typos, irritatingly -- it assumes you are referring
to another token it simply hasn't seen the definition for yet --
even if it never does.)
Pete Siemsen
2007-10-07 16:04:05 UTC
Permalink
Gavin,

That was the problem. I specified whitespace, and now it works.

Thank you very much!

-- Pete
Post by Gavin Lambert
Post by Pete Siemsen
PragmaInclude
: INCLUDE LPAREN f=StringConstant RPAREN
;
[...]
Post by Pete Siemsen
The above grammar produces the error. What's wrong?
Since PragmaInclude is a lexer rule, it won't discard whitespace
(or to be more specific, it won't generate whitespace tokens and
then either skip them or push them onto a hidden channel). So
anywhere that whitespace may occur within a lexer rule you will
need to explicitly specify it.
Pete Siemsen
2007-10-07 16:11:16 UTC
Permalink
Austin,
You don't provide a definition of StringConstant. Does it perchance
start with a SINGLEQUOTE instead of a DOUBLEQUOTE?
When I cut/pasted the grammar in the message that started this
thread, I somehow missed two rules at the end. Sorry about that.
Appended is a newer example grammar that addresses that problem and
the ones you mention below.
Also, your examples include backslashes, but your StringCharacter
definition implicitly excludes them.
Perhaps the appended grammar is better about this.
Finally, your StringCharacter definition is just dumb. If you want
to exclude certain characters, use the ~ (tilde) exclusion
character. Don't code around them. It took me a trip to an ascii
table to figure out what you were doing. Had you asked earlier this
afternoon, when my laptop wasn't in wifi range of the internet, I
wouldn't have had one available.
Quite right. The appended grammar is better.

But it still exhibited the problem with "mismatched input 'include'
expecting set null". Another person answered this thread, explaining
that I have to explicitly specify whitespace in lexer rules like the
PragmaInclude rule. I did so and fixed the problem.

Thank you very much!

-- Pete


grammar cimmof2java;

tokens {
BACKSLASH = '\\' ;
DOUBLEQUOTE = '"' ;
INCLUDE = 'include' ;
LOCALE = 'locale' ;
LPAREN = '(' ;
PRAGMA = '#pragma' ;
RPAREN = ')' ;
SINGLEQUOTE = '\'' ;
}


mofSpecification
: (mofProduction)+
;

mofProduction
: compilerDirective
;

compilerDirective
: PRAGMA (PragmaInclude | pragmaLocale)
;

PragmaInclude
: INCLUDE WhiteSpace LPAREN f=StringConstant RPAREN
;

pragmaLocale
: LOCALE WhiteSpace LPAREN StringConstant RPAREN
;

WhiteSpace
: ( ' ' | '\t' | '\n' | '\r')+ {$channel=HIDDEN;}
;

StringConstant
: DOUBLEQUOTE ( EscapeSequence | StupidEscapeSequence | ~( '\\'
| '"' ) )* DOUBLEQUOTE
;

fragment
EscapeSequence
: BACKSLASH ( 'b' | 't' | 'n' | 'f' | 'r' | DOUBLEQUOTE |
SINGLEQUOTE | BACKSLASH)
| HexEscape
;

fragment
StupidEscapeSequence
: BACKSLASH ( 'C' | 'P' )
;

fragment
HexEscape
: BACKSLASH 'x' HexDigit
| BACKSLASH 'x' HexDigit HexDigit
| BACKSLASH 'x' HexDigit HexDigit HexDigit
| BACKSLASH 'x' HexDigit HexDigit HexDigit HexDigit
;

fragment
HexDigit
: ('0'..'9'|'a'..'f'|'A'..'F') ;

Loading...