2013-05-20 69 views
0

我想編譯下面的程序,但收到無法識別的規則錯誤。我有以下lex程序,它給我許多無法識別的行18,25,28,37,41,43,44,50,56,58,61的錯誤規則錯誤無法識別的規則lex中的錯誤

命令用於運行:$ 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; 
} 

回答

1

該動作必須從與模式相同的行開始才能被識別爲動作。將您的代碼更改爲:

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

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

它應該可以正常工作。