我想在Bison中实现多个分析语句,但每次只有在第一次执行时才会成功。

huangapple go评论47阅读模式
英文:

I want to implement multiple analysis statements in bison, but each time it will succeed only when it is executed for the first time

问题

这是我的Flex代码:

%{
#include "parser.tab.h"
%}
%%
"save"                  { return SAVE; }
"depth"                 { return DEPTH; }
"exclude"               { return EXCLUDE; }
[0-9]+                  { yylval.number = atoi(yytext); return NUMBER; }
"["                     { return LB; }
"]"                     { return RB; }
"="                     { return EQ; }
","                     { return COMMA; }
[a-zA-Z0-9_*?]+         { yylval.str = strdup(yytext); return STRING; }
[ \t\n]                 { /* 忽略空格 */ }
.                       { /* 忽略未知字符 */ }
%%
int yywrap()
{
    return 1;
}

这是我的Bison代码:

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.tab.h"
void yyerror(char *s);
%}
%union {
    char *str;
    int number;
}
%token SAVE DEPTH EXCLUDE
%token LB RB EQ COMMA NUMBER STRING
%%
command:
    | SAVE DEPTH EQ NUMBER EXCLUDE EQ NUMBER { printf("命令解析成功。\n"); }
    | error { printf("错误:无效命令。\n"); }
    ;
%%
int main()
{
    yyparse();
    yyrestart();
    return 0;
}
void yyerror(char *s)
{
    printf("错误:%s\n", s);
}

运行./parser时的结果:

save depth=1 exclude=1
命令解析成功。
save
错误:语法错误
错误:无效命令。
错误:无效命令。
save depth=1 exclude=1
错误:无效命令。
错误:无效命令。
错误:无效命令。
错误:无效命令。
错误:无效命令。
错误:无效命令。
错误:无效命令。

第一次成功,接下来都失败。

英文:

This is my flex code:

%{
#include &quot;parser.tab.h&quot;
%}
%%
&quot;save&quot;                  { return SAVE; }
&quot;depth&quot;                { return DEPTH; }
&quot;exclude&quot;              { return EXCLUDE; }
[0-9]+                  { yylval.number = atoi(yytext); return NUMBER; }
&quot;[&quot;                     { return LB; }
&quot;]&quot;                     { return RB; }
&quot;=&quot;                     { return EQ; }
&quot;,&quot;                     { return COMMA; }
[a-zA-Z0-9_*?]+         { yylval.str = strdup(yytext); return STRING; }
[ \t\n]                 { /* ignore whitespace */ }
.                       { /* ignore unknown characters */ }
%%
int yywrap()
{
    return 1;
}

This is my bison code:

%{
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &quot;parser.tab.h&quot;
void yyerror(char *s);
%}
%union {
    char *str;
    int number;
}
%token SAVE DEPTH EXCLUDE
%token LB RB EQ COMMA NUMBER STRING
%%
command:
    | SAVE DEPTH EQ NUMBER EXCLUDE EQ NUMBER { printf(&quot;Command parsed successfully.\n&quot;); }
    | error { printf(&quot;Error: Invalid command.\n&quot;); }
    ;
%%
int main()
{
    yyparse();
    yyrestart();
    return 0;
}
void yyerror(char *s)
{
    printf(&quot;ERROR:%s\n&quot;,s);
}

This is the result when I ./parser :

save depth=1 exclude=1
Command parsed successfully.
save
ERROR:syntax error
Error: Invalid command.
Error: Invalid command.
save depth=1 exclude=1
Error: Invalid command.
Error: Invalid command.
Error: Invalid command.
Error: Invalid command.
Error: Invalid command.
Error: Invalid command.
Error: Invalid command.

The first time it will succeed, the next time it will all fail.

答案1

得分: 1

你只定义了单个命令的语法,但提供了包含多个命令的文件,因此bison通知您它不符合单个命令的语法!

你需要定义整个语言|文件|程序的语法。我看到你希望每行输入一个命令,所以这是你要指导bison的方式:

input: command
     | command NEWLINE input

然后它将处理一系列命令,每个命令占据一行。

接下来,您需要重新定义词法分析器中的空白字符,因为现在行尾具有重要意义。

英文:

You have made the standard beginners mistake of only defining the syntax of a single command, but supplied a file containing more than one command, and thus bison informs you that it does not match the syntax of a single command!

You need to define the syntax of the whole language|file|program. I see you expect the input to be one command per-line, so this is what you have to instruct bison:

input: command
     | command NEWLINE input

Then it will process a sequence of commands, each one on a line.

You will then need to redefine your whitespace in the lexer, as the end-of-line now has significance.

huangapple
  • 本文由 发表于 2023年5月22日 17:56:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定