简体   繁体   English

Lex/Yacc 计算器不处理特殊情况

[英]Lex/Yacc Calculator Not Handling Unique Situation

I'm working on a Lex+Yacc calculator that is capable of performing basic arithmetic operations, as well as supporting usage of parentheses.我正在研究 Lex+Yacc 计算器,它能够执行基本的算术运算,并支持括号的使用。 From my testing, it is capable of handling essentially every scenario, providing an integer solution for valid inputs, and indicating an input was invalid if invalid input is provided.根据我的测试,它基本上能够处理所有情况,为有效输入提供 integer 解决方案,并在提供无效输入时指示输入无效。

However, in my testing I found that if I simply type in: 5) or 99+22+33) or basically anything where ')' is the final character, it'll still be deemed valid.然而,在我的测试中,我发现如果我简单地输入: 5)99+22+33)或基本上任何以 ')' 为结尾的字符,它仍然会被视为有效。 In fact, I could even type in "7)asdfghjk", and it would deem it valid and still output 7. It seems like when there is a lone ')' at the end it does not care.事实上,我什至可以输入“7)asdfghjk”,它会认为它有效并且仍然是 output 7。似乎当最后有一个单独的 ')' 时它并不关心。 Whereas if I had (5 it would error out.而如果我有(5它会出错。

My Lex file and Yacc file are provided below.下面提供了我的 Lex 文件和 Yacc 文件。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thank you so much.太感谢了。

Lex File:莱克斯文件:

/* ========== DECLARATIONS SECTION ========== */
%{
    #include <stdio.h>
    #include <stdlib.h>
    #include "y.tab.h"
    extern int yylval;
%}
/* ========================================== */



/* ========== SHORTHAND DEFINITIONS ========= */
/* ========================================== */



/* ================== RULES ================= */
%%
([1-9][0-9]*|0) { 
    yylval = atoi(yytext);
    return INT;
}

[+] {return PLUS;}
[-] {return MINUS;}
[*] {return MULT;}
[/] {return DIV;}
[(] {return LPAREN;}
[)] {return RPAREN;}
[ \t] {;}
[\n] {return 0;}
. {yyerror();}
%%
/* ========================================== */



/* ================ USER CODE =============== */
int yywrap() {
    return 1;
}
/* ========================================== */

Yacc File:亚克文件:

/* ========== DECLARATIONS SECTION ========== */
%{
    #include <stdio.h>
    #include <stdlib.h>
%}

%token INT

%left PLUS MINUS
%left MULT DIV
%left UMINUS
%left LPAREN RPAREN
/* ========================================== */



/* ============ GRAMMAR SECTION ============= */
%%
ResultantExpression: E{
    printf("Result = %d\n", $$);

    return 0;
};


E: E PLUS T {$$ = $1 + $3;}
 | E MINUS T {$$ = $1 - $3;}
 | MINUS E %prec UMINUS {$$ = -$2;}
 | T {$$ = $1;}
 ;

T: T MULT F {$$ = $1 * $3;}
 | T DIV F {if ($3 == 0) {yyerror();} else {$$ = $1 / $3;}}
 | F {$$ = $1;}
 ;

F: INT {$$ = $1;}
 | LPAREN E RPAREN {$$ = ($2);}
 ; 
%%
/* ========================================== */



/* ============ USER CODE SECTION =========== */
int main() {
    printf("Enter the expression:\n");
    yyparse();

    return 0;
}

void yyerror() {
    printf("The entered expression is invalid!\n");
    exit(1);
}

/* ========================================== */

Because you have a return 0;因为你有一个return 0; in your ResultExpression action, it will exit successfully after recognizing a ResultExpression, even if there is more input.在您的ResultExpression操作中,即使有更多输入,它也会在识别出 ResultExpression 后成功退出。 If you want to check for a correct EOF after that, remove the return 0;如果之后要检查正确的 EOF,请删除return 0; and let it continue parsing -- it will give a syntax error message if there's any garbage left in the input, or return successfully if there is the expected newline (which your lexer returns as EOF).并让它继续解析——如果输入中有任何垃圾,它将给出语法错误消息,或者如果有预期的换行符(您的词法分析器将其作为 EOF 返回)则成功返回。

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

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