Bison Flex 计算器和布尔解释器

发布于 2025-01-12 00:44:45 字数 2182 浏览 5 评论 0原文

我正在做一个 bison/flex 解释器,需要比较数字(<,>),执行 aritmethics 基本操作,以及像 AND OR XOR NOT 这样的布尔值。

第一步是比较两个数字,代码对此很好。现在,我应用所需的更改来查找数字是否为素数,并且代码仅返回错误。

我尝试多次更改代码,但没有任何更改

有词法分析器的代码:

#include <stdlib.h> /* for atof() */
#include "test.tab.h"
%}

%%

[0-1]+  { yylval = atoi(yytext); return NUM; }
[false]   { yylval = 0; return FALSE; }
[true]   { yylval = 1; return TRUE; }
[and]   { yylval = atoi(yytext); return AND; }
[xor]   { yylval = atoi(yytext); return XOR; }
[not]   { yylval = atoi(yytext); return NOT; }




[ \t\n]+        /* whitespace */
.       return yytext[0];

这是 .y


%{
  #define YYSTYPE int

  #include <math.h>     
  #include <stdio.h>    
  #include <stdlib.h>   

  int yylex(void);
  void yyerror(char*);
%}

%token NUM
%token BOOLEAN
%left '<' '>' '=' '!=' 'mod' 'div' AND NOT XOR TRUE FALSE

%%
program:       /* empty */
              | programa command
;

command:        ';'
              | expression ';'       //{ printf("%i\n", $1);}
;



expression:          NUM                     { $$ = $1; }

             | expression '<' expression          { $$ = $1 < $3;
                  if ($$==1)
                 printf("TRUE \n");
                  else {
                    printf("FALSE \n");
                  }
                }
             | izraz '>' izraz          { $$ = $1 > $3;
                  if ($$==1)
                 printf("TRUE \n");
                  else {
                    printf("FALSE \n");
                  }
                }
             | izraz '=' izraz          { $$ = $1 == $3;
                  if ($$==1)
                 printf("TRUE \n");
                  else {
                    printf("FALSE \n");
                  }
                }
             | izraz '!=' izraz         { $$ = $1 != $3;
                  if ($$==1)
                 printf("TRUE \n");
                  else {
                    printf("FALSE \n");
                  }
                }
;
%%
main() {
  yyparse();
}

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

该代码可以比较两个自然数,但现在只是返回错误,我现在真的迷路了

I'm doing a bison/flex interpreter that need to compare numbers (<,>), do the aritmethics basic operations, and boolean like AND OR XOR NOT.

The first step was comparing two numbers and the code was fine to that. Now I apply the changes needed to find if a number is prime or not, and the code just return Error.

I tried to change the code several times but with no changes

There is the code of the lexer:

#include <stdlib.h> /* for atof() */
#include "test.tab.h"
%}

%%

[0-1]+  { yylval = atoi(yytext); return NUM; }
[false]   { yylval = 0; return FALSE; }
[true]   { yylval = 1; return TRUE; }
[and]   { yylval = atoi(yytext); return AND; }
[xor]   { yylval = atoi(yytext); return XOR; }
[not]   { yylval = atoi(yytext); return NOT; }




[ \t\n]+        /* whitespace */
.       return yytext[0];

and here is the .y


%{
  #define YYSTYPE int

  #include <math.h>     
  #include <stdio.h>    
  #include <stdlib.h>   

  int yylex(void);
  void yyerror(char*);
%}

%token NUM
%token BOOLEAN
%left '<' '>' '=' '!=' 'mod' 'div' AND NOT XOR TRUE FALSE

%%
program:       /* empty */
              | programa command
;

command:        ';'
              | expression ';'       //{ printf("%i\n", $1);}
;



expression:          NUM                     { $ = $1; }

             | expression '<' expression          { $ = $1 < $3;
                  if ($==1)
                 printf("TRUE \n");
                  else {
                    printf("FALSE \n");
                  }
                }
             | izraz '>' izraz          { $ = $1 > $3;
                  if ($==1)
                 printf("TRUE \n");
                  else {
                    printf("FALSE \n");
                  }
                }
             | izraz '=' izraz          { $ = $1 == $3;
                  if ($==1)
                 printf("TRUE \n");
                  else {
                    printf("FALSE \n");
                  }
                }
             | izraz '!=' izraz         { $ = $1 != $3;
                  if ($==1)
                 printf("TRUE \n");
                  else {
                    printf("FALSE \n");
                  }
                }
;
%%
main() {
  yyparse();
}

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

That code was okay to compare two natural numbers, but now just return the error and i'm really lost now

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蛮可爱 2025-01-19 00:44:45

您的扫描器和解析器存在一些严重问题。

  1. [false] 并不意味着您认为的那样。

    您似乎认为这意味着“匹配关键字 false”。该模式将是简单的false

    [false] 的意思是“如果是 aef、<代码>l或<代码>s”。 […] 是一个字符类,它定义了一组可能的字符。

    也许值得阅读正则表达式的介绍,但也一定要阅读 flex 文档,因为与其他正则表达式库有一些差异。

  2. 您的大多数解析规则都引用 izraz,您似乎已在一个地方将其翻译为 表达式。我不知道这是否是一个复制错误,或者您的实际源文件是否是这样的。

  3. '!=' 不是有效的单字符标记,您的扫描仪无法识别该两字符标记。

除此之外,由于您没有提供您遇到的实际问题的任何描述,因此提供解决方案并不容易。修复明显的问题,如果仍然有问题,请创建一个包含完整详细信息的新问题:可编译的程序、示例输入以及结果与您期望的差异的描述。

There are some serious issues with your scanner and parser.

  1. [false] does not mean what you think it does.

    You seem to think that it means something lke "match the keyword false". That pattern would be simply false.

    What [false] means is "match exactly one character if it is one of a, e, f, l or s". […] is a character class, which defines a set of possible characters.

    Perhaps it would be worth reading an introduction to regular expressions, but also definitely read the flex documentation, because there are some differences with other regex libraries.

  2. Most of your parse rules refer to izraz, which you seem to have translated in one place to expression. I don't know if this is a copying error or if your actual source file looks like that.

  3. '!=' is not a valid single-character token, and there is no way your scanner can recognise the two-character token.

Beyond that, since you have provided no description whatsoever of the actual problem you encounter, it is not easy to provide a solution. Fix the obvious problems and if you continue to have a problem, create a new question with complete details: a compilable program, a sample input, and a description of how the result differs from what you expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文