aboutsummaryrefslogtreecommitdiffstats
path: root/c_parser
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-02-17 17:27:38 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-02-17 17:27:38 +0000
commitc85746b6a6c3080bac409e0acb8cc8b332b2761e (patch)
tree7e37429cbe6014e2ed6aba6466b69cf914e2af38 /c_parser
parentb81f60d8b523260526af15361d4ce0ac4a7757c8 (diff)
downloadCompiler-c85746b6a6c3080bac409e0acb8cc8b332b2761e.tar.gz
Compiler-c85746b6a6c3080bac409e0acb8cc8b332b2761e.zip
Finished compound statement for function and added test cases
Diffstat (limited to 'c_parser')
-rw-r--r--c_parser/include/ast_declaration.hpp21
-rw-r--r--c_parser/include/ast_function.hpp5
-rw-r--r--c_parser/include/ast_primitives.hpp10
-rw-r--r--c_parser/include/ast_statement.hpp2
-rw-r--r--c_parser/src/c_parser.y31
-rw-r--r--c_parser/test/in/01.c6
-rw-r--r--c_parser/test/in/02.c1
-rw-r--r--c_parser/test/in/03.c1
-rw-r--r--c_parser/test/in/04.c4
-rw-r--r--c_parser/test/out/01.diff.txt0
-rw-r--r--c_parser/test/out/01.stderr.txt0
-rw-r--r--c_parser/test/out/01.stdout.txt9
-rw-r--r--c_parser/test/out/02.diff.txt8
-rw-r--r--c_parser/test/out/02.stderr.txt1
-rw-r--r--c_parser/test/out/02.stdout.txt0
-rw-r--r--c_parser/test/out/03.diff.txt0
-rw-r--r--c_parser/test/out/03.stderr.txt0
-rw-r--r--c_parser/test/out/03.stdout.txt8
-rw-r--r--c_parser/test/out/04.diff.txt0
-rw-r--r--c_parser/test/out/04.stderr.txt0
-rw-r--r--c_parser/test/out/04.stdout.txt12
-rw-r--r--c_parser/test/output.xml37
-rw-r--r--c_parser/test/ref/01.stdout.txt9
-rw-r--r--c_parser/test/ref/02.stdout.txt7
-rw-r--r--c_parser/test/ref/03.stdout.txt8
-rw-r--r--c_parser/test/ref/04.stdout.txt12
-rw-r--r--c_parser/test/test_parser.c16
27 files changed, 135 insertions, 73 deletions
diff --git a/c_parser/include/ast_declaration.hpp b/c_parser/include/ast_declaration.hpp
index 01b1498..72bd375 100644
--- a/c_parser/include/ast_declaration.hpp
+++ b/c_parser/include/ast_declaration.hpp
@@ -7,14 +7,27 @@
// Declaration that holds a list of declarations
-class ast_Declaration : public ast_Base {
+class ast_DeclarationList : public ast_Base {
private:
+ mutable std::vector<const ast_Base*> dec_list;
public:
- virtual void print() const = 0;
+ ast_DeclarationList(const ast_Base* _dec) {
+ dec_list.push_back(_dec);
+ }
+
+ virtual void print() const {
+ for(size_t i = 0; i < dec_list.size(); ++i) {
+ dec_list[i]->print();
+ }
+ }
+
+ virtual void push(const ast_Base* _dec) const {
+ dec_list.push_back(_dec);
+ }
};
-class ast_VariableDeclaration : public ast_Declaration {
+class ast_VariableDeclaration : public ast_Base {
private:
mutable std::vector<const ast_Base*> var_list;
@@ -24,7 +37,7 @@ public:
}
virtual void print() const {
- for(int i = 0; i < var_list.size(); ++i) {
+ for(size_t i = 0; i < var_list.size(); ++i) {
var_list[i]->print();
}
}
diff --git a/c_parser/include/ast_function.hpp b/c_parser/include/ast_function.hpp
index 86230d1..1086f49 100644
--- a/c_parser/include/ast_function.hpp
+++ b/c_parser/include/ast_function.hpp
@@ -22,7 +22,10 @@ public:
std::cout << "</Function>" << std::endl;
}
- virtual void push(const ast_Base* var) const {}
+ virtual void push(const ast_Base* var) const {
+ std::cerr << "Error: Can't call this function on this class" << std::endl;
+ (void)var;
+ }
};
class ast_ParamList : public ast_Base {
diff --git a/c_parser/include/ast_primitives.hpp b/c_parser/include/ast_primitives.hpp
index d878780..5ae6d12 100644
--- a/c_parser/include/ast_primitives.hpp
+++ b/c_parser/include/ast_primitives.hpp
@@ -15,7 +15,10 @@ public:
std::cout << "<Variable id=\"" << id << "\" />" << std::endl;
}
- virtual void push(const ast_Base* var) const {}
+ virtual void push(const ast_Base* var) const {
+ std::cerr << "Error: Can't call this function on this class" << std::endl;
+ (void)var;
+ }
};
class ast_Parameter : public ast_Base {
@@ -28,7 +31,10 @@ public:
std::cout << "<Parameter id=\"" << id << "\" />" << std::endl;
}
- virtual void push(const ast_Base* var) const {}
+ virtual void push(const ast_Base* var) const {
+ std::cerr << "Error: Can't call this function on this class" << std::endl;
+ (void)var;
+ }
};
#endif
diff --git a/c_parser/include/ast_statement.hpp b/c_parser/include/ast_statement.hpp
index 725308b..f2c7175 100644
--- a/c_parser/include/ast_statement.hpp
+++ b/c_parser/include/ast_statement.hpp
@@ -6,6 +6,7 @@ protected:
mutable std::vector<const ast_Base*> ast_list;
public:
+ ast_Statement() {}
ast_Statement(const ast_Base* _el) {
ast_list.push_back(_el);
}
@@ -23,6 +24,7 @@ public:
class ast_CompoundStatement : public ast_Statement {
public:
+ ast_CompoundStatement() : ast_Statement() {}
ast_CompoundStatement(const ast_Base* _el) : ast_Statement(_el) {}
virtual void print() const override {
diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y
index 13f3d13..6d5db38 100644
--- a/c_parser/src/c_parser.y
+++ b/c_parser/src/c_parser.y
@@ -23,10 +23,10 @@ void yyerror(const char *);
%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 EXT_DECLARATION_2
+%type <stmnt> EXT_DEF EXT_DECLARATION
%type <stmnt> FUNC_DEF PARAMETER_LIST PARAMETER PARAM_DECLARATOR
-%type <stmnt> DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER
-%type <stmnt> COMPOUND_STATEMENT
+%type <stmnt> DECLARATION_LIST DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER
+%type <stmnt> COMPOUND_STATEMENT COMPOUND_STATEMENT_2
// %type <number> // T_CONSTANT
%type <string> T_IDENTIFIER //T_OPERATOR
@@ -43,30 +43,31 @@ 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 { $$ = $1; }
+EXT_DECLARATION : DECLARATION { $$ = $1; }
| FUNC_DEF { $$ = $1; }
;
// FUNCTION DEFINITION
-FUNC_DEF : T_IDENTIFIER T_LRB PARAMETER_LIST T_RRB COMPOUND_STATEMENT { $$ = new ast_Function(*$1, $3, $5); }
+FUNC_DEF : DECLARATION_SPEC T_IDENTIFIER T_LRB PARAMETER_LIST T_RRB COMPOUND_STATEMENT { $$ = new ast_Function(*$2, $4, $6); }
;
-PARAMETER_LIST: PARAMETER { $$ = new ast_ParamList($1); }
+PARAMETER_LIST : PARAMETER { $$ = new ast_ParamList($1); }
| PARAMETER_LIST T_CMA PARAMETER { $$->push($3); }
;
-PARAMETER: DECLARATION_SPEC PARAM_DECLARATOR { $$ = $2; }
+PARAMETER : DECLARATION_SPEC PARAM_DECLARATOR { $$ = $2; }
;
-PARAM_DECLARATOR: T_IDENTIFIER { $$ = new ast_Parameter(*$1);}
+PARAM_DECLARATOR : T_IDENTIFIER { $$ = new ast_Parameter(*$1);}
+;
// DECLARATION
-DECLARATION : INIT_DECLARATOR_LIST T_SC { $$ = $1; }
+DECLARATION_LIST : DECLARATION { $$ = new ast_DeclarationList($1); }
+ | DECLARATION_LIST DECLARATION { $$->push($2); }
+
+DECLARATION : DECLARATION_SPEC INIT_DECLARATOR_LIST T_SC { $$ = $2; }
;
DECLARATION_SPEC : DECLARATION_SPEC_T { ; }
@@ -94,7 +95,11 @@ INITIALIZER : T_INT_CONST { ; }
// STATEMENT
-COMPOUND_STATEMENT: T_LCB EXT_DEF T_RCB { $$ = new ast_CompoundStatement($2); }
+COMPOUND_STATEMENT : T_LCB COMPOUND_STATEMENT_2 { $$ = $2; }
+;
+
+COMPOUND_STATEMENT_2 : T_RCB { $$ = new ast_CompoundStatement(); }
+ | DECLARATION_LIST T_RCB { $$ = new ast_CompoundStatement($1); }
;
%%
diff --git a/c_parser/test/in/01.c b/c_parser/test/in/01.c
new file mode 100644
index 0000000..76425b5
--- /dev/null
+++ b/c_parser/test/in/01.c
@@ -0,0 +1,6 @@
+int a;
+int b = 0;
+int c, d;
+
+int e,
+ f;
diff --git a/c_parser/test/in/02.c b/c_parser/test/in/02.c
new file mode 100644
index 0000000..45b1467
--- /dev/null
+++ b/c_parser/test/in/02.c
@@ -0,0 +1 @@
+int f() {}
diff --git a/c_parser/test/in/03.c b/c_parser/test/in/03.c
new file mode 100644
index 0000000..e57aaa9
--- /dev/null
+++ b/c_parser/test/in/03.c
@@ -0,0 +1 @@
+int foo(int bar) {}
diff --git a/c_parser/test/in/04.c b/c_parser/test/in/04.c
new file mode 100644
index 0000000..0e50c88
--- /dev/null
+++ b/c_parser/test/in/04.c
@@ -0,0 +1,4 @@
+int foo(int bar1, int bar2) {
+ int x;
+ int y, z;
+}
diff --git a/c_parser/test/out/01.diff.txt b/c_parser/test/out/01.diff.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/01.diff.txt
diff --git a/c_parser/test/out/01.stderr.txt b/c_parser/test/out/01.stderr.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/01.stderr.txt
diff --git a/c_parser/test/out/01.stdout.txt b/c_parser/test/out/01.stdout.txt
new file mode 100644
index 0000000..bd7cd1f
--- /dev/null
+++ b/c_parser/test/out/01.stdout.txt
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<Program>
+<Variable id="a" />
+<Variable id="b" />
+<Variable id="c" />
+<Variable id="d" />
+<Variable id="e" />
+<Variable id="f" />
+</Program>
diff --git a/c_parser/test/out/02.diff.txt b/c_parser/test/out/02.diff.txt
new file mode 100644
index 0000000..09321ae
--- /dev/null
+++ b/c_parser/test/out/02.diff.txt
@@ -0,0 +1,8 @@
+1,7d0
+< <?xml version="1.0"?>
+< <Program>
+< <Function id="f">
+< <Scope>
+< </Scope>
+< </Function>
+< </Program>
diff --git a/c_parser/test/out/02.stderr.txt b/c_parser/test/out/02.stderr.txt
new file mode 100644
index 0000000..6200509
--- /dev/null
+++ b/c_parser/test/out/02.stderr.txt
@@ -0,0 +1 @@
+Parse error : syntax error
diff --git a/c_parser/test/out/02.stdout.txt b/c_parser/test/out/02.stdout.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/02.stdout.txt
diff --git a/c_parser/test/out/03.diff.txt b/c_parser/test/out/03.diff.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/03.diff.txt
diff --git a/c_parser/test/out/03.stderr.txt b/c_parser/test/out/03.stderr.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/03.stderr.txt
diff --git a/c_parser/test/out/03.stdout.txt b/c_parser/test/out/03.stdout.txt
new file mode 100644
index 0000000..6ca0ab2
--- /dev/null
+++ b/c_parser/test/out/03.stdout.txt
@@ -0,0 +1,8 @@
+<?xml version="1.0"?>
+<Program>
+<Function id="foo">
+<Parameter id="bar" />
+<Scope>
+</Scope>
+</Function>
+</Program>
diff --git a/c_parser/test/out/04.diff.txt b/c_parser/test/out/04.diff.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/04.diff.txt
diff --git a/c_parser/test/out/04.stderr.txt b/c_parser/test/out/04.stderr.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/04.stderr.txt
diff --git a/c_parser/test/out/04.stdout.txt b/c_parser/test/out/04.stdout.txt
new file mode 100644
index 0000000..9257eac
--- /dev/null
+++ b/c_parser/test/out/04.stdout.txt
@@ -0,0 +1,12 @@
+<?xml version="1.0"?>
+<Program>
+<Function id="foo">
+<Parameter id="bar1" />
+<Parameter id="bar2" />
+<Scope>
+<Variable id="x" />
+<Variable id="y" />
+<Variable id="z" />
+</Scope>
+</Function>
+</Program>
diff --git a/c_parser/test/output.xml b/c_parser/test/output.xml
deleted file mode 100644
index d4594a2..0000000
--- a/c_parser/test/output.xml
+++ /dev/null
@@ -1,37 +0,0 @@
-<?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" />
-<Variable id="a" />
-<Function id="f">
-<Parameter id="i" />
-<Parameter id="b" />
-<Parameter id="c" />
-<Parameter id="d" />
-<Scope>
-<Variable id="a" />
-</Scope>
-</Function>
-<Variable id="a" />
-<Variable id="b" />
-<Variable id="c" />
-<Variable id="c" />
-<Variable id="d" />
-<Function id="func">
-<Parameter id="asd" />
-<Parameter id="b" />
-<Scope>
-<Variable id="a" />
-<Variable id="b" />
-<Variable id="c" />
-</Scope>
-</Function>
-</Program>
diff --git a/c_parser/test/ref/01.stdout.txt b/c_parser/test/ref/01.stdout.txt
new file mode 100644
index 0000000..bd7cd1f
--- /dev/null
+++ b/c_parser/test/ref/01.stdout.txt
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<Program>
+<Variable id="a" />
+<Variable id="b" />
+<Variable id="c" />
+<Variable id="d" />
+<Variable id="e" />
+<Variable id="f" />
+</Program>
diff --git a/c_parser/test/ref/02.stdout.txt b/c_parser/test/ref/02.stdout.txt
new file mode 100644
index 0000000..bc37d7a
--- /dev/null
+++ b/c_parser/test/ref/02.stdout.txt
@@ -0,0 +1,7 @@
+<?xml version="1.0"?>
+<Program>
+<Function id="f">
+<Scope>
+</Scope>
+</Function>
+</Program>
diff --git a/c_parser/test/ref/03.stdout.txt b/c_parser/test/ref/03.stdout.txt
new file mode 100644
index 0000000..6ca0ab2
--- /dev/null
+++ b/c_parser/test/ref/03.stdout.txt
@@ -0,0 +1,8 @@
+<?xml version="1.0"?>
+<Program>
+<Function id="foo">
+<Parameter id="bar" />
+<Scope>
+</Scope>
+</Function>
+</Program>
diff --git a/c_parser/test/ref/04.stdout.txt b/c_parser/test/ref/04.stdout.txt
new file mode 100644
index 0000000..9257eac
--- /dev/null
+++ b/c_parser/test/ref/04.stdout.txt
@@ -0,0 +1,12 @@
+<?xml version="1.0"?>
+<Program>
+<Function id="foo">
+<Parameter id="bar1" />
+<Parameter id="bar2" />
+<Scope>
+<Variable id="x" />
+<Variable id="y" />
+<Variable id="z" />
+</Scope>
+</Function>
+</Program>
diff --git a/c_parser/test/test_parser.c b/c_parser/test/test_parser.c
deleted file mode 100644
index 55a15b7..0000000
--- a/c_parser/test/test_parser.c
+++ /dev/null
@@ -1,16 +0,0 @@
-int a;
-int b;
-int c;
-int yann, is, the, best;
-int d = 0;
-int hello = 122, asd = 123;
-
-int f(int i, int b, int c, int d) {
- int a;
-}
-
-int func(int asd, int b) {
- int a, b, c;
- int c = 0;
- int d;
-}