Matching Floating-Point Numbers
Floating-point numbers are more complex to represent, and additional rules will need to be implemented because of them. Take the example below:
../../DelphiDFM.g4
grammar DelphiDFM ;
// Parser Rules
file : object
object : objectElements ;
objectElements : CAPTION '=' STRING | LEFT '=' INT | TOP '=' FLOAT | ... ;
// Lexer Rules
CAPTION : 'Caption' ;
LEFT : 'Left' ;
TOP : 'Top' ;
ID : [a-zA-Z]+ ;
FLOAT : DIGIT+ '.' DIGIT* | '.' DIGIT+ ;
INT : DIGIT+ ;
fragmented DIGIT : [0-9] ;
STRING : '\'' .*? '\'' ;
Note:
- The order of precedence in the lexer rules, specifically
FLOATandINT. If their order would be reversed, an ambiguity conflict would occur. For example, if the lexer sees the sequence123it could match either. Thus, ifINTis defined first, the lexer will always preferINTand never reachFLOAT. - The separation between property elements and their values, through the
=operator. - The addition of the helper rule
DIGIT, to avoid writing out[0-9]everywhere. DIGITis prefixed withfragmentedto let ANTLR know that the rule will be used only by other lexical rules (will be elaborated further down the line).