【问题标题】:Unrecognized rule Errors in lexlex 中无法识别的规则错误
【发布时间】:2013-05-21 04:30:59
【问题描述】:

我正在尝试编译以下程序,但收到无法识别的规则错误。我有以下 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;
}

【问题讨论】:

    标签: linux lex


    【解决方案1】:

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

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

    它应该可以正常工作。

    【讨论】:

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