[antlr-interest] Tree grammar's rule with alternatives -> no viable alternative

Петров Александр gmdidro at gmail.com
Mon May 26 12:25:33 PDT 2008


---------- Forwarded message ----------
From: Петров Александр <gmdidro at gmail.com>
Date: 26 May 2008 23:20
Subject: Tree grammar's rule with alternatives cause no viable alternative
To: antlr-interest at antlr.org


Hello, I've sample grammars to illustrate my problem:

/////------------------------T1.g BEGIN-----------------------------/////
grammar T1;

options{ output = AST;
ASTLabelType=CommonTree;
language = CSharp;}

tokens{ CPROGRAM; NODES; SM; OBJECTNAME; }


program : name=ID ('.' d+=ID)* '.' sm=ID '.' 'go'
       -> ^(CPROGRAM ^(OBJECTNAME $name) ^(NODES $d*) ^(SM $sm) );

ID   :   ('a'..'z' | 'A'..'Z' | '_') ('a'..'z' | 'A'..'Z' | '0'..'9' | '_')*;
/////------------------------T1.g END-----------------------------/////

/////------------------------T1tree.g BEGIN-----------------------------/////
tree grammar T1tree;

options {tokenVocab=T1;
ASTLabelType=CommonTree;
language = CSharp;}

param : ^(CPARAM ID);
program
 :
     ^(CPROGRAM ^(OBJECTNAME ID) ^(NODES ID*) ^(SM ID) )
   | ^(CPROGRAM ^(OBJECTNAME ID) ^(NODES ID*) ^(SM ID) param)
   {Console.WriteLine("OK");}
 ;
/////------------------------T1tree.g END-----------------------------/////

So when I parse the text "a.id1.id2.go", I have:

------------- GENERATED AST TREE -------------
(CPROGRAM (OBJECTNAME a) (NODES id1) (SM id2))
------------- GENERATED AST TREE -------------
Ok

But when I parse the text "a.id.go", I have:

Processing file: D:\antlr\empty nodes\test.txt
------------- GENERATED AST TREE -------------
(CPROGRAM (OBJECTNAME a) NODES (SM id))
------------- GENERATED AST TREE -------------
T1tree.g: node from line 0:-1 no viable alternative at input 'SM'
Finished processing file: D:\antlr\empty nodes\test.txt

Why ? What is the cause of "no viable alternative" error ?
I note, that if I erase the second alternative in T1.g, that all will
be all right. I don't understand it.

Thank you,
Alexander Petrov


More information about the antlr-interest mailing list