diff options
Diffstat (limited to 'c_parser')
44 files changed, 280 insertions, 76 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..085957c 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 { @@ -30,6 +33,8 @@ private: mutable std::vector<const ast_Base*> param_list; public: + ast_ParamList() {} + ast_ParamList(const ast_Base* param) { param_list.push_back(param); } 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..d3f6e96 100644 --- a/c_parser/include/ast_statement.hpp +++ b/c_parser/include/ast_statement.hpp @@ -1,15 +1,41 @@ #ifndef AST_STATEMENT_HPP #define AST_STATEMENT_HPP +class ast_StatementList : public ast_Base { +protected: + mutable std::vector<const ast_Base*> statement_list; + +public: + ast_StatementList(const ast_Base* _statement) { + statement_list.push_back(_statement); + } + + virtual void print() const { + for(size_t i = 0; i < statement_list.size(); ++i) { + statement_list[i]->print(); + } + } + + virtual void push(const ast_Base* _statement) const { + statement_list.push_back(_statement); + } +}; + class ast_Statement : public ast_Base { protected: mutable std::vector<const ast_Base*> ast_list; public: + ast_Statement() {} + ast_Statement(const ast_Base* _el) { ast_list.push_back(_el); } + ast_Statement(const ast_Base* _dec, const ast_Base* _statement) { + ast_list.push_back(_dec); + ast_list.push_back(_statement); + } virtual void print() const { for(size_t i = 0; i < ast_list.size(); ++i) { ast_list[i]->print(); @@ -23,7 +49,10 @@ public: class ast_CompoundStatement : public ast_Statement { public: + ast_CompoundStatement() : ast_Statement() {} ast_CompoundStatement(const ast_Base* _el) : ast_Statement(_el) {} + ast_CompoundStatement(const ast_Base* _dec, const ast_Base* _statement) : + ast_Statement(_dec, _statement) {} virtual void print() const override { std::cout << "<Scope>" << std::endl; diff --git a/c_parser/src/c_lexer.flex b/c_parser/src/c_lexer.flex index 59a48e7..5723e38 100644 --- a/c_parser/src/c_lexer.flex +++ b/c_parser/src/c_lexer.flex @@ -44,11 +44,15 @@ const|volatile { return T_TYPE_QUAL; } [;] { return T_SC; } [=] { return T_EQ; } +[=][=] { return T_EQUALITY; } [,] { return T_CMA; } [(] { return T_LRB; } [)] { return T_RRB; } -[{] { return T_LCB; } -[}] { return T_RCB; } +[{] { return T_LCB; } +[}] { return T_RCB; } + +if { return T_IF; } +else { return T_ELSE; } ({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 13f3d13..9c65dfd 100644 --- a/c_parser/src/c_parser.y +++ b/c_parser/src/c_parser.y @@ -20,13 +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 +%token T_SC T_CMA T_EQ T_LRB T_RRB T_LCB T_RCB T_EQUALITY %token T_INT_CONST +%token T_IF T_ELSE -%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> STATEMENT_LIST STATEMENT COMPOUND_STATEMENT COMPOUND_STATEMENT_2 SELECTION_STATEMENT +%type <stmnt> EXPRESSION EQUALITY_EXPRESSION // %type <number> // T_CONSTANT %type <string> T_IDENTIFIER //T_OPERATOR @@ -43,30 +45,32 @@ 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 : %empty { $$ = new ast_ParamList(); } + | 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 +98,34 @@ INITIALIZER : T_INT_CONST { ; } // STATEMENT -COMPOUND_STATEMENT: T_LCB EXT_DEF T_RCB { $$ = new ast_CompoundStatement($2); } +STATEMENT_LIST : STATEMENT { $$ = new ast_StatementList($1); } + | STATEMENT_LIST STATEMENT { $$->push($2); } +; + +STATEMENT : COMPOUND_STATEMENT { $$ = $1; } + | SELECTION_STATEMENT { ; } +; + +COMPOUND_STATEMENT : T_LCB COMPOUND_STATEMENT_2 { $$ = $2; } +; + +COMPOUND_STATEMENT_2 : T_RCB { $$ = new ast_CompoundStatement; } + | DECLARATION_LIST T_RCB { $$ = new ast_CompoundStatement($1); } + | DECLARATION_LIST STATEMENT_LIST T_RCB { $$ = new ast_CompoundStatement($1, $2); } + | STATEMENT_LIST T_RCB { $$ = new ast_CompoundStatement($1); } +; + +SELECTION_STATEMENT : T_IF T_LRB EXPRESSION T_RRB STATEMENT SELECTION_STATEMENT_2 { ; } + +SELECTION_STATEMENT_2 : %empty { ; } + | T_ELSE STATEMENT { ; } + +// Expressions + +EXPRESSION : EQUALITY_EXPRESSION { ; } +; + +EQUALITY_EXPRESSION : T_IDENTIFIER T_EQUALITY T_IDENTIFIER { ; } ; %% 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/in/05.c b/c_parser/test/in/05.c new file mode 100644 index 0000000..a5ac579 --- /dev/null +++ b/c_parser/test/in/05.c @@ -0,0 +1,3 @@ +int foo(int x, int y) { + {} +} diff --git a/c_parser/test/in/06.c b/c_parser/test/in/06.c new file mode 100644 index 0000000..f182bd9 --- /dev/null +++ b/c_parser/test/in/06.c @@ -0,0 +1,6 @@ +int f(int a, int b, int c) { + int d; + { + int e; + } +} diff --git a/c_parser/test/in/07.c b/c_parser/test/in/07.c new file mode 100644 index 0000000..baa3ba5 --- /dev/null +++ b/c_parser/test/in/07.c @@ -0,0 +1,3 @@ +if(x == y) { + int 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/01.stdout.xml b/c_parser/test/out/01.stdout.xml new file mode 100644 index 0000000..bd7cd1f --- /dev/null +++ b/c_parser/test/out/01.stdout.xml @@ -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..e69de29 --- /dev/null +++ b/c_parser/test/out/02.diff.txt diff --git a/c_parser/test/out/02.stderr.txt b/c_parser/test/out/02.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/02.stderr.txt diff --git a/c_parser/test/out/02.stdout.xml b/c_parser/test/out/02.stdout.xml new file mode 100644 index 0000000..bc37d7a --- /dev/null +++ b/c_parser/test/out/02.stdout.xml @@ -0,0 +1,7 @@ +<?xml version="1.0"?> +<Program> +<Function id="f"> +<Scope> +</Scope> +</Function> +</Program> 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.xml b/c_parser/test/out/03.stdout.xml new file mode 100644 index 0000000..6ca0ab2 --- /dev/null +++ b/c_parser/test/out/03.stdout.xml @@ -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.xml b/c_parser/test/out/04.stdout.xml new file mode 100644 index 0000000..9257eac --- /dev/null +++ b/c_parser/test/out/04.stdout.xml @@ -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/out/05.diff.txt b/c_parser/test/out/05.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/05.diff.txt diff --git a/c_parser/test/out/05.stderr.txt b/c_parser/test/out/05.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/05.stderr.txt diff --git a/c_parser/test/out/05.stdout.xml b/c_parser/test/out/05.stdout.xml new file mode 100644 index 0000000..7381e51 --- /dev/null +++ b/c_parser/test/out/05.stdout.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> +<Function id="foo"> +<Parameter id="x" /> +<Parameter id="y" /> +<Scope> +<Scope> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/06.diff.txt b/c_parser/test/out/06.diff.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/06.diff.txt diff --git a/c_parser/test/out/06.stderr.txt b/c_parser/test/out/06.stderr.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/06.stderr.txt diff --git a/c_parser/test/out/06.stdout.xml b/c_parser/test/out/06.stdout.xml new file mode 100644 index 0000000..fbcbebd --- /dev/null +++ b/c_parser/test/out/06.stdout.xml @@ -0,0 +1,14 @@ +<?xml version="1.0"?> +<Program> +<Function id="f"> +<Parameter id="a" /> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Variable id="d" /> +<Scope> +<Variable id="e" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/out/07.diff.txt b/c_parser/test/out/07.diff.txt new file mode 100644 index 0000000..292a596 --- /dev/null +++ b/c_parser/test/out/07.diff.txt @@ -0,0 +1,7 @@ +1,6d0 +< <?xml version="1.0"?> +< <Program> +< <Scope> +< <Variable id="z" /> +< </Scope> +< </Program> diff --git a/c_parser/test/out/07.stderr.txt b/c_parser/test/out/07.stderr.txt new file mode 100644 index 0000000..6200509 --- /dev/null +++ b/c_parser/test/out/07.stderr.txt @@ -0,0 +1 @@ +Parse error : syntax error diff --git a/c_parser/test/out/07.stdout.xml b/c_parser/test/out/07.stdout.xml new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/c_parser/test/out/07.stdout.xml 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.xml b/c_parser/test/ref/01.stdout.xml new file mode 100644 index 0000000..bd7cd1f --- /dev/null +++ b/c_parser/test/ref/01.stdout.xml @@ -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.xml b/c_parser/test/ref/02.stdout.xml new file mode 100644 index 0000000..bc37d7a --- /dev/null +++ b/c_parser/test/ref/02.stdout.xml @@ -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.xml b/c_parser/test/ref/03.stdout.xml new file mode 100644 index 0000000..6ca0ab2 --- /dev/null +++ b/c_parser/test/ref/03.stdout.xml @@ -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.xml b/c_parser/test/ref/04.stdout.xml new file mode 100644 index 0000000..9257eac --- /dev/null +++ b/c_parser/test/ref/04.stdout.xml @@ -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/ref/05.stdout.xml b/c_parser/test/ref/05.stdout.xml new file mode 100644 index 0000000..7381e51 --- /dev/null +++ b/c_parser/test/ref/05.stdout.xml @@ -0,0 +1,11 @@ +<?xml version="1.0"?> +<Program> +<Function id="foo"> +<Parameter id="x" /> +<Parameter id="y" /> +<Scope> +<Scope> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/ref/06.stdout.xml b/c_parser/test/ref/06.stdout.xml new file mode 100644 index 0000000..fbcbebd --- /dev/null +++ b/c_parser/test/ref/06.stdout.xml @@ -0,0 +1,14 @@ +<?xml version="1.0"?> +<Program> +<Function id="f"> +<Parameter id="a" /> +<Parameter id="b" /> +<Parameter id="c" /> +<Scope> +<Variable id="d" /> +<Scope> +<Variable id="e" /> +</Scope> +</Scope> +</Function> +</Program> diff --git a/c_parser/test/ref/07.stdout.xml b/c_parser/test/ref/07.stdout.xml new file mode 100644 index 0000000..baedd20 --- /dev/null +++ b/c_parser/test/ref/07.stdout.xml @@ -0,0 +1,6 @@ +<?xml version="1.0"?> +<Program> +<Scope> +<Variable id="z" /> +</Scope> +</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; -} |