linux - 无法识别的规则 lex 中的错误

标签 linux lex

我正在尝试编译以下程序,但收到无法识别的规则错误。我有以下 lex 程序,它为第 18、25、28、37、41、43、44、50、56、58、、、61 行提供了许多无法识别的规则错误

用于运行的命令:$ lex lab.l

%{
#include <iostream>
#include <string>
using namespace std;

%}

%option noyywrap

%%

(\"(?:[^"]|\"\")*\")(,|\r\n?|\n)?   
{   
    string temp = yytext;

    while(temp.find("\"\"")!=string::npos){ 
        temp.replace(temp.find("\"\""),2,"&quot;");
        temp.replace(temp.find("\"\""),2,"&quot;");
    }


    temp.erase(0,1);
    temp.erase(temp.find("\""), 1);


    while(temp[temp.size() - 1] == '\n'){
        temp.erase(temp.size() - 1,1);
    }


    while(temp.find("\n")!=string::npos)
    temp.replace(temp.find("\n"),1,"<br>");


    if(temp[temp.size() - 1] == ','){
        temp.erase(temp.size() - 1,1);
        cout << "<td>" << temp << "</td>";
    }
    else{
        cout << "<td>" << temp << "</td>\n</tr>\n<tr>";                             
    }
}

("(?:|"")*"|[^",\r\n]*),?   
{   
    string temp = yytext;

    if(temp[temp.size() - 1] == ','){
        temp.erase(temp.size() - 1,1);
        if(yyleng == 1)
        temp = "&nbsp;";
        cout << "<td>" << temp << "</td>";
    }
    else{

        if(yyleng > 1)
        cout << "<td>" << temp << "</td>\n</tr>\n<tr>";                             
    }
}

%%
int main(void)
{
    cout << "<html>\n\t<body>\n\t\t<table border=3>\n<tr>";
    yylex();
    cout << "</tr>\n\t\t</table>\n\t</body>\n</html>";
    return 0;
}

最佳答案

Action 必须与模式在同一行开始才能被识别为 Action 。将您的代码更改为:

(\"(?:[^"]|\"\")*\")(,|\r\n?|\n)?   {   
    string temp = yytext;
          :
}

("(?:|"")*"|[^",\r\n]*),?   {   
    string temp = yytext;
          :
}

它应该可以正常工作。

关于linux - 无法识别的规则 lex 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16651744/

相关文章:

linux - 有没有办法为 X 中生成的应用程序指定窗口偏移量?

parsing - lex/flex可以用来解析二进制格式的源文件吗?

c - 如何删除以下 'implicit declaration of function' 警告?

c - lex - 无法识别的规则 - 使用正则表达式

c - 即使未执行写入操作,内容也会写入文件

c - 在 Linux 中以独占方式打开一个设备文件

c++ - 如何在 Qt 的 Linux 上获取 USB 驱动器的路径?

lex - ml-lex中正则表达式的行尾字符

c - 信号处理程序为什么在处理相同信号时被阻塞

linux - 如何提取tar文件?