参考
- 使用flex,bison和C ++ 14的C编译器,可编译为mips32汇编 这老哥完成度相当不错, 大家可以
直接抄参考. - 程序员的事情怎么能说是抄袭呢? --鲁迅
- 或者也可以看我的flex+bison进行语法分析 只生成了AST
而且打印格式有问题.
项目
- 目录
- 你问我为啥后缀是
yy
,ll
,hh
? 我也不知道,但是能编译通过!
这里只用提及Makefile
与ast.hh
Makefile
a.out: gt.ll gt.yy ast.hh
bison -d gt.yy -v --debug
flex -o gt.lex.c gt.ll
g++ -g -o $@ gt.yy.tab.c gt.lex.c main.cpp # -g是为了调试
test:
./a.out ./tests/t1
clean:
rm ./a.out gt.lex.c gt.yy.tab.c gt.yy.tab.h
ast.hh
请自行忽略掉糟糕的打印格式.
#include <iostream>
#include <vector>
class NStatement;
class NExpression;
class NVariableDeclaration;
typedef std::vector<NStatement *> StatementList;
typedef std::vector<NExpression *> ExpressionList;
class Node
{
public:
virtual ~Node() {}
virtual void debug() {}
};
class NExpression : public Node
{
};
class NStatement : public Node
{
};
class NInteger : public NExpression
{
public:
long long value;
NInteger(long long value) : value(value) {}
void debug()
{
std::cout << "Int_value= " << value << "\t";
}
};
class NDouble : public NExpression
{
public:
double value;
NDouble(double value) : value(value) {}
void debug()
{
std::cout << "Double_value= " << value << "\t";
}
};
class NIdentifier : public NExpression
{
public:
std::string name;
NIdentifier(const std::string &name) : name(name) {}
void debug()