我想解析以下脚本
str="HELLO"
if [ $str = "HELLO" ]; then
echo FOO
fi
我可以解析第一行str="HELLO"
,并保存该变量以供使用。可以运行脚本
str="HELLO"
echo $str
并得到输出CCD_ 2。
但我还不能做条件if语句。所以我想知道该如何处理条件语句。我试着定义这个柠檬语法
expr(A) ::= IF '[' expr(B) EQEQ expr(C) '];then' expr(C) 'fi'.
但它给了我一个错误,引用是非法字符。所以我不知道如何为if语句编写语法。我的整个语法都是
%include
{
#include "types.h"
#include "openshell.h"
#include "assert.h"
}
%syntax_error { fprintf(stderr, "Syntax errorn"); }
%token_type { struct SToken* }
%type expr { int }
%nonassoc EQEQ NOTEQ SET LARGER SMALLER.
%left FOR WHILE IF.
%left PLUS MINUS.
%left TIMES DIVIDE.
program ::= expr(A). { setresult(A); /*printf("%dn", A);*/ }
expr(A) ::= expr(B) PLUS expr(C). {A = B + C; }
expr(A) ::= expr(B) MINUS expr(C). {A = B - C; }
expr(A) ::= expr(B) TIMES expr(C). {A = B * C; }
expr(A) ::= expr(B) EQEQ expr(C). {if(B==C) {A=1;} else A=0;}
expr(A) ::= expr(B) NOTEQ expr(C). {if(B==C) {A=0;} else A=1;}
expr(A) ::= expr(B) LARGER expr(C). {if(B>C) {A=1;} else A=0;}
expr(A) ::= expr(B) SMALLER expr(C). {if(B<C) {A=1;} else A=0;}
expr(A) ::= expr(B) SET expr(C). {A=C;B=C;}
expr(A) ::= IF '[' expr(B) EQEQ expr(C) '];then' expr(C) 'fi'.
expr(A) ::= FOR LPAR expr(B) SEMICOLON expr(C) SEMICOLON expr(D) RPAR expr(E). {A = D*B*E*C+1; } /* $((for ( 1 == 1 ; 2 == 2 ; 3 == 3 ) 55)) */
expr(A) ::= WHILE LPAR expr(B) RPAR expr(C).
{
while (B) { printf("%d", C);
}
A=C;printf("n");
}
expr(A) ::= expr(B) DIVIDE expr(C).
{
if (C != 0)
{
A = B / C;
}
else
{
fprintf(stderr, "divide by 0");
}
}
expr(A) ::= LPAR expr(B) RPAR. { A = B; }
expr(A) ::= INTEGER(B).
{
//printf("the result = %sn", B->token);
A = B->value;
//printf("Passed argument: %sn", B->token);
}
我的主循环如下所示。
int main(int argc, char *argv[]) {
bool donotrun = false;
struct sigaction new_action, old_action;
hashtable_t *hashtable = ht_create(65536);
/* Set up the structure to specify the new action. */
new_action.sa_handler = termination_handler;
sigemptyset(&new_action.sa_mask);
new_action.sa_flags = 0;
sigaction(SIGINT, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN)
sigaction(SIGINT, &new_action, NULL);
sigaction(SIGHUP, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN)
sigaction(SIGHUP, &new_action, NULL);
sigaction(SIGTERM, NULL, &old_action);
if (old_action.sa_handler != SIG_IGN)
sigaction(SIGTERM, &new_action, NULL);
int value;
void *pParser;
char *c;
// struct SToken v[argc];
int index = 0;
int i;
char *cvalue = NULL;
const char *commandFile;
bool quietFlag;
while (1) {
index = 0;
i = getopt_long(argc, argv, "pc:vh",
options, &index);
if (i == -1)
break;
switch (i) {
case 'p': {
exit(EXIT_SUCCESS);
}
case 'v': {
printf("sh OpenShell version 0.1(a)n");
printf("Version: %sn", VERSION);
exit(EXIT_SUCCESS);
}
case 'h': {
usage();
exit(EXIT_SUCCESS);
}
case 'c': {
cvalue = optarg;
command(cvalue, hashtable);
exit(EXIT_SUCCESS);
}
case 'f':
/*
* Execute commands from file.
* This is used for osh script files.
* The quiet flag is also set.
*/
if ((argc != 1) || commandFile)
usage();
quietFlag = true;
argc--;
break;
case '?':
if (optopt == 'c')
fprintf(stderr, "Option -%c requires an argument.n", optopt);
else if (isprint (optopt))
fprintf(stderr, "Unknown option `-%c'.n", optopt);
else
fprintf(stderr,
"Unknown option character `\x%x'.n",
optopt);
default: {
return 1;
}
}
}
getPath();
pParser = (void *) ParseAlloc(malloc);
char *copy;
for (; ;) {
bool scanning = true;
bool calc = true;
while (scanning) {
char *line = NULL;
line = readline("$ ");
//return 0;
if (line)
copy = strdup(line);
if (line && strstr(line, "=")) {
donotrun = true;
char str[128];
char *ptr;
strcpy(str, line);
strtok_r (str, "=", &ptr);
ht_set(hashtable, str, ptr);
}
if (!scanning)
break;
if (!isatty(fileno(stdin))) {
*argv++;
readFile(*argv++, hashtable);
free(line);
exit(0);
}
else {
if (!donotrun) {
command(line, hashtable);
}
donotrun = false;
add_history(copy);
}
free(copy);
}
}
return 0;
}
问题是我不知道如何在if语句的语法中声明方括号。如果条件为true,则if语句是条件和要计算的表达式的"复合"语句,因此至少会涉及2或3个表达式。我已经定义了有效的等式表达式:
expr(A) ::= expr(B) EQEQ expr(C). {if(B==C) {A=1;} else A=0;}
但是,当我试图在引号中使用方括号时,我从柠檬那里得到了一个编译器错误,如果我不使用引号,那么我会得到另一个错误:
Illegal character on RHS of rule: "[".
以上错误如果我尝试并声明:
expr(A) ::= IF [ expr(B) EQEQ expr(C) ];then expr(C) 'fi'.
根据文档:
Yacc和bison允许终端符号具有字母数字名称,或者是包含在单引号中的单个字符,例如:")"或"$"。Lemon不允许使用这种替代形式的终端符号。对于Lemon,所有符号、终端和非终端都必须具有字母数字名称。