aboutsummaryrefslogtreecommitdiff
path: root/src/tpc.y
blob: c5758637e9bdd9cedde75e231245dbe4182e89c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
%{
/*
 * UPEM / Compilation / Projet
 * Pacien TRAN-GIRARD, Adam NAILI
 */

#include <stdio.h>
#include "symboltable.h"

extern int lineno;
int yylex();
void yyerror(char *);

#define GLOBAL 0
#define LOCAL 1
static int status = GLOBAL;
%}

%union {
    char caractere;
    int num;
    char ident[64];
    int type;
    char comp[3];
    char addsub;
    char divstar;
}
%token <caractere> CARACTERE
%token <num> NUM
%token <ident> IDENT
%token <comp> ORDER EQ
%token <addsub> ADDSUB
%token <divstar> DIVSTAR
%token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE
%token <type> TYPE

%type <num> Exp EB TB FB M E T F 
%type <ident> LValue

%left ','
%precedence ')'
%precedence ELSE

%%
Prog:{printf("extern printf\n");
			printf("section .data\n");
			printf("format_int db \"%%d \\n\",10,0\n\n");
			printf("section .bss\nsection .text\n\nglobal _start\n");
			printf("print: ;print needs an argument in rax\n\n");
			printf("push rbp\n");
			printf("mov rbp, rsp\n");
			printf("push rsi\n");
			printf("mov rsi, rax\n");
			printf("mov rdi, format_int\n");
			printf("mov rax, 0\n");
		  printf("call printf WRT ..plt\n");	
			printf("pop rsi\n");
			printf("pop rbp\n");
			printf("ret\n");
			printf("\n_start:\n");	
			}
     DeclConsts DeclVars DeclFoncts 
     {
        printf("mov rax,60 \n");
        printf("mov rdi,0 \n");  
        printf("syscall \n");
     }
  ;
DeclConsts:
     DeclConsts CONST ListConst ';'
  |
  ;
ListConst:
     ListConst ',' IDENT '=' Litteral
  |  IDENT '=' Litteral
  ;
Litteral:
     NombreSigne
  |  CARACTERE
  ;
NombreSigne:
     NUM
  |  ADDSUB NUM
  ;
DeclVars:
     DeclVars TYPE Declarateurs ';'
  |
  ;
Declarateurs:
     Declarateurs ',' Declarateur   {glo_addVar($<ident>3, $<type>0);}
  |  Declarateur                    {glo_addVar($<ident>1, $<type>0);}
  ;
Declarateur:
     IDENT
  |  IDENT '[' NUM ']'
  ;
DeclFoncts:
     DeclFoncts DeclFonct
  |  DeclFonct
  ;
DeclFonct:
     EnTeteFonct {status = LOCAL;} Corps {status = GLOBAL;}
  ;
EnTeteFonct:
     TYPE IDENT '(' Parametres ')'
  |  VOID IDENT '(' Parametres ')'
  ;
Parametres:
     VOID
  |  ListTypVar
  ;
ListTypVar:
     ListTypVar ',' TYPE IDENT    {glo_addVar($<ident>4, $<type>3);}
  |  TYPE IDENT                   {glo_addVar($<ident>2, $<type>1);}
  ;
Corps:
     '{' DeclConsts DeclVars SuiteInstr '}'
  ;
SuiteInstr:
     SuiteInstr Instr
  |  ;
Instr:
     Exp ';'
  |  ';'
  |  RETURN Exp ';'
  |  RETURN ';'
  |  READE '(' IDENT ')' ';'      {glo_lookup($<ident>3);}
  |  READC '(' IDENT ')' ';'      {glo_lookup($<ident>3);}
  |  PRINT '(' Exp ')' ';'
  |  IF '(' Exp ')' Instr
  |  IF '(' Exp ')' Instr ELSE Instr
  |  WHILE '(' Exp ')' Instr
  |  '{' SuiteInstr '}'
  ;
Exp:
     LValue '=' Exp               {$$ = glo_lookup($<ident>1);}
  |  EB
  ;
EB:
     EB OR TB
  |  TB
  ;
TB:
     TB AND FB
  |  FB
  ;
FB:
     FB EQ M
  |  M
  ;
M:
     M ORDER E
  |  E
  ;
E:
     E ADDSUB T
  |  T
  ;
T:
     T DIVSTAR F
  |  F
  ;
F:
     ADDSUB F                   {$$ = $2;} //on fait remonter le type
  |  '!' F                      {$$ = $2;} 
  |  '(' Exp ')'                {$$ = $2;}
  |  LValue                     {$$ = glo_lookup($<ident>1);}
  |  NUM                        {$$ = INT;} // on stocke les types pour l'analyse sémantique
  |  CARACTERE                  {$$ = CHAR;}
  |  IDENT '(' Arguments  ')'   {$$ = INT;} //tableau d'entiers uniquement
  ;
LValue:
     IDENT                      {glo_lookup($<ident>1);}
  |  IDENT  '[' Exp  ']'        {glo_lookup($<ident>1);}
  ;
Arguments:
     ListExp
  |
  ;
ListExp:
     ListExp ',' Exp
  |  Exp
  ;
%%

void yyerror(char *msg){
  fprintf(stderr, "%s at line %d\n", msg, lineno);
}

int main(int argc, char **argv) {
  return yyparse();
}