[Q] How to get string value without double quote.

38 views
Skip to first unread message

Hiroyuki Sato

unread,
Aug 23, 2016, 4:24:26 AM8/23/16
to antlr-discussion
Hello, members.

ANTLR4 Question.

I would like to create some tool with JSON.g4
And I want to get STRING value without double quote characters.

Original

STRING
  : '"' (ESC | ~ ["\\])* '"'
  ;

Question
  Is it possible to get string value without the double quote from STRING token?
  Or is it better to erase double quote char in visitor method?

I tried (But It does not work as I expected)

//STRING
string
//   : '"' (ESC | ~ ["\\])* '"'
  : '"' STRING_VALUE '"'
  ;

STRING_VALUE
  :  (ESC | ~ ["\\])*
  ;

I got the following warning. 

warning(146): JSON.g4:44:0: non-fragment lexer rule STRING_VALUE can match the empty string

Best regards.







Mike Lischke

unread,
Aug 23, 2016, 4:28:58 AM8/23/16
to antlr-di...@googlegroups.com
> I would like to create some tool with JSON.g4
> (https://github.com/antlr/grammars-v4/blob/master/json/JSON.g4)
> And I want to get STRING value without double quote characters.

Don't overload your grammar with these details. Simply remove the quotes in your listener. You may later also want the original text, which wouldn't be possible if you manipulate it already in the lexer.

Mike
--
www.soft-gems.net

Hiroyuki Sato

unread,
Aug 23, 2016, 5:13:49 AM8/23/16
to antlr-discussion
Hello, Mike. 

Thank you for advice.
I'll remove quotes in my listener.

2016年8月23日火曜日 17時28分58秒 UTC+9 Mike Lischke:
Reply all
Reply to author
Forward
0 new messages