[antlr-interest] Understanding nondeterminism warnings: and how to
debug them
Vladimir Sutskever
vs667 at nyu.edu
Thu Feb 23 11:16:56 PST 2006
I cant seem to get rid of the few nondeterminism warnings that appear
when I attempt to generate the parser.
How would I debug something like this? Line #14 does not have "BEGIN"
...how do the warning line #'s correspond to my grammar?
This is my first attempt to generate some grammar, and I know I lack
much(all) of the intuition behind it.
Hopefully you guys/gals can help me get trough this :)
I have attached my grammar file.
This is the error nondeterminism error I get:
grammer.txt:14:75: warning:nondeterminism between alts 1 and 2 of block upon
grammer.txt:14:75: k==1:BEGIN
grammer.txt:40: warning:nondeterminism upon
grammer.txt:40: k==1:SEMI_COLON
grammer.txt:40: between alt 1 and exit branch of block
grammer.txt:58:21: warning:nondeterminism between alts 1 and 2 of block upon
grammer.txt:58:21: k==1:ID
grammer.txt:69:56: warning:nondeterminism between alts 1 and 2 of block upon
grammer.txt:69:56: k==1:ELSE
grammer.txt:105: warning:nondeterminism between alts 3 and 4 of block upon
grammer.txt:105: k==1:ID
-Vladimir
-------------- next part --------------
//******************************************************** [PARSER]
//********************************************************
//********************************************************
// [X] 0 or 1 = (X)?
// {X} 0 or more instances (X)*
options {
language = "CSharp";
}
class ParserCore extends Parser;
//options { k=1; }
//========================================================[]
program: PROGRAM ID SEMI_COLON (typeDefinitions)? (variableDeclarations)? (subprogramDeclarations)? compoundStatement;
//========================================================[]
typeDefinitions : TYPE typeDefinition SEMI_COLON (typeDefinition SEMI_COLON )*;
//========================================================[]
variableDeclarations : VAR variableDeclaration SEMI_COLON (variableDeclaration SEMI_COLON ) ;
//========================================================[]
subprogramDeclarations : ((procedureDeclaration | functionDeclaration) SEMI_COLON )*;
//========================================================[]
typeDefinition: ID EQUAL type;
//========================================================[]
variableDeclaration : identifierList COLON type;
//========================================================[]
procedureDeclaration : PROCEDURE ID ( formalParameterList ) SEMI_COLON ( block | FORWARD );
//========================================================[]
functionDeclaration : FUNCTION ID ( formalParameterList ) COLON resultType SEMI_COLON ( block | FORWARD );
//========================================================[]
formalParameterList : (identifierList COLON type ( SEMI_COLON identifierList : type )* )?;
//========================================================[]
block : (variableDeclarations)? compoundStatement;
//========================================================[]
compoundStatement : BEGIN statementSequence END;
//========================================================[]
statementSequence : statement (SEMI_COLON statement )?;
//========================================================[]
statement : simpleStatement | structuredStatement;
//========================================================[]
simpleStatement : ( (assignmentStatement | procedureStatement) )?;
//========================================================[]
assignmentStatement : variable (":=") expression;
//========================================================[]
procedureStatement : ID ( actualParameterList );
//========================================================[]
structuredStatement : compoundStatement
| IF expression THEN statement ( ELSE statement )?
| WHILE expression DO statement
| FOR ID ":=" expression TO expression DO statement
;
//========================================================[]
type : ID| ARRAY BRAKET_S_OPEN(constant PERIOD_PERIOD constant)BRACKET_S_CLOSE OF type | RECORD fieldList END;
//========================================================[]
resultType : ID;
//========================================================[]
fieldList : ( identifierList : type ( SEMI_COLON identifierList COLON type )* )?;
//========================================================[]
constant : ( sign )? INTEGER;
//========================================================[]
expression : simpleExpression ( relationalOp simpleExpression )*;
//========================================================[]
relationalOp : OP_LESS| LESS_EQUAL | OP_GREATER| GREATER_EQUAL | NOT_EQUAL | EQUAL;
//========================================================[]
simpleExpression : (sign)? term (addOp term )*;
//========================================================[]
addOp : OP_ADD_SUB| OR;
//========================================================[]
term : factor ( mulOp factor )*;
//========================================================[]
mulOp : OP_MULT|DIV|MOD|AND;
//========================================================[]
factor : INTEGER | STRING| variable | functionReference
| NOT factor | PAREN_OPEN expression PAREN_CLOSE;
//========================================================[]
functionReference : ID PAREN_OPEN actualParameterList PAREN_CLOSE;
//========================================================[]
variable : ID componentSelection;
//========================================================[]
componentSelection : BRAKET_S_OPEN ( PERIOD ID componentSelection | (expression)? componentSelection ) BRAKET_S_CLOSE;
//========================================================[]
actualParameterList : BRAKET_S_OPEN expression (COMMA expression )* BRAKET_S_CLOSE;
//========================================================[]
identifierList : ID (COMMA ID)*;
//========================================================[]
sign : OP_ADD_SUB;
//******************************************************** LEXER
//********************************************************
//********************************************************
class LexerCore extends Lexer;
options {
k=2;
}
//========================================================[STRING-ALL PRINTABLE CHARS]
STR : BRACKET_C_OPEN (
LETTER
|DIGIT
|'!'
|'#'
|'$'
|'%'
|'&'
|''
|'('
|')'
|OP_MULT
|'/'
|OP_ADD_SUB
|','
|OP_LESS
|OP_GREATER
|'='
|'.'
|':'
|';'
|'@'
|'['
|'\\'
|']'
|'^'
|'_'
|''
|'|'
|'~'
|'?'
|WS
)* BRACKET_C_CLOSE
{ $setType(Token.SKIP); }
;
//========================================================[KEYWORDS]
AND: "and";
BEGIN: "begin";
FORWARD_OR_FOR
: ("forward") =>FORWARD{$setType(FORWARD);}
| ("for")=>FOR {$setType(FOR); }
;
protected
FORWARD: "forward";
protected
FOR: "for";
DIV: "div";
DO: "do";
ELSE: "else";
END: "end";
FUNCTION: "function";
IF: "if";
ARRAY:"array";
MOD: "mod";
NOT: "not";
OF: "of";
OR: "or";
PROCEDURE: "procedure";
PROGRAM: "Program";
RECORD: "record";
THEN: "then";
TO: "to";
TYPE: "type";
VAR: "var";
WHILE: "while";
//========================================================[WHITE SPACE]
WS
: (SPACE
| '\t'
| '\n'
| '\r')
{ $setType(Token.SKIP); }
;
//========================================================[INTEGER]
INT
: (DIGIT)+
;
//========================================================[SYMBOLS]
protected
SPACE
: ' '
;
PAREN_OPEN
: '('
;
PAREN_CLOSE
: ')'
;
OP_MULT
: '*'
;
OP_ADD_SUB
: '+'
| '-'
;
NOT_EQUAL
: "<>"
;
EQUAL
: '='
;
SEMICOLON
: ';'
;
COLON
: ':'
;
OP_GREATER
: '>'
;
OP_LESS
: '<'
;
GREATER_EQUAL
: ">=";
LESS_EQUAL
: "<="
;
PIPE
: '|'
;
BRAKET_S_OPEN
: '['
;
BRACKET_S_CLOSE
: ']'
;
BRACKET_C_OPEN
: '{'
;
BRACKET_C_CLOSE
: '}'
;
PERIOD_PERIOD: PERIOD PERIOD
;
PERIOD
: '.'
;
UNDERSCORE
: '_'
;
COMMA: ','
;
QUOTE: '"';
//========================================================[DIGITS]
protected
DIGIT
: '0'..'9'
;
//========================================================[LETTERS]
protected
LETTER
: ('a'..'z'|'A'..'Z')
;
//========================================================[IDENTIFIER]
ID
options
{
testLiterals=true;
generateAmbigWarnings=false;
}
: LETTER (LETTER|DIGIT|UNDERSCORE)*
;
More information about the antlr-interest
mailing list