如何使用flex bison让他们理解各种规则并按照这些规则行事?

如何使用flex bison让他们理解各种规则并按照这些规则行事?,bison,flex-lexer,Bison,Flex Lexer,这是.l文件 %{ #include <stdlib.h> #include "y.tab.h" %} %% "true" {yylval=1; return BOOLEAN;} "false" {yylval=0; return BOOLEAN;} "nor" {return NOR;} " " { } .

这是.l文件

 %{
   #include <stdlib.h>
   #include "y.tab.h"
 %}

 %% 
 "true"              {yylval=1;  return BOOLEAN;}
 "false"             {yylval=0;  return BOOLEAN;}
 "nor"               {return NOR;}
 " "                 { }
 .                   {return yytext[0];}

 %%

 int main(void)
 {
   yyparse();
   return 0;
 }

 int yywrap(void)
 {
  return 0;
 }
 int yyerror(void)
 {
 printf("Error\n");
 }

 **This is the .y file**

/* Bison declarations.  */
 %token BOOLEAN
 %token NOR
 %left NOR
 %% /* The grammar follows.  */

 input:
  /* empty */
  | input line
    ;

  line:
   '\n'
   | exp '\n' {printf ("%s",$1); }
   ;

  exp:
   BOOLEAN     { $$ = $1;}
   | exp NOR exp   { $$ = !($1 || $3); }
   | '(' exp ')'  { $$ = $2;}
   ;
   %%
%{
#包括
#包括“y.tab.h”
%}
%% 
“true”{yylval=1;返回布尔值;}
“false”{yylval=0;返回布尔值;}
“nor”{返回nor;}
" "                 { }
.                   {返回yytext[0];}
%%
内部主(空)
{
yyparse();
返回0;
}
int-yywrap(无效)
{
返回0;
}
int yyerror(无效)
{
printf(“错误\n”);
}
**这是.y文件**
/*野牛宣言*/
%标记布尔
%标记NOR
%左撇子
%%/*语法如下*/
输入:
/*空的*/
|输入线
;
行:
“\n”
|exp'\n'{printf(“%s”,$1);}
;
经验:
布尔值{$$=$1;}
|exp或exp{$$=!($1 | |$3);}
|“('exp')”{$$=$2;}
;
%%
当我在VisualStudio中使用控制台应用程序执行这些命令时,会出现一个命令窗口,显示“按任意键继续”,然后在按任意键后,命令窗口消失。 此代码工作不正常。需要做些什么才能将此功能与现有代码集成?

我不知道这是否仍然与答案有关,但以下是我的想法

这里有几个问题。
1. <代码>在flex中,不包括
\n

2.你对这些工具要求太高了

至于第一点,很容易解决。只需在flex中添加一条规则来匹配
\n

对于第二点,bison和flex应该如何知道解析单词
true
意味着将其内部值设置为布尔值,意思是
true

以下是您应该进行的更改:

/*Flex*/
/* ... */
真{yylval=1;返回布尔值;}
false{yylval=0;返回布尔值;}
/* ... */
/*野牛*/
/* ... */
|exp'\n'{printf($1?“true\n”:“false\n”);}
/* ... */

虽然我在回答中提到了<代码> \n>代码>,但是你应该考虑将结束字符改为“Windows”。你的标题仅仅概括了这些工具的功能,你的文章也没有提出任何实际问题“工作不正常”不是问题描述-1.