0

I have a grammar which parses alphabet characters and numbers separately:

grammar Demo;

options
{
  language = C;
}

program : process+
        ;

process :  Alphanumeric {printf("\%s",$Alphanumeric.text->chars);}
        ;

Alphanumeric : (Alphabet | Number)+
             ;


fragment Alphabet : ('a'..'z')+
                  ;

fragment Number : ('0'..'9')+
                ;

Suppose, the input is 'a10' or 'b10', the printf statement would display a10 or b10, but I want the alphabet character and the number to be split i.e, a and 10 has to be split separately, because I need 'a' to be compared with an other string and save the number, the one next to 'a' or 'b' etc., to a table.

To be precise, a10 has to be split -> a for comparison and 10 for the storage and I should be able to fetch both the alphabet and number separately.

How to define a grammar for something like this?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
sh_ara
  • 1
  • 2

1 Answers1

1

You need to expose Alphabet and Number to your parser separately, which means they should be top-level rules in the lexer (not fragment rules). As a result, Alphanumeric will also become a parser rule:

alphanumeric : (Alphabet | Number)+
             ;

Alphabet : ('a'..'z')+
         ;

Number : ('0'..'9')+
       ;
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280