2013-08-19 10 views
0

萊克斯代碼解析註釋:錯誤而在法

identifier [\._a-zA-Z0-9\/]+ 
comment "//" 

<*>{comment} { 
    cout<<"Comment\n"; 
    char c; 
    while((c= yyinput()) != '\n') 
    { 
    } 
} 

<INITIAL>{s}{e}{t} { 
    BEGIN(SAMPLE_STATE); 
    return SET; 
} 

<SAMPLE_STATE>{identifier} { 
    strncpy(yylval.str, yytext,1023); 
    yylval.str[1023] = '\0'; 
    return IDENTIFIER; 
} 

在上面的代碼法,有當「//設置名爲」解析沒有錯誤。請注意句子中「//」後面的空格。但是,當「// set name」被解析時,會報告一個錯誤。你能指出我出錯的地方嗎?謝謝。

錯誤是由yyerror捕捉並報告

SampleParser.y:43: int CMTSTapTestSeq_yyerror(char*): Assertion `0 && "Error parsing Sample file\n"' failed. 

這種說法是由我添加。

+0

有什麼錯誤?它應該包含在你的問題中。 – Michelle

+0

錯誤被yyerror捕獲並報告SampleParser.y:43:int CMTSTapTestSeq_yyerror(char *):斷言'0 &&「解析示例文件\ n」'失敗。這個斷言是由我添加的。 – mickeyj

回答

0

我認爲你在簡化你的例子時犯了一個錯誤,因爲你提供的代碼工作正常,沒有你指出的錯誤。我編碼並測試它(爲了方便,我使用了C而不是C++)。然而,I see you posted a later question with more code that explained the problem better. I answered that one also.

s s 
e e 
t t 
identifier [\._a-zA-Z0-9\/]+ 
comment "//" 

%s SAMPLE_STATE 

%{ 
//#include <iostream> 
//using namespace std; 
#include <stdio.h> 
#define SET 1 
#define IDENTIFIER 2 
#define yyinput input 
%} 

%% 
<*>{comment} { 
    // cout<<"Comment\n"; 
    printf("Comment\n"); 
    char c; 
    while((c= yyinput()) != '\n') 
    { 
    } 
} 

<INITIAL>{s}{e}{t} { 
    BEGIN(SAMPLE_STATE); 
    //return SET; 
    printf("SET\n"); 
} 

<SAMPLE_STATE>{identifier} { 
    //strncpy(yylval.str, yytext,1023); 
    //yylval.str[1023] = '\0'; 
    //return IDENTIFIER; 
    printf("identifier"); 
} 

的同時接受:

//set name 
// set name