From c85746b6a6c3080bac409e0acb8cc8b332b2761e Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Fri, 17 Feb 2017 17:27:38 +0000 Subject: Finished compound statement for function and added test cases --- c_parser/include/ast_declaration.hpp | 21 ++++++++++++++++---- c_parser/include/ast_function.hpp | 5 ++++- c_parser/include/ast_primitives.hpp | 10 ++++++++-- c_parser/include/ast_statement.hpp | 2 ++ c_parser/src/c_parser.y | 31 +++++++++++++++++------------- c_parser/test/in/01.c | 6 ++++++ c_parser/test/in/02.c | 1 + c_parser/test/in/03.c | 1 + c_parser/test/in/04.c | 4 ++++ c_parser/test/out/01.diff.txt | 0 c_parser/test/out/01.stderr.txt | 0 c_parser/test/out/01.stdout.txt | 9 +++++++++ c_parser/test/out/02.diff.txt | 8 ++++++++ c_parser/test/out/02.stderr.txt | 1 + c_parser/test/out/02.stdout.txt | 0 c_parser/test/out/03.diff.txt | 0 c_parser/test/out/03.stderr.txt | 0 c_parser/test/out/03.stdout.txt | 8 ++++++++ c_parser/test/out/04.diff.txt | 0 c_parser/test/out/04.stderr.txt | 0 c_parser/test/out/04.stdout.txt | 12 ++++++++++++ c_parser/test/output.xml | 37 ------------------------------------ c_parser/test/ref/01.stdout.txt | 9 +++++++++ c_parser/test/ref/02.stdout.txt | 7 +++++++ c_parser/test/ref/03.stdout.txt | 8 ++++++++ c_parser/test/ref/04.stdout.txt | 12 ++++++++++++ c_parser/test/test_parser.c | 16 ---------------- test_parser.sh | 29 +++++++++++++++++++++++++--- 28 files changed, 161 insertions(+), 76 deletions(-) create mode 100644 c_parser/test/in/01.c create mode 100644 c_parser/test/in/02.c create mode 100644 c_parser/test/in/03.c create mode 100644 c_parser/test/in/04.c create mode 100644 c_parser/test/out/01.diff.txt create mode 100644 c_parser/test/out/01.stderr.txt create mode 100644 c_parser/test/out/01.stdout.txt create mode 100644 c_parser/test/out/02.diff.txt create mode 100644 c_parser/test/out/02.stderr.txt create mode 100644 c_parser/test/out/02.stdout.txt create mode 100644 c_parser/test/out/03.diff.txt create mode 100644 c_parser/test/out/03.stderr.txt create mode 100644 c_parser/test/out/03.stdout.txt create mode 100644 c_parser/test/out/04.diff.txt create mode 100644 c_parser/test/out/04.stderr.txt create mode 100644 c_parser/test/out/04.stdout.txt delete mode 100644 c_parser/test/output.xml create mode 100644 c_parser/test/ref/01.stdout.txt create mode 100644 c_parser/test/ref/02.stdout.txt create mode 100644 c_parser/test/ref/03.stdout.txt create mode 100644 c_parser/test/ref/04.stdout.txt delete mode 100644 c_parser/test/test_parser.c 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 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 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 << "" << 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 << "" << 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 << "" << 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 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 EXT_DEF EXT_DECLARATION EXT_DECLARATION_2 +%type EXT_DEF EXT_DECLARATION %type FUNC_DEF PARAMETER_LIST PARAMETER PARAM_DECLARATOR -%type DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER -%type COMPOUND_STATEMENT +%type DECLARATION_LIST DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER +%type COMPOUND_STATEMENT COMPOUND_STATEMENT_2 // %type // T_CONSTANT %type 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 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 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 @@ + + + + + + + + + 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 +< +< +< +< +< +< +< 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 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 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 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 @@ + + + + + + + + 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 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 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 @@ + + + + + + + + + + + + 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 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 @@ + + + + + + + + + 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 @@ + + + + + + + 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 @@ + + + + + + + + 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 @@ + + + + + + + + + + + + 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; -} diff --git a/test_parser.sh b/test_parser.sh index 5f8e606..96644c2 100755 --- a/test_parser.sh +++ b/test_parser.sh @@ -9,10 +9,33 @@ echo " Force building lexer" make -B bin/c_parser if [[ "$?" -ne 0 ]]; then - echo "Build failed."; + echo "Build failed."; fi +echo "" +echo "" echo "=========================================" -echo " Testing lexer" +echo " Testing parser" -cat c_parser/test/test_parser.c | ./bin/c_parser | tee c_parser/test/output.xml +PASSED=0 +CHECKED=0 + +for i in c_parser/test/in/*.c; do + echo "===========================" + echo "" + echo "Input file : ${i}" + BASENAME=$(basename $i .c); + cat $i | ./bin/c_parser > c_parser/test/out/$BASENAME.stdout.txt 2> c_parser/test/out/$BASENAME.stderr.txt + + diff <(cat c_parser/test/ref/$BASENAME.stdout.txt) <(cat c_parser/test/out/$BASENAME.stdout.txt) > c_parser/test/out/$BASENAME.diff.txt + if [[ "$?" -ne "0" ]]; then + echo -e "\nERROR" + else + PASSED=$(( ${PASSED}+1 )); + fi + CHECKED=$(( ${CHECKED}+1 )); +done + +echo "########################################" +echo "Passed ${PASSED} out of ${CHECKED}". +echo "" -- cgit