0
%type <string> Id
%type <ExprRes> Factor
%type <ExprRes> Term
%type <ExprRes> Expr
%type <InstrSeq> StmtSeq
%type <InstrSeq> Stmt
%type <BExprRes> BExpr
%type <ExprRes> Primary

%token Ident        
%token IntLit   
%token Int
%token Write
%token IF
%token EQ
%token GTE
%token LTE
%token UM
%token UP   

%%

Prog            :   Declarations StmtSeq                                {Finish($2); } ;
Declarations    :   Dec Declarations                                    { };
Declarations    :                                                       { };
Dec             :   Int Ident {EnterName(table, yytext, &entry); }';'   { };
StmtSeq         :   Stmt StmtSeq                                        { $$ = AppendSeq($1, $2); } ;
StmtSeq         :                                                       { $$ = NULL;} ;
Stmt            :   Write Expr ';'                                      { $$ = doPrint($2); };
Stmt            :   Id '=' Expr ';'                                     { $$ = doAssign($1, $3);} ;
Stmt            :   IF '(' BExpr ')' '{' StmtSeq '}'                    { $$ = doIf($3, $6);};
BExpr           :   Expr EQ Expr                                        { $$ = doBExpr($1, $3);};
Expr            :   Expr '+' Term                                       { $$ = doAdd($1, $3); } ;
Expr            :   Expr '-' Term                                       { $$ = doMinus($1, $3); };
Expr            :   Term                                                { $$ = $1; } ;
Term            :   Term '*' Factor                                     { $$ = doMult($1, $3); } ;
Term            :   Term '/' Factor                                     { $$ = doDiv($1, $3); } ;
Term            :   Factor                                              { $$ = $1; } ;
Factor          :   Primary                                             { $$ = $1;};
Primary         :   '-'Primary                                              { $$ = doUnaryMinus($1);};

Primary         :   IntLit                                              { $$ = doIntLit(yytext); };
Primary         :   Ident                                               { $$ = doRval(yytext); };
Id              :   Ident                                               { $$ = strdup(yytext);}

这是我的 yacc 语法。我试图让一元减号工作。x——例如。

但是,当我尝试在我的测试文件上运行它时

int num1;    
int num2;  
int num3;   
int num4;

num3 = 100;    
num4 = 200;   
num3 = num4 / num3;  
num1 = 1;   
num1 = num3-num1;  
num1--;

print num3;   
print num4;   
print num1;

我在第 num1-- 行收到 yyerror;

yyerror 在我能找到的任何细节上都非常模糊。我只能发现它是在遇到输入错误时调用的。我不知道该错误是否来自我对应该如何编写语法的误解,或者是什么。我有一个指向非法字符的程序,它说问题来自行中的第一个“-”符号。我想知道在哪里可以找到我的答案或者您是否看到错误的任何想法。

这是我的 lex 文件:

%{
#include "yaccExample.h"
#include "y.tab.h"

#define YY_INPUT(buf,result,max_size) \
    { int c = GetSourceChar(); \
          result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \
        }

%}


letter [A-Za-z]
digit [0-9]

%%
if                              {return IF;}
int                             {return Int;}
print                           {return Write;}
{letter}({letter}|{digit})*     {return Ident;}
{digit}{digit}*                 {return IntLit;}
\=\=                            {return EQ;}
\-\-                            {return UM;}
\+\+                            {return UP;}
\>\=                            {return GTE;}
\<\=                            {return LTE;}
\=                              {return '=';}
\+                              {return '+';}
\*                              {return '*';}
\;                              {return ';';}
\{                              {return '{';}
\}                              {return '}';}
\(                              {return '(';}
\)                              {return ')';}

[ ]                             {}
\t                              {}
\r                              {}
\n                              {}

.               {WriteIndicator(GetCurrentColumn());
                   WriteMessage("Illegal Character in lex");}

%%


yywrap () {
}
4

2 回答 2

1

问题是对术语的误解。根据您的语法,一元减号表示仅在主要之前的减号。

Primary         :   '-'Primary 

您的语法的正确测试代码如下所示。它应该正确编译。

num1 = -99;  
num1 = -num3;
num1 = -(num3-num1);  

运算符“--”称为减量,不会出现在您的语法中。如果您需要编译减量运算符,则必须将其添加到您的语法中。

于 2014-04-28T05:21:25.687 回答
0

yacc 对调试语法的支持有限。在这种情况下通常有帮助的一件事是定义YYDEBUG预处理器符号并将yydebug全局设置为非零。这将导致在解析时将所有扫描和移动的标记转储到标准输出,并减少规则,这会有所帮助。

添加:

#ifdef YYDEBUG
    extern int yydebug;
    yydebug = 1;
#endif

到您的主要功能(或可能使用命令行开关设置yydebug),并编译所有源文件-DYYDEBUG

于 2014-04-27T23:09:49.700 回答