首页 > 解决方案 > 我在这个问题上遇到错误,有人可以提出他们的答案吗?

问题描述

问题是:- YACC 中用于评估表达式的程序(用于加减法、乘法、除法的简单计算器程序)。我也做过 mod (%)。这些是我的 yacc 和 lex 部分:-

LEX部分:-

 %{
   /* Definition section */
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}

/* Rule Section */
%%
[0-9]+ {
      yylval=atoi(yytext);
      return NUMBER;

   }
[\t] ;

[\n] return 0;

. return yytext[0];

%%

int yywrap()
{
  return 1;
}

YACC部分:-

   %{
       /* Definition section */
       #include<stdio.h>
       int flag=0;
   %}

  %token NUMBER

  %left '+' '-'

  %left '*' '/' '%'

  %left '(' ')'

     /* Rule Section */
  %%

   ArithmeticExpression: E{

        printf("\nResult=%d\n", $$);

     return 0;

    };
     E:E'+'E {$$=$1+$3;}

      |E'-'E {$$=$1-$3;}

      |E'*'E {$$=$1*$3;}

      |E'/'E {$$=$1/$3;}

      |E'%'E {$$=$1%$3;}

      |'('E')' {$$=$2;}

      | NUMBER {$$=$1;}

      ;

     %%

     //driver code
     void main()
      {
          printf("\nEnter Any Arithmetic Expression which 
               can have operations Addition, 
               Subtraction, Multiplication, Division, 
                      Modulus and Round brackets:\n");

          yyparse();
          if(flag==0)
          printf("\nEntered arithmetic expression is Valid\n\n");
       }

      void yyerror()
      {
         printf("\nEntered arithmetic expression is Invalid\n\n");
         flag=1;
       }

现在,当我在终端上执行我的代码时,我使用这些代码 -

lex calc.l yacc calc.y gcc lex.yy.c y.tab.c -w

但是当我输入第三个代码时,会发生一个错误,即“NUMBER”未声明,每个未声明的标识符对于它出现的每个函数只报告一次。

我在这里附上截图。 屏幕截图错误

标签: yacclex

解决方案


推荐阅读