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

G++ isn't recognizing my c++ classes when compiling my flex and bison code

本文关键字:我的 识别 c++ bison 编译 flex G++ 代码      更新时间:2023-10-16

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

我一直在遵循本指南:在某种程度上 https://gnuu.org/2009/09/18/writing-your-own-toy-compiler/。我尝试更改野牛的输出设置以生成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   [ tn]
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); }
%%

说明:

#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);
^~~
};

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

希望这有帮助!