2012-08-01 70 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