2012-08-01 73 views
1

你能否幫我找出我在下面的代碼中出錯的地方 - (或者指向我可以找到/學習的地方)。請幫我改進下面的語法

柔性輸入 -

%{ 
     #include "jq.tab.h" 
     void yyerror(char *); 
%} 
method   add|map|.. and other methods go here 

%% 

"/*"   { return CS; } 

"*/"   { return CE; } 

"jQuery"  { 
       printf("%s is yytext\n", yytext); 
       return *yytext; 
       } 

"args"   { return ARGUMENT; } 

{method}  { return METHOD; } 

[().\n]   { return *yytext; } 

[ \t]+   { return WS; } 

.    { return IGNORE; } 

%% 

int yywrap(void) { 
     return 1; 
} 

野牛輸入 -

%{ 
     #include <stdio.h> 
     int yylex(void); 
     void yyerror(char *); 
%} 

%token ARGUMENT METHOD IGNORE WS CS CE 
%error-verbose 

%% 

stmts: 
     stmt '\n'    { printf("A single stmt\n"); } 
     | stmt '\n' stmts  { printf("Multi stmts\n"); } 
     ; 

stmt: 
     jQuerycall      { printf("A complete call ends here\n"); } 
     | ignorechars     { printf("Ignoring\n"); } 
     | ignorechars WS jQuerycall  { printf("ignore+js\n"); } 
     | jQuerycall WS ignorechars  { printf("js+ignore\n"); } 
     | optionalws stmt optionalws 
     | CS stmt CE     { printf("comment\n"); } 
     ; 

jQuerycall: 
     'jQuery' '(' ARGUMENT ')' '.' methodchain  { printf("args n methodchain\n"); } 
     | 'jQuery' '(' ')' '.' methodchain    { printf("methodchain\n"); } 
     | 'jQuery' '(' ARGUMENT ')'      { printf("args\n"); } 
     | 'jQuery' '(' ')'        { printf("empty call\n"); } 
     ; 

methodchain: 
     methodchain '.' methodcall 
     | methodcall 
     ; 

methodcall: 
     METHOD '(' ')' 
     ; 

ignorechars: 
     IGNORE 
     | IGNORE optionalws ignorechars 
     ; 

optionalws: 
     | WS 
     ; 

%% 

void yyerror(char *s) { 
     fprintf(stderr, "%s\n", s); 
} 

int main(void) { 
     yyparse(); 
     return 0; 
} 

目的是識別任何jQuery的電話與所有的元素。而忽略任何其他語句/串。也忽略評論..現在,這段代碼做了很多假設 - 比如'args'是jQuery中唯一的選擇器元素()

[編輯] 請看下面的輸入輸出情況。像10和12是我想弄明白的..

> 1.input: statement\n output: Ignoring 
> 
> 2.input: statement statement\n output: Ignoring 
> 
> 3.input: statement statement statement\n output: Ignoring 
> 
> 4.input: jQuery()\n output: jQuery is yytext empty call A complete call ends here 
> 
> 5.input: jQuery(args)\n output: jQuery is yytext args A complete call ends here 
> 
> 6.input: jQuery().add()\n output: jQuery is yytext methodchain A complete call ends here 
> 
> 7.input: jQuery(args).add().map()\n output: jQuery is yytext args n methodchain A complete call ends here 
> 
> 8.input: /*comment*/\n output: Ignoring comment 
> 
> 9.input: /*jQuery()*/\n output: jQuery is yytext empty call A complete call ends here comment 
> 
> 10.input: /* comment */\n output: syntax error, unexpected CE, expecting IGNORE 
> 
> 11.input: var a = b\n output: Ignoring 
> 
> 12.input: var a = jQuery(args)\n output: jQuery is yytext syntax error, unexpected 'jQuery', expecting IGNORE 

非常感謝!

+0

「 Adobe Flex「標籤並將其替換爲w/gnu-Flex。 – JeffryHouser 2012-08-01 14:43:23

+0

好吧,我會記得下一次 – trinity 2012-08-01 16:15:02

+0

如果你需要幫助,告訴我們你的問題的症狀是什麼,以及爲什麼你很難診斷根本原因。我們很少有人會費心去閱讀你的代碼,試圖猜測你的症狀是什麼。如果你懶惰,我們也是。 – 2012-08-01 16:34:56

回答

0

在你的檔案法的規則:

"jQuery"  { 
       printf("%s is yytext\n", yytext); 
       return *yytext; 
       } 

返回令牌'j',當它看到的jQuery的輸入字符串。既然你的野牛文件從來沒有用令牌'j'做任何事情,這通常會給你一個語法錯誤。

您需要將JQUERY添加到您的%token聲明中並使該lex規則返回該聲明。

編輯

一般評論可以在任何地方的程序(任何其他兩個標記之間)出現,並且被完全忽略。所以對付他們的最簡單的方法是在詞法分析器:

%x comment 
%% 
"/*"   { BEGIN comment; } 
<comment>.  ; 
<comment>"*/" { BEGIN 0; } 

這將跳過的意見(所有沒有返回標記),所以語法並不需要擔心。如果你不希望使用詞法分析器啓動狀態,你也可以使用複雜的正則表達式:

"/*"([^*]|\*+[^*/])*\*+"/"   ; 
+0

好的,我修正了這個問題。此外,規則ignorechars:IGNORE | IGNORE的optionalws ignorechars有一個正確的遞歸。修正了這個問題..但是要修復案例10 – trinity 2012-08-02 06:36:31

+0

謝謝你!我用詞法分析器:) – trinity 2012-08-12 06:44:41

0

我想我可以給你,將解決10的情況下的修復程序,但有一個更深層次的問題。

由於情況下,8爲您提供您所期望的結果,我推斷輸入

/*comment*/ 

由prouduction

stmt: CS stmt CE 

這就是說字符串「評論」是公認的認可作爲stmt。但是,當您在CSstmt之間添加空格時,解析失敗,這是您的情況10。您可以通過重寫你的生產爲

stmt: CS optionalws stmt optionalws CE 

修補這其中更深層次的問題是,我刪除了你的解析器無法識別的其他意見,比如

/* This is a remarkable remark, isn't it? */ 

/** 
* This is a multi-line comment. 
*/ 
+0

不工作,謝謝你嘗試雖然.. – trinity 2012-08-05 09:35:22