From 0e9e8dab385375c0cbb83d89a71fbf8d71206b47 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 19 Feb 2017 05:06:45 +0000 Subject: Finished if statement for the most part --- c_parser/include/ast.hpp | 1 + c_parser/include/ast_declaration.hpp | 2 +- c_parser/include/ast_expression.hpp | 30 ++++++++++++++++++++++++++++++ c_parser/include/ast_statement.hpp | 14 ++++++++++++++ c_parser/src/c_lexer.flex | 5 +++-- c_parser/src/c_parser.y | 28 +++++++++++++++++++--------- c_parser/test/in/07.c | 6 ++++-- c_parser/test/in/08.c | 10 ++++++++++ c_parser/test/in/09.c | 15 +++++++++++++++ c_parser/test/out/07.diff.txt | 7 ------- c_parser/test/out/07.stderr.txt | 1 - c_parser/test/out/07.stdout.xml | 11 +++++++++++ c_parser/test/out/08.diff.txt | 0 c_parser/test/out/08.stderr.txt | 0 c_parser/test/out/08.stdout.xml | 16 ++++++++++++++++ c_parser/test/out/09.diff.txt | 0 c_parser/test/out/09.stderr.txt | 0 c_parser/test/out/09.stdout.xml | 21 +++++++++++++++++++++ c_parser/test/ref/07.stdout.xml | 5 +++++ c_parser/test/ref/08.stdout.xml | 16 ++++++++++++++++ c_parser/test/ref/09.stdout.xml | 21 +++++++++++++++++++++ 21 files changed, 187 insertions(+), 22 deletions(-) create mode 100644 c_parser/include/ast_expression.hpp create mode 100644 c_parser/test/in/08.c create mode 100644 c_parser/test/in/09.c create mode 100644 c_parser/test/out/08.diff.txt create mode 100644 c_parser/test/out/08.stderr.txt create mode 100644 c_parser/test/out/08.stdout.xml create mode 100644 c_parser/test/out/09.diff.txt create mode 100644 c_parser/test/out/09.stderr.txt create mode 100644 c_parser/test/out/09.stdout.xml create mode 100644 c_parser/test/ref/08.stdout.xml create mode 100644 c_parser/test/ref/09.stdout.xml diff --git a/c_parser/include/ast.hpp b/c_parser/include/ast.hpp index 787e928..b73506f 100644 --- a/c_parser/include/ast.hpp +++ b/c_parser/include/ast.hpp @@ -5,6 +5,7 @@ #include "ast_statement.hpp" #include "ast_function.hpp" #include "ast_declaration.hpp" +#include "ast_expression.hpp" #include "ast_primitives.hpp" #include "ast_top.hpp" diff --git a/c_parser/include/ast_declaration.hpp b/c_parser/include/ast_declaration.hpp index 72bd375..7bc8177 100644 --- a/c_parser/include/ast_declaration.hpp +++ b/c_parser/include/ast_declaration.hpp @@ -15,7 +15,7 @@ public: 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(); diff --git a/c_parser/include/ast_expression.hpp b/c_parser/include/ast_expression.hpp new file mode 100644 index 0000000..2e223f4 --- /dev/null +++ b/c_parser/include/ast_expression.hpp @@ -0,0 +1,30 @@ +#ifndef AST_EXPRESSION_HPP +#define AST_EXPRESSION_HPP + +#include "ast.hpp" + +#include +#include + +class ast_Expression : public ast_Base { +private: + std::string id; +public: + ast_Expression(const std::string& _id) : id(_id) {} + + virtual void print() const { + + } + + virtual void push(const ast_Base* _base) const { + std::cerr << "Can't call this function for this type" << std::endl; + (void)_base; + } +}; + +class ast_ReturnExpression : public ast_Expression { +public: + ast_ReturnExpression(const std::string& _id) : ast_Expression(_id) {} +}; + +#endif diff --git a/c_parser/include/ast_statement.hpp b/c_parser/include/ast_statement.hpp index d3f6e96..e9864e9 100644 --- a/c_parser/include/ast_statement.hpp +++ b/c_parser/include/ast_statement.hpp @@ -63,4 +63,18 @@ public: } }; +class ast_SelectionStatement : public ast_Statement { +public: + ast_SelectionStatement() : ast_Statement() {} + ast_SelectionStatement(const ast_Base* _el) : ast_Statement(_el) {} + ast_SelectionStatement(const ast_Base* _if, const ast_Base* _else) : + ast_Statement(_if, _else) {} + + virtual void print() const override { + for(size_t i = 0; i < ast_list.size(); ++i) { + ast_list[i]->print(); + } + } +}; + #endif diff --git a/c_parser/src/c_lexer.flex b/c_parser/src/c_lexer.flex index 5723e38..32ef4fb 100644 --- a/c_parser/src/c_lexer.flex +++ b/c_parser/src/c_lexer.flex @@ -40,8 +40,6 @@ typedef|extern|static|auto|register { return T_STRG_SPEC; } void|char|short|int|long|float|double|signed|unsigned { return T_TYPE_SPEC; } const|volatile { return T_TYPE_QUAL; } -{IDENTIFIER} { yylval.string = new std::string(yytext); return T_IDENTIFIER; } - [;] { return T_SC; } [=] { return T_EQ; } [=][=] { return T_EQUALITY; } @@ -53,6 +51,9 @@ const|volatile { return T_TYPE_QUAL; } if { return T_IF; } else { return T_ELSE; } +return { return T_RETURN; } + +{IDENTIFIER} { yylval.string = new std::string(yytext); return T_IDENTIFIER; } ({HEXCONSTANT}|{OCTALCONSTANT}|{DECIMALCONSTANT}){INTEGERSUFFIX}? { return T_INT_CONST; } diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y index 9c65dfd..5b3af36 100644 --- a/c_parser/src/c_parser.y +++ b/c_parser/src/c_parser.y @@ -20,15 +20,15 @@ void yyerror(const char *); } %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 T_EQUALITY +%token T_SC T_CMA T_EQ T_LRB T_RRB T_LCB T_RCB T_EQUALITY T_RETURN %token T_INT_CONST %token T_IF T_ELSE %type EXT_DEF EXT_DECLARATION %type FUNC_DEF PARAMETER_LIST PARAMETER PARAM_DECLARATOR %type DECLARATION_LIST DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR INITIALIZER -%type STATEMENT_LIST STATEMENT COMPOUND_STATEMENT COMPOUND_STATEMENT_2 SELECTION_STATEMENT -%type EXPRESSION EQUALITY_EXPRESSION +%type STATEMENT_LIST STATEMENT COMPOUND_STATEMENT COMPOUND_STATEMENT_2 SELECTION_STATEMENT SELECTION_STATEMENT_2 EXPRESSION_STATEMENT +%type EXPRESSION EQUALITY_EXPRESSION RETURN_EXPRESSION // %type // T_CONSTANT %type T_IDENTIFIER //T_OPERATOR @@ -103,7 +103,8 @@ STATEMENT_LIST : STATEMENT { $$ = new ast_StatementList($1); } ; STATEMENT : COMPOUND_STATEMENT { $$ = $1; } - | SELECTION_STATEMENT { ; } + | SELECTION_STATEMENT { $$ = $1; } + | EXPRESSION_STATEMENT { $$ = $1; } ; COMPOUND_STATEMENT : T_LCB COMPOUND_STATEMENT_2 { $$ = $2; } @@ -115,17 +116,26 @@ COMPOUND_STATEMENT_2 : T_RCB { $$ = new ast_CompoundStatement; } | STATEMENT_LIST T_RCB { $$ = new ast_CompoundStatement($1); } ; -SELECTION_STATEMENT : T_IF T_LRB EXPRESSION T_RRB STATEMENT SELECTION_STATEMENT_2 { ; } +SELECTION_STATEMENT : T_IF T_LRB EXPRESSION T_RRB STATEMENT SELECTION_STATEMENT_2 { $$ = new ast_SelectionStatement($5, $6); } +; + +SELECTION_STATEMENT_2 : %empty { $$ = new ast_SelectionStatement(); } + | T_ELSE STATEMENT { $$ = $2; } +; -SELECTION_STATEMENT_2 : %empty { ; } - | T_ELSE STATEMENT { ; } +EXPRESSION_STATEMENT : EXPRESSION T_SC { $$ = $1; } +; // Expressions -EXPRESSION : EQUALITY_EXPRESSION { ; } +EXPRESSION : EQUALITY_EXPRESSION { $$ = $1; } + | RETURN_EXPRESSION { $$ = $1; } +; + +EQUALITY_EXPRESSION : T_IDENTIFIER T_EQUALITY T_IDENTIFIER { $$ = new ast_Variable(*$1); } ; -EQUALITY_EXPRESSION : T_IDENTIFIER T_EQUALITY T_IDENTIFIER { ; } +RETURN_EXPRESSION : T_RETURN T_IDENTIFIER { $$ = new ast_ReturnExpression(*$2); } ; %% diff --git a/c_parser/test/in/07.c b/c_parser/test/in/07.c index baa3ba5..6abb5b5 100644 --- a/c_parser/test/in/07.c +++ b/c_parser/test/in/07.c @@ -1,3 +1,5 @@ -if(x == y) { - int z; +int foo(int a) { + if(x == y) { + int z; + } } diff --git a/c_parser/test/in/08.c b/c_parser/test/in/08.c new file mode 100644 index 0000000..3d8e983 --- /dev/null +++ b/c_parser/test/in/08.c @@ -0,0 +1,10 @@ +int func(int a, int b) { + int c = 0; + if(a == b) { + int d; + } else if(a == d) { + int e; + return d; + } + return c; +} diff --git a/c_parser/test/in/09.c b/c_parser/test/in/09.c new file mode 100644 index 0000000..08a7ee4 --- /dev/null +++ b/c_parser/test/in/09.c @@ -0,0 +1,15 @@ +int x() +{} + +int g; + +int zz(int a, int b, int c) +{ + if(a==b){ + int a; + return a; + }else{ + int fsdfsdfs; + return c; + } +} diff --git a/c_parser/test/out/07.diff.txt b/c_parser/test/out/07.diff.txt index 292a596..e69de29 100644 --- a/c_parser/test/out/07.diff.txt +++ b/c_parser/test/out/07.diff.txt @@ -1,7 +0,0 @@ -1,6d0 -< -< -< -< -< -< diff --git a/c_parser/test/out/07.stderr.txt b/c_parser/test/out/07.stderr.txt index 6200509..e69de29 100644 --- a/c_parser/test/out/07.stderr.txt +++ b/c_parser/test/out/07.stderr.txt @@ -1 +0,0 @@ -Parse error : syntax error diff --git a/c_parser/test/out/07.stdout.xml b/c_parser/test/out/07.stdout.xml index e69de29..ff0efb2 100644 --- a/c_parser/test/out/07.stdout.xml +++ b/c_parser/test/out/07.stdout.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/c_parser/test/out/08.diff.txt b/c_parser/test/out/08.diff.txt new file mode 100644 index 0000000..e69de29 diff --git a/c_parser/test/out/08.stderr.txt b/c_parser/test/out/08.stderr.txt new file mode 100644 index 0000000..e69de29 diff --git a/c_parser/test/out/08.stdout.xml b/c_parser/test/out/08.stdout.xml new file mode 100644 index 0000000..7929e75 --- /dev/null +++ b/c_parser/test/out/08.stdout.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/c_parser/test/out/09.diff.txt b/c_parser/test/out/09.diff.txt new file mode 100644 index 0000000..e69de29 diff --git a/c_parser/test/out/09.stderr.txt b/c_parser/test/out/09.stderr.txt new file mode 100644 index 0000000..e69de29 diff --git a/c_parser/test/out/09.stdout.xml b/c_parser/test/out/09.stdout.xml new file mode 100644 index 0000000..7a7a770 --- /dev/null +++ b/c_parser/test/out/09.stdout.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/c_parser/test/ref/07.stdout.xml b/c_parser/test/ref/07.stdout.xml index baedd20..ff0efb2 100644 --- a/c_parser/test/ref/07.stdout.xml +++ b/c_parser/test/ref/07.stdout.xml @@ -1,6 +1,11 @@ + + + + + diff --git a/c_parser/test/ref/08.stdout.xml b/c_parser/test/ref/08.stdout.xml new file mode 100644 index 0000000..7929e75 --- /dev/null +++ b/c_parser/test/ref/08.stdout.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/c_parser/test/ref/09.stdout.xml b/c_parser/test/ref/09.stdout.xml new file mode 100644 index 0000000..7a7a770 --- /dev/null +++ b/c_parser/test/ref/09.stdout.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + -- cgit