【问题标题】:JFlex: Unexpected end of file macrosJFlex:文件宏意外结束
【发布时间】:2018-02-19 00:30:11
【问题描述】:

这是我的第一个 JFlex 程序,所以我不明白为什么会出现这些错误。当我运行我的代码时,它给了我消息“文件宏意外结束”并且没有指定错误在哪一行。此外,在注释掉 %eof 代码(即第 18 到 24 行)后,它给了我一个错误“意外符号”,它指向我的第一个正则表达式的开头。谁能告诉我为什么会遇到这些问题?

import java.io.*;
import java.util.*;

%%
%class test
%line
%column


%{
    PrintWriter pw = new PrintWriter(new File(args[0]));
    int ids = 0;
    int nums = 0;
    int comments = 0;
    int literals = 0;
%}

%eof{
    pw.println("number of identifiers = " + ids);
    pw.println("number of numbers = " + nums);
    pw.println("number of comments = " + comments);
    pw.println("number of string literals = " + literals);
    pw.close();
eof%}

[A-Za-z_][A-Z-a-z_0-9]*     {ids++;
                             System.out.print(ids);}

(\n|\s|\t)[0-9](\n|\s|\t)   {nums++;
                             System.out.print(nums);}

\/*.*/\                     {comments++;
                             System.out.print(comments);}

\p{Punct}                   {literals++;
                             System.out.print(literals);}


                             System.out.println(literals);}

【问题讨论】:

    标签: java jflex


    【解决方案1】:

    您需要将% 符号放在文件宏结束标记的eof 之前。

    %eof{
        /* Code to put at the end */
    %eof}
    

    【讨论】:

      猜你喜欢
      • 2012-06-10
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多