G++ 在编译我的 flex 和 bison 代码时无法识别我的 c++ 类
G++ isn't recognizing my c++ classes when compiling my flex and bison code
我正在编写一个词法分析和解析器组合,它将 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
既是类型的名称,又是语法中标记的名称。将类型或令牌重命名为其他名称应该可以解决此问题。
希望这有帮助!
相关文章:
- C++出现控制台错误.我无法识别源代码的问题
- 为什么C++无法识别我的对象实例化?
- 我的头文件 DNode 中的我的数据类型未被识别为数据类型
- Metatrader 5 向导无法识别我的自定义交易信号模块
- 在我的类中无法识别用户定义的文本运算符
- C ++中的函数为什么我的编译器无法识别错误()
- 为什么我的程序有时只能正确识别数组中的最高和最低值
- G++ 在编译我的 flex 和 bison 代码时无法识别我的 c++ 类
- 为什么 IDE 无法识别我的输入?
- 为什么我的程序无法识别结构中的变量?
- 编译器无法识别我的结构成员
- QVariant 转换无法识别我的模板调用的 std::string
- 为什么我的Visual Studio 2017编译器无法识别C++17类型推导指南
- 与2017年相比,我认为这是因为它没有识别我的课程
- C++无法识别我的 for 循环
- Visual Studio无法识别我的网络摄像头激光测距仪代码的MFC库
- std::strtoull在我的JNI NDK环境下无法识别
- 为什么我的重载乘法运算符没有被识别
- 我可以't识别if语句中的endl字符
- 为什么我的程序不能识别素数