I use javacc 5.0 to generate a json parser with the grammar file: https://github.com/inqwell/json/blob/master/src/main/javacc/com/inqwell/json/JSON.jj
But there are some errors in the generated java source code.
char escape = '\u005c\u005c';
and
switch(echar)
{
case 'n':
buf.append(System.getProperties().get("line.separator")); to++;
break;
case 'r': buf.append('\u005cr'); to++; break;
case 't': buf.append('\u005ct'); to++; break;
case 'b': buf.append('\u005cb'); to++; break;
case 'f': buf.append('\u005cf'); to++; break;
case '\u005c\u005c': buf.append('\u005c\u005c'); to++; break;
case '"': buf.append('"'); to++; break;
case '\u005c'': buf.append('\u005c''); to++; break;
case '\u005cr': to++; if (to < len && s.charAt(to) == '\u005cn') to++; break;
case '\u005cn': to++; break;
}
And in the grammer file, they are:
char escape = '\\';
And
switch(echar)
{
case 'n':
buf.append(System.getProperties().get("line.separator")); to++;
break;
case 'r': buf.append('\r'); to++; break;
case 't': buf.append('\t'); to++; break;
case 'b': buf.append('\b'); to++; break;
case 'f': buf.append('\f'); to++; break;
case '\\': buf.append('\\'); to++; break;
case '"': buf.append('"'); to++; break;
case '\'': buf.append('\''); to++; break;
case '\r': to++; if (to < len && s.charAt(to) == '\n') to++; break;
case '\n': to++; break;
}
These case chars should not be escaped, but how to do it?
update
The code can’t be compiled in my IDEA, please see my screenshot:

There is no error in the generated code. The literals
'\\'and'\u005c\u005c'mean the same thing in Java. The reason is that unicode escapes are processed first and other escape sequences are processed later. See section 3.2 of the JLS (3rd edition) http://docs.oracle.com/javase/specs/jls/se5.0/html/lexical.htmlEdit: The following was added in response to Freewind’s comment