2

我开始使用 JFlex,我想先尝试编写一个词法分析器,然后再转到解析器。但是,如果不使用 CUP 编写解析器,似乎就无法测试您的 JFlex 词法分析器。

我要做的就是编写一个词法分析器,给它一个输入文件,然后输出词位以检查它是否正确读取了所有内容。稍后我想输出标记,但词位将是一个好的开始。

4

1 回答 1

0

是的 可以编写独立的扫描仪。您可以阅读页面上的详细信息。如果您指定%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+"!"); }
于 2016-02-09T12:37:28.497 回答