【发布时间】:2016-02-04 16:10:35
【问题描述】:
我开始使用 JFlex,我想先尝试编写一个词法分析器,然后再转到解析器。但是,如果不使用 CUP 编写解析器,似乎就无法测试您的 JFlex 词法分析器。
我要做的就是编写一个词法分析器,给它一个输入文件,然后输出词位以检查它是否正确读取所有内容。稍后我想输出标记,但词位会是一个好的开始。
【问题讨论】:
-
你看
%debug指令了吗?
我开始使用 JFlex,我想先尝试编写一个词法分析器,然后再转到解析器。但是,如果不使用 CUP 编写解析器,似乎就无法测试您的 JFlex 词法分析器。
我要做的就是编写一个词法分析器,给它一个输入文件,然后输出词位以检查它是否正确读取所有内容。稍后我想输出标记,但词位会是一个好的开始。
【问题讨论】:
%debug指令了吗?
是的 可以编写一个独立的扫描器。您可以在this 页面上阅读详细信息。如果您指定%standalone 指令,它将在生成的类中添加main 方法。您可以将输入文件作为命令行参数来运行该程序。 jflex tar 带有一个示例目录,您可以在 examples/standalone-maven/src/main/jflex 目录中找到一个独立的示例。为了快速参考,我在这里发布了一个示例代码
/**
This is a small example of a standalone text substitution scanner
It reads a name after the keyword name and substitutes all occurences
of "hello" with "hello <name>!". There is a sample input file
"sample.inp" provided in this directory
*/
package de.jflex.example.standalone;
%%
%public
%class Subst
%standalone
%unicode
%{
String name;
%}
%%
"name " [a-zA-Z]+ { name = yytext().substring(5); }
[Hh] "ello" { System.out.print(yytext()+" "+name+"!"); }
【讨论】: