[antlr-interest] simple rules not deterministic

Jim Idle jimi at temporal-wave.com
Tue Oct 9 11:48:43 PDT 2007


Mark,

With the full grammar, I can confirm that your issue is what I said. In that
the rule block has statement+ and this means that your methodCall can see
the same input in two different ways, being one call to it or two calls to
it.

You can try to rejig the grammar or use a predicate to get rid of the
warning, though I bet if you look at the code it is probably doing what you
'expect' anyway. Warnings are not good to leave hanging about of course.

Try:

methodCall: t=target ((parameter)=>p+=parameter)* -> ^(CALL $t $p*);

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Mark Volkmann
> Sent: Tuesday, October 09, 2007 11:41 AM
> To: Bruce Pierson
> Cc: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] simple rules not deterministic
> 
> On 10/9/07, Bruce Pierson <bpierson at theglobal.net> wrote:
> > In addition to what I said before:
> >
> > The whole thing should look like this:
> >
> > methodCall: t=target p+=parameter* -> ^(CALL $t $p*);
> >
> > Notice the '*' after the $p
> 
> Thanks! Unfortunately even with that change I still get the warning on
> that line.
> Maybe it would help if I include the entire grammar. It's still pretty
> short. This is the first grammar I've written for ANTLR so any
> suggestions are welcomed! Here it is.
> 
> grammar LIM;
> 
> options {
>   output = AST;
>   ASTLabelType = CommonTree;
> }
> 
> tokens {
>   ASSIGN;
>   ATTR;
>   BLOCK;
>   CALL;
>   CLASS;
>   METHOD;
>   OBJECT = 'Object';
>   PARAMS;
> }
> 
> @lexer::header {
> package org.lessismore.parser;
> }
> 
> @parser::header {
> package org.lessismore.parser;
> }
> 
> access: 'private' | 'readonly' | 'rw'; // default is 'rw' (read/write)
> 
> attr
>   : NAME -> ^(ATTR NAME OBJECT 'rw')
>   | NAME ':' type -> ^(ATTR NAME type 'rw')
>   | NAME ':' type ':' access -> ^(ATTR NAME type access);
> 
> assignment: variableDef '=' expression NEWLINE -> ^(ASSIGN variableDef
> expression);
> 
> block: '[' parameterList? statement+ ']' -> ^(BLOCK parameterList?
> statement+);
> 
> classDef: attr* methodDef* -> ^(CLASS attr* methodDef*);
> 
> comment: commentBlock | commentLine;
> commentBlock:	'##' TEXT '##';
> commentLine: '#' TEXT NEWLINE;
> 
> constant: yesno | NUMBER | textLiteral;
> 
> // When an expression is a single name, we'll first see if it's a
> method name.
> // If not, we'll see if it's a variable name.
> expression: constant | methodCall;
> 
> methodDef: NAME '=' block -> ^(METHOD NAME block);
> 
> // TODO: Can't distinguish between a target and a parameter?
> methodCall: t=target p+=parameter* -> ^(CALL $t $p*);
> 
> objectReference: 'me' | NAME;
> 
> parameterList: parameter+ '|' -> ^(PARAMS parameter+);
> 
> parameter: variableDef;
> 
> statement: assignment | comment | methodCall;
> 
> target: objectReference | CLASS_NAME;
> 
> textLiteral: '"' TEXT '"';
> 
> type:	'integer' | 'float' | 'text' | 'yesno';
> 
> variableDef: NAME (':' type)?;
> 
> yesno: 'yes' | 'no';
> 
> CHARACTER: ' '..'~'; // all ASCII characters except control characters
> CLASS_NAME: UPPERCASE_LETTER (LETTER | DIGIT)*;
> DIGIT: ZERO | NONZERO_DIGIT;
> FLOAT: INTEGER '.' UNSIGNED_INTEGER ('e' INTEGER)?;
> INTEGER: ZERO | SIGN? NONZERO_DIGIT DIGIT*;
> LETTER:	LOWERCASE_LETTER | UPPERCASE_LETTER;
> LOWERCASE_LETTER: 'a'..'z';
> NAME: LOWERCASE_LETTER (LETTER | DIGIT)*;
> NEWLINE: '\r'? '\n';
> NUMBER:	INTEGER | FLOAT;
> NONZERO_DIGIT:	'1'..'9';
> UNSIGNED_INTEGER:	ZERO | (NONZERO_DIGIT DIGIT*);
> SIGN:	'+' | '-';
> 
> // All ASCII characters except letters, digits and control characters.
> SYMBOL:	'!'..'/' | ':'..'@' | ']'..'`' | '{'..'~';
> 
> TEXT:	CHARACTER+;
> UPPERCASE_LETTER:	'A'..'Z';
> WS:	(' ' | '\n' | '\r' | '\t')+ { $channel = HIDDEN; };
> ZERO:	'0';
> 
> > --Bruce
> >
> > -----Original Message-----
> > From: antlr-interest-bounces at antlr.org
> > [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Mark Volkmann
> > Sent: Tuesday, October 09, 2007 11:07 AM
> > To: antlr-interest at antlr.org
> > Subject: [antlr-interest] simple rules not deterministic
> >
> > When I generate code from my grammar I get a warning I don't
> > understand that says "Decision can match input such as "NAME" using
> > multiple alternatives: 1, 2". This is on the line that defines
> > methodCall.
> >
> > Here's a snippet of my grammar.
> >
> > methodCall: t=target p=parameter* -> ^(CALL $t $p);
> > objectReference: 'me' | NAME;
> > parameter: variableDef;
> > target: objectReference | CLASS_NAME;
> > type:   'integer' | 'float' | 'text' | 'yesno';
> > variableDef: NAME (':' type)?;
> > CLASS_NAME: UPPERCASE_LETTER (LETTER | DIGIT)*;
> > NAME: LOWERCASE_LETTER (LETTER | DIGIT)*;
> >
> > I see that NAME matches both target and parameter, but target is
> > required and parameter is optional. I don't understand why this isn't
> > deterministic.
> >
> > --
> > R. Mark Volkmann
> > Object Computing, Inc.
> >
> >
> 
> 
> --
> R. Mark Volkmann
> Object Computing, Inc.
> 
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.488 / Virus Database: 269.14.5/1058 - Release Date:
> 10/8/2007 4:54 PM
> 

No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.488 / Virus Database: 269.14.5/1058 - Release Date: 10/8/2007
4:54 PM
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071009/8f2b5c1b/attachment-0001.html 


More information about the antlr-interest mailing list