0

我正在研究 lex 和 yacc 中的编译器和编写代码。当我尝试执行我的程序时发现错误,但它总是有警告。我已经尝试了互联网上的方法,但仍然无法解决我的问题。希望可以有人帮帮我!

这是我的 lex 代码(exp.l):

/*%option outfile="scanner.cpp"*/
%{
/*#include "exp.tab.h"*/
#include "y.tab.h"
%}
%%
0|[1-9][0-9]*           { yylval = atoi(yytext); return INTEGER; }
[+*()\n]                { return yytext[0]; }
.                       { /* do nothing */ }
%% 

这是我的 yacc 代码(exp.y):

/*
%output "parser.cpp"
%skeleton "lalr1.cc"
*/
%{ 
#include <stdio.h>
%} 
%token INTEGER
%left '+'
%left '*'

%% 
input   : /* empty string */
            | input line 
            ;
line    : '\n' 
            | exp '\n' { printf ("\t%d\n", $1); } 
            | error '\n'
            ;
exp : INTEGER { $$ = $1; }
            | exp '+' exp { $$ = $1 + $3; }
            | exp '*' exp { $$ = $1 * $3; } 
            | '(' exp ')' { $$ = $2; } ;
%%

main () {
  yyparse (); 
}


yyerror (char *s) {
  printf ("%s\n", s);
} 

我的命令是:

flex exp.l
bison -d exp.y
gcc exp.tab.c lex.yy.c -o exp -lfl

它显示如下:

exp.tab.c: In function ‘yyparse’:
exp.tab.c:1217:16: warning: implicit declaration of function ‘yylex’ [-Wimplicit-function-declaration]
 1217 |       yychar = yylex ();
      |                ^~~~~
exp.tab.c:1374:7: warning: implicit declaration of function ‘yyerror’; did you mean ‘yyerrok’? [-Wimplicit-function-declaration]
 1374 |       yyerror (YY_("syntax error"));
      |       ^~~~~~~
      |       yyerrok
exp.y: At top level:
exp.y:28:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
   28 | main () {
      | ^~~~
exp.y:33:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
   33 | yyerror (char *s) {
      | ^~~~~~~
exp.l:4:10: fatal error: y.tab.h: No such file or directory
    4 | #include "y.tab.h"
      |          ^~~~~~~~~
compilation terminated.

希望可以有人帮帮我。

4

0 回答 0