diff options
author | pacien | 2018-02-20 23:40:23 +0100 |
---|---|---|
committer | pacien | 2018-02-20 23:40:23 +0100 |
commit | 66f66d2363e41274589ccad098269e379e348632 (patch) | |
tree | 5f66fac4c92e946ee087e641618d6d22c3f1c0ab /src | |
parent | 5a30c608fd311c8ce05760335d88208982efc4a5 (diff) | |
download | tpc-compiler-66f66d2363e41274589ccad098269e379e348632.tar.gz |
Import from TD11
Diffstat (limited to 'src')
-rw-r--r-- | src/tpc.lex | 50 | ||||
-rw-r--r-- | src/tpc.y | 139 |
2 files changed, 189 insertions, 0 deletions
diff --git a/src/tpc.lex b/src/tpc.lex new file mode 100644 index 0000000..eaeea89 --- /dev/null +++ b/src/tpc.lex | |||
@@ -0,0 +1,50 @@ | |||
1 | %{ | ||
2 | /** | ||
3 | * UPEM / Compilation / Projet | ||
4 | * Pacien TRAN-GIRARD, Adam NAILI | ||
5 | */ | ||
6 | |||
7 | #include "tpc.tab.h" | ||
8 | |||
9 | int lineno = 1; | ||
10 | %} | ||
11 | |||
12 | %option noinput nounput noyywrap | ||
13 | %x COMMENT | ||
14 | |||
15 | %% | ||
16 | [ \t\r]+ ; | ||
17 | \n { lineno++;} | ||
18 | "/*" { BEGIN COMMENT;} | ||
19 | && { return AND; } | ||
20 | "||" { return OR; } | ||
21 | "*"|"/"|% { yylval.divstar=yytext[0]; return DIVSTAR; } | ||
22 | "+"|- { yylval.addsub=yytext[0]; return ADDSUB; } | ||
23 | "<"|"<="|">"|>= { strcpy(yylval.comp, yytext); return ORDER; } | ||
24 | ==|!= { strcpy(yylval.comp, yytext); return EQ; } | ||
25 | int { strcpy(yylval.type, yytext); return TYPE; } | ||
26 | float { strcpy(yylval.type, yytext); return TYPE; } | ||
27 | double { strcpy(yylval.type, yytext); return TYPE; } | ||
28 | char { strcpy(yylval.type, yytext); return TYPE; } | ||
29 | void { return VOID; } | ||
30 | const { return CONST; } | ||
31 | if { return IF;} | ||
32 | else { return ELSE;} | ||
33 | while { return WHILE; } | ||
34 | return { return RETURN; } | ||
35 | [a-zA-Z_][a-zA-Z0-9_]* { strcpy(yylval.ident, yytext); return IDENT; } | ||
36 | [0-9]+ { sscanf(yytext, "%d", &(yylval.num)); return NUM; } | ||
37 | '\\?.' { if (strlen(yytext)==3) | ||
38 | yylval.caractere=yytext[1]; | ||
39 | else switch(yytext[2]) { | ||
40 | case 'n': yylval.caractere='\n'; break; | ||
41 | case 't': yylval.caractere='\t'; break; | ||
42 | case '\'': yylval.caractere='\''; break; | ||
43 | } | ||
44 | return CARACTERE; } | ||
45 | . { return yytext[0];} | ||
46 | <COMMENT>"*/" { BEGIN INITIAL;} | ||
47 | <COMMENT>\n { lineno++;} | ||
48 | <COMMENT>. ; | ||
49 | %% | ||
50 | |||
diff --git a/src/tpc.y b/src/tpc.y new file mode 100644 index 0000000..88e31fb --- /dev/null +++ b/src/tpc.y | |||
@@ -0,0 +1,139 @@ | |||
1 | %{ | ||
2 | /* | ||
3 | * UPEM / Compilation / Projet | ||
4 | * Pacien TRAN-GIRARD, Adam NAILI | ||
5 | */ | ||
6 | |||
7 | #include <stdio.h> | ||
8 | |||
9 | extern int lineno; | ||
10 | int yylex(); | ||
11 | void yyerror(char *); | ||
12 | |||
13 | %} | ||
14 | |||
15 | %union { | ||
16 | char caractere; | ||
17 | int num; | ||
18 | char ident[64]; | ||
19 | char type[16]; | ||
20 | char comp[3]; | ||
21 | char addsub; | ||
22 | char divstar; | ||
23 | } | ||
24 | |||
25 | %token <caractere> CARACTERE | ||
26 | %token <num> NUM | ||
27 | %token <ident> IDENT | ||
28 | %token <comp> ORDER EQ | ||
29 | %token <addsub> ADDSUB | ||
30 | %token <divstar> DIVSTAR | ||
31 | %token OR AND CONST IF WHILE RETURN VOID | ||
32 | %token <type> TYPE | ||
33 | |||
34 | %left ',' | ||
35 | %precedence ')' | ||
36 | %precedence ELSE | ||
37 | |||
38 | %% | ||
39 | Prog : DeclConsts DeclVars DeclFoncts | ||
40 | ; | ||
41 | DeclConsts : DeclConsts CONST ListConst ';' | ||
42 | | | ||
43 | ; | ||
44 | ListConst : ListConst ',' IDENT '=' Litteral | ||
45 | | IDENT '=' Litteral | ||
46 | ; | ||
47 | Litteral : NombreSigne | ||
48 | | CARACTERE | ||
49 | ; | ||
50 | NombreSigne : NUM | ||
51 | | ADDSUB NUM | ||
52 | ; | ||
53 | DeclVars : | ||
54 | DeclVars TYPE Declarateurs ';' | ||
55 | | | ||
56 | ; | ||
57 | Declarateurs : Declarateurs ',' Declarateur | ||
58 | | Declarateur | ||
59 | ; | ||
60 | Declarateur : IDENT | ||
61 | | IDENT '[' NUM ']' | ||
62 | ; | ||
63 | DeclFoncts : DeclFoncts DeclFonct | ||
64 | | DeclFonct | ||
65 | ; | ||
66 | DeclFonct : EnTeteFonct Corps | ||
67 | ; | ||
68 | EnTeteFonct : TYPE IDENT '(' Parametres ')' | ||
69 | | VOID IDENT '(' Parametres ')' | ||
70 | ; | ||
71 | Parametres : VOID | ||
72 | | ListTypVar | ||
73 | ; | ||
74 | ListTypVar : ListTypVar ',' TYPE IDENT | ||
75 | | TYPE IDENT | ||
76 | ; | ||
77 | Corps : '{' DeclConsts DeclVars SuiteInstr '}' | ||
78 | ; | ||
79 | SuiteInstr : SuiteInstr Instr | ||
80 | | | ||
81 | ; | ||
82 | Instr : Exp ';' | ||
83 | | RETURN Exp ';' | ||
84 | | RETURN ';' | ||
85 | | ';' | ||
86 | | IF '(' Exp ')' Instr | ||
87 | | IF '(' Exp ')' Instr ELSE Instr | ||
88 | | WHILE '(' Exp ')' Instr | ||
89 | | '{' SuiteInstr '}' | ||
90 | ; | ||
91 | Exp : LValue '=' Exp | ||
92 | | EB | ||
93 | ; | ||
94 | EB : EB OR TB | ||
95 | | TB | ||
96 | ; | ||
97 | TB : TB AND FB | ||
98 | | FB | ||
99 | ; | ||
100 | FB : FB EQ M | ||
101 | | M | ||
102 | ; | ||
103 | M : M ORDER E | ||
104 | | E | ||
105 | ; | ||
106 | E : E ADDSUB T | ||
107 | | T | ||
108 | ; | ||
109 | T : T DIVSTAR F | ||
110 | | F | ||
111 | ; | ||
112 | F : ADDSUB F | ||
113 | | '!' F | ||
114 | | '(' Exp ')' | ||
115 | | LValue | ||
116 | | NUM | ||
117 | | CARACTERE | ||
118 | | IDENT '(' Arguments ')' | ||
119 | ; | ||
120 | LValue : IDENT | ||
121 | | IDENT '[' Exp ']' | ||
122 | ; | ||
123 | Arguments : ListExp | ||
124 | | | ||
125 | ; | ||
126 | ListExp : ListExp ',' ListExp | ||
127 | | Exp | ||
128 | ; | ||
129 | %% | ||
130 | |||
131 | void yyerror(char *s){ | ||
132 | fprintf(stderr, "Line no%d: %s\n", lineno, s); | ||
133 | } | ||
134 | |||
135 | int main(int argc, char **argv) { | ||
136 | yyparse(); | ||
137 | return 0; | ||
138 | } | ||
139 | |||