Reputation: 29
I am new to ANTLR4 and have a trouble.
I want to have the code accept integers with _ between digits, but the output would not include _ characters. For example: it would accept 12_34_5 and the expected token would be 12345.
Is there a way to do that in ANTLR4?
Upvotes: 1
Views: 206
Reputation: 1408
I think what you mean by "output would not include _ characters" is to rewrite the text value of the recognized token. The "output" of a parse is a parse tree.
Use a lexer action.
CSharp:
grammar Rewrite;
everything: .* EOF;
Int: [0-9_]+ { Text = Text.Replace("_",""); };
WS: [ \n\r\t]+ -> skip;
Note, the "action code" is target-specific, i.e., in C# for the CSharp target. You will need to change the action code for another target.
Upvotes: 2