[antlr-interest] Is tokenVocab an Option for Parser Grammars?

titech at metaorb.com titech at metaorb.com
Sun Aug 10 15:11:03 PDT 2008


I retested with the simple grammar files below.  The combined grammar works fine, but the separate parse file causes an error regarding the literals (error text below).  Do separate parse files require that all literals have associated lexer rules?
 
BTW I just got your book--great stuff, wish I'd looked into ANTLR a long time ago!
 
Thanks,
--Mike
 
 
Parse.g
-----------------------------------------------
parser grammar Parse;
 
options
{
  tokenVocab = Lex;
}
 
rule :     '(' INT ')' ;
 
 
Lex.g
-----------------------------------------------
lexer grammar Lex;
 
INT: '0' | SIGN? '1'..'9' '0'..'9'*;
 
fragment SIGN: '+' | '-';
 
 
plex.g
-----------------------------------------------
grammar plex;
 
rule :     '(' INT ')' ;
 
 
INT: '0' | SIGN? '1'..'9' '0'..'9'*;
 
fragment SIGN: '+' | '-';
 
 
 
-----------------------------------------------
>> java org.antlr.Tool Parse.g
ANTLR Parser Generator  Version 3.0.1 (August 13, 2007)  1989-2007
error(107): Parse.g:8:10: literal has no associated lexer rule: '('
error(107): Parse.g:8:18: literal has no associated lexer rule: ')'
 
 
 
 
 
>On Aug 9, 2008, at 6:01 AM, Johannes Luber wrote:
 
>>>> Of course this all works if I copy the lexer rules to the parser.   
>>>> Is this the correct syntax?
>>> Yes, it is the correct syntax and I verified that it works on b2 on  
>>> my local machine. Are you generating the lexer first and is it in  
>>> the same directory as the parser? What error message are you  
>>> getting? Your parser grammar name needs to be the same as the file  
>>> name by the way.
>>
 
>> It should be mentioned that the tokenVocab-option requires the use  
>> of the tokens file, which must be in the same directory as the  
>> grammar which uses the tokenVocab-option. There is -o option for  
>> ANTLR which moves all the output files into another directory,  
>> included the generated source code files. If that doesn't work for  
>> you and you don't want merge the directories containing the grammar  
>> files, you have to include a file copy in your build setup.
 
>I think ANTLR looks in -lib too for .tokens files.
>Ter


More information about the antlr-interest mailing list