[antlr-interest] Question on lexer rules

Ashish Thusoo athusoo at facebook.com
Thu Jun 5 16:42:18 PDT 2008


Hi,

I am an antlr newbie and am trying to use antlr v3 for recognizing the
following grammar:

--------------------------------------------------
grammar Test;

options
{
output=AST;
ASTLabelType=CommonTree;
backtrack=true;
memoize=true;
}

start
  :
  'Id' qualifiedName |
  'uri' Uri
  ;
  
fragment
Letter
  :
  'a'..'z' | 'A'..'Z'
  ;

qualifiedName
  :
  Id '.' Id
  ;

Uri
  :
  (Letter | '/' | '.')+
  ;

Id
  :
  Letter+
  ;

WS
  :
  (' '|'\r'|'\t'|'\n') {$channel=HIDDEN;}
  ;
-------------------------------------------------

The problem that I have is that the lexel rule Uri subsumes the rule for
Id as well as for qualifiedName. As a result nothing gets tokenized as
Id or a qualifiedName and so the following string 

Id abc.xyz

does not match the grammar. I thought that this would work with
backtracking but it didn't. How do I achieve this with antlr v3? Do I
have to use semantic predicates to achieve this and if so then how do I
turn of the lexical rule for Uri when I see the 'Id' keyword.

Confused...
Ashish


More information about the antlr-interest mailing list