[antlr-interest] MismatchedTokenException encountered when moving from literal to lexical token

Loring Craymer lgcraymer at yahoo.com
Fri Sep 14 12:43:19 PDT 2007


You are right about the source of conflicts.  Change
CONJ to
CONJ : 'AND' | 'and' ;
and body to

body
    :
    substatement
    ( ( CONJ^ | ','^) substatement )*
    ;

and you should be okay.

--Loring

--- Randall Barnhart <randall_barnhart at hotmail.com>
wrote:

> Hey ANTLR Folks,
> 
> I am trying to generate a grammar for a Prolog like
> language, and I am 
> running into a problem when I try to provide more
> than 1 token alternative 
> for a comma separated list.  I've provided the full
> grammar below for 
> reference, but here is the rule that has the
> problem:
> 
> When I changed -
> body
> 	: subStatement (','^ subStatement)*
> 	;
> 
> to the following -
> body
> 	: subStatement (CONJ^ subStatement)*
> 	;
> 
> where -
> CONJ : 'AND' | 'and' | ',' ;
> 
> I now get a MismatchedTokenException when I use a
> ',' whereas before it was 
> recognizing it just fine.  Specifically:
> 
> recoverFromMismatchedToken
> BR.recoverFromMismatchedToken
> line 2:24 mismatched input ',' expecting ENDSTMT
> 
> Using the following input to match on the "clause"
> rule:
> pred1(cp1, np1, dp1) :-
> 	cp1 := "predicateCode1",
> 	np1 := 2 * 3 / 10,
> 	dp1 := 12/31/2007 12:00 AM
> 	;
> 
> Also, I don't get the error if I use 'AND' or 'and'.
>  Ultimately I think it 
> is a conflict with some of the other rules that use
> ',' to signifiy a list 
> of expressions, because when I change those to
> different symbols then I 
> don't get this error.
> 
> I think I should maybe use a syntactic or semantic
> predicate, but I'm not 
> real sure on how to go about doing this.  Does
> anyone have any specific 
> solutions, ideas, or advice that might lead me in
> the right direction?
> 
> Thanks,
> Randall Barnhart
> 
> 
> 
> grammar Test;
> 
> options {
> 	output=AST;
> }
> 
> tokens {
> 	RULE;
> 	HEAD;
> 	BODY;
> 	STMTCONJ;
> 	STMTDISJ;
> 	FACT;
> 	VARLIST;
> 	LITLIST;
> 	NEGATE;
> 	EXPR;
> 	VARDECL;
> 	VARASSIGN;
> 	PREDCALL;
> 	EXPRLIST;
> }
> 
> clause
> 	: rule
> 	| fact
> 	;
> 
> rule
> 	: head ':-' body ENDSTMT
> 	  -> ^(RULE head ^(BODY body))
> 	;
> 
> head
> 	: predicateName '(' varList ')'
> 	  -> ^(HEAD predicateName varList)
> 	;
> 
> body
> 	: subStatement (CONJ^ subStatement)*
> 	;
> 
> fact
> 	: predicateName '(' literalList ')' ENDSTMT
> 	  -> ^(FACT predicateName literalList)
> 	;
> 
> predicateName
> 	: ID
> 	;
> 
> varDecl
> 	: type ID
> 	  -> ^(VARDECL type ID)
> 	;
> 
> varAssign
> 	: ID ':=' expr
> 	  -> ^(VARASSIGN ID expr)
> 	;
> 
> varList
> 	: ID (',' ID)* -> ^(VARLIST ID+)
> 	;
> 
> type
> 	: 'Code'
> 	| 'Numeric'
> 	| 'Date'
> 	;
> 
> predicateInvocation
> 	: predicateName '(' exprList ')'
> 	  -> ^(PREDCALL predicateName exprList)
> 	;
> 
> subStatement
> 	: expr -> ^(EXPR expr)
> 	| varDecl
> 	| varAssign
> 	| predicateInvocation
> 	;
> 
> exprList
> 	: expr (',' expr)*
> 	  -> ^(EXPRLIST expr+)
> 	;
> 
> expr
> 	: boolAndExpr (OR^ boolAndExpr)*
> 	;
> 
> boolAndExpr
> 	: equalityExpr (AND^ equalityExpr)*
> 	;
> 
> equalityExpr
> 	: relationalExpr ((EQUALS|NOTEQUALS)^
> relationalExpr)*
> 	;
> 
> relationalExpr
> 	: addExpr ((LT|LTEQ|GT|GTEQ)^ addExpr)*
> 	;
> 
> addExpr
> 	: multExpr ((PLUS|MINUS)^ multExpr)*
> 	;
> 
> multExpr
> 	: powExpr ((MULT|DIV|MOD)^ powExpr)*
> 	;
> 
> powExpr
> 	: unaryExpr (POW^ unaryExpr)*
> 	;
> 
> unaryExpr
> 	: primaryExpr
> 	| NOT^ primaryExpr
> 	| MINUS primaryExpr -> ^(NEGATE primaryExpr)
> 	;
> 
> primaryExpr
> 	: '('! expr ')'!
> 	| literal
> 	;
> 
> literalList
> 	: literal (',' literal)*
> 	  -> ^(LITLIST literal+)
> 	;
> 
> literal
> 	: BOOLEAN
> 	| CODE
> 	| NUMERIC
> 	| DATE
> 	| ID
> 	;
> 
> 
=== message truncated ===



       
____________________________________________________________________________________
Building a website is a piece of cake. Yahoo! Small Business gives you all the tools to get online.
http://smallbusiness.yahoo.com/webhosting 


More information about the antlr-interest mailing list