aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-02-20 23:40:23 +0100
committerpacien2018-02-20 23:40:23 +0100
commit66f66d2363e41274589ccad098269e379e348632 (patch)
tree5f66fac4c92e946ee087e641618d6d22c3f1c0ab
parent5a30c608fd311c8ce05760335d88208982efc4a5 (diff)
downloadtpc-compiler-66f66d2363e41274589ccad098269e379e348632.tar.gz
Import from TD11
-rw-r--r--src/tpc.lex50
-rw-r--r--src/tpc.y139
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
9int 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; }
25int { strcpy(yylval.type, yytext); return TYPE; }
26float { strcpy(yylval.type, yytext); return TYPE; }
27double { strcpy(yylval.type, yytext); return TYPE; }
28char { strcpy(yylval.type, yytext); return TYPE; }
29void { return VOID; }
30const { return CONST; }
31if { return IF;}
32else { return ELSE;}
33while { return WHILE; }
34return { 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
9extern int lineno;
10int yylex();
11void 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%%
39Prog : DeclConsts DeclVars DeclFoncts
40 ;
41DeclConsts : DeclConsts CONST ListConst ';'
42 |
43 ;
44ListConst : ListConst ',' IDENT '=' Litteral
45 | IDENT '=' Litteral
46 ;
47Litteral : NombreSigne
48 | CARACTERE
49 ;
50NombreSigne : NUM
51 | ADDSUB NUM
52 ;
53DeclVars :
54 DeclVars TYPE Declarateurs ';'
55 |
56 ;
57Declarateurs : Declarateurs ',' Declarateur
58 | Declarateur
59 ;
60Declarateur : IDENT
61 | IDENT '[' NUM ']'
62 ;
63DeclFoncts : DeclFoncts DeclFonct
64 | DeclFonct
65 ;
66DeclFonct : EnTeteFonct Corps
67 ;
68EnTeteFonct : TYPE IDENT '(' Parametres ')'
69 | VOID IDENT '(' Parametres ')'
70 ;
71Parametres : VOID
72 | ListTypVar
73 ;
74ListTypVar : ListTypVar ',' TYPE IDENT
75 | TYPE IDENT
76 ;
77Corps : '{' DeclConsts DeclVars SuiteInstr '}'
78 ;
79SuiteInstr : SuiteInstr Instr
80 |
81 ;
82Instr : Exp ';'
83 | RETURN Exp ';'
84 | RETURN ';'
85 | ';'
86 | IF '(' Exp ')' Instr
87 | IF '(' Exp ')' Instr ELSE Instr
88 | WHILE '(' Exp ')' Instr
89 | '{' SuiteInstr '}'
90 ;
91Exp : LValue '=' Exp
92 | EB
93 ;
94EB : EB OR TB
95 | TB
96 ;
97TB : TB AND FB
98 | FB
99 ;
100FB : FB EQ M
101 | M
102 ;
103M : M ORDER E
104 | E
105 ;
106E : E ADDSUB T
107 | T
108 ;
109T : T DIVSTAR F
110 | F
111 ;
112F : ADDSUB F
113 | '!' F
114 | '(' Exp ')'
115 | LValue
116 | NUM
117 | CARACTERE
118 | IDENT '(' Arguments ')'
119 ;
120LValue : IDENT
121 | IDENT '[' Exp ']'
122 ;
123Arguments : ListExp
124 |
125 ;
126ListExp : ListExp ',' ListExp
127 | Exp
128 ;
129%%
130
131void yyerror(char *s){
132 fprintf(stderr, "Line no%d: %s\n", lineno, s);
133}
134
135int main(int argc, char **argv) {
136 yyparse();
137 return 0;
138}
139