aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--c_compiler/include/ast.hpp14
-rw-r--r--c_compiler/include/declaration.hpp18
-rw-r--r--c_compiler/include/function.hpp21
-rw-r--r--c_compiler/include/initializer.hpp13
-rw-r--r--c_compiler/include/statement.hpp11
-rw-r--r--c_compiler/include/translation_unit.hpp18
-rw-r--r--c_compiler/include/type.hpp4
-rw-r--r--c_compiler/src/c_parser.y235
-rw-r--r--c_compiler/src/compiler_main.cpp4
-rw-r--r--makefile4
10 files changed, 88 insertions, 254 deletions
diff --git a/c_compiler/include/ast.hpp b/c_compiler/include/ast.hpp
index 4a57282..5a31fca 100644
--- a/c_compiler/include/ast.hpp
+++ b/c_compiler/include/ast.hpp
@@ -6,16 +6,16 @@
#include <iostream>
#include "node.hpp"
-#include "statement.hpp"
-#include "function.hpp"
-#include "declaration.hpp"
-#include "expression.hpp"
-#include "primitives.hpp"
+//#include "expression.hpp"
+//#include "primitives.hpp"
#include "type.hpp"
#include "initializer.hpp"
+#include "declaration.hpp"
+#include "statement.hpp"
+#include "function.hpp"
#include "translation_unit.hpp"
-#include "ast_top.hpp"
+//#include "ast_top.hpp"
-const BaseList* parseAST();
+TranslationUnit* parseAST();
#endif
diff --git a/c_compiler/include/declaration.hpp b/c_compiler/include/declaration.hpp
index 7fdee1c..82bf157 100644
--- a/c_compiler/include/declaration.hpp
+++ b/c_compiler/include/declaration.hpp
@@ -13,15 +13,19 @@ protected:
Declaration* decl;
public:
- Declaration(const Type* _type = nullptr,
- const std::string _id = "",
- const Initializer* _init = nullptr);
+ Declaration(const std::string& _id = "") : id(_id) {}
- virtual void print() const;
- virtual void printxml() const;
- virtual void printasm() const;
+ virtual void print() const {
+ std::cout << id << std::endl;
+ if(decl != nullptr)
+ decl->print();
+ }
+ virtual void printxml() const {}
+ virtual void printasm() const {}
-
+ void addDeclaration(Declaration* _decl) {
+ decl = _decl;
+ }
};
#endif
diff --git a/c_compiler/include/function.hpp b/c_compiler/include/function.hpp
index c58699c..a6d44f0 100644
--- a/c_compiler/include/function.hpp
+++ b/c_compiler/include/function.hpp
@@ -6,20 +6,21 @@
class Function : public Node {
protected:
- const Type* type;
+ Type* type;
std::string id;
- const DeclarationList* parameter_list;
- const Statement* statement;
+ Declaration* parameter_list;
+ Statement* statement;
public:
- Function(const std::string& _id, const Statement* _comp_statement);
+ Function(const std::string& _id, Declaration* _parameter_list) : id(_id), parameter_list(_parameter_list) {}
- virtual ~Function();
-
- virtual void printxml() const override;
- virtual void printasm() const override;
-
- void push_parameter(const Declaration* declaration) const;
+ virtual void print() const {
+ std::cout << id << std::endl;
+ parameter_list->print();
+ }
+
+ virtual void printxml() const {}
+ virtual void printasm() const {}
};
diff --git a/c_compiler/include/initializer.hpp b/c_compiler/include/initializer.hpp
index 723f7aa..f28fbcc 100644
--- a/c_compiler/include/initializer.hpp
+++ b/c_compiler/include/initializer.hpp
@@ -5,17 +5,24 @@
class Initializer : public Node {
-
+public:
+ Initializer() {}
+
+ virtual void print() const {}
+ virtual void printxml() const {}
+ virtual void printasm() const {}
};
class Integer : public Initializer {
-
+public:
+ Integer() : Initializer() {}
};
class StringLiteral : public Initializer {
-
+public:
+ StringLiteral() : Initializer() {}
};
diff --git a/c_compiler/include/statement.hpp b/c_compiler/include/statement.hpp
index e715937..ee4887a 100644
--- a/c_compiler/include/statement.hpp
+++ b/c_compiler/include/statement.hpp
@@ -6,9 +6,16 @@
class Statement : public Node {
public:
- Statement(const Node* _left = new EmptyNode, const Node* _right = new EmptyNode);
+ //Statement(const Node* _left = new EmptyNode, const Node* _right = new EmptyNode);
+ Statement() {}
+
+ virtual void print() const {}
+ virtual void printxml() const {}
+ virtual void printasm() const {}
};
+/*
+
class CompoundStatement : public Statement {
public:
CompoundStatement(const Node* _dec = new EmptyNode, const Node* _statement = new EmptyNode);
@@ -38,4 +45,6 @@ public:
IterationStatement(const Node* _el);
};
+*/
+
#endif
diff --git a/c_compiler/include/translation_unit.hpp b/c_compiler/include/translation_unit.hpp
index 6601994..dd8ff03 100644
--- a/c_compiler/include/translation_unit.hpp
+++ b/c_compiler/include/translation_unit.hpp
@@ -6,9 +6,23 @@
class TranslationUnit : public Node {
protected:
- // TODO includes all the variable declarations and function definitions
+ std::vector<Node* > m_transUnit;
public:
- TranslationUnit() {}
+ TranslationUnit(Node* decl) {
+ m_transUnit.push_back(decl);
+ }
+
+ virtual void print() const {
+ for(auto& i : m_transUnit) {
+ i->print();
+ }
+ }
+ virtual void printxml() const {}
+ virtual void printasm() const {}
+
+ void push(Node* decl) {
+ m_transUnit.push_back(decl);
+ }
};
diff --git a/c_compiler/include/type.hpp b/c_compiler/include/type.hpp
index 8b24b30..6e8eefa 100644
--- a/c_compiler/include/type.hpp
+++ b/c_compiler/include/type.hpp
@@ -7,6 +7,10 @@
class Type : public Node {
public:
Type();
+
+ virtual void print() const {}
+ virtual void printxml() const {}
+ virtual void printasm() const {}
};
diff --git a/c_compiler/src/c_parser.y b/c_compiler/src/c_parser.y
index 39bc13c..7a3899b 100644
--- a/c_compiler/src/c_parser.y
+++ b/c_compiler/src/c_parser.y
@@ -2,7 +2,7 @@
#include "ast.hpp"
-extern const TranslationUnit* g_root; // A way of getting the AST out
+extern TranslationUnit* g_root; // A way of getting the AST out
//! This is to fix problems when generating C++
// We are declaring the functions provided by Flex, so
@@ -19,6 +19,7 @@ void yyerror(const char *);
TranslationUnit* trans_unit;
Function* function;
Type* type;
+ Initializer* initializer;
Declaration* declaration;
double number;
std::string* string;
@@ -33,40 +34,24 @@ void yyerror(const char *);
%nonassoc T_RRB
%nonassoc T_ELSE
-
-%type <base_list> ExtDef DeclarationList InitDeclaratorList IdentifierList
- StatementList ArgumentExpressionList
-
%type <node> ExternalDeclaration
%type <trans_unit> TranslationUnit
%type <function> FunctionDefinition
-%type <declaration> Parameter ParamDeclarator Declaration
-
-%type <decl_list> ParameterList
+%type <declaration> Parameter Declaration InitDeclaratorList InitDeclarator ParameterList
%type <type> DeclarationSpec
%type <string> Declarator DirectDeclarator
-
-%type <base_node> ExtDeclaration Declaration DeclarationSpec DeclarationSpec_T
- Statement CompoundStatement CompoundStatement_2 PrimaryExpression
- SelectionStatement ExpressionStatement JumpStatement IterationStatement
- Expression AssignmentExpression ConditionalExpression LogicalOrExpression
- LogicalAndExpression InclusiveOrExpression ExclusiveOrExpression
- AndExpression EqualityExpression RelationalExpression ShiftExpression
- AdditiveExpression MultiplicativeExpression CastExpression UnaryExpression
- PostfixExpression PostfixExpression2
-
-%type <base_prim> Declarator DirectDeclarator InitDeclarator Constant
%type <number> T_INT_CONST
+%type <initializer> Initializer
-%type <string> T_IDENTIFIER ASSIGN_OPER T_ASSIGN_OPER T_EQ T_AND T_ADDSUB_OP T_TILDE T_NOT
- T_MULT T_DIV T_REM MultDivRemOP UnaryOperator
+%type <string> T_IDENTIFIER T_ASSIGN_OPER T_EQ T_AND T_ADDSUB_OP T_TILDE T_NOT
+ T_MULT T_DIV T_REM
%start ROOT
@@ -91,28 +76,23 @@ ExternalDeclaration:
// FUNCTION DEFINITION
FunctionDefinition:
- DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB CompoundStatement { $$ = new Function($1, $2, $4, $6); }
+ DeclarationSpec T_IDENTIFIER T_LRB ParameterList T_RRB T_SC { $$ = new Function(*$2, $4); }
;
ParameterList:
- %empty { $$ = new DeclarationList(); }
- | Parameter { $$ = new DeclarationList($1); }
- | ParameterList T_CMA Parameter { $$->push($3); }
+ %empty { $$ = new Declaration(); }
+ | Parameter { $$ = $1; }
+ | ParameterList T_CMA Parameter { $3->addDeclaration($$); $$ = $3; }
;
Parameter:
- DeclarationSpec T_IDENTIFIER { $$ = new Declaration($1, *$2); }
+ DeclarationSpec T_IDENTIFIER { $$ = new Declaration(*$2); }
;
// Declaration
-DeclarationList:
- Declaration { $$ = new DeclarationList($1); }
- | DeclarationList Declaration { $$->push($2); }
- ;
-
Declaration:
- DeclarationSpec InitDeclaratorList T_SC { $$ = new Declaration($2); }
+ DeclarationSpec InitDeclaratorList T_SC { $$ = $2; }
;
DeclarationSpec:
@@ -128,12 +108,12 @@ DeclarationSpec_T:
InitDeclaratorList:
InitDeclarator { $$ = $1; }
- | InitDeclarator T_CMA InitDeclaratorList { $1->addDeclaration($$); $$ = $1; }
+ | InitDeclaratorList T_CMA InitDeclarator { $3->addDeclaration($$); $$ = $3; }
;
InitDeclarator:
Declarator { $$ = new Declaration(*$1); }
- | Declarator T_EQ Initializer { $$ = new Declaration(*$1, $3); }
+ | Declarator T_EQ Initializer { $$ = new Declaration(*$1); }
;
Initializer:
@@ -147,196 +127,13 @@ Declarator:
DirectDeclarator:
T_IDENTIFIER { $$ = $1; }
- | T_LRB Declarator T_RRB { $$ = $2; }
- | DirectDeclarator T_LSB ConditionalExpression T_RSB { $$ = $1; }
- | DirectDeclarator T_LSB T_RSB { $$ = $1; }
- | DirectDeclarator T_LRB ParameterList T_RRB { $$ = $1; }
- | DirectDeclarator T_LRB IdentifierList T_RRB { $$ = $1; }
- ;
-
-IdentifierList:
- T_IDENTIFIER { $$ = new BaseList(); }
- | IdentifierList T_CMA T_IDENTIFIER { $$ = new BaseList(); }
- ;
-
-// Statement
-
-StatementList:
- Statement { $$ = new StatementList($1); }
- | StatementList Statement { $$->push($2); }
- ;
-
-Statement:
- CompoundStatement { $$ = $1; }
- | SelectionStatement { $$ = $1; }
- | ExpressionStatement { $$ = $1; }
- | JumpStatement { $$ = $1; }
- | IterationStatement { $$ = $1; }
- ;
-
-CompoundStatement:
- T_LCB CompoundStatement_2 { $$ = $2; }
- ;
-
-CompoundStatement_2:
- T_RCB { $$ = new CompoundStatement; }
- | DeclarationList T_RCB { $$ = new CompoundStatement($1); }
- | DeclarationList StatementList T_RCB { $$ = new CompoundStatement($1, $2); }
- | StatementList T_RCB { $$ = new CompoundStatement($1); }
- ;
-
-SelectionStatement:
- T_IF T_LRB Expression T_RRB Statement { $$ = new SelectionStatement($5); }
- | T_IF T_LRB Expression T_RRB Statement T_ELSE Statement { $$ = new SelectionStatement($5, $7); }
- ;
-
-ExpressionStatement:
- T_SC { $$ = new ExpressionStatement(); }
- | Expression T_SC { $$ = $1; }
- ;
-
-JumpStatement:
- T_RETURN ExpressionStatement { $$ = new JumpStatement($2); }
- ;
-
-IterationStatement:
- T_WHILE T_LRB Expression T_RRB Statement { $$ = $5; }
- | T_DO Statement T_WHILE T_LRB Expression T_RRB T_SC { $$ = $2; }
- | T_FOR T_LRB Expression T_SC Expression T_SC Expression T_RRB Statement { $$ = $9; }
- ;
-
-// Expressions
-
-Expression:
- AssignmentExpression { $$ = $1; }
- ;
-
-AssignmentExpression:
- ConditionalExpression { $$ = $1; }
- | UnaryExpression ASSIGN_OPER AssignmentExpression { $$ = $1; }
- ;
-
-ASSIGN_OPER:
- T_ASSIGN_OPER { ; }
- | T_EQ { ; }
- ;
-
-ConditionalExpression:
- LogicalOrExpression { $$ = $1; }
- | LogicalOrExpression T_QU Expression T_COL ConditionalExpression { $$ = $1; }
- ;
-
-LogicalOrExpression:
- LogicalAndExpression { $$ = $1; }
- | LogicalOrExpression T_LOG_OR LogicalAndExpression { $$ = $3; }
- ;
-
-LogicalAndExpression:
- InclusiveOrExpression { $$ = $1; }
- | LogicalAndExpression T_LOG_AND InclusiveOrExpression { $$ = $3; }
- ;
-
-InclusiveOrExpression:
- ExclusiveOrExpression { $$ = $1; }
- | InclusiveOrExpression T_OR ExclusiveOrExpression { $$ = $3; }
- ;
-
-ExclusiveOrExpression:
- AndExpression { $$ = $1; }
- | ExclusiveOrExpression T_XOR AndExpression { $$ = $3; }
- ;
-
-AndExpression:
- EqualityExpression { $$ = $1; }
- | AndExpression T_AND EqualityExpression { $$ = $3; }
- ;
-
-EqualityExpression:
- RelationalExpression { $$ = $1; }
- | EqualityExpression T_EQUALITY_OP RelationalExpression { $$ = $3; }
- ;
-
-RelationalExpression:
- ShiftExpression { $$ = $1; }
- | RelationalExpression T_REL_OP ShiftExpression { $$ = $3; }
- ;
-
-ShiftExpression:
- AdditiveExpression { $$ = $1; }
- | ShiftExpression T_SHIFT_OP AdditiveExpression { $$ = $3; }
- ;
-
-AdditiveExpression:
- MultiplicativeExpression { $$ = $1; }
- | AdditiveExpression T_ADDSUB_OP MultiplicativeExpression { $$ = $3; }
- ;
-
-MultiplicativeExpression:
- CastExpression { $$ = $1; }
- | MultiplicativeExpression MultDivRemOP CastExpression { $$ = $3; }
- ;
-
-MultDivRemOP:
- T_MULT { $$ = $1; }
- | T_DIV { $$ = $1; }
- | T_REM { $$ = $1; }
- ;
-
-CastExpression:
- UnaryExpression { $$ = $1; }
- | T_LRB T_TYPE_SPEC T_RRB CastExpression { $$ = $4; }
- ;
-
-UnaryExpression:
- PostfixExpression { $$ = $1; }
- | T_INCDEC UnaryExpression { $$ = $2; }
- | UnaryOperator CastExpression { $$ = $2; }
- | T_SIZEOF UnaryExpression { $$ = $2; }
- | T_SIZEOF T_LRB T_TYPE_SPEC T_RRB { $$ = new Expression(); }
- ;
-
-UnaryOperator:
- T_AND { $$ = $1; }
- | T_ADDSUB_OP { $$ = $1; }
- | T_MULT { $$ = $1; }
- | T_TILDE { $$ = $1; }
- | T_NOT { $$ = $1; }
- ;
-
-PostfixExpression:
- PrimaryExpression { $$ = $1; }
- | PostfixExpression T_LSB Expression T_RSB { $$ = $3; }
- | PostfixExpression T_LRB PostfixExpression2 { $$ = $3; }
- | PostfixExpression T_DOT T_IDENTIFIER { $$ = new Expression(); }
- | PostfixExpression T_ARROW T_IDENTIFIER { $$ = new Expression(); }
- | PostfixExpression T_INCDEC { $$ = new Expression(); }
- ;
-
-PostfixExpression2:
- T_RRB { $$ = new Expression(); }
- | ArgumentExpressionList T_RRB { $$ = new BaseNode; }
- ;
-
-ArgumentExpressionList:
- AssignmentExpression { $$ = new BaseList; }
- | ArgumentExpressionList T_CMA AssignmentExpression { $$ = new BaseList; }
- ;
-
-PrimaryExpression:
- T_IDENTIFIER { $$ = new Expression(); }
- | Constant { $$ = new Expression($1); }
- | T_LRB Expression T_RRB { $$ = $2; }
- ;
-
-Constant:
- T_INT_CONST { $$ = new Immediate($1); }
;
%%
-const TranslationUnit* g_root; // Definition of variable (to match declaration earlier)
+TranslationUnit* g_root; // Definition of variable (to match declaration earlier)
-const TranslationUnit* parseAST() {
+TranslationUnit* parseAST() {
g_root = 0;
yyparse();
return g_root;
diff --git a/c_compiler/src/compiler_main.cpp b/c_compiler/src/compiler_main.cpp
index d551b8b..8dce76f 100644
--- a/c_compiler/src/compiler_main.cpp
+++ b/c_compiler/src/compiler_main.cpp
@@ -3,9 +3,9 @@
#include <iostream>
int main(int argc, char *argv[]) {
- const BaseList* ast = parseAST();
+ TranslationUnit* ast = parseAST();
- ast->printasm();
+ ast->print();
return 0;
}
diff --git a/makefile b/makefile
index 64d746d..4908107 100644
--- a/makefile
+++ b/makefile
@@ -67,9 +67,7 @@ $(PARSRCDIR)/c_parser.tab.cpp $(PARSRCDIR)/c_parser.tab.hpp : $(PARSRCDIR)/c_par
@echo " bison -v -d $< -o $(PARSRCDIR)/c_parser.tab.cpp"; bison -v -d $< -o $(PARSRCDIR)/c_parser.tab.cpp
# Make the c_parser
-bin/c_compiler: $(COMPBUILDDIR)/compiler_main.o $(COMPBUILDDIR)/function.o \
- $(COMPBUILDDIR)/statement.o $(COMPBUILDDIR)/primitives.o \
- $(COMPBUILDDIR)/expression.o \
+bin/c_compiler: $(COMPBUILDDIR)/compiler_main.o \
$(COMPBUILDDIR)/c_parser.tab.o $(COMPBUILDDIR)/c_lexer.yy.o
@echo "Linking..."
@echo " mkdir -p bin"; mkdir -p bin