aboutsummaryrefslogtreecommitdiffstats
path: root/c_parser
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-02-19 15:31:55 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-02-19 15:31:55 +0000
commitf820e632c9c985a938f32c4d3adb9bafb3b9393f (patch)
tree8da58b7b3e40f9a96f954b88970f45d5e95e98bc /c_parser
parent563fc043d2ed88cc72b825e3916287f8a67545c2 (diff)
downloadCompiler-f820e632c9c985a938f32c4d3adb9bafb3b9393f.tar.gz
Compiler-f820e632c9c985a938f32c4d3adb9bafb3b9393f.zip
Nearly all working perfectly
Diffstat (limited to 'c_parser')
-rw-r--r--c_parser/src/c_lexer.flex4
-rw-r--r--c_parser/src/c_parser.y26
-rw-r--r--c_parser/test/in/04.c4
-rw-r--r--c_parser/test/in/05.c2
-rw-r--r--c_parser/test/in/06.c8
-rw-r--r--c_parser/test/in/07.c6
-rw-r--r--c_parser/test/in/08.c16
-rw-r--r--c_parser/test/in/09.c14
-rw-r--r--c_parser/test/in/10.c22
-rw-r--r--c_parser/test/in/11.c16
-rw-r--r--c_parser/test/in/12.c4
-rw-r--r--c_parser/test/in/13.c8
-rw-r--r--c_parser/test/in/14.c12
-rw-r--r--c_parser/test/in/15.c28
-rw-r--r--c_parser/test/out/13.diff.txt0
-rw-r--r--c_parser/test/out/13.pretty.xml13
-rw-r--r--c_parser/test/out/13.stderr.txt0
-rw-r--r--c_parser/test/out/13.stdout.xml13
-rw-r--r--c_parser/test/out/14.diff.txt0
-rw-r--r--c_parser/test/out/14.pretty.xml13
-rw-r--r--c_parser/test/out/14.stderr.txt0
-rw-r--r--c_parser/test/out/14.stdout.xml13
-rw-r--r--c_parser/test/out/15.diff.txt0
-rw-r--r--c_parser/test/out/15.pretty.xml32
-rw-r--r--c_parser/test/out/15.stderr.txt0
-rw-r--r--c_parser/test/out/15.stdout.xml32
-rw-r--r--c_parser/test/ref/13.stdout.xml13
-rw-r--r--c_parser/test/ref/14.stdout.xml13
-rw-r--r--c_parser/test/ref/15.stdout.xml32
29 files changed, 281 insertions, 63 deletions
diff --git a/c_parser/src/c_lexer.flex b/c_parser/src/c_lexer.flex
index 63366cb..c8ca90a 100644
--- a/c_parser/src/c_lexer.flex
+++ b/c_parser/src/c_lexer.flex
@@ -70,9 +70,9 @@ const|volatile { return T_TYPE_QUAL; }
[-][>] { return T_ARROW; }
[+-][+-] { return T_INCDEC; }
[+-] { return T_ADDSUB_OP; }
-[=] { return T_EQ; }
+[=] { yylval.string = new std::string(yytext); return T_EQ; }
-{ASSIGNMENT_OPERATOR} { return T_ASSIGN_OPER; }
+{ASSIGNMENT_OPERATOR} { yylval.string = new std::string(yytext); return T_ASSIGN_OPER; }
if { return T_IF; }
else { return T_ELSE; }
diff --git a/c_parser/src/c_parser.y b/c_parser/src/c_parser.y
index b9b2f31..53108ad 100644
--- a/c_parser/src/c_parser.y
+++ b/c_parser/src/c_parser.y
@@ -26,11 +26,11 @@ void yyerror(const char *);
%type <stmnt> EXT_DEF EXT_DECLARATION
%type <stmnt> FUNC_DEF PARAMETER_LIST PARAMETER PARAM_DECLARATOR
-%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 SELECTION_STATEMENT_2 EXPRESSION_STATEMENT JUMP_STATEMENT JUMP_STATEMENT_2 ITERATION_STATEMENT
+%type <stmnt> DECLARATION_LIST DECLARATION DECLARATION_SPEC DECLARATION_SPEC_T INIT_DECLARATOR INIT_DECLARATOR_LIST DECLARATOR
+%type <stmnt> STATEMENT_LIST STATEMENT COMPOUND_STATEMENT COMPOUND_STATEMENT_2 SELECTION_STATEMENT SELECTION_STATEMENT_2 EXPRESSION_STATEMENT JUMP_STATEMENT ITERATION_STATEMENT
%type <stmnt> EXPRESSION ASSIGNMENT_EXPRESSION CONDITIONAL_EXPRESSION LOGICAL_OR_EXPRESSION LOGICAL_AND_EXPRESSION INCLUSIVE_OR_EXPRESSION EXCLUSIVE_OR_EXPRESSION AND_EXPRESSION EQUALITY_EXPRESSION RELATIONAL_EXPRESSION SHIFT_EXPRESSION ADDITIVE_EXPRESSION MULTIPLICATIVE_EXPRESSION CAST_EXPRESSION UNARY_EXPRESSION POSTFIX_EXPRESSION POSTFIX_EXPRESSION_2 ARGUMENT_EXPRESSION_LIST PRIMARY_EXPRESSION
%type <number> CONSTANT T_INT_CONST
-%type <string> T_IDENTIFIER MULTDIVREM_OP UNARY_OPERATOR T_AND T_ADDSUB_OP T_TILDE T_NOT T_MULT T_DIV T_REM //T_OPERATOR
+%type <string> T_IDENTIFIER MULTDIVREM_OP UNARY_OPERATOR ASSIGN_OPER T_ASSIGN_OPER T_EQ T_AND T_ADDSUB_OP T_TILDE T_NOT T_MULT T_DIV T_REM //T_OPERATOR
%start ROOT
@@ -101,17 +101,13 @@ INIT_DECLARATOR_LIST:
INIT_DECLARATOR:
DECLARATOR { ; }
- | DECLARATOR T_EQ INITIALIZER { ; }
+ | DECLARATOR T_EQ ASSIGNMENT_EXPRESSION { ; }
;
DECLARATOR:
T_IDENTIFIER {$$ = new ast_Variable(*$1); }
;
-INITIALIZER:
- ASSIGNMENT_EXPRESSION { ; }
- ;
-
// STATEMENT
STATEMENT_LIST:
@@ -153,12 +149,7 @@ EXPRESSION_STATEMENT:
;
JUMP_STATEMENT:
- T_RETURN JUMP_STATEMENT_2 { $$ = $2; }
- ;
-
-JUMP_STATEMENT_2:
- T_SC { $$ = new ast_JumpStatement(); }
- | EXPRESSION T_SC { $$ = $1; }
+ T_RETURN EXPRESSION_STATEMENT { $$ = $2; }
;
ITERATION_STATEMENT:
@@ -175,9 +166,14 @@ EXPRESSION:
ASSIGNMENT_EXPRESSION:
CONDITIONAL_EXPRESSION { $$ = $1; }
- | UNARY_EXPRESSION T_ASSIGN_OPER ASSIGNMENT_EXPRESSION { $$ = $1; }
+ | UNARY_EXPRESSION ASSIGN_OPER ASSIGNMENT_EXPRESSION { $$ = $1; }
;
+ASSIGN_OPER:
+ T_ASSIGN_OPER { ; }
+ | T_EQ { ; }
+ ;
+
CONDITIONAL_EXPRESSION:
LOGICAL_OR_EXPRESSION { $$ = $1; }
| LOGICAL_OR_EXPRESSION T_QU EXPRESSION T_COL CONDITIONAL_EXPRESSION { $$ = $1; }
diff --git a/c_parser/test/in/04.c b/c_parser/test/in/04.c
index 0e50c88..2e3aa5d 100644
--- a/c_parser/test/in/04.c
+++ b/c_parser/test/in/04.c
@@ -1,4 +1,4 @@
int foo(int bar1, int bar2) {
- int x;
- int y, z;
+ int x;
+ int y, z;
}
diff --git a/c_parser/test/in/05.c b/c_parser/test/in/05.c
index a5ac579..2317163 100644
--- a/c_parser/test/in/05.c
+++ b/c_parser/test/in/05.c
@@ -1,3 +1,3 @@
int foo(int x, int y) {
- {}
+ {}
}
diff --git a/c_parser/test/in/06.c b/c_parser/test/in/06.c
index f182bd9..2dcdc2b 100644
--- a/c_parser/test/in/06.c
+++ b/c_parser/test/in/06.c
@@ -1,6 +1,6 @@
int f(int a, int b, int c) {
- int d;
- {
- int e;
- }
+ int d;
+ {
+ int e;
+ }
}
diff --git a/c_parser/test/in/07.c b/c_parser/test/in/07.c
index 6abb5b5..80d8051 100644
--- a/c_parser/test/in/07.c
+++ b/c_parser/test/in/07.c
@@ -1,5 +1,5 @@
int foo(int a) {
- if(x == y) {
- int z;
- }
+ if(x == y) {
+ int z;
+ }
}
diff --git a/c_parser/test/in/08.c b/c_parser/test/in/08.c
index 3d8e983..c57a6ab 100644
--- a/c_parser/test/in/08.c
+++ b/c_parser/test/in/08.c
@@ -1,10 +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;
+ 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
index 08a7ee4..ae86bb1 100644
--- a/c_parser/test/in/09.c
+++ b/c_parser/test/in/09.c
@@ -5,11 +5,11 @@ int g;
int zz(int a, int b, int c)
{
- if(a==b){
- int a;
- return a;
- }else{
- int fsdfsdfs;
- return c;
- }
+ if(a==b){
+ int a;
+ return a;
+ }else{
+ int fsdfsdfs;
+ return c;
+ }
}
diff --git a/c_parser/test/in/10.c b/c_parser/test/in/10.c
index 3d4bb3c..37a6ecc 100644
--- a/c_parser/test/in/10.c
+++ b/c_parser/test/in/10.c
@@ -4,15 +4,15 @@ int f()
int g = 2;
int x(int y) {
- int z = 3;
-
- if(y < z || g < z) {
- int r;
- ++y;
- } else if(y == z) {
- int f;
- --y;
- } else return y;
-
- return g;
+ int z = 3;
+
+ if(y < z || g < z) {
+ int r;
+ ++y;
+ } else if(y == z) {
+ int f;
+ --y;
+ } else return y;
+
+ return g;
}
diff --git a/c_parser/test/in/11.c b/c_parser/test/in/11.c
index 7b6c0b5..2d8cac6 100644
--- a/c_parser/test/in/11.c
+++ b/c_parser/test/in/11.c
@@ -1,10 +1,10 @@
int f() {
- int x = 0;
-
- while(x < 5) {
- int y = 0;
- x++;
- }
-
- return x;
+ int x = 0;
+
+ while(x < 5) {
+ int y = 0;
+ x++;
+ }
+
+ return x;
}
diff --git a/c_parser/test/in/12.c b/c_parser/test/in/12.c
index bc51045..37b8b7e 100644
--- a/c_parser/test/in/12.c
+++ b/c_parser/test/in/12.c
@@ -1,4 +1,4 @@
int f(int b, int c) {
- int a = b+c;
- return a;
+ int a = b+c;
+ return a;
}
diff --git a/c_parser/test/in/13.c b/c_parser/test/in/13.c
new file mode 100644
index 0000000..d912d70
--- /dev/null
+++ b/c_parser/test/in/13.c
@@ -0,0 +1,8 @@
+int func(int x, int y) {
+ int i;
+ for(i = 0; i < 5; ++i) {
+ int z = 0;
+ z = x + y + i;
+ return x + y;
+ }
+}
diff --git a/c_parser/test/in/14.c b/c_parser/test/in/14.c
new file mode 100644
index 0000000..94506d7
--- /dev/null
+++ b/c_parser/test/in/14.c
@@ -0,0 +1,12 @@
+int function_1(int a, int b) {
+ int x = 0;
+
+ do {
+ int c = a + b;
+ x += a;
+ x = x * b;
+ x -= c;
+ } while(x < 500);
+
+ return x;
+}
diff --git a/c_parser/test/in/15.c b/c_parser/test/in/15.c
new file mode 100644
index 0000000..3189b56
--- /dev/null
+++ b/c_parser/test/in/15.c
@@ -0,0 +1,28 @@
+int add(int a, int b)
+{
+ int c = a + b;
+ return c;
+}
+
+int sub(int a, int b)
+{
+ int c = a - b;
+ return c;
+}
+
+int mult_by_5(int a)
+{
+ int b = 5;
+ return a * b;
+}
+
+int add_5(int a)
+{
+ int i;
+ for(i = 0; i < 5; ++i)
+ {
+ int b = ++a;
+ }
+
+ return a;
+}
diff --git a/c_parser/test/out/13.diff.txt b/c_parser/test/out/13.diff.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/13.diff.txt
diff --git a/c_parser/test/out/13.pretty.xml b/c_parser/test/out/13.pretty.xml
new file mode 100644
index 0000000..a604ecd
--- /dev/null
+++ b/c_parser/test/out/13.pretty.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<Program>
+ <Function id="func">
+ <Parameter id="x" />
+ <Parameter id="y" />
+ <Scope>
+ <Variable id="i" />
+ <Scope>
+ <Variable id="z" />
+ </Scope>
+ </Scope>
+ </Function>
+</Program>
diff --git a/c_parser/test/out/13.stderr.txt b/c_parser/test/out/13.stderr.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/13.stderr.txt
diff --git a/c_parser/test/out/13.stdout.xml b/c_parser/test/out/13.stdout.xml
new file mode 100644
index 0000000..511d93b
--- /dev/null
+++ b/c_parser/test/out/13.stdout.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<Program>
+<Function id="func">
+<Parameter id="x" />
+<Parameter id="y" />
+<Scope>
+<Variable id="i" />
+<Scope>
+<Variable id="z" />
+</Scope>
+</Scope>
+</Function>
+</Program>
diff --git a/c_parser/test/out/14.diff.txt b/c_parser/test/out/14.diff.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/14.diff.txt
diff --git a/c_parser/test/out/14.pretty.xml b/c_parser/test/out/14.pretty.xml
new file mode 100644
index 0000000..866d44e
--- /dev/null
+++ b/c_parser/test/out/14.pretty.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<Program>
+ <Function id="function_1">
+ <Parameter id="a" />
+ <Parameter id="b" />
+ <Scope>
+ <Variable id="x" />
+ <Scope>
+ <Variable id="c" />
+ </Scope>
+ </Scope>
+ </Function>
+</Program>
diff --git a/c_parser/test/out/14.stderr.txt b/c_parser/test/out/14.stderr.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/14.stderr.txt
diff --git a/c_parser/test/out/14.stdout.xml b/c_parser/test/out/14.stdout.xml
new file mode 100644
index 0000000..1b412d3
--- /dev/null
+++ b/c_parser/test/out/14.stdout.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<Program>
+<Function id="function_1">
+<Parameter id="a" />
+<Parameter id="b" />
+<Scope>
+<Variable id="x" />
+<Scope>
+<Variable id="c" />
+</Scope>
+</Scope>
+</Function>
+</Program>
diff --git a/c_parser/test/out/15.diff.txt b/c_parser/test/out/15.diff.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/15.diff.txt
diff --git a/c_parser/test/out/15.pretty.xml b/c_parser/test/out/15.pretty.xml
new file mode 100644
index 0000000..b06dde0
--- /dev/null
+++ b/c_parser/test/out/15.pretty.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0"?>
+<Program>
+ <Function id="add">
+ <Parameter id="a" />
+ <Parameter id="b" />
+ <Scope>
+ <Variable id="c" />
+ </Scope>
+ </Function>
+ <Function id="sub">
+ <Parameter id="a" />
+ <Parameter id="b" />
+ <Scope>
+ <Variable id="c" />
+ </Scope>
+ </Function>
+ <Function id="mult_by_5">
+ <Parameter id="a" />
+ <Scope>
+ <Variable id="b" />
+ </Scope>
+ </Function>
+ <Function id="add_5">
+ <Parameter id="a" />
+ <Scope>
+ <Variable id="i" />
+ <Scope>
+ <Variable id="b" />
+ </Scope>
+ </Scope>
+ </Function>
+</Program>
diff --git a/c_parser/test/out/15.stderr.txt b/c_parser/test/out/15.stderr.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/c_parser/test/out/15.stderr.txt
diff --git a/c_parser/test/out/15.stdout.xml b/c_parser/test/out/15.stdout.xml
new file mode 100644
index 0000000..d4c29b2
--- /dev/null
+++ b/c_parser/test/out/15.stdout.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0"?>
+<Program>
+<Function id="add">
+<Parameter id="a" />
+<Parameter id="b" />
+<Scope>
+<Variable id="c" />
+</Scope>
+</Function>
+<Function id="sub">
+<Parameter id="a" />
+<Parameter id="b" />
+<Scope>
+<Variable id="c" />
+</Scope>
+</Function>
+<Function id="mult_by_5">
+<Parameter id="a" />
+<Scope>
+<Variable id="b" />
+</Scope>
+</Function>
+<Function id="add_5">
+<Parameter id="a" />
+<Scope>
+<Variable id="i" />
+<Scope>
+<Variable id="b" />
+</Scope>
+</Scope>
+</Function>
+</Program>
diff --git a/c_parser/test/ref/13.stdout.xml b/c_parser/test/ref/13.stdout.xml
new file mode 100644
index 0000000..f0385cb
--- /dev/null
+++ b/c_parser/test/ref/13.stdout.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<Program>
+ <Function id="func">
+ <Parameter id="x" />
+ <Parameter id="y" />
+ <Scope>
+ <Variable id="i" />
+ <Scope>
+ <Variable id="z" />
+ </Scope>
+ </Scope>
+ </Function>
+</Program>
diff --git a/c_parser/test/ref/14.stdout.xml b/c_parser/test/ref/14.stdout.xml
new file mode 100644
index 0000000..63aec85
--- /dev/null
+++ b/c_parser/test/ref/14.stdout.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<Program>
+ <Function id="function_1">
+ <Parameter id="a" />
+ <Parameter id="b" />
+ <Scope>
+ <Variable id="x" />
+ <Scope>
+ <Variable id="c" />
+ </Scope>
+ </Scope>
+ </Function>
+</Program>
diff --git a/c_parser/test/ref/15.stdout.xml b/c_parser/test/ref/15.stdout.xml
new file mode 100644
index 0000000..029ccb8
--- /dev/null
+++ b/c_parser/test/ref/15.stdout.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0"?>
+<Program>
+ <Function id="add">
+ <Parameter id="a" />
+ <Parameter id="b" />
+ <Scope>
+ <Variable id="c" />
+ </Scope>
+ </Function>
+ <Function id="sub">
+ <Parameter id="a" />
+ <Parameter id="b" />
+ <Scope>
+ <Variable id="c" />
+ </Scope>
+ </Function>
+ <Function id="mult_by_5">
+ <Parameter id="a" />
+ <Scope>
+ <Variable id="b" />
+ </Scope>
+ </Function>
+ <Function id="add_5">
+ <Parameter id="a" />
+ <Scope>
+ <Variable id="i" />
+ <Scope>
+ <Variable id="b" />
+ </Scope>
+ </Scope>
+ </Function>
+</Program>