简体   繁体   中英

Antlr grammar, implicit token definition in parser rule

A weird thing is going on. I defined the grammar and this is an excerpt.

name  
   : Letter                 
   | Digit name              
   | Letter name          
   ;

numeral  
   : Digit                
   | Digit numeral         
   ;

fragment  
Digit  
   : [0-9]  
   ;  

fragment  
Letter  
   : [a-zA-Z]  
   ;

So why does it show warnings for just two lines (Letter and Digit name) where i referenced a fragment and others below are completely fine...

Lexer rules you mark as fragment s can only be used by other lexer rules, not by parser rules. Fragment rules never become a token of their own.

Be sure you understand the difference: What does "fragment" mean in ANTLR?

EDIT

Also, I now see that you're doing too much in the parser. The rules name and numeral should really be a lexer rule:

Name
 : ( Digit | Letter)* Letter
 ;

Numeral
 : Digit+
 ;

in which case you don't need to account for a Space rule in any of your parser rules (this is about your last question which was just removed).

Just in case you are using an older version of antlr: [0-9]

and

[a-zA-Z] 

are not valid regular expressions in old Antlr. replace them with

'0'..'9'

and

('a'..'z' | 'A'..'Z') 

and your issues should go away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM