c++ - 在编译 lex.yy.c 期间找不到 header y.tab.h

标签 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 解析了解析器

最佳答案

在您的代码中更改为“y.tab.h”。此外,您可能会在 gcc 中包含一个参数以包含文件的路径

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

关于c++ - 在编译 lex.yy.c 期间找不到 header y.tab.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41215171/

相关文章:

android - 在 Android Studio 上构建失败

c++ - 从字符串文字中推导模板参数

c++ - 如何发送到 Google Logger 和 cout?

laravel - Laravel错误消失了

c++ - Bison 修复解析深度

c++ - 弹性和 Bison : how to recognize an array

c++ - Flex++ Bisonc++ 解析器

c++ - Boost 调试和发布库是否相同?

c++ - C/C++ 中的 typedef 是否真的通过组合复合类型(例如 int*)来创建新类型?

compiler-errors - "Error: Universe inconsistency"在 Coq 中是什么意思?