aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam NAILI2018-02-23 22:03:52 +0100
committerAdam NAILI2018-02-23 22:03:52 +0100
commit4d52709e24e05d3f2e1ea449b4a4203604b41aec (patch)
tree24da9272cdae9e5777e3b27bc3113cafec8bebfc /src
parentde916ff97b1a15f7188295cb084058dc5e8054c1 (diff)
parentf83fee146e1159344ee30711652b0ae6ebf07a5d (diff)
downloadtpc-compiler-4d52709e24e05d3f2e1ea449b4a4203604b41aec.tar.gz
Merge branch 'master' of https://github.com/pacien/upem-compil-tpc
Diffstat (limited to 'src')
-rw-r--r--src/tpc.lex46
-rw-r--r--src/tpc.y238
2 files changed, 147 insertions, 137 deletions
diff --git a/src/tpc.lex b/src/tpc.lex
index eaeea89..8b824c0 100644
--- a/src/tpc.lex
+++ b/src/tpc.lex
@@ -14,37 +14,31 @@ int lineno = 1;
14 14
15%% 15%%
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"*"|"/"|% { yylval.divstar=yytext[0]; return DIVSTAR; } 21"*"|"/"|% { return DIVSTAR; }
22"+"|- { yylval.addsub=yytext[0]; return ADDSUB; } 22"+"|- { return ADDSUB; }
23"<"|"<="|">"|>= { strcpy(yylval.comp, yytext); return ORDER; } 23"<"|"<="|">"|">=" { return ORDER; }
24==|!= { strcpy(yylval.comp, yytext); return EQ; } 24==|!= { return EQ; }
25int { strcpy(yylval.type, yytext); return TYPE; } 25int { return TYPE; }
26float { strcpy(yylval.type, yytext); return TYPE; } 26char { return TYPE; }
27double { strcpy(yylval.type, yytext); return TYPE; }
28char { strcpy(yylval.type, yytext); return TYPE; }
29void { return VOID; } 27void { return VOID; }
30const { return CONST; } 28const { return CONST; }
31if { return IF;} 29if { return IF; }
32else { return ELSE;} 30else { return ELSE; }
33while { return WHILE; } 31while { return WHILE; }
34return { return RETURN; } 32return { return RETURN; }
35[a-zA-Z_][a-zA-Z0-9_]* { strcpy(yylval.ident, yytext); return IDENT; } 33print { return PRINT; }
36[0-9]+ { sscanf(yytext, "%d", &(yylval.num)); return NUM; } 34readc { return READC; }
37'\\?.' { if (strlen(yytext)==3) 35reade { return READE; }
38 yylval.caractere=yytext[1]; 36[a-zA-Z_][a-zA-Z0-9_]* { return IDENT; }
39 else switch(yytext[2]) { 37[0-9]+ { return NUM; }
40 case 'n': yylval.caractere='\n'; break; 38'\\?.' { return CARACTERE; }
41 case 't': yylval.caractere='\t'; break; 39. { return yytext[0]; }
42 case '\'': yylval.caractere='\''; break; 40<COMMENT>"*/" { BEGIN INITIAL; }
43 } 41<COMMENT>\n { lineno++; }
44 return CARACTERE; }
45. { return yytext[0];}
46<COMMENT>"*/" { BEGIN INITIAL;}
47<COMMENT>\n { lineno++;}
48<COMMENT>. ; 42<COMMENT>. ;
49%% 43%%
50 44
diff --git a/src/tpc.y b/src/tpc.y
index 11635f9..a44a9aa 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -12,127 +12,143 @@ void yyerror(char *);
12 12
13%} 13%}
14 14
15%union { 15%token CARACTERE NUM IDENT
16 char caractere; 16%token ADDSUB DIVSTAR
17 int num; 17%token ORDER EQ OR AND
18 char ident[64]; 18%token IF WHILE RETURN
19 char type[16]; 19%token CONST VOID TYPE
20 char comp[3]; 20%token PRINT READC READE
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 21
34%left ',' 22%left ','
35%precedence ')' 23%precedence ')'
36%precedence ELSE 24%precedence ELSE
37 25
38%% 26%%
39Prog : DeclConsts DeclVars DeclFoncts 27Prog:
40 ; 28 DeclConsts DeclVars DeclFoncts
41DeclConsts : DeclConsts CONST ListConst ';' 29 ;
42 | 30DeclConsts:
43 ; 31 DeclConsts CONST ListConst ';'
44ListConst : ListConst ',' IDENT '=' Litteral 32 |
45 | IDENT '=' Litteral 33 ;
46 ; 34ListConst:
47Litteral : NombreSigne 35 ListConst ',' IDENT '=' Litteral
48 | CARACTERE 36 | IDENT '=' Litteral
49 ; 37 ;
50NombreSigne : NUM 38Litteral:
51 | ADDSUB NUM 39 NombreSigne
52 ; 40 | CARACTERE
53DeclVars : DeclVars TYPE Declarateurs ';' 41 ;
54 | 42NombreSigne:
55 ; 43 NUM
56Declarateurs : Declarateurs ',' Declarateur 44 | ADDSUB NUM
57 | Declarateur 45 ;
58 ; 46DeclVars:
59Declarateur : IDENT 47 DeclVars TYPE Declarateurs ';'
60 | IDENT '[' NUM ']' 48 |
61 ; 49 ;
62DeclFoncts : DeclFoncts DeclFonct 50Declarateurs:
63 | DeclFonct 51 Declarateurs ',' Declarateur
64 ; 52 | Declarateur
65DeclFonct : EnTeteFonct Corps 53 ;
66 ; 54Declarateur:
67EnTeteFonct : TYPE IDENT '(' Parametres ')' 55 IDENT
68 | VOID IDENT '(' Parametres ')' 56 | IDENT '[' NUM ']'
69 ; 57 ;
70Parametres : VOID 58DeclFoncts:
71 | ListTypVar 59 DeclFoncts DeclFonct
72 ; 60 | DeclFonct
73ListTypVar : ListTypVar ',' TYPE IDENT 61 ;
74 | TYPE IDENT 62DeclFonct:
75 ; 63 EnTeteFonct Corps
76Corps : '{' DeclConsts DeclVars SuiteInstr '}' 64 ;
77 ; 65EnTeteFonct:
78SuiteInstr : SuiteInstr Instr 66 TYPE IDENT '(' Parametres ')'
79 | 67 | VOID IDENT '(' Parametres ')'
80 ; 68 ;
81Instr : Exp ';' 69Parametres:
82 | RETURN Exp ';' 70 VOID
83 | RETURN ';' 71 | ListTypVar
84 | ';' 72 ;
85 | IF '(' Exp ')' Instr 73ListTypVar:
86 | IF '(' Exp ')' Instr ELSE Instr 74 ListTypVar ',' TYPE IDENT
87 | WHILE '(' Exp ')' Instr 75 | TYPE IDENT
88 | '{' SuiteInstr '}' 76 ;
89 ; 77Corps:
90Exp : LValue '=' Exp 78 '{' DeclConsts DeclVars SuiteInstr '}'
91 | EB 79 ;
92 ; 80SuiteInstr:
93EB : EB OR TB 81 SuiteInstr Instr
94 | TB 82 | ;
95 ; 83Instr:
96TB : TB AND FB 84 Exp ';'
97 | FB 85 | ';'
98 ; 86 | RETURN Exp ';'
99FB : FB EQ M 87 | RETURN ';'
100 | M 88 | READE '(' IDENT ')' ';'
101 ; 89 | READC '(' IDENT ')' ';'
102M : M ORDER E 90 | PRINT '(' Exp ')' ';'
103 | E 91 | IF '(' Exp ')' Instr
104 ; 92 | IF '(' Exp ')' Instr ELSE Instr
105E : E ADDSUB T 93 | WHILE '(' Exp ')' Instr
106 | T 94 | '{' SuiteInstr '}'
107 ; 95 ;
108T : T DIVSTAR F 96Exp:
109 | F 97 LValue '=' Exp
110 ; 98 | EB
111F : ADDSUB F 99 ;
112 | '!' F 100EB:
113 | '(' Exp ')' 101 EB OR TB
114 | LValue 102 | TB
115 | NUM 103 ;
116 | CARACTERE 104TB:
117 | IDENT '(' Arguments ')' 105 TB AND FB
118 ; 106 | FB
119LValue : IDENT 107 ;
120 | IDENT '[' Exp ']' 108FB:
121 ; 109 FB EQ M
122Arguments : ListExp 110 | M
123 | 111 ;
124 ; 112M:
125ListExp : ListExp ',' ListExp 113 M ORDER E
126 | Exp 114 | E
127 ; 115 ;
116E:
117 E ADDSUB T
118 | T
119 ;
120T:
121 T DIVSTAR F
122 | F
123 ;
124F:
125 ADDSUB F
126 | '!' F
127 | '(' Exp ')'
128 | LValue
129 | NUM
130 | CARACTERE
131 | IDENT '(' Arguments ')'
132 ;
133LValue:
134 IDENT
135 | IDENT '[' Exp ']'
136 ;
137Arguments:
138 ListExp
139 |
140 ;
141ListExp:
142 ListExp ',' Exp
143 | Exp
144 ;
128%% 145%%
129 146
130void yyerror(char *s){ 147void yyerror(char *m