在编译lex.yy.c期间找不到头文件y.tab.h

时间:2016-12-19 02:55:50

标签: c++ compiler-errors bison flex-lexer

我正在尝试创建一个基本的编译器,我遇到了一些问题。

我的弹力代码:

%{
    #include <stdio.h>
    #include "SymbolTable.h"
    #include <y.tab.h>

    //extern "C"
    //{
    //   extern int yylex(void);
    //   extern int yyparse(void);
    //}

    int line_count = 1;
    SymbolTable table;
    static int yywrap(void);

    void insert(const char* token, char* yytext) {
    ////printf ("%s: %s ", token, yytext);
    //printf ("%s ", token);
    SymbolInfo symbolInfo(yytext, token);
    table.insert(symbolInfo, line_count);
    }
    %}

    newLine         \n
    delim           [  \t]
    digit           [0-9]
    unsigned        {digit}+
    exponent        [eE][+-]?{unsigned}
    number          [-+]?({unsigned}|{unsigned}\.{unsigned}?|{unsigned}?\.{unsigned}){exponent}?
    letter          [A-Za-z]
    keyword         program|if|not|end|begin|else|then|do|while|function|Procedure|integer|real|var|oh|array|write|include
    id              (_|{letter})({letter}|{digit}|_)*
    header          {id}\.h
    sin_comment     \/\/.*
    mul_comment     "/*"(([^*]|(("*"+)[^*/]))*)("*"+)"/"
    string          \"(\\.|[^"])*\"
    char            \'(\\.|[^'])*\'
    addop           [\+-]|or
    mulop           [\*\/]|div|mod|and
    orop            ||
    leop            <=
    geop            >=
    eqop            ==
    neop            !=
    assignop        :=
    dotdot          \.\.
    brace           [\[\]\(\)]
    other           [,;:]
    hash            #
    err_dotdot      [0-9]+\.([0-9]+\.){1,}
    err_id          {digit}+{id}
    leftcurl        {
    rightcurl       }



    %%
    {newLine} {
    line_count++;
    }

    {string} {
    //printf ("str: %s\n", yytext);
    //printf ("line no: %d\n",line_count);
    //fprintf(tokenout,"\n<STRING, %s>\n",yytext);
    //fprintf(logout,"\nLine no %d: STRING <%s> found\n",line_count,yytext);
    }

    {char} {
    //printf ("str: %s\n", yytext);
    //printf ("line no: %d\n",line_count);
    //fprintf(tokenout,"\n<CHAR, %s>\n",yytext);
    //fprintf(logout,"\nLine no %d: CHAR <%s> found\n",line_count,yytext);
    }

    {keyword} {

    ////printf ("keyword");
    //fprintf(tokenout,"\n<KEYWORD, %s>\n",yytext);
    //fprintf(logout,"\nLine no %d: KEYWORD <%s> found\n",line_count,yytext);

    }

    {header} {
    insert("header",yytext);
    //fprintf(tokenout,"\n<HEADER %s>\n",yytext);
    //fprintf(logout,"\nLine no %d: HEADER <%s> found\n",line_count,yytext);

    }

    {id} {
    SymbolInfo symbolInfo(yytext, "ID");
    table.insert(symbolInfo, line_count);
    //printf ("id: %s\n", yytext); 
    //fprintf(tokenout,"<ID, %s>\n",yytext);
    //fprintf(logout,"\nLine no %d: ID <%s> found\n",line_count,yytext);
    }

    {sin_comment} {
    //printf ("\nSingleline comment found: %s\n",yytext);
    //fprintf(logout,"\nLine no %d: SINGLELINECOMMENTP <%s> found\n",line_count,yytext);
    }

    {mul_comment} {
    //printf ("\nMultiline comment found: %s\n",yytext);
    //fprintf(logout,"\nLine no %d: MULTILINECOMMENT <%s> found\n",line_count,yytext);
    }

    {number} {
    //printf ("number:%s\n",yytext);
    //printf ("line no: %d\n",line_count);
    SymbolInfo symbolInfo(yytext, "NUMBER");
    table.insert(symbolInfo, line_count);
    //fprintf(tokenout,"<NUMBER, %s>\n",yytext);
    //fprintf(logout,"\nLine no %d: NUMBER <%s> found\n",line_count,yytext);
    return NUMBER;

    }

    {addop} {
    insert("ADDOP", yytext);
    //printf ("line no: %d\n",line_count);
    //fprintf(tokenout,"\n<ADDOP, %s>\n",yytext);
    //fprintf(logout,"\nLine no %d: ADDOP <%s> found\n",line_count,yytext);
    return ADDOP;
    }

    {mulop} {
    insert("MULOP", yytext);
    //printf ("line no: %d\n",line_count);
    //fprintf(tokenout,"\n<MULOP, %s>\n",yytext);
    //fprintf(logout,"\nLine no %d: MULOP <%s> found\n",line_count,yytext);
    }

    {assignop} {
    insert("ASSIGNOP", yytext);
    //printf ("line no: %d\n",line_count);
    //fprintf(tokenout,"\n<ASSIGNOP, %s>\n",yytext);
    //fprintf(logout,"\nLine no %d: ASSIGNOP <%s> found\n",line_count,yytext);
    }


    {dotdot} {
    insert("DOTDOT", yytext);
    //printf ("line no: %d\n",line_count);
    }   
    {hash}  {
    insert("#",yytext);
    }

    {delim}+ {}

    {err_dotdot} {
    //printf ("Illegal usage of decimal\n");
    //fprintf(logout,"\nLine no %d: Illegal usage of decimal <%s> found\n",line_count,yytext);
    }

    {err_id} {
    //printf ("Illegal id\n");
    //fprintf(logout,"\nLine no %d: Illegal usage of id <%s> found\n",line_count,yytext);
    }

    %%

我可以愉快地创建lex.yy.c  lex -o lex.yy.c lex.l

但是当我尝试使用g++ -c -w -o lexer.o lex.yy.c创建lex对象文件时,它说

lex.l:4:19: fatal error: y.tab.h: No such file or directory

我已经使用bison -d -y grammar.y

解析了解析器

1 个答案:

答案 0 :(得分:0)

更改为&#34; y.tab.h&#34;在你的代码中。此外,您可以在gcc中包含一个参数,以包含文件的路径

g++ -c -w -I /path/to/header -o lexer.o lex.yy.c