diff options
author | pacien | 2018-02-23 01:11:34 +0100 |
---|---|---|
committer | pacien | 2018-02-23 01:11:34 +0100 |
commit | 806dd4ff649a1fb9a28dc469cc17d7dd5dbc1da5 (patch) | |
tree | da414ebbb1807a2dea82235faa429f6e6613db69 | |
parent | 11acfa2e75350d31a665b8ee99925916c2c8b5f1 (diff) | |
download | tpc-compiler-806dd4ff649a1fb9a28dc469cc17d7dd5dbc1da5.tar.gz |
Adapt for new grammar
-rw-r--r-- | res/exp.tpc | 5 | ||||
-rw-r--r-- | res/instr.tpc | 28 | ||||
-rw-r--r-- | res/ko_sample.tpc | 24 | ||||
-rw-r--r-- | res/ok_sample.tpc | 24 | ||||
-rw-r--r-- | res/trinome.tpc | 43 | ||||
-rw-r--r-- | src/tpc.lex | 9 | ||||
-rw-r--r-- | src/tpc.y | 215 |
7 files changed, 177 insertions, 171 deletions
diff --git a/res/exp.tpc b/res/exp.tpc deleted file mode 100644 index 471c068..0000000 --- a/res/exp.tpc +++ /dev/null | |||
@@ -1,5 +0,0 @@ | |||
1 | /* exp.tpc */ | ||
2 | |||
3 | /* double valeur(double a, double b, double c, double x) ; */ | ||
4 | /* Résolution d'une équation du deuxième degré */ | ||
5 | r1=(-b+s)/(2*a)+print(valeur(1,b,c,r1),valeur(1,b,c,r2)) | ||
diff --git a/res/instr.tpc b/res/instr.tpc deleted file mode 100644 index 7153ac2..0000000 --- a/res/instr.tpc +++ /dev/null | |||
@@ -1,28 +0,0 @@ | |||
1 | /* instr.tpc */ | ||
2 | /* int trinome(float a, float b, float c) ; | ||
3 | double valeur(double a, double b, double c, double x) ; */ | ||
4 | /* Résolution d'une équation du deuxième degré */ | ||
5 | b=-1; | ||
6 | while (b<=1) { | ||
7 | c=-1; | ||
8 | while (c<=1) { | ||
9 | print(b, c); | ||
10 | if (trinome(1,b,c)) { | ||
11 | print(r1, r2); | ||
12 | print(valeur(1,b,c,r1),valeur(1,b,c,r2)); | ||
13 | } | ||
14 | c=c+1; | ||
15 | } | ||
16 | b=b+1; | ||
17 | } | ||
18 | return 0; | ||
19 | d=b*b-4*a*c; | ||
20 | if (d>=0) { | ||
21 | s=sqrt(d); | ||
22 | r1=(-b+s)/(2*a); | ||
23 | r2=(-b-s)/(2*a); | ||
24 | return 1; | ||
25 | } | ||
26 | return 0; | ||
27 | if (0<=r1 && r1<=max && 0>=r2 && r2>=-max) | ||
28 | return; \ No newline at end of file | ||
diff --git a/res/ko_sample.tpc b/res/ko_sample.tpc new file mode 100644 index 0000000..f3a4c7a --- /dev/null +++ b/res/ko_sample.tpc | |||
@@ -0,0 +1,24 @@ | |||
1 | /** | ||
2 | * UPEM / Compilation / Projet TPC | ||
3 | * Exemple de fichier source TPC invalide | ||
4 | * Pacien TRAN-GIRARD, Adam NAILI | ||
5 | */ | ||
6 | |||
7 | const special = 'b'; | ||
8 | |||
9 | char funky_func(int arg) { | ||
10 | return !arg; | ||
11 | } | ||
12 | |||
13 | void main(void) { | ||
14 | void ret; | ||
15 | ret = 1 + funky_func(0) * 2; | ||
16 | |||
17 | while (ret) { | ||
18 | readc(ret); | ||
19 | print(ret); | ||
20 | |||
21 | if (ret == special) print('!'); | ||
22 | } | ||
23 | } | ||
24 | |||
diff --git a/res/ok_sample.tpc b/res/ok_sample.tpc new file mode 100644 index 0000000..7591dfd --- /dev/null +++ b/res/ok_sample.tpc | |||
@@ -0,0 +1,24 @@ | |||
1 | /** | ||
2 | * UPEM / Compilation / Projet TPC | ||
3 | * Exemple de fichier source TPC valide | ||
4 | * Pacien TRAN-GIRARD, Adam NAILI | ||
5 | */ | ||
6 | |||
7 | const special = 'b'; | ||
8 | |||
9 | char funky_func(int arg) { | ||
10 | return !arg; | ||
11 | } | ||
12 | |||
13 | void main(void) { | ||
14 | char ret; | ||
15 | ret = 1 + funky_func(0) * 2; | ||
16 | |||
17 | while (ret) { | ||
18 | readc(ret); | ||
19 | print(ret); | ||
20 | |||
21 | if (ret == special) print('!'); | ||
22 | } | ||
23 | } | ||
24 | |||
diff --git a/res/trinome.tpc b/res/trinome.tpc deleted file mode 100644 index c3e2f94..0000000 --- a/res/trinome.tpc +++ /dev/null | |||
@@ -1,43 +0,0 @@ | |||
1 | /* #include <stdio.h> | ||
2 | #include <math.h> */ | ||
3 | /* int trinome(float a, float b, float c) ; | ||
4 | double valeur(double a, double b, double c, double x) ; */ | ||
5 | /* Résolution d'une équation du deuxième degré */ | ||
6 | float r1, r2; | ||
7 | |||
8 | int main(void) { | ||
9 | int b, c; | ||
10 | b=-1; | ||
11 | while (b<=1) { | ||
12 | c=-1; | ||
13 | while (c<=1) { | ||
14 | print(b, c); | ||
15 | if (trinome(1,b,c)) { | ||
16 | print(r1, r2); | ||
17 | print(valeur(1,b,c,r1),valeur(1,b,c,r2)); | ||
18 | if (0<=r1 && r1<=max && 0>=r2 && r2>=-max) | ||
19 | return 0; | ||
20 | } | ||
21 | c=c+1; | ||
22 | } | ||
23 | b=b+1; | ||
24 | } | ||
25 | return 0; | ||
26 | } | ||
27 | |||
28 | int trinome(float a,float b, float c) { | ||
29 | float d; | ||
30 | float s; | ||
31 | d=b*b-4*a*c; | ||
32 | if (d>=0) { | ||
33 | s=sqrt(d); | ||
34 | r1=(-b+s)/(2*a); | ||
35 | r2=(-b-s)/(2*a); | ||
36 | return 1; | ||
37 | } | ||
38 | return 0; | ||
39 | } | ||
40 | |||
41 | double valeur(double a, double b, double c, double x) { | ||
42 | return a*x*x+b*x+c; | ||
43 | } \ No newline at end of file | ||
diff --git a/src/tpc.lex b/src/tpc.lex index 63d4391..8b824c0 100644 --- a/src/tpc.lex +++ b/src/tpc.lex | |||
@@ -16,15 +16,13 @@ int lineno = 1; | |||
16 | [ \t\r]+ ; | 16 | [ \t\r]+ ; |
17 | \n { lineno++; } | 17 | \n { lineno++; } |
18 | "/*" { BEGIN COMMENT; } | 18 | "/*" { BEGIN COMMENT; } |
19 | && { return AND; } | 19 | "&&" { return AND; } |
20 | "||" { return OR; } | 20 | "||" { return OR; } |
21 | "*"|"/"|% { return DIVSTAR; } | 21 | "*"|"/"|% { return DIVSTAR; } |
22 | "+"|- { return ADDSUB; } | 22 | "+"|- { return ADDSUB; } |
23 | "<"|"<="|">"|>= { return ORDER; } | 23 | "<"|"<="|">"|">=" { return ORDER; } |
24 | ==|!= { return EQ; } | 24 | ==|!= { return EQ; } |
25 | int { return TYPE; } | 25 | int { return TYPE; } |
26 | float { return TYPE; } | ||
27 | double { return TYPE; } | ||
28 | char { return TYPE; } | 26 | char { return TYPE; } |
29 | void { return VOID; } | 27 | void { return VOID; } |
30 | const { return CONST; } | 28 | const { return CONST; } |
@@ -32,6 +30,9 @@ if { return IF; } | |||
32 | else { return ELSE; } | 30 | else { return ELSE; } |
33 | while { return WHILE; } | 31 | while { return WHILE; } |
34 | return { return RETURN; } | 32 | return { return RETURN; } |
33 | print { return PRINT; } | ||
34 | readc { return READC; } | ||
35 | reade { return READE; } | ||
35 | [a-zA-Z_][a-zA-Z0-9_]* { return IDENT; } | 36 | [a-zA-Z_][a-zA-Z0-9_]* { return IDENT; } |
36 | [0-9]+ { return NUM; } | 37 | [0-9]+ { return NUM; } |
37 | '\\?.' { return CARACTERE; } | 38 | '\\?.' { return CARACTERE; } |
@@ -12,103 +12,136 @@ void yyerror(char *); | |||
12 | 12 | ||
13 | %} | 13 | %} |
14 | 14 | ||
15 | %token CARACTERE NUM IDENT ORDER EQ ADDSUB DIVSTAR | 15 | %token CARACTERE NUM IDENT |
16 | %token OR AND CONST IF WHILE RETURN VOID TYPE | 16 | %token ADDSUB DIVSTAR |
17 | %token ORDER EQ OR AND | ||
18 | %token IF WHILE RETURN | ||
19 | %token CONST VOID TYPE | ||
20 | %token PRINT READC READE | ||
17 | 21 | ||
18 | %left ',' | 22 | %left ',' |
19 | %precedence ')' | 23 | %precedence ')' |
20 | %precedence ELSE | 24 | %precedence ELSE |
21 | 25 | ||
22 | %% | 26 | %% |
23 | Prog : DeclConsts DeclVars DeclFoncts | 27 | Prog: |
24 | ; | 28 | DeclConsts DeclVars DeclFoncts |
25 | DeclConsts : DeclConsts CONST ListConst ';' | 29 | ; |
26 | | | 30 | DeclConsts: |
27 | ; | 31 | DeclConsts CONST ListConst ';' |
28 | ListConst : ListConst ',' IDENT '=' Litteral | 32 | | |
29 | | IDENT '=' Litteral | 33 | ; |
30 | ; | 34 | ListConst: |
31 | Litteral : NombreSigne | 35 | ListConst ',' IDENT '=' Litteral |
32 | | CARACTERE | 36 | | IDENT '=' Litteral |
33 | ; | 37 | ; |
34 | NombreSigne : NUM | 38 | Litteral: |
35 | | ADDSUB NUM | 39 | NombreSigne |
36 | ; | 40 | | CARACTERE |
37 | DeclVars : DeclVars TYPE Declarateurs ';' | 41 | ; |
38 | | | 42 | NombreSigne: |
39 | ; | 43 | NUM |
40 | Declarateurs : Declarateurs ',' Declarateur | 44 | | ADDSUB NUM |
41 | | Declarateur | 45 | ; |
42 | ; | 46 | DeclVars: |
43 | Declarateur : IDENT | 47 | DeclVars TYPE Declarateurs ';' |
44 | | IDENT '[' NUM ']' | 48 | | |
45 | ; | 49 | ; |
46 | DeclFoncts : DeclFoncts DeclFonct | 50 | Declarateurs: |
47 | | DeclFonct | 51 | Declarateurs ',' Declarateur |
48 | ; | 52 | | Declarateur |
49 | DeclFonct : EnTeteFonct Corps | 53 | ; |
50 | ; | 54 | Declarateur: |
51 | EnTeteFonct : TYPE IDENT '(' Parametres ')' | 55 | IDENT |
52 | | VOID IDENT '(' Parametres ')' | 56 | | IDENT '[' NUM ']' |
53 | ; | 57 | ; |
54 | Parametres : VOID | 58 | DeclFoncts: |
55 | | ListTypVar | 59 | DeclFoncts DeclFonct |
56 | ; | 60 | | DeclFonct |
57 | ListTypVar : ListTypVar ',' TYPE IDENT | 61 | ; |
58 | | TYPE IDENT | 62 | DeclFonct: |
59 | ; | 63 | EnTeteFonct Corps |
60 | Corps : '{' DeclConsts DeclVars SuiteInstr '}' | 64 | ; |
61 | ; | 65 | EnTeteFonct: |
62 | SuiteInstr : SuiteInstr Instr | 66 | TYPE IDENT '(' Parametres ')' |