diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-17 10:55:21 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-17 10:55:21 +0000 |
commit | a60337264c2551fffc2b5aeea12f40a06b7cb0e9 (patch) | |
tree | f9898cfcfed008a001bb03e3f8919f889107e469 /c_parser | |
parent | 392805699c8d411400901b8e3d7298f0f9198bb5 (diff) | |
download | Compiler-a60337264c2551fffc2b5aeea12f40a06b7cb0e9.tar.gz Compiler-a60337264c2551fffc2b5aeea12f40a06b7cb0e9.zip |
Working variables finally by adding mutable
Diffstat (limited to 'c_parser')
-rw-r--r-- | c_parser/include/ast.hpp | 1 | ||||
-rw-r--r-- | c_parser/include/ast_base.hpp | 1 | ||||
-rw-r--r-- | c_parser/include/ast_declaration.hpp | 26 | ||||
-rw-r--r-- | c_parser/include/ast_primitives.hpp | 4 | ||||
-rw-r--r-- | c_parser/include/ast_top.hpp | 3 | ||||
-rw-r--r-- | c_parser/src/c_parser.y | 14 | ||||
-rw-r--r-- | c_parser/src/parser_main.cpp | 4 | ||||
-rw-r--r-- | c_parser/test/output.xml | 13 | ||||
-rw-r--r-- | c_parser/test/test_parser.c | 3 |
9 files changed, 51 insertions, 18 deletions
diff --git a/c_parser/include/ast.hpp b/c_parser/include/ast.hpp index a0889ae..e17ab49 100644 --- a/c_parser/include/ast.hpp +++ b/c_parser/include/ast.hpp @@ -3,6 +3,7 @@ #include "ast_base.hpp" #include "ast_declaration.hpp" +#include "ast_primitives.hpp" #include "ast_top.hpp" ast_Top *parseAST(); diff --git a/c_parser/include/ast_base.hpp b/c_parser/include/ast_base.hpp index 432a54b..c4293a8 100644 --- a/c_parser/include/ast_base.hpp +++ b/c_parser/include/ast_base.hpp @@ -10,6 +10,7 @@ public: virtual ~ast_Base() {} virtual void print() const = 0; + virtual void push(const ast_Base* var) const = 0; }; #endif diff --git a/c_parser/include/ast_declaration.hpp b/c_parser/include/ast_declaration.hpp index 98fe255..50cff2a 100644 --- a/c_parser/include/ast_declaration.hpp +++ b/c_parser/include/ast_declaration.hpp @@ -3,23 +3,35 @@ #include "ast.hpp" +#include <vector> + +// Declaration that holds a list of declarations + class ast_Declaration : public ast_Base { private: public: - virtual void print() const override = 0; + virtual void print() const = 0; }; -class ast_VariableDeclaration : public ast_Declartaion { +class ast_VariableDeclaration : public ast_Declaration { private: - const std::string id; - const std::string type; + mutable std::vector<const ast_Base*> var_list; public: - ast_VariableDeclaration(const std::string& _id, const std::string& _type) : - id(_id), type(_type) {} + ast_VariableDeclaration(const ast_Base* _var) { + var_list.push_back(_var); + } + + virtual void print() const { + for(int i = 0; i < var_list.size(); ++i) { + var_list[i]->print(); + } + } - virtual void print() const override {} + virtual void push(const ast_Base* var) const { + var_list.push_back(var); + } }; #endif diff --git a/c_parser/include/ast_primitives.hpp b/c_parser/include/ast_primitives.hpp index fbcb1b4..21629e7 100644 --- a/c_parser/include/ast_primitives.hpp +++ b/c_parser/include/ast_primitives.hpp @@ -11,9 +11,11 @@ private: public: ast_Variable(const std::string& _id) : id(_id) {} - virtual void print() const override { + virtual void print() const { std::cout << "<Variable id=\"" << id << "\" />" << std::endl; } + + virtual void push(const ast_Base* var) const {} }; #endif diff --git a/c_parser/include/ast_top.hpp b/c_parser/include/ast_top.hpp index ff1cffa..142dfb8 100644 --- a/c_parser/include/ast_top.hpp +++ b/c_parser/include/ast_top.hpp @@ -12,10 +12,11 @@ public: ast_vec[i]->print(); } } - + void push(const ast_Base *stmnt) { ast_vec.push_back(stmnt); } + private: std::vector<const ast_Base *> ast_vec; }; diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y index 5429f76..9171a1b 100644 --- a/c_parser/src/c_parser.y +++ b/c_parser/src/c_parser.y @@ -37,12 +37,12 @@ ROOT : EXT_DEF { ; } // EXTERNAL DEFINITION -EXT_DEF : EXT_DECLARATION { ; } - | EXT_DEF EXT_DECLARATION { $$ = $2; } +EXT_DEF : EXT_DECLARATION { g_root->push($1); } + | EXT_DEF EXT_DECLARATION { g_root->push($2); } EXT_DECLARATION : DECLARATION_SPEC EXT_DECLARATION_2 { $$ = $2; } -EXT_DECLARATION_2 : DECLARATION { ; } +EXT_DECLARATION_2 : DECLARATION { $$ = $1; } | FUNC_DEF { ; } // FUNCTION DEFINITION @@ -51,7 +51,7 @@ FUNC_DEF : T_IDENTIFIER T_LRB T_RRB T_LCB T_RCB { ; } // DECLARATION -DECLARATION : INIT_DECLARATOR_LIST T_SC { ; } +DECLARATION : INIT_DECLARATOR_LIST T_SC { $$ = $1; } DECLARATION_SPEC : DECLARATION_SPEC_T { ; } | DECLARATION_SPEC_T DECLARATION_SPEC { ; } @@ -60,13 +60,13 @@ DECLARATION_SPEC_T : T_TYPE_SPEC { ; } | T_TYPE_QUAL { ; } | T_STRG_SPEC { ; } -INIT_DECLARATOR_LIST : INIT_DECLARATOR { g_root->push($1); } - | INIT_DECLARATOR_LIST T_CMA INIT_DECLARATOR { g_root->push($3); } +INIT_DECLARATOR_LIST : INIT_DECLARATOR { $$ = new ast_VariableDeclaration($1); } + | INIT_DECLARATOR_LIST T_CMA INIT_DECLARATOR { $$->push($3); } INIT_DECLARATOR : DECLARATOR { ; } | DECLARATOR T_EQ INITIALIZER { ; } -DECLARATOR : T_IDENTIFIER {$$ = new ast_Declaration(*$1); } +DECLARATOR : T_IDENTIFIER {$$ = new ast_Variable(*$1); } INITIALIZER : T_INT_CONST { ; } diff --git a/c_parser/src/parser_main.cpp b/c_parser/src/parser_main.cpp index 02f3b3f..9626334 100644 --- a/c_parser/src/parser_main.cpp +++ b/c_parser/src/parser_main.cpp @@ -5,9 +5,11 @@ int main(int argc, char *argv[]) { ast_Top *ast = parseAST(); + std::cout << "<?xml version=\"1.0\"?>" << std::endl << "<Program>" << std::endl; + ast->print(); - std::cout << std::endl; + std::cout << "</Program>" << std::endl; return 0; } diff --git a/c_parser/test/output.xml b/c_parser/test/output.xml new file mode 100644 index 0000000..852ed49 --- /dev/null +++ b/c_parser/test/output.xml @@ -0,0 +1,13 @@ +<?xml version="1.0"?> +<Program> +<Variable id="a" /> +<Variable id="b" /> +<Variable id="c" /> +<Variable id="yann" /> +<Variable id="is" /> +<Variable id="the" /> +<Variable id="best" /> +<Variable id="d" /> +<Variable id="hello" /> +<Variable id="asd" /> +</Program> diff --git a/c_parser/test/test_parser.c b/c_parser/test/test_parser.c index a685aff..557fd56 100644 --- a/c_parser/test/test_parser.c +++ b/c_parser/test/test_parser.c @@ -2,4 +2,5 @@ int a; int b; int c; int yann, is, the, best; -int hello = 0; +int d = 0; +int hello = 123, asd = 123; |