aboutsummaryrefslogtreecommitdiffstats
path: root/c_parser
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-02-16 22:52:40 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-02-16 22:52:40 +0000
commit392805699c8d411400901b8e3d7298f0f9198bb5 (patch)
tree2d724780d9e787d7e17211ce6181d395aae1fcaf /c_parser
parent24d1547515ddeb03c6fcf7ae0162b0c397180a40 (diff)
downloadCompiler-392805699c8d411400901b8e3d7298f0f9198bb5.tar.gz
Compiler-392805699c8d411400901b8e3d7298f0f9198bb5.zip
Changing ast and improving it
Diffstat (limited to 'c_parser')
-rw-r--r--c_parser/include/ast_declaration.hpp18
-rw-r--r--c_parser/include/ast_primitives.hpp19
-rw-r--r--c_parser/src/c_lexer.flex6
-rw-r--r--c_parser/src/c_parser.y20
-rw-r--r--c_parser/test/test_parser.c5
5 files changed, 51 insertions, 17 deletions
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 <string>
+
+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 << "<Variable id=\"" << id << "\" />" << 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 <stmnt> EXT_DEF EXT_DECLARATION FUNC_DEF DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER
+%type <stmnt> EXT_DEF EXT_DECLARATION EXT_DECLARATION_2
+%type <stmnt> FUNC_DEF
+%type <stmnt> DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER
// %type <number> // T_CONSTANT
%type <string> 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;