Bison Yacc代码同时读取IF和ELSE语句
我有一个蜗牛程序,应该由我的Yacc代码来解释。 但是,我在解析.y文件的snail源代码中的IF-ELSE语句时遇到了问题 这是我的蜗牛计划Bison Yacc代码同时读取IF和ELSE语句,bison,interpreter,flex-lexer,yacc,lex,Bison,Interpreter,Flex Lexer,Yacc,Lex,我有一个蜗牛程序,应该由我的Yacc代码来解释。 但是,我在解析.y文件的snail源代码中的IF-ELSE语句时遇到了问题 这是我的蜗牛计划 print "Start of Program"; print newline; print 4+5*2; print newline; //testing 3 < 4 if (3 < 4) then print "3 is smaller than 4"; print newline; else print "your inte
print "Start of Program";
print newline;
print 4+5*2;
print newline;
//testing 3 < 4
if (3 < 4) then
print "3 is smaller than 4";
print newline;
else
print "your interpreter is not working";
endif
这是我的预期输出
Start of Program
14
3 is smaller than 4
请大家就如何正确解析IF-ELSE语句提供一些建议?谢谢大家! 您正在无条件地打印
字符串
和换行符
,即使这些打印在if
中,因此可能会被取消计算。您需要在这些规则中进行if(top()==1)
测试,与您在PRINT expr
规则操作中的测试相同
您需要明确解析语句和计算语句之间的区别。您需要无条件地解析所有内容,因为这就是您识别程序的方式。您只想在您正在评估的上下文中评估语句,并将其记录在堆栈中--
top()==1
表示您在您要评估的上下文中,而top()==0
表示您在而不是正在评估的上下文中(只是解析)。为了确保严格的正确性,还应该检查所有表达式规则中的top()
,而不是在它为0时求值。但在实践中,没有副作用的事情并不重要——你可以无条件地评估它们,这并不重要;在未计算的上下文中,您只需忽略计算的值。您正在无条件地打印字符串和换行符
,即使这些打印在if
中,因此可能未计算。您需要在这些规则中进行if(top()==1)
测试,与您在PRINT expr
规则操作中的测试相同
您需要明确解析语句和计算语句之间的区别。您需要无条件地解析所有内容,因为这就是您识别程序的方式。您只想在您正在评估的上下文中评估语句,并将其记录在堆栈中--top()==1
表示您在您要评估的上下文中,而top()==0
表示您在而不是正在评估的上下文中(只是解析)。为了确保严格的正确性,还应该检查所有表达式规则中的top()
,而不是在它为0时求值。但在实践中,没有副作用的事情并不重要——你可以无条件地评估它们,这并不重要;在未计算的上下文中,只需忽略计算的值
/* Put new code here */
%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int yyerror(char *s);
extern int linenum;
%}
/* define all types of variables and terminals */
%union
{
int int_val;
char *str_val;
}
/* define the individual types of variables and terminals */
%token PRINT
%token NEWLINE
%token IF
%token THEN
%token ELSE
%token ENDIF
%token <str_val> STRING
%token <int_val> INT
%type <int_val> expr
/* assign priorities to operators in order to avoid shift/reduce conflicts (grammar ambiguities) */
%left '+' '-'
%left '*' '/'
%left '<' '>'
%left UMINUS
/* the start variable of your program */
%start program
%%
program : stmt_list
| error {printf("YACC: syntax error near line %d \n", linenum);
abort();}
;
stmt_list : stmt_list stmt
| stmt
;
stmt : print_stmt
| if_stmt
print_stmt : expr ';' {printf("expression found\n");}
| PRINT expr ';' {if (top() == 1) then {printf("%d", $2);}}
| PRINT STRING ';' {printf("%s", $2);}
| PRINT NEWLINE ';' {printf("\n");}
if_stmt : IF expr THEN {top()==1 ? push($2!=0) : push(0);} stmt_list {pop();}
| ELSE {top()==1 ? push($2==0) : push(0);} stmt_list {pop();} ENDIF
expr : '(' expr ')' {$$ = $2;}
| expr '+' expr {$$ = $1 + $3;}
| expr '-' expr {$$ = $1 - $3;}
| expr '*' expr {$$ = $1 * $3;}
| expr '/' expr {$$ = $1 / $3;}
| expr '<' expr {$$ = $1 < $3;}
| expr '>' expr {$$ = $1 > $3;}
| expr '<=' expr {$$ = $1 >= $3;}
| expr '>=' expr {$$ = $1 <= $3;}
| expr '==' expr {$$ = $1 == $3;}
| expr '!=' expr {$$ = $1 != $3;}
| '-' expr %prec UMINUS {$$ = -$2;}
| INT {$$ = $1;}
;
%%
/* link lex code */
/* #include "lex.yy.c" */
/* insert additional code here */
int main(void)
{
return yyparse();
}
int yyerror(char *s)
{
fprintf(stderr, "%s \n",s);
}
Start of Program
14
3 is smaller than 4
your interpreter is not working
Start of Program
14
3 is smaller than 4