Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 788
Sava Gavran Avatar asked Feb 13 '23 11:02

Sava Gavran


1 Answers

Lexer rules you mark as fragments 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).

like image 149
Bart Kiers Avatar answered Mar 16 '23 18:03

Bart Kiers