[antlr-interest] simple rules not deterministic

Mark Volkmann r.mark.volkmann at gmail.com
Tue Oct 9 11:40:57 PDT 2007


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.


More information about the antlr-interest mailing list