4

我正在尝试使用flexyacc解析“C”源代码。不幸的是,我在第 1、12、13、14 行收到错误“expected identifier or '(' before '{' token”...。有什么想法吗?

这是我的 flex 文件(称为 mini.l):

%{

%}
digit [0-9]
letter [a-zA-Z]
number (digit)+
id (letter|_)(letter|digit|_)*
integer (int)
character (char)
comma [,]
%%
{integer} {return INT;}
{character} {return CHAR;}
{number} {return NUM;}
{id} {return IDENTIFIER;}
{comma} {return ',';}
[-+*/] {return *yytext;}
. {}
%%
main()
{
   yylex();
}

对应的yacc文件(名为my_yacc.y)如下图:

%{
#include <ctype.h>
#include <stdio.h>
/* #include "myhead.h" */
#include "mini.l"
#define YYSTYPE double
# undef fprintf
%}

%token INT
%token CHAR
%token IDENTIFIER
%token NUM
%token ','
%left '+' '-'
%left '*' '/'
%right UMINUS

%%

lines:lines expr '\n' {printf("%g\n",$2);}
      |lines '\n'
      |D
      |     
      ;
expr :expr '*' expr {$$=$1*$3;}
     |expr '/' expr {$$=$1/$3;}
     |expr '+' expr {$$=$1+$3;}
     |expr '-' expr {$$=$1+$3;}
     |'(' expr ')' {$$=$2;}
     |'-' expr %prec UMINUS {$$=-$2;}
     |IDENTIFIER {}
     |NUM   {}  
     ;
T    :INT {}
     |CHAR {}
     ;
L    :L ',' IDENTIFIER {}
     |IDENTIFIER {}
     ;
D    :T L {printf("T is %g, L is %g",$1,$2);}
     ;

%%
/*void yyerror (char *s)
{
  fprintf (stderr, "%s\n", s);
}  
*/  

我正在使用以下命令编译生成的代码:

flex mini.l
yacc my_yacc.y
gcc y.tab.c -ly
4

1 回答 1

1

您看到的错误来自 C 编译器,是由于您对 flex 和 yacc 如何协同工作的误解造成的。当我通过与您相同的工具运行它时,我收到以下错误,正如您所做的那样,正如@KeithThompson 所指出的:

In file included from my_yacc.y:5:0:
mini.l:1:1: error: expected identifier or '(' before '%' token
 %{
 ^
mini.l:5:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before' letter'
 letter [a-zA-Z]
 ^
mini.l:12:11: error: expected identifier or '(' before '{' token
 {integer} {return INT;}
           ^
... elided the rest ...

尽管您使用了命令flexyacc并且gcc以正确的顺序,但您已将该文件包含mini.l在您的野牛输入中。这是不正确的。您应该包括从创建的 flex的输出。该文件名为. 还必须在 yacc 输入文件的末尾包含此内容。(这是因为如果你不这样做,你会得到@flolo 发现的错误)。如果您对文件进行必要的更改,您将拥有:mini.llex.yy.cyacc

%{
#include <ctype.h>
#include <stdio.h>
/* #include "myhead.h" */
/* #include "mini.l" */

#define YYSTYPE double
# undef fprintf
%}

%token INT
%token CHAR
%token IDENTIFIER
%token NUM
%token ','
%left '+' '-'
%left '*' '/'
%right UMINUS

%%

lines:lines expr '\n' {printf("%g\n",$2);}
      |lines '\n'
      |D
      |     
      ;
expr :expr '*' expr {$$=$1*$3;}
     |expr '/' expr {$$=$1/$3;}
     |expr '+' expr {$$=$1+$3;}
     |expr '-' expr {$$=$1+$3;}
     |'(' expr ')' {$$=$2;}
     |'-' expr %prec UMINUS {$$=-$2;}
     |IDENTIFIER {}
     |NUM   {}  
     ;
T    :INT {}
     |CHAR {}
     ;
L    :L ',' IDENTIFIER {}
     |IDENTIFIER {}
     ;
D    :T L {printf("T is %g, L is %g",$1,$2);}
     ;

%%
void yyerror (char *s)
{
  fprintf (stderr, "%s\n", s);
}  

#include "lex.yy.c"

如果您现在在此运行命令序列,您会发现它既可以编译又可以运行,并且还能正确处理 C 语言输入。

您犯的错误是新用户对工具flexyacc.

于 2015-04-09T20:53:06.063 回答