未声明的“ yylex”和“ yyin”

时间:2019-12-05 13:30:59

标签: c yacc lex

我附上lex和yacc文件以供参考。为什么会出现此错误?如何成功编译? (y.tab.c文件)

借助于一些已经可用的代码,我创建了lex和yacc(语法)文件。然后,我还添加了语义动作和三个地址生成代码。我想测试一个示例程序的代码,但是会发生以下错误:

错误:

view

Lex文件:

In function `yyparse':
y.tab.c:(.text+0x3f8): undefined reference to `yylex'
/tmp/cc2SiGTP.o: In function `main':
y.tab.c:(.text+0x372e): undefined reference to `yyin'
y.tab.c:(.text+0x373a): undefined reference to `yyin'
collect2: error: ld returned 1 exit status

Yacc文件:

%{
#include <stdio.h>
#include <string.h>
#include<conio.h>
#include<string.h>
#include "yaccWithSemanticAnd3AD.tab.h"

char c;
void removeComment();
void removeWhiteSpace();

%}

DIGIT  [0-9]+
NUMBER [+-]?[0-9]+|[+-]?[0-9]*"."[0-9]+
STRING \"(\\.|[^\"])*\"
BOOLEAN ["True" "False"]
ID [a-zA-Z_][0-9a-zA-Z_]*
FLOAT [0-9]+[.][0-9]*

ARITHMETIC_OPERATORS ['+' '-' '*' '/' '%']
OTHER_CHARACTERS ['=' '.' '(' ')' '{' '}' '[' ']']
TYPES ["Number" "String" "Boolean" "Return"]

%%
[ \t] ;
[\n] ;


"If"    { return (IF); }
"Else"  { return (ELSE); }
"While" { return (WHILE); }
"Main()" {return (MAIN); }
"Read()" {return (READ);}
"Class"  {return (CLASS);}
"True"   {return (TRUE);}
"False"  {return (FALSE);}
"Number" {return (TYPES);}
"String" {return (TYPES);}
"Boolean" {return (TYPES);}
"Print"   {return (PRINT);}
"Return"  {return (TYPES);}
"<!"      { removeComment();}

">" {yylval.sval = strdup(yytext);
        return (REL_OPT);}
"<" {yylval.sval = strdup(yytext);
        return (REL_OPT);}
">="    {yylval.sval = strdup(yytext);
        return (REL_OPT);}
"<="    {yylval.sval = strdup(yytext);
        return (REL_OPT);}
"!="    {yylval.sval = strdup(yytext);
        return (REL_OPT);}
"=="    {yylval.sval = strdup(yytext);
        return (REL_OPT);}

"|" {yylval.sval = strdup(yytext);
        return (OR);}
"&" {yylval.sval = strdup(yytext);
        return (AND);}
"!" {yylval.sval = strdup(yytext);
        return (NOT);}

{NUMBER}  { yylval.nval = atof(yytext);
       return NUMBER; }

{BOOLEAN}  { yylval.fval = atof(yytext);
       return BOOLEAN; }

{STRING}  { yylval.fval = atof(yytext);
       return STRING; }

{ID} {
        yylval.sval = strdup(yytext);
        return ID; 
}
{ARITHMETIC_OPERATORS}   {
                c = yytext[0];
                 return(c);
              }

{OTHER_CHARACTERS}   {
                 c = yytext[0];
                 return(c);
              }

%%

void removeComment()
{
    register int c;

    while(1)
    {
        while((c=input())!='!' && c!=EOF);
        {

            if(c==EOF)
            {

                exit(1);
            }
        }

        if(c==EOF)
        {

            exit(1);
        }

        if((c=input())=='<')
        break;
        else
        unput(c);
    }
    removeWhiteSpace();
}

void removeWhiteSpace()
{
    c=input();
    while(c==' '||c=='\t'||c=='\n')
    {

        c=input();

    }
    unput(c);
}

1 个答案:

答案 0 :(得分:2)

您似乎试图编译并链接独立的y.tab.c。您必须将其与生成的词法分析器文件lex.yy.c链接在一起,后者提供函数yylex(),该函数从解析器yyparse()调用以获取下一个标记以及{{1 }}用于词法分析器输入流。

所以正确的方法基本上是:

yyin