作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的大学有一个项目,用C语言制作一个迷你shell。为了解析命令行,我使用以下工具:lex 和 yacc。但我想解析该行。当检测到 token 时,在我的 struct 命令中添加参数,当我找到换行符时,退出 yyparse 来执行命令。打印提示并重新启动解析后...我的 lex 文件:
Chiffre [0-9]
Lettre [a-zA-Z]
Alphanum ({Chiffre}{Lettre})+
%{
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "parser.tab.h"
%}
%%
[ \t]+ {}
"\n" {
return '\n';
}
"||" {
return OR;
}
"|" {
return PIPE;
}
"&&" {
return AND;
}
"&" {
return AMPERSAND;
}
"2>" {
return ERR_GREAT;
}
">&" {
return GREAT_AMP;
}
"2>>" {
return ERR_GREAT_GREAT;
}
">>&" {
return GREAT_GREAT_AMP;
}
">>" {
return GREAT_GREAT;
}
">" {
return GREAT;
}
"<" {
return LESS;
}
";" {
return SEMICOLON;
}
"exit" {
return EXIT;
}
({Lettre}_){1}({Alphanum}_) {
return IDENTIFIER;
}
[^ \t|><&\;][^ \t|><&\;]* {
return WORD;
}
%%
int yywrap(void){return 1;}
我尝试使用 YYACCEPT 退出 yyparse 但不起作用。yacc 文件:
%{
#include <stdio.h>
void yyerror(const char *s);
extern int yylex();
extern char* yytext;
%}
%token PIPE AND OR AMPERSAND BLANK WORD IDENTIFIER GREAT GREAT_GREAT LESS ERR_GREAT ERR_GREAT_GREAT GREAT_AMP GREAT_GREAT_AMP SEMICOLON EXIT
%start cmd_lists
%union { char str[256]; int val; }
%error-verbose
%%
list_arg: list_arg WORD | /*empty*/;
cmd_args: WORD list_arg io_list;
pipeline: pipeline PIPE cmd_args | pipeline SEMICOLON cmd_args | pipeline AND cmd_args | pipeline OR cmd_args | cmd_args ;
io: GREAT WORD | GREAT_GREAT WORD | LESS WORD | ERR_GREAT WORD | ERR_GREAT_GREAT WORD | GREAT_AMP WORD | GREAT_GREAT_AMP WORD;
io_list: io_list io | /*empty*/ ;
background: AMPERSAND | /*empty*/ ;
command: pipeline background '\n'{
printf("cmd newline\n");
YYACCEPT;
}
|'\n'{
printf("newline\n");
YYACCEPT;
}
|EXIT '\n'{
printf("exit newline\n");
YYACCEPT;
}
;
cmd_lists: cmd_lists command | ;
%%
void yyerror(const char *s){
fprintf(stderr,"yyerror : erreur : %s.\n",s);
}
感谢您的帮助。
最佳答案
cmd_lists: cmd_lists command | ;
这似乎是终端符号,尽管它们通常放在顶部,而不是底部。不管怎样,这个语法表明 cmd_list
可以包含零个或多个 command
项,因此解析器通过准备接受另一个项来表现正确。
如果您不希望这样,请删除 cmd_list
产品。
关于c - 如何在换行后退出 yyparse 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39803846/
在 Vaadin 7.0,显示时JavaBean Table 中的数据与 BeanContainer ,用新数据刷新表的正确方法是什么? 最佳答案 该表通过监听器监视表项的属性。如果您通过表的 Ite
首先,我使用的是带有 Axis2 1.6.2 的 eclipse,我正在 tomcat 6 上部署我创建的 Web 服务。Web 服务是在 eclipse 中通过自上而下的方法创建的。 我被要求使对我
我已将 Rails 3.1.1 应用程序升级到 Rails 3.1.3,现在,对于每个请求,它仅响应错误数量的参数(3 for 1)。不幸的是,它没有说明错误在哪里,并且应用程序跟踪为空。我认为存在一
我是一名优秀的程序员,十分优秀!