aboutsummaryrefslogtreecommitdiffstats
path: root/c_parser
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-02-14 14:02:40 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-02-14 14:02:40 +0000
commitec1bd2264a19542b4e36df08ad2dba2c50f437b3 (patch)
tree808dd28f552355f78b92465071c4abacc9935eba /c_parser
parent494894c2072494f199214acc5855155baae6d174 (diff)
downloadCompiler-ec1bd2264a19542b4e36df08ad2dba2c50f437b3.tar.gz
Compiler-ec1bd2264a19542b4e36df08ad2dba2c50f437b3.zip
Working vector in parser
Diffstat (limited to 'c_parser')
-rw-r--r--c_parser/include/ast.hpp3
-rw-r--r--c_parser/include/ast_base.hpp5
-rw-r--r--c_parser/include/ast_base.hpp~14
-rw-r--r--c_parser/include/ast_declaration.hpp6
-rw-r--r--c_parser/include/ast_top.hpp23
-rw-r--r--c_parser/include/ast_top.hpp~23
-rw-r--r--c_parser/include/top_ast.hpp~14
-rw-r--r--c_parser/src/c_parser.y20
-rw-r--r--c_parser/src/parser_main.cpp6
9 files changed, 82 insertions, 32 deletions
diff --git a/c_parser/include/ast.hpp b/c_parser/include/ast.hpp
index 91c0796..a0889ae 100644
--- a/c_parser/include/ast.hpp
+++ b/c_parser/include/ast.hpp
@@ -3,7 +3,8 @@
#include "ast_base.hpp"
#include "ast_declaration.hpp"
+#include "ast_top.hpp"
-extern const ast_Base *parseAST();
+ast_Top *parseAST();
#endif
diff --git a/c_parser/include/ast_base.hpp b/c_parser/include/ast_base.hpp
index 7f8d56e..432a54b 100644
--- a/c_parser/include/ast_base.hpp
+++ b/c_parser/include/ast_base.hpp
@@ -1,8 +1,9 @@
-#ifndef AST_EXPRESSION_HPP
-#define AST_EXPRESSION_HPP
+#ifndef AST_BASE_HPP
+#define AST_BASE_HPP
#include <string>
#include <iostream>
+#include <vector>
class ast_Base {
public:
diff --git a/c_parser/include/ast_base.hpp~ b/c_parser/include/ast_base.hpp~
deleted file mode 100644
index ef0368a..0000000
--- a/c_parser/include/ast_base.hpp~
+++ /dev/null
@@ -1,14 +0,0 @@
-#ifndef AST_EXPRESSION_HPP
-#define AST_EXPRESSION_HPP
-
-#include <string>
-#include <iostream>
-
-class ast_base {
-public:
- virtual ~ast_base() {}
-
- virtual void print() const = 0;
-};
-
-#endif
diff --git a/c_parser/include/ast_declaration.hpp b/c_parser/include/ast_declaration.hpp
index 2eed3a2..e529378 100644
--- a/c_parser/include/ast_declaration.hpp
+++ b/c_parser/include/ast_declaration.hpp
@@ -6,9 +6,11 @@
class ast_Declaration : public ast_Base {
private:
const std::string id;
+
+protected:
+
public:
- ast_Declaration(const std::string& _id) : id(_id) {}
-
+ ast_Declaration(const std::string _id) : id(_id) {}
virtual void print() const override {
std::cout << id;
}
diff --git a/c_parser/include/ast_top.hpp b/c_parser/include/ast_top.hpp
new file mode 100644
index 0000000..cafae3a
--- /dev/null
+++ b/c_parser/include/ast_top.hpp
@@ -0,0 +1,23 @@
+#ifndef TOP_AST_HPP
+#define TOP_AST_HPP
+
+#include "ast.hpp"
+
+#include <vector>
+
+class ast_Top {
+public:
+ void print_vec() {
+ for(size_t i = 0; i < ast_vec.size(); ++i) {
+ ast_vec[i]->print();
+ }
+ }
+
+ void push_back(const ast_Base *stmnt) {
+ ast_vec.push_back(stmnt);
+ }
+private:
+ std::vector<const ast_Base *> ast_vec;
+};
+
+#endif
diff --git a/c_parser/include/ast_top.hpp~ b/c_parser/include/ast_top.hpp~
new file mode 100644
index 0000000..89cf415
--- /dev/null
+++ b/c_parser/include/ast_top.hpp~
@@ -0,0 +1,23 @@
+#ifndef TOP_AST_HPP
+#define TOP_AST_HPP
+
+#include "ast.hpp"
+
+#include <vector>
+
+class ast_Top {
+public:
+ void print_vec() {
+ for(size_t i = 0; i < ast_vec.size(); ++i) {
+ ast_vec[i]->print();
+ }
+ }
+
+ void push_back(const ast_Base *stmnt) {
+ ast_vec.push_back(stmnt);
+ }
+private:
+ ast_Top_t ast_vec;
+};
+
+#endif
diff --git a/c_parser/include/top_ast.hpp~ b/c_parser/include/top_ast.hpp~
new file mode 100644
index 0000000..0252bde
--- /dev/null
+++ b/c_parser/include/top_ast.hpp~
@@ -0,0 +1,14 @@
+#ifndef TOP_AST_HPP
+#define TOP_AST_HPP
+
+#include "ast.hpp"
+
+#include <vector>
+
+class top_ast {
+public:
+private:
+ std::vector<const ast_Base*> ast_vec;
+};
+
+#endif
diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y
index 351cbf2..ae80446 100644
--- a/c_parser/src/c_parser.y
+++ b/c_parser/src/c_parser.y
@@ -1,7 +1,7 @@
%code requires{
#include "ast.hpp"
-extern const ast_Base *g_root; // A way of getting the AST out
+extern ast_Top *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
@@ -14,14 +14,14 @@ void yyerror(const char *);
// Represents the value associated with any kind of
// AST node.
%union{
- const ast_Base *expr;
+ const ast_Base *stmnt;
// double number;
std::string *string;
}
-%token T_KEYWORD T_IDENTIFIER T_SC //T_CONSTANT T_OPERATOR T_LCBRACKET T_RCBRACKET
+%token T_KEYWORD T_IDENTIFIER T_SC //T_CONSTANT T_OPERATOR T_LCBRACKET T_RCBRACKET
-%type <expr> STMNT_LIST STMNT DCLRTN // COMP_STMNT EXPR_STMNT SLCT_STMNT ITR_STMNT JMP_STMNT
+%type <stmnt> STMNT DCLRTN STMNT_LIST // COMP_STMNT STMNT_LIST_STMNT SLCT_STMNT ITR_STMNT JMP_STMNT
// %type <number> // T_CONSTANT
%type <string> T_KEYWORD T_IDENTIFIER //T_OPERATOR
@@ -29,10 +29,10 @@ void yyerror(const char *);
%%
-ROOT : STMNT_LIST { g_root = $1; }
+ROOT : STMNT_LIST { ; }
-STMNT_LIST : STMNT { $$ = $1; }
- | STMNT_LIST STMNT { $$ = $2; }
+STMNT_LIST : STMNT { g_root->push_back($1); }
+ | STMNT_LIST STMNT { g_root->push_back($2); }
STMNT : DCLRTN { $$ = $1; }
@@ -40,10 +40,10 @@ DCLRTN : T_KEYWORD T_IDENTIFIER T_SC { $$ = new ast_Declaration(*$2); }
%%
-const ast_Base *g_root; // Definition of variable (to match declaration earlier)
+ast_Top *g_root; // Definition of variable (to match declaration earlier)
-const ast_Base *parseAST() {
- g_root = 0;
+ast_Top *parseAST() {
+ g_root = new ast_Top;
yyparse();
return g_root;
}
diff --git a/c_parser/src/parser_main.cpp b/c_parser/src/parser_main.cpp
index 4dd25eb..53209e3 100644
--- a/c_parser/src/parser_main.cpp
+++ b/c_parser/src/parser_main.cpp
@@ -3,9 +3,9 @@
#include <iostream>
int main(int argc, char *argv[]) {
-
- const ast_Base *ast = parseAST();
- ast->print();
+ ast_Top *ast = parseAST();
+
+ ast->print_vec();
std::cout << std::endl;