如何按正确顺序报告错误消息



我想用Flex&野牛在我的工具中,我接受以";"结尾的表达式并检查表达式中是否有错误。当错误发生时,我想得到错误标记的正确位置。问题是,当出现不止一个错误时,我总是会得到错误的位置。

解析器:

%{
#  include <stdio.h>
#  include <stdlib.h>
#  include "roofexp.h"
#  include "symbol.h"
%}
%locations
%union {
  struct ast *a;
  double d;
  struct symbol *s;     /* which symbol */
  struct symlist *sl;
  int fn;           /* which function */
  char *str;
}
/* edeclare tokens */
%token <d> NUMBER
%token <str> STRING
%token <s> NAME
%token <fn> FUNC
%token EOL
%token IF THEN ELSE WHILE DO LET

%nonassoc <fn> CMP
%right '='
%left '+' '-'
%left '*' '/'
%nonassoc '|' UMINUS
%type <a> exp stmt list explist
%start calclist
%%
calclist: /* nothing */
    | calclist stmt ';' {
                            if(debug) 
                                dumpast($2, 0);
                             printf("= %4.4gn> ", eval($2));
                             treefree($2);
                             free_string_table();
                             FreeSymbolTable();
                        }
    | calclist error EOL { YYERROR; }
 ;
stmt: IF exp THEN list           { $$ = newflow('I', $2, $4, NULL); }
   | IF exp THEN list ELSE list  { $$ = newflow('I', $2, $4, $6); }
   | exp
;
list: /* nothing */ { $$ = NULL; }
   | stmt ';' list { if ($3 == NULL)
                    $$ = $1;
                      else
            $$ = newast('L', $1, $3);
                    }
   ;
exp: exp CMP exp          { $$ = newcmp($2, $1, $3); }
   | exp '+' exp          { $$ = newast('+', $1,$3); }
   | exp '-' exp          { $$ = newast('-', $1,$3);}
   | exp '*' exp          { $$ = newast('*', $1,$3); }
   | exp '/' exp          { 
                                $$ = newast('/', $1, $3);
                          }
   | '|' exp              { $$ = newast('|', $2, NULL); }
   | '(' exp ')'          { $$ = $2; }
   | '-' exp %prec UMINUS { $$ = newast('M', $2, NULL); }
   | NUMBER               { $$ = newnum($1); }
   | STRING               { $$ = newstr($1); add_string($1); } 
   | FUNC '(' explist ')' { $$ = newfunc($1, $3); }
   | NAME                 { $$ = newref($1); }
   | NAME '=' exp         { $$ = newasgn($1, $3); }
   | NAME '(' explist ')' { $$ = newcall($1, $3); }
;
explist: exp                  
        | exp ',' explist  { $$ = newast('L', $1, $3); }
;

lexer:

%%
%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
# include "roofexp.h"
# include "roofexp.tab.h"
# include "symbol.h"
/* handle locations */
int yycolumn = 1;
#define YY_USER_ACTION 
    yylloc.first_line = yylloc.last_line = yylineno;    
    yylloc.first_column = yycolumn; 
    yylloc.last_column = yycolumn + yyleng - 1; 
    yycolumn += yyleng;
%}
%option yylineno noyywrap
/* float exponent */
EXP ([Ee][-+]?[0-9]+)
%%
 /* single character ops */
"#" |
"+" |
"-" |
"*" |
"/" |
"=" |
"|" |
"," |
";" |
"(" |
")"     { return yytext[0]; }
 /* comparison ops */
">"     { yylval.fn = 1; return CMP; }
"<"     { yylval.fn = 2; return CMP; }
"<>"    { yylval.fn = 3; return CMP; }
"=="    { yylval.fn = 4; return CMP; }
">="    { yylval.fn = 5; return CMP; }
"<="    { yylval.fn = 6; return CMP; }
 /* keywords */
"if"    { return IF; }
"then"  { return THEN; }
"else"  { return ELSE; }
"while" { return WHILE; }
"do"    { return DO; }
"let"   { return LET;}
 /* built in functions */
"sin"   { yylval.fn = FUNC_sin; return FUNC; }
"cos"   { yylval.fn = FUNC_cos; return FUNC; }
"pow" { yylval.fn = FUNC_pow; return FUNC; }
"GetDz" { yylval.fn = FUNC_GetDz; return FUNC;}
 /* debug hack */
"debug"[0-9]+ { debug = atoi(&yytext[5]); printf("debug set to %dn", debug); }
 /* names */
[_a-zA-Z][_a-zA-Z0-9]*  { 
                        if(LookupSymbolTable(yytext, 0, VARIABLE) == NULL)
                            yyerror("未定义的变量: %s", yytext);
                        else
                            yylval.s = lookup(yytext); return NAME; 
                        }
[0-9]+"."[0-9]*{EXP}? |
"."?[0-9]+{EXP}? { yylval.d = atof(yytext); return NUMBER; }
"[^"n]*"    { printf("string=%sn", yytext); }
"[^"n]*$     { yyerror("unterminated string literal: %sn", yytext); }
"//".*  
[ t]   
n      { yycolumn = 1;  }
.       { yyerror("Mystery character %cn", *yytext); }
%%

表达式:

pow(2)+
pow(2, 4)
;

回声:

3-1: error: at ';': too few arguments for call

但正确的位置应该是1-1!我的lexer和解析器出了什么问题。如果我想得到合适的职位,我该怎么办?

如果您显示生成错误消息的代码会有所帮助,但我猜您的yyerror函数只使用yyloc的当前值,它将对应于最后读取的令牌。因此,如果直到表达式末尾的分号才诊断出错误(如"参数太少"),则yyloc将具有分号的位置,如您的示例所示。

如果将%locations指定为bison,则bison会为每个非终端以及非终端保留一个源范围。(默认情况下,范围从生产中第一个组件的开始到最后一个组件的结束。)您可以使用@N(对于组件N)或@$(对于整个缩减范围)从野牛操作访问位置结构。

但是,在calclist stmt ';'操作中使用@$(假设您的错误是在调用eval的过程中产生的)不会给您带来更高的精度。此时,您所能做的最好的事情就是将错误报告为源代码范围1:1-3:1中的某个位置。为了生成更准确的消息,您需要在AST中包括每个节点中的位置。那么eval将只需要知道是哪个AST节点导致了错误。

当然,当您解析函数调用时,您可能会产生类似too few arguments的错误,假设您知道此时每个函数需要多少个参数。但这更难维持,也不那么普遍。

尽管bison做了大量维护位置所需的工作,但您必须自己保持位置信息与AST节点的关联。通常的方法是在每个AST节点(您的struct ast)中包含一个YYLTYPE结构;您可以在创建节点的操作中将适当的位置复制到AST节点中。

相关内容

最新更新