简体   繁体   English

Bison yyerror 忽略语法上的下一个标记

[英]Bison yyerror ignore next token on grammar

So I'm having a litle problem on my Bison program, first be aware that my program works and is fully function what you see here is just an exerpt and sufficiant to help me.所以我的 Bison 程序有一个小问题,首先要知道我的程序有效并且完全是 function 你在这里看到的只是一个摘录,足以帮助我。

basicaly the parser reads " vector(x,2) " the program is checking if the variable " x " already exists and if the number of elements" 2 " are greater then 1. I'm throwing yyerror when this situations dont happen.基本上,解析器读取“ vector(x,2) ”程序正在检查变量“ x ”是否已经存在以及元素的数量“ 2 ”是否大于 1。当这种情况不发生时,我会抛出 yyerror。 the problem is that the error is not ignoring the rest of the token.问题是错误没有忽略令牌的 rest。 Let me show an example of vector(x,1), you will see that it should ignore the statement when reducing but is not.让我展示一个 vector(x,1) 的例子,你会看到它应该在减少时忽略该语句,但不是。

Starting parse
Entering state 0
Stack now 0
Reducing stack by rule 1 (line 58):
-> $$ = nterm PROGRAM (1.1: )
Entering state 1
...
....
Entering state 24
Stack now 0 1 5 12 15 18 24
Next token is token PARENTSIS_RIGHT (1.1: )
Shifting token PARENTSIS_RIGHT (1.1: )
Entering state 33
Stack now 0 1 5 12 15 18 24 33
Reducing stack by rule 10 (line 87):
   $1 = token RESEV_VAR (1.1: )
   $2 = token PARENTSIS_LEFT (1.1: )
   $3 = token STRING (1.1: )
   $4 = token COMMA (1.1: )
   $5 = nterm ELEMENTS (1.1: )
   $6 = token PARENTSIS_RIGHT (1.1: )
        Erro: blalalala.
-> $$ = nterm DEFINE_VECTOR (1.1: ) //here it should not run this one should skip it
Entering state 9
Stack now 0 1 9
Reducing stack by rule 6 (line 65):
   $1 = nterm DEFINE_VECTOR (1.1: )
        VECTOR
-> $$ = nterm DECLARACAO (1.1: )

As you can see at the end he is running the reduction to DEFINE_VECTOR when i specify error.正如你在最后看到的那样,当我指定错误时,他正在运行减少到 DEFINE_VECTOR。

%start PROGRAM

%%

PROGRAM:
       | PROGRAM STATMENT
       | PROGRAM error STATMENT
;

STATMENT: QUIT  { fclose(yyin); printf("\nBYE\n"); exit('0');}
    | DEFINE_VAR    { printf("\tVAR\n"); }
    | DEFINE_VECTOR { printf("\tVAR\n"); }
;

...
DEFINE_VECTOR: RESEV_VAR PARENTSIS_LEFT STRING COMMA ELEMENT PARENTSIS_RIGHT { 
            if($5<=1){
                 yyerror("Vector to small");
            }else{
                 if(check($3)==0){
                    createVar($3, 2, $5);
                 }else{
                    yyerror("Variable alrady exists");
                 }
            }
}
;

ELEMENTS: ELMENTINT
    | ELMENTFLOAT{ $$ = $1;}
;

SOLUTION:解决方案:

%start PROGRAM

%%

PROGRAM:
       | PROGRAM STATMENT
       | PROGRAM error STATMENT
       | PROGRAM error PARENTSIS_RIGHT STATMENT // added here
;

STATMENT: QUIT  { fclose(yyin); printf("\nBYE\n"); exit('0');}
    | DEFINE_VAR    { printf("\tVAR\n"); }
    | DEFINE_VECTOR { printf("\tVAR\n"); }
;

...
DEFINE_VECTOR: RESEV_VAR PARENTSIS_LEFT STRING COMMA ELEMENT PARENTSIS_RIGHT { 
            if($5<=1){
                 yyerror("Vector to small");
                 YYERROR;  //added here
            }else{
                 if(check($3)==0){
                    createVar($3, 2, $5);
                 }else{
                    yyerror("Variable alrady exists");
                    YYERROR;  added here
                 }
            }
}
;

ELEMENTS: ELMENTINT
    | ELMENTFLOAT{ $$ = $1;}
;

yyerror is just a function that prints an error message. yyerror只是一个打印错误消息的 function。 It is called by the bison parser when it has a syntax error, but you can also call it elsewhere to print an error.野牛解析器在出现语法错误时调用它,但您也可以在其他地方调用它来打印错误。 It has no effect on the parsing (it doesn't interrupt or change it -- it does not "throw")它对解析没有影响(它不会中断或改变它——它不会“抛出”)

If you want to affect the parsing, you need to use YYERROR (a macro that triggers a parser error, which the parser will then try to recover from).如果要影响解析,则需要使用YYERROR (触发解析器错误的宏,然后解析器将尝试从中恢复)。 However, if you have a semantic error like this (not a syntax error), you probably don't want to do this, as there's no syntax to recover.但是,如果您有这样的语义错误(不是语法错误),您可能不想这样做,因为没有可恢复的语法。 You might want to use YYABORT to abort the parse immediately (return from yyparse).您可能想使用YYABORT立即中止解析(从 yyparse 返回)。 Or you might want to just continue parsing, to possibly identify other errors.或者您可能只想继续解析,以识别其他错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM