From 392805699c8d411400901b8e3d7298f0f9198bb5 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 16 Feb 2017 22:52:40 +0000 Subject: Changing ast and improving it --- c_parser/include/ast_declaration.hpp | 18 ++++++++++++------ c_parser/include/ast_primitives.hpp | 19 +++++++++++++++++++ c_parser/src/c_lexer.flex | 6 +++--- c_parser/src/c_parser.y | 20 ++++++++++++-------- c_parser/test/test_parser.c | 5 +++++ 5 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 c_parser/include/ast_primitives.hpp (limited to 'c_parser') diff --git a/c_parser/include/ast_declaration.hpp b/c_parser/include/ast_declaration.hpp index e529378..98fe255 100644 --- a/c_parser/include/ast_declaration.hpp +++ b/c_parser/include/ast_declaration.hpp @@ -5,15 +5,21 @@ class ast_Declaration : public ast_Base { private: - const std::string id; -protected: +public: + virtual void print() const override = 0; +}; + +class ast_VariableDeclaration : public ast_Declartaion { +private: + const std::string id; + const std::string type; public: - ast_Declaration(const std::string _id) : id(_id) {} - virtual void print() const override { - std::cout << id; - } + ast_VariableDeclaration(const std::string& _id, const std::string& _type) : + id(_id), type(_type) {} + + virtual void print() const override {} }; #endif diff --git a/c_parser/include/ast_primitives.hpp b/c_parser/include/ast_primitives.hpp new file mode 100644 index 0000000..fbcb1b4 --- /dev/null +++ b/c_parser/include/ast_primitives.hpp @@ -0,0 +1,19 @@ +#ifndef AST_PRIMITIVES_HPP +#define AST_PRIMITIVES_HPP + +#include "ast.hpp" + +#include + +class ast_Variable : public ast_Base { +private: + std::string id; +public: + ast_Variable(const std::string& _id) : id(_id) {} + + virtual void print() const override { + std::cout << "" << std::endl; + } +}; + +#endif diff --git a/c_parser/src/c_lexer.flex b/c_parser/src/c_lexer.flex index ba19cd5..59a48e7 100644 --- a/c_parser/src/c_lexer.flex +++ b/c_parser/src/c_lexer.flex @@ -42,9 +42,9 @@ const|volatile { return T_TYPE_QUAL; } {IDENTIFIER} { yylval.string = new std::string(yytext); return T_IDENTIFIER; } -; { return T_SC; } -= { return T_EQ; } -, { return T_CMA; } +[;] { return T_SC; } +[=] { return T_EQ; } +[,] { return T_CMA; } [(] { return T_LRB; } [)] { return T_RRB; } [{] { return T_LCB; } diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y index 97d13a7..5429f76 100644 --- a/c_parser/src/c_parser.y +++ b/c_parser/src/c_parser.y @@ -19,9 +19,13 @@ void yyerror(const char *); std::string *string; } -%token T_TYPE_SPEC T_TYPE_QUAL T_STRG_SPEC T_IDENTIFIER T_SC T_CMA T_EQ T_LRB T_RRB T_LCB T_RCB T_INT_CONST +%token T_TYPE_SPEC T_TYPE_QUAL T_STRG_SPEC T_IDENTIFIER +%token T_SC T_CMA T_EQ T_LRB T_RRB T_LCB T_RCB +%token T_INT_CONST -%type EXT_DEF EXT_DECLARATION FUNC_DEF DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER +%type EXT_DEF EXT_DECLARATION EXT_DECLARATION_2 +%type FUNC_DEF +%type DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER // %type // T_CONSTANT %type T_IDENTIFIER //T_OPERATOR @@ -36,20 +40,20 @@ ROOT : EXT_DEF { ; } EXT_DEF : EXT_DECLARATION { ; } | EXT_DEF EXT_DECLARATION { $$ = $2; } -EXT_DECLARATION : DECLARATION_SPEC EXT_DECLARATION_2 { ; } +EXT_DECLARATION : DECLARATION_SPEC EXT_DECLARATION_2 { $$ = $2; } EXT_DECLARATION_2 : DECLARATION { ; } | FUNC_DEF { ; } // FUNCTION DEFINITION -FUNC_DEF : T_IDENTIFIER T_LRB T_RRB T_LCB T_RCB { ; } +FUNC_DEF : T_IDENTIFIER T_LRB T_RRB T_LCB T_RCB { ; } // DECLARATION DECLARATION : INIT_DECLARATOR_LIST T_SC { ; } -DECLARATION_SPEC : DECLARATION_SPEC_T { ; } +DECLARATION_SPEC : DECLARATION_SPEC_T { ; } | DECLARATION_SPEC_T DECLARATION_SPEC { ; } DECLARATION_SPEC_T : T_TYPE_SPEC { ; } @@ -59,12 +63,12 @@ DECLARATION_SPEC_T : T_TYPE_SPEC { ; } INIT_DECLARATOR_LIST : INIT_DECLARATOR { g_root->push($1); } | INIT_DECLARATOR_LIST T_CMA INIT_DECLARATOR { g_root->push($3); } -INIT_DECLARATOR : DECLARATOR { ; } +INIT_DECLARATOR : DECLARATOR { ; } | DECLARATOR T_EQ INITIALIZER { ; } -DECLARATOR : T_IDENTIFIER {$$ = new ast_Declaration(*$1); } +DECLARATOR : T_IDENTIFIER {$$ = new ast_Declaration(*$1); } -INITIALIZER : T_INT_CONST { ; } +INITIALIZER : T_INT_CONST { ; } // STATEMENTS diff --git a/c_parser/test/test_parser.c b/c_parser/test/test_parser.c index e69de29..a685aff 100644 --- a/c_parser/test/test_parser.c +++ b/c_parser/test/test_parser.c @@ -0,0 +1,5 @@ +int a; +int b; +int c; +int yann, is, the, best; +int hello = 0; -- cgit