英文:
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 "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] { /* ignore whitespace */ }
. { /* ignore unknown characters */ }
%%
int yywrap()
{
return 1;
}
This is my bison code:
%{
#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("Command parsed successfully.\n"); }
| error { printf("Error: Invalid command.\n"); }
;
%%
int main()
{
yyparse();
yyrestart();
return 0;
}
void yyerror(char *s)
{
printf("ERROR:%s\n",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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论