3

I am new to Antlr-4, but have some idea about Antlr-3, where the lexer could be created without the need of any parser rule; in order to match a lexer rule by matching some tokens like:

CLASS: 'Class' WS+ id=ID
{
System.out.println($id.text);
}
ID : [a-z]+ ;
WS : [ \r\t\n]+

How could I do this in Antlr-4 without any parser, since the Antlr-4 lexer does not allow using attributes in the rule's action?

Any idea/example will be helpful for me!

Kishore_2021
  • 645
  • 12
  • 38
  • 1
    By using the antlr-4.7, Its giving the enough information about my question `warning(201): AntlrPLexer.g4:16:20: labels in lexer rules are not supported in ANTLR 4; actions cannot reference elements of lexical rules but you can use getText() to get the entire text matched for the rule ` --------------------------------------------------------------------- By why Antlr 4, discouraging the labels in lexer rules?? – Kishore_2021 Jul 12 '17 at 08:28

1 Answers1

0

According to the ANTLR4 grammar you can certainly use a label in a lexer rule. Just create your standalone lexer grammar and feed that to ANTLR to have it generate your lexer class.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • I did that but since Antlr-4 does no special $x attribute translations in lexer actions (unlike v3). So unable to get the value of lexer rule like *$id.text* in the above example. – Kishore_2021 Jul 10 '17 at 06:43
  • Did you try $ID instead, which is actually the ID rule context (htat least that's what I expect, can't check myself ATM.)? – Mike Lischke Jul 10 '17 at 08:38
  • 1
    $ID also not working, and getting the same error: `error(128): AntlrPHPLexer.g4:210:27: attribute references not allowed in lexer actions: $ID.text` `1 error(s)` `BUILD FAIL` – Kishore_2021 Jul 11 '17 at 05:49