我已经尝试过这个链接:Print in JavaCC。但是由于某种未知的原因,该答案对我不起作用。我将文本复制并粘贴到文件中并运行它,但是当我输入时µ
,例如,它没有打印任何内容。
我希望能够在我的字符串标记中使用非英语。仅出于测试目的,现在我有:
options
{
UNICODE_INPUT = true;
JAVA_UNICODE_ESCAPE = false;
}
PARSER_BEGIN(Unicode)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class Unicode
{
public static void main(String[] args)
{
if(args.length == 0)
{
System.out.println("File name not specified!");
return;
}
System.out.println("-----Start-----\n\n");
try
{
FileInputStream fis = new FileInputStream(args[0]);
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
Unicode parser = new Unicode(isr);
parser.start();
}
catch(FileNotFoundException ex){
System.out.println(ex);
}
catch(UnsupportedEncodingException ex){
System.out.println(ex);
}
catch(ParseException ex){
System.out.println(ex);
}
catch(TokenMgrError ex){
System.out.println(ex);
}
System.out.println("\n\n------End-------");
}
}
PARSER_END(Unicode)
TOKEN:{
// á é í ó ú
<STR: ("\u00e1" | "\u00e9" | "\u00ed" | "\u00f3" | "\u00fa")>
}
void start():
{
Token found;
}
{
(
found = <STR>
{System.out.println("Input: " + found.image);}
)+
<EOF>
}
当我运行解析器并为其提供一个包含 的文件á, é, í, ó, ú
时,我得到的只是一堆问号。
Input: ?
Input: ?
Input: ?
Input: ?
Input: ?
我读过一些关于必须修改自动生成的字符流文件的内容,但我不太明白。