编译flex和bison代码时,G ++无法识别我的c ++类

时间:2019-06-17 20:17:37

标签: c++ bison

我正在编写一个词法分析器和语法分析器组合,该组合器将arm8汇编代码存储在数据结构中以进行进一步测试。但是,在编译代码时,编译器不会将我导入的类识别为合法的数据类型。

我一直在遵循本指南:https://gnuu.org/2009/09/18/writing-your-own-toy-compiler/。我尝试更改bison的输出设置以生成c ++文件,并部分解决了该问题,但是它打开了我希望避免的其他蠕虫病毒。我看过的所有指南在此过程中都使用c ++代码,但我并不真正理解为什么它在这里失败。

assembly_bison.y:

%{
    #include <cstdio>
    #include <iostream>
    #include <string>
    #include "instructionds.h"
    #include "AssemblyBlock.h"
    using namespace std;

    extern int yylex();
    extern int yyparse();
    extern FILE *yyin;

    AssemblyBlock *assembly = new AssemblyBlock();
    STP *input;
    void yyerror(const char *s);
%}

%union {
    long long imm;
    std::string *string;
    int token;
}

%token STP INSTRUCTION 
%token STACKPOINTER "sp"

%token <imm> IMMEDIATE
%token <string> DIRECTIVE LABEL INLINELABEL
%token <token> REGISTER64 REGISTER32
%token <token> COMMA ","
%token <token> BANG "!"
%token <token> OPENBRACKET "["
%token <token> CLOSEBRACKET "]"

%%
document:
    document line
    | /* empty */
;

line:
    LABEL
    | DIRECTIVE {/* */}
    | LABEL instruction
    | instruction
;

instruction:
    stp
;

stp:
    STP REGISTER64 "," REGISTER64 "," "[" "sp" "," IMMEDIATE "]" "!"
        {
            input = new STP(true, true, $2, $4, -1, $9);
            assembly->insert(input);
        }
%%

int main(int, char**) {
  // Open a file handle to a particular file:
  FILE *myfile = fopen("Hello_World_Assembly_Code.asm", "r");
  // Make sure it is valid:
  if (!myfile) {
    cout << "I can't open a.snazzle.file!" << endl;
    return -1;
  }
  // Set Flex to read from it instead of defaulting to STDIN:
  yyin = myfile;

  // Parse through the input:
  yyparse();

}

void yyerror(const char *s) {
  cout << "EEK, parse error!  Message: " << s << endl;
  // might as well halt now:
  exit(-1);
}

assembly_lexer.l

%{
    #include <cstdio>
    #include <string>
    #include "instructionds.h"
    #include "AssemblyBlock.h"
    #include "parser.hpp"
    #define SAVE_TOKEN yylval.string = new std::string(yytext, yyleng)
    #define TOKEN(t) (yylval.token = t)
%}
%option noyywrap

delim   [ \t\n]
ws      [delim+]
letter  [A-Za-z]
digit   [0-9]
id      {letter}({letter}|{digit})*
alphanumeric [A-Za-z0-9]
%%
{delim} {/* no action and return */}
\.L[A-Z0-9]*: { SAVE_TOKEN; return LABEL; }
\.[a-z0-9_]+.* { SAVE_TOKEN; return DIRECTIVE; }
{alphanumeric}+\: { SAVE_TOKEN; return LABEL; }
stp { return STP; }
add { return INSTRUCTION; }
adrp { return INSTRUCTION; }
bl { return INSTRUCTION; }
mov { return INSTRUCTION; }
ldp { return INSTRUCTION; }
ret { return INSTRUCTION; }
sp { return STACKPOINTER; }
x{digit}+ { yylval.register = stoi(yytext.substr(1,yytext.length())); return REGISTER64; }
w{digit}+ { yylval.register = stoi(yytext.substr(1,yytext.length())); return REGISTER32; }
, { return TOKEN(COMMA); }
\.L[A-Z0-9]* { yylval.sval = strdup(yytext); return INLINELABEL; } //Needs revision
\[ { return TOKEN(OPENBRACKET); }
\] { return TOKEN(CLOSEBRACKET); }
:{id}: { }
#?[+-]?{digit}+ { if(yytext[0] == '#') yytext.erase(0); yylval.imm = stoll(yytext); return IMMEDIATE } //Needs revision
{alphanumeric}+ { SAVE_TOKEN; return LABEL; }
! { return TOKEN(BANG); }
%%

instructionds.h:

#pragma once
class Instruction {
    public:
        virtual void print();
};

class STP : public Instruction{
    private: 

        //Possible inputs
        int Rn1;
        int Rn2;
        int Xn;
        bool SP;
        long long immediate;

        //Instruction Modes
        bool is64;
        bool isPreindex;
    public:
        STP(bool is64, bool isPreindex, int n1, int n2, int Xn, long long immediate);
        void print();
};

AssemblyBlock.h:

#pragma once
#include "instructionds.h"

struct InstStruct {
    Instruction* line;
    struct InstStruct *prev;
    struct InstStruct *next;
};

class AssemblyBlock {
    private:
        struct InstStruct *head;

    public:
        AssemblyBlock();
        void insert(Instruction *inst);
        void display();
};

以后可以根据需要为这些类添加.cpp文件。

当我使用以下命令编译代码时,出现这些错误。编译器似乎未读取标头。我使用一个测试文件来确保我构建的类在bison之外可以正常工作,并且一切正常。如果有人对此有所了解,我非常感谢您的帮助。

mattersonline@mattersonline-VirtualBox:~/Documents/flex/soonergy$ bison -d -o parser.cpp assembly_bison.y
assembly_bison.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
mattersonline@mattersonline-VirtualBox:~/Documents/flex/soonergy$ flex -o tokens.cpp assembly_lexer.l
mattersonline@mattersonline-VirtualBox:~/Documents/flex/soonergy$ g++ -o parser parser.cpp tokens.cpp AssemblyBlock.cpp instructionds.cpp
assembly_bison.y:15:5: error: ‘STP’ does not name a type
     STP *input;
     ^~~
assembly_bison.y: In function ‘int yyparse()’:
assembly_bison.y:56:13: error: ‘input’ was not declared in this scope
             input = new STP(true, true, $2, $4, -1, $9);
             ^~~~~
assembly_bison.y:56:13: note: suggested alternative: ‘ino_t’
             input = new STP(true, true, $2, $4, -1, $9);
             ^~~~~
             ino_t
assembly_bison.y:56:25: error: expected type-specifier before ‘STP’
             input = new STP(true, true, $2, $4, -1, $9);
                         ^~~
};

1 个答案:

答案 0 :(得分:1)

问题似乎是STP既是类型的名称,也是语法中标记的名称。将您的类型或令牌重命名为其他名称应该可以解决此问题。

希望这会有所帮助!