Table des symboles ok, start expressions arithm

This commit is contained in:
Foussats Morgane 2021-04-01 12:17:31 +02:00
parent 2b9f6bde69
commit 22d9a82ba6
11 changed files with 980 additions and 892 deletions

View file

@ -1,13 +0,0 @@
#include <stdio.h>
#include "table_symboles.h"
int main() {
Table_Symboles T;
initialise_table(&T);
add_symbole_top(&T, "toto", 0, 1);
add_symbole_top(&T, "titi", 1, 0);
add_symbole_bottom(&T, "titi", 0, 1);
print_table(&T);
return 0;
}

BIN
a.out

Binary file not shown.

File diff suppressed because it is too large Load diff

View file

@ -162,8 +162,13 @@ int yyparse (void);
#line 6 "analyse_syntaxique.y" /* yacc.c:358 */ #line 6 "analyse_syntaxique.y" /* yacc.c:358 */
#include <stdio.h> #include <stdio.h>
#include "table_symboles.h"
#line 167 "analyse_syntaxique.tab.c" /* yacc.c:358 */ enum Initialised_Variable init;
enum Symbole_Type type;
Table_Symboles table;
#line 172 "analyse_syntaxique.tab.c" /* yacc.c:358 */
#ifdef short #ifdef short
# undef short # undef short
@ -405,14 +410,14 @@ union yyalloc
/* YYFINAL -- State number of the termination state. */ /* YYFINAL -- State number of the termination state. */
#define YYFINAL 4 #define YYFINAL 4
/* YYLAST -- Last index in YYTABLE. */ /* YYLAST -- Last index in YYTABLE. */
#define YYLAST 132 #define YYLAST 133
/* YYNTOKENS -- Number of terminals. */ /* YYNTOKENS -- Number of terminals. */
#define YYNTOKENS 35 #define YYNTOKENS 35
/* YYNNTS -- Number of nonterminals. */ /* YYNNTS -- Number of nonterminals. */
#define YYNNTS 24 #define YYNNTS 23
/* YYNRULES -- Number of rules. */ /* YYNRULES -- Number of rules. */
#define YYNRULES 54 #define YYNRULES 53
/* YYNSTATES -- Number of states. */ /* YYNSTATES -- Number of states. */
#define YYNSTATES 125 #define YYNSTATES 125
@ -463,12 +468,12 @@ static const yytype_uint8 yytranslate[] =
/* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
static const yytype_uint8 yyrline[] = static const yytype_uint8 yyrline[] =
{ {
0, 63, 63, 65, 66, 68, 71, 72, 75, 77, 0, 68, 68, 70, 71, 73, 76, 77, 80, 82,
78, 80, 81, 82, 83, 84, 85, 87, 89, 90, 83, 85, 86, 87, 88, 89, 90, 92, 94, 95,
92, 93, 95, 96, 98, 100, 102, 103, 104, 105, 97, 98, 100, 101, 104, 106, 107, 108, 109, 110,
106, 107, 108, 109, 110, 112, 113, 115, 116, 118, 111, 112, 113, 114, 117, 118, 120, 121, 123, 125,
120, 121, 123, 125, 126, 127, 128, 129, 130, 131, 126, 128, 130, 131, 132, 133, 134, 135, 136, 137,
132, 133, 135, 137, 139 138, 140, 142, 144
}; };
#endif #endif
@ -483,8 +488,8 @@ static const char *const yytname[] =
"tINT", "tPRINT", "tRETURN", "tOR", "tAND", "tIF", "tELSE", "tWHILE", "tINT", "tPRINT", "tRETURN", "tOR", "tAND", "tIF", "tELSE", "tWHILE",
"tCONST", "tVAR", "tNOT", "$accept", "Main", "Params", "Param", "tCONST", "tVAR", "tNOT", "$accept", "Main", "Params", "Param",
"SuiteParams", "Body", "Instructions", "Instruction", "Decl", "SuiteParams", "Body", "Instructions", "Instruction", "Decl",
"SuiteDecl", "Type", "Valeur", "Affbis", "Aff", "E", "Args", "SuiteArgs", "SuiteDecl", "Type", "Valeur", "Aff", "E", "Args", "SuiteArgs", "If",
"If", "Else", "While", "Cond", "Invocation", "Print", "Return", YY_NULLPTR "Else", "While", "Cond", "Invocation", "Print", "Return", YY_NULLPTR
}; };
#endif #endif
@ -500,10 +505,10 @@ static const yytype_uint16 yytoknum[] =
}; };
# endif # endif
#define YYPACT_NINF -36 #define YYPACT_NINF -35
#define yypact_value_is_default(Yystate) \ #define yypact_value_is_default(Yystate) \
(!!((Yystate) == (-36))) (!!((Yystate) == (-35)))
#define YYTABLE_NINF -1 #define YYTABLE_NINF -1
@ -514,19 +519,19 @@ static const yytype_uint16 yytoknum[] =
STATE-NUM. */ STATE-NUM. */
static const yytype_int8 yypact[] = static const yytype_int8 yypact[] =
{ {
-7, -4, 26, 18, -36, 9, 12, 40, 36, -36, -3, -12, 31, 34, -35, 20, 25, 50, 44, -35,
50, 9, -36, 54, -36, 36, -36, 55, 68, 79, 57, 20, -35, 46, -35, 44, -35, 52, 60, 65,
39, -2, 63, 54, -36, 60, 80, -36, -36, -36, 56, -4, 58, 46, -35, 43, -35, -35, -35, 69,
81, -36, 82, 64, 15, 15, -36, 65, 19, 19, -35, 79, 61, 14, 14, -35, 63, 16, 16, 85,
88, -36, 86, 89, -36, -36, -36, -36, 93, -36, -35, 82, 91, -35, -35, 90, -35, 16, 16, 98,
19, 19, 96, 15, 48, 97, -36, 99, 91, 100, 14, 45, 99, -35, 100, 101, 102, 81, 97, -35,
24, 66, -36, 60, 98, 101, 1, 52, -36, 19, 16, 43, 103, 104, 8, 22, -35, 16, 16, 16,
19, 19, 19, 19, 19, 19, 19, 19, 19, 15, 16, 16, 16, 16, 16, 16, 16, 14, 14, 108,
15, 102, 103, 78, -36, -36, -36, 89, -36, -36, 109, 80, -35, -35, -35, -35, 49, 91, -35, -35,
-36, 5, 1, 108, -36, 24, 24, 24, 24, 24, -35, 1, 8, 106, -35, 49, 49, 49, 49, 49,
24, -36, -36, 54, 54, 91, -36, 105, 106, -36, 49, -35, -35, 46, 46, 101, -35, 110, 111, -35,
90, -36, -6, -36, 54, 110, 109, 15, -36, 112, 94, -35, -5, -35, 46, 112, 113, 14, -35, 105,
113, 54, 111, 90, -36 115, 46, 116, 94, -35
}; };
/* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM. /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
@ -536,33 +541,33 @@ static const yytype_uint8 yydefact[] =
{ {
0, 0, 0, 0, 1, 3, 0, 0, 7, 5, 0, 0, 0, 0, 1, 3, 0, 0, 7, 5,
0, 0, 4, 10, 2, 7, 20, 0, 0, 0, 0, 0, 4, 10, 2, 7, 20, 0, 0, 0,
0, 0, 0, 10, 15, 0, 0, 11, 12, 13, 0, 0, 0, 10, 15, 0, 11, 12, 13, 0,
0, 14, 0, 0, 0, 0, 21, 36, 0, 0, 14, 0, 0, 0, 0, 21, 35, 0, 0, 0,
0, 9, 22, 19, 23, 25, 16, 6, 0, 26, 9, 23, 19, 16, 6, 0, 25, 0, 0, 26,
0, 0, 27, 0, 0, 0, 32, 0, 38, 0, 0, 0, 0, 32, 0, 37, 0, 0, 0, 8,
24, 0, 8, 0, 0, 0, 34, 0, 51, 0, 0, 0, 0, 0, 31, 0, 50, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 35, 52, 54, 19, 17, 53, 0, 0, 34, 51, 24, 53, 22, 19, 17, 52,
33, 28, 30, 29, 31, 43, 44, 45, 46, 48, 33, 27, 29, 28, 30, 42, 43, 44, 45, 47,
47, 50, 49, 10, 10, 38, 18, 0, 0, 37, 46, 49, 48, 10, 10, 37, 18, 0, 0, 36,
0, 42, 0, 39, 10, 0, 0, 0, 40, 0, 0, 41, 0, 38, 10, 0, 0, 0, 39, 0,
0, 10, 0, 0, 41 0, 10, 0, 0, 40
}; };
/* YYPGOTO[NTERM-NUM]. */ /* YYPGOTO[NTERM-NUM]. */
static const yytype_int8 yypgoto[] = static const yytype_int8 yypgoto[] =
{ {
-36, -36, -36, 114, 115, -36, -22, -36, -36, 41, -35, -35, -35, 118, 117, -35, -22, -35, -35, 40,
-36, 69, -19, -36, -35, -36, 21, -36, 4, -36, -35, 70, -35, -34, -35, 28, -35, 7, -35, -32,
-33, -13, -36, -36 -13, -35, -35
}; };
/* YYDEFGOTO[NTERM-NUM]. */ /* YYDEFGOTO[NTERM-NUM]. */
static const yytype_int8 yydefgoto[] = static const yytype_int8 yydefgoto[] =
{ {
-1, 2, 7, 8, 12, 14, 22, 23, 24, 64, -1, 2, 7, 8, 12, 14, 22, 23, 24, 62,
25, 43, 26, 27, 54, 59, 84, 28, 113, 29, 25, 42, 26, 51, 56, 82, 27, 113, 28, 52,
55, 56, 31, 40 53, 30, 39
}; };
/* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If
@ -570,38 +575,38 @@ static const yytype_int8 yydefgoto[] =
number is the opposite. If YYTABLE_NINF, syntax error. */ number is the opposite. If YYTABLE_NINF, syntax error. */
static const yytype_uint8 yytable[] = static const yytype_uint8 yytable[] =
{ {
30, 41, 57, 60, 61, 114, 44, 37, 71, 72, 29, 40, 54, 57, 58, 36, 114, 68, 69, 70,
30, 70, 71, 72, 38, 66, 67, 1, 49, 3, 29, 3, 37, 64, 65, 69, 70, 46, 66, 46,
68, 50, 49, 115, 51, 50, 4, 5, 51, 69, 47, 1, 47, 48, 115, 48, 86, 67, 68, 69,
70, 71, 72, 6, 91, 92, 93, 94, 95, 96, 70, 4, 90, 91, 92, 93, 94, 95, 96, 97,
97, 98, 99, 100, 44, 9, 101, 102, 52, 53, 98, 99, 100, 5, 6, 101, 102, 49, 50, 49,
10, 11, 52, 69, 70, 71, 72, 69, 70, 71, 67, 68, 69, 70, 67, 68, 69, 70, 9, 11,
72, 13, 90, 36, 33, 73, 74, 75, 76, 77, 10, 32, 71, 72, 73, 74, 75, 76, 13, 33,
78, 69, 70, 71, 72, 79, 80, 34, 16, 17, 16, 17, 77, 78, 34, 18, 41, 19, 20, 21,
86, 107, 108, 18, 119, 19, 20, 21, 35, 39, 35, 107, 108, 43, 38, 119, 67, 68, 69, 70,
30, 30, 116, 42, 45, 46, 47, 48, 58, 122, 29, 29, 116, 44, 45, 84, 55, 59, 60, 122,
62, 30, 38, 65, 63, 37, 83, 81, 30, 82, 63, 29, 67, 68, 69, 70, 61, 36, 29, 79,
85, 105, 88, 103, 104, 89, 72, 110, 111, 117, 80, 85, 83, 105, 70, 120, 81, 88, 89, 103,
112, 118, 120, 123, 121, 15, 109, 124, 106, 0, 104, 117, 110, 111, 112, 118, 121, 106, 123, 15,
32, 0, 87 124, 87, 31, 109
}; };
static const yytype_int8 yycheck[] = static const yytype_uint8 yycheck[] =
{ {
13, 23, 35, 38, 39, 11, 25, 9, 7, 8, 13, 23, 34, 37, 38, 9, 11, 6, 7, 8,
23, 6, 7, 8, 16, 50, 51, 24, 3, 23, 23, 23, 16, 47, 48, 7, 8, 3, 50, 3,
53, 6, 3, 29, 9, 6, 0, 9, 9, 5, 6, 24, 6, 9, 29, 9, 60, 5, 6, 7,
6, 7, 8, 24, 69, 70, 71, 72, 73, 74, 8, 0, 10, 67, 68, 69, 70, 71, 72, 73,
75, 76, 77, 78, 63, 33, 79, 80, 33, 34, 74, 75, 76, 9, 24, 77, 78, 33, 34, 33,
10, 15, 33, 5, 6, 7, 8, 5, 6, 7, 5, 6, 7, 8, 5, 6, 7, 8, 33, 15,
8, 11, 10, 24, 9, 17, 18, 19, 20, 21, 10, 9, 17, 18, 19, 20, 21, 22, 11, 9,
22, 5, 6, 7, 8, 27, 28, 9, 24, 25, 24, 25, 27, 28, 9, 29, 33, 31, 32, 33,
14, 103, 104, 29, 117, 31, 32, 33, 9, 26, 24, 103, 104, 14, 26, 117, 5, 6, 7, 8,
103, 104, 114, 33, 14, 14, 14, 33, 33, 121, 103, 104, 114, 14, 33, 14, 33, 12, 16, 121,
12, 114, 16, 10, 15, 9, 15, 10, 121, 10, 10, 114, 5, 6, 7, 8, 15, 9, 121, 10,
10, 33, 14, 11, 11, 14, 8, 12, 12, 9, 10, 14, 10, 33, 8, 10, 15, 14, 14, 11,
30, 12, 10, 12, 11, 11, 105, 123, 87, -1, 11, 9, 12, 12, 30, 12, 11, 87, 12, 11,
15, -1, 63 123, 61, 15, 105
}; };
/* YYSTOS[STATE-NUM] -- The (internal number of the) accessing /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
@ -610,17 +615,17 @@ static const yytype_uint8 yystos[] =
{ {
0, 24, 36, 23, 0, 9, 24, 37, 38, 33, 0, 24, 36, 23, 0, 9, 24, 37, 38, 33,
10, 15, 39, 11, 40, 38, 24, 25, 29, 31, 10, 15, 39, 11, 40, 38, 24, 25, 29, 31,
32, 33, 41, 42, 43, 45, 47, 48, 52, 54, 32, 33, 41, 42, 43, 45, 47, 51, 53, 55,
56, 57, 39, 9, 9, 9, 24, 9, 16, 26, 56, 39, 9, 9, 9, 24, 9, 16, 26, 57,
58, 41, 33, 46, 47, 14, 14, 14, 33, 3, 41, 33, 46, 14, 14, 33, 3, 6, 9, 33,
6, 9, 33, 34, 49, 55, 56, 55, 33, 50, 34, 48, 54, 55, 54, 33, 49, 48, 48, 12,
49, 49, 12, 15, 44, 10, 49, 49, 55, 5, 16, 15, 44, 10, 48, 48, 54, 5, 6, 7,
6, 7, 8, 17, 18, 19, 20, 21, 22, 27, 8, 17, 18, 19, 20, 21, 22, 27, 28, 10,
28, 10, 10, 15, 51, 10, 14, 46, 14, 14, 10, 15, 50, 10, 14, 14, 48, 46, 14, 14,
10, 49, 49, 49, 49, 49, 49, 49, 49, 49, 10, 48, 48, 48, 48, 48, 48, 48, 48, 48,
49, 55, 55, 11, 11, 33, 44, 41, 41, 51, 48, 54, 54, 11, 11, 33, 44, 41, 41, 50,
12, 12, 30, 53, 11, 29, 41, 9, 12, 55, 12, 12, 30, 52, 11, 29, 41, 9, 12, 54,
10, 11, 41, 12, 53 10, 11, 41, 12, 52
}; };
/* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */ /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
@ -628,10 +633,10 @@ static const yytype_uint8 yyr1[] =
{ {
0, 35, 36, 37, 37, 38, 39, 39, 40, 41, 0, 35, 36, 37, 37, 38, 39, 39, 40, 41,
41, 42, 42, 42, 42, 42, 42, 43, 44, 44, 41, 42, 42, 42, 42, 42, 42, 43, 44, 44,
45, 45, 46, 46, 47, 48, 49, 49, 49, 49, 45, 45, 46, 46, 47, 48, 48, 48, 48, 48,
49, 49, 49, 49, 49, 50, 50, 51, 51, 52, 48, 48, 48, 48, 49, 49, 50, 50, 51, 52,
53, 53, 54, 55, 55, 55, 55, 55, 55, 55, 52, 53, 54, 54, 54, 54, 54, 54, 54, 54,
55, 55, 56, 57, 58 54, 55, 56, 57
}; };
/* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */ /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN. */
@ -639,10 +644,10 @@ static const yytype_uint8 yyr2[] =
{ {
0, 2, 6, 0, 2, 2, 4, 0, 4, 2, 0, 2, 6, 0, 2, 2, 4, 0, 4, 2,
0, 1, 1, 1, 1, 1, 2, 4, 3, 0, 0, 1, 1, 1, 1, 1, 2, 4, 3, 0,
1, 2, 1, 1, 3, 2, 1, 1, 3, 3, 1, 2, 3, 1, 4, 1, 1, 3, 3, 3,
3, 3, 1, 3, 2, 2, 0, 3, 0, 8, 3, 2, 1, 3, 2, 0, 3, 0, 8, 4,
4, 9, 7, 3, 3, 3, 3, 3, 3, 3, 9, 7, 3, 3, 3, 3, 3, 3, 3, 3,
3, 2, 4, 5, 3 2, 4, 5, 3
}; };
@ -1319,205 +1324,205 @@ yyreduce:
switch (yyn) switch (yyn)
{ {
case 2: case 2:
#line 63 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 68 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Dans main\n");} {}
#line 1325 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1330 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 3: case 3:
#line 65 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 70 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Sans params\n");} {printf("Sans params\n");}
#line 1331 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1336 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 5: case 5:
#line 68 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 73 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Parametre : %s\n", (yyvsp[0].id));} {printf("Parametre : %s\n", (yyvsp[0].id));}
#line 1337 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1342 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 8: case 8:
#line 75 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 80 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Dans body\n");} {printf("Dans body\n");}
#line 1343 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1348 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break;
case 17:
#line 87 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Declaration\n");}
#line 1349 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 20: case 20:
#line 92 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 97 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("int\n");} {type = TYPE_INT;}
#line 1355 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1354 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 21: case 21:
#line 93 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 98 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("const int\n");} {type = TYPE_CONST_INT;}
#line 1361 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1360 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 22: case 22:
#line 95 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 100 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Declaration %s\n", (yyvsp[0].id));} {add_symbole_top(&table, (yyvsp[-2].id), type, INITIALISED, table.depth);}
#line 1367 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1366 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break;
case 23:
#line 101 "analyse_syntaxique.y" /* yacc.c:1646 */
{add_symbole_top(&table, (yyvsp[0].id), type, NOT_INITIALISED, table.depth);}
#line 1372 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 24: case 24:
#line 98 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 104 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Affectation : %s\n", (yyvsp[-2].id));} {printf("Affectation : %s\n", (yyvsp[-3].id));}
#line 1373 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1378 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break;
case 25:
#line 106 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("int %d\n", (yyvsp[0].nombre));}
#line 1384 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 26: case 26:
#line 102 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 107 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("int %d\n", (yyvsp[0].nombre));} {printf("var %s\n", (yyvsp[0].id));}
#line 1379 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1390 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 27: case 27:
#line 103 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 108 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("var %s\n", (yyvsp[0].id));} {printf("Addition\n");}
#line 1385 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1396 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 28: case 28:
#line 104 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 109 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Addition\n");} {printf("Multiplication\n");}
#line 1391 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1402 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 29: case 29:
#line 105 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 110 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Multiplication\n");} {printf("Soustraction\n");}
#line 1397 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1408 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 30: case 30:
#line 106 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 111 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Soustraction\n");} {printf("Division\n");}
#line 1403 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1414 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 31: case 31:
#line 107 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 112 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Division\n");} {printf("Soustraction\n");}
#line 1409 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1420 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 33: case 33:
#line 109 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 114 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Parenthèse\n");} {printf("Parenthèse\n");}
#line 1415 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1426 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 34: case 38:
#line 110 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 123 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Soustraction\n");} {printf("Dans if\n");}
#line 1421 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1432 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 39: case 39:
#line 118 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 125 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Dans if\n");} {printf("else\n");}
#line 1427 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1438 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 40: case 40:
#line 120 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 126 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("else\n");} {printf("elsif\n");}
#line 1433 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1444 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 41: case 41:
#line 121 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 128 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("elsif\n");} {printf("Dans while\n");}
#line 1439 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1450 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 42: case 42:
#line 123 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 130 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Dans while\n");} {printf("Cond ==\n");}
#line 1445 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1456 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 43: case 43:
#line 125 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 131 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Cond ==\n");} {printf("Cond !=\n");}
#line 1451 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1462 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 44: case 44:
#line 126 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 132 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Cond !=\n");} {printf("Cond <\n");}
#line 1457 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1468 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 45: case 45:
#line 127 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 133 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Cond <\n");} {printf("Cond >\n");}
#line 1463 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1474 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 46: case 46:
#line 128 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 134 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Cond >\n");} {printf("Cond <=\n");}
#line 1469 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1480 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 47: case 47:
#line 129 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 135 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Cond <=\n");} {printf("Cond >=\n");}
#line 1475 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1486 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 48: case 48:
#line 130 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 136 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Cond >=\n");} {printf("Cond &&\n");}
#line 1481 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1492 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 49: case 49:
#line 131 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 137 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Cond &&\n");} {printf("Cond ||\n");}
#line 1487 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1498 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 50: case 50:
#line 132 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 138 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Cond ||\n");} {printf("Cond !\n");}
#line 1493 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1504 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 51: case 51:
#line 133 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 140 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Cond !\n");} {printf("Dans invocation\n");}
#line 1499 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1510 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 52: case 52:
#line 135 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 142 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("Dans invocation\n");} {printf("printf de %s\n", (yyvsp[-2].id));}
#line 1505 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1516 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
case 53: case 53:
#line 137 "analyse_syntaxique.y" /* yacc.c:1646 */ #line 144 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("printf de %s\n", (yyvsp[-2].id));}
#line 1511 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break;
case 54:
#line 139 "analyse_syntaxique.y" /* yacc.c:1646 */
{printf("return\n");} {printf("return\n");}
#line 1517 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1522 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
break; break;
#line 1521 "analyse_syntaxique.tab.c" /* yacc.c:1646 */ #line 1526 "analyse_syntaxique.tab.c" /* yacc.c:1646 */
default: break; default: break;
} }
/* User semantic actions sometimes alter yychar, and that requires /* User semantic actions sometimes alter yychar, and that requires
@ -1745,10 +1750,14 @@ yyreturn:
#endif #endif
return yyresult; return yyresult;
} }
#line 141 "analyse_syntaxique.y" /* yacc.c:1906 */ #line 146 "analyse_syntaxique.y" /* yacc.c:1906 */
#include <stdio.h> #include <stdio.h>
void main(void){ void main(void){
initialise_table(&table);
yyparse(); yyparse();
print_table(&table);
remove_symboles(&table, 0);
print_table(&table);
} }

View file

@ -5,6 +5,11 @@ char id[30];
%{ %{
#include <stdio.h> #include <stdio.h>
#include "table_symboles.h"
enum Initialised_Variable init;
enum Symbole_Type type;
Table_Symboles table;
%} %}
@ -60,7 +65,7 @@ char id[30];
//Fonction : tINT tVAR tPO Params tPF Body; //Fonction : tINT tVAR tPO Params tPF Body;
Main : tINT tMAIN tPO Params tPF Body {printf("Dans main\n");}; Main : tINT tMAIN tPO Params tPF Body {};
Params : {printf("Sans params\n");} ; Params : {printf("Sans params\n");} ;
Params : Param SuiteParams ; Params : Param SuiteParams ;
@ -84,20 +89,19 @@ Instruction : Print ;
Instruction : Decl ; Instruction : Decl ;
Instruction : Invocation tPV ; Instruction : Invocation tPV ;
Decl : Type Valeur SuiteDecl tPV {printf("Declaration\n");} ; Decl : Type Valeur SuiteDecl tPV ;
SuiteDecl: tVIRGULE Valeur SuiteDecl ; SuiteDecl: tVIRGULE Valeur SuiteDecl ;
SuiteDecl: ; SuiteDecl: ;
Type : tINT {printf("int\n");} ; Type : tINT {type = TYPE_INT;} ;
Type : tCONST tINT {printf("const int\n");} ; Type : tCONST tINT {type = TYPE_CONST_INT;} ;
Valeur : tVAR {printf("Declaration %s\n", $1);}; Valeur : tVAR tAFFECTATION E {add_symbole_top(&table, $1, type, INITIALISED, table.depth);};
Valeur : Affbis ; Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);};
Affbis : tVAR tAFFECTATION E {printf("Affectation : %s\n", $1);};
Aff : Affbis tPV ; Aff : tVAR tAFFECTATION E tPV {printf("Affectation : %s\n", $1);};
E : tENTIER {printf("int %d\n", $1);}; E : tENTIER {printf("int %d\n", $1);};
E : tVAR {printf("var %s\n", $1);}; E : tVAR {printf("var %s\n", $1);};
@ -105,9 +109,10 @@ E : E tADD E {printf("Addition\n");} ;
E : E tMUL E {printf("Multiplication\n");} ; E : E tMUL E {printf("Multiplication\n");} ;
E : E tSUB E {printf("Soustraction\n");} ; E : E tSUB E {printf("Soustraction\n");} ;
E : E tDIV E {printf("Division\n");} ; E : E tDIV E {printf("Division\n");} ;
E : tSUB E {printf("Soustraction\n");} ;
E : Invocation ; E : Invocation ;
E : tPO E tPF {printf("Parenthèse\n");} ; E : tPO E tPF {printf("Parenthèse\n");} ;
E : tSUB E {printf("Soustraction\n");} ;
Args : tVAR SuiteArgs ; Args : tVAR SuiteArgs ;
Args : ; Args : ;
@ -141,6 +146,11 @@ Return : tRETURN E tPV {printf("return\n");};
%% %%
#include <stdio.h> #include <stdio.h>
void main(void){ void main(void){
//TODO: rajouter gestion des erreurs
initialise_table(&table);
yyparse(); yyparse();
print_table(&table);
remove_symboles(&table, 0);
print_table(&table);
} }

0
gen_assembleur.c Normal file
View file

21
gen_assembleur.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef GEN_ASSEMBLEUR_H
#define GEN_ASSEMBLEUR_H
#define INSTRUCTION_TABLE_SIZE 1000
#define INSTRUCTION_SIZE 100
//table des instructions
typedef struct table{
char*
}
enum operation{ADD, SUB, MUL, DIV};
void gen_arithmetique(enum operation op, int arg1, int arg2);
//renvoie l'index (ou valeur?) de la premiere @ dispo
int new_temp();
#endif

18
poubelle/test.c Normal file
View file

@ -0,0 +1,18 @@
#include <stdio.h>
#include "table_symboles.h"
int main() {
Table_Symboles table;
initialise_table(&table);
add_symbole_top(&table, "var1" , TYPE_INT, INITIALISED);
add_symbole_top(&table, "var1" , TYPE_INT, INITIALISED);
add_symbole_top(&table, "var2" , TYPE_INT, INITIALISED);
add_symbole_bottom(&table, "varTemp1" , TYPE_CONST_INT, INITIALISED);
add_symbole_bottom(&table, "varTemp2" , TYPE_INT, NOT_INITIALISED);
print_table(&table);
return 0;
}

View file

@ -3,12 +3,9 @@ flex analyse_lexicale.lex
gcc -w *.c -ly gcc -w *.c -ly
echo " echo "
int main(){ int main(){
const int toto = 1, mimi, lolo = 6; const int var1 = 1, var2;
int tata = 5, lala; int var2;
while (tata == 5){ int var3, var4 = 1;
printf(tata);
lala = lolo + 6;
}
return 1; return 1;
} }
" | ./a.out " | ./a.out

View file

@ -1,7 +1,3 @@
//
// Created by Nahom Belay on 31/03/2021.
//
#include "table_symboles.h" #include "table_symboles.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -10,31 +6,33 @@
void initialise_table(Table_Symboles * table){ void initialise_table(Table_Symboles * table){
table->indexAvailableBottom = TABLE_SIZE - 1; table->indexAvailableBottom = TABLE_SIZE - 1;
table->indexAvailableTop = 0; table->indexAvailableTop = 0;
table->depth = 0;
} }
int variable_exists(Table_Symboles * table, char * varName){ int variable_exists(Table_Symboles * table, char * varName){
for (int i = 0; i < table->indexAvailableTop; i++){ for (int i = 0; i < table->indexAvailableTop; i++){
if (strcmp(varName, table->array[i].Variable_Name) == 0){ if (strcmp(varName, table->array[i].Variable_Name) == 0){
return -1; return i;
} }
} }
for (int i = (table->indexAvailableBottom + 1); i < TABLE_SIZE; i++){ for (int i = (table->indexAvailableBottom + 1); i < TABLE_SIZE; i++){
if (strcmp(varName, table->array[i].Variable_Name) == 0){ if (strcmp(varName, table->array[i].Variable_Name) == 0){
return -1; return i;
} }
} }
return 0; return 0;
} }
int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type type, enum Initialised_Variable init){ int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type type, enum Initialised_Variable init, int depth){
Symbole symbole; Symbole symbole;
symbole.Variable_Name = varName; strcpy(symbole.Variable_Name, varName);
symbole.addr = table->indexAvailableTop; symbole.addr = table->indexAvailableTop;
symbole.init = init; symbole.init = init;
symbole.type = type; symbole.type = type;
symbole.symbole_depth = table->depth;
if (table->indexAvailableTop >= table->indexAvailableBottom){ if (table->indexAvailableTop >= table->indexAvailableBottom){
return -1; return -1;
} else if (variable_exists(table, varName) == -1){ } else if (variable_exists(table, varName) != 0){
return -2; return -2;
} else { } else {
table->array[table->indexAvailableTop] = symbole; table->array[table->indexAvailableTop] = symbole;
@ -45,13 +43,14 @@ int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type ty
int add_symbole_bottom(Table_Symboles * table, char * varName, enum Symbole_Type type, enum Initialised_Variable init){ int add_symbole_bottom(Table_Symboles * table, char * varName, enum Symbole_Type type, enum Initialised_Variable init){
Symbole symbole; Symbole symbole;
symbole.Variable_Name = varName; strcpy(symbole.Variable_Name, varName);
symbole.addr = table->indexAvailableBottom; symbole.addr = table->indexAvailableBottom;
symbole.init = init; symbole.init = init;
symbole.type = type; symbole.type = type;
//symbole.symbole_depth = -1;
if (table->indexAvailableTop >= table->indexAvailableBottom){ if (table->indexAvailableTop >= table->indexAvailableBottom){
return -1; return -1;
} else if (variable_exists(table, varName) == 1){ } else if (variable_exists(table, varName) != 0){
return -2; return -2;
} else { } else {
table->array[table->indexAvailableBottom] = symbole; table->array[table->indexAvailableBottom] = symbole;
@ -60,28 +59,55 @@ int add_symbole_bottom(Table_Symboles * table, char * varName, enum Symbole_Type
return 0; return 0;
} }
int remove_symboles(Table_Symboles * table, int depth){
int index;
if (depth < table->depth){
return -1;
} else {
index = table->indexAvailableTop;
while(table->array[index].symbole_depth == depth && index >=0){
//remove
table->indexAvailableTop--;
index--;
}
//TODO: vérifier qu'il n'y a pas de varaibles temporarires au moment de changement de profondeur
}
return 0;
}
int initialise_symbole(Table_Symboles * table, char * varName){
int index = variable_exists(table, varName);
if (index == -1){
return -1;
} else {
table->array[index].init = INITIALISED;
}
}
void print_symbole(Symbole * symbole){ void print_symbole(Symbole * symbole){
char * var = symbole->Variable_Name; char * var = symbole->Variable_Name;
int addr = symbole->addr; int addr = symbole->addr;
int type = symbole->type; enum Symbole_Type type = symbole->type;
char * typeStr; char typeStr[20];
if (type == 0){ if (type == TYPE_INT){
typeStr = "INT"; strcpy(typeStr, "INT");
} else{ } else{
typeStr = "CONST_INT"; strcpy(typeStr, "CONST_INT");
} }
int init = symbole->init; enum Initialised_Variable init = symbole->init;
char * initStr; char initStr[20];
if (type == 0){ if (init == INITIALISED){
initStr = "INITIALISED"; strcpy(initStr,"INITIALISED");
} else{ } else{
initStr = "NOT_INITIALISED"; strcpy(initStr,"NOT_INITIALISED");
} }
printf("%-20s\t\t %-12s\t\t %-12d\t %-12s\n", var, typeStr, addr, initStr); int depth = symbole->symbole_depth;
printf("%-20s\t\t %-12s\t\t %-12d\t %-20s\t %-12d\n", var, typeStr, addr, initStr, depth);
} }
void print_table(Table_Symboles * table){ void print_table(Table_Symboles * table){
printf("%-20s\t\t %-12s\t\t %-12s\t %-12s\n", "Variable Name", "Type", "Address", "Initialised"); printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\t %-12s\n", "Variable Name", "Type", "Address", "Initialised", "Depth");
int indexTop = table->indexAvailableTop; int indexTop = table->indexAvailableTop;
int indexBottom = table->indexAvailableBottom; int indexBottom = table->indexAvailableBottom;
Symbole symbole; Symbole symbole;
@ -90,11 +116,13 @@ void print_table(Table_Symboles * table){
print_symbole(&symbole); print_symbole(&symbole);
} }
if (table->indexAvailableBottom != TABLE_SIZE - 1){ if (table->indexAvailableBottom != TABLE_SIZE - 1){
printf("%-20s\t\t %-12s\t\t %-12s\t %-12s\n", "...", "...", "...", "..."); printf("%-20s\t\t %-12s\t\t %-12s\t %-20s\t %-12s\n", "...", "...", "...", "...", "...");
for (int i = (indexBottom + 1); i < TABLE_SIZE; i++){ for (int i = (indexBottom + 1); i < TABLE_SIZE; i++){
symbole = table->array[i]; symbole = table->array[i];
print_symbole(&symbole); print_symbole(&symbole);
} }
} }
} }

View file

@ -1,26 +1,25 @@
//
// Created by Nahom Belay on 31/03/2021.
//
#ifndef TABLE_SYMBOLES_H #ifndef TABLE_SYMBOLES_H
#define TABLE_SYMBOLES_H #define TABLE_SYMBOLES_H
#define TABLE_SIZE 50 #define TABLE_SIZE 50
#define VARIABLE_SIZE 30
enum Symbole_Type {TYPE_INT , TYPE_CONST_INT}; enum Symbole_Type {TYPE_INT , TYPE_CONST_INT};
enum Initialised_Variable{INITIALISED , NOT_INITIALISED}; enum Initialised_Variable{INITIALISED , NOT_INITIALISED};
typedef struct Symboles { typedef struct Symboles {
char * Variable_Name; char Variable_Name[VARIABLE_SIZE];
int addr ; int addr ;
enum Symbole_Type type; enum Symbole_Type type;
enum Initialised_Variable init; enum Initialised_Variable init;
int symbole_depth;
} Symbole; } Symbole;
typedef struct Table_Symboles { typedef struct Table_Symboles {
Symbole array[TABLE_SIZE]; Symbole array[TABLE_SIZE];
int indexAvailableTop; int indexAvailableTop;
int indexAvailableBottom; int indexAvailableBottom;
int depth;
} Table_Symboles; } Table_Symboles;
/** /**
@ -37,7 +36,7 @@ void initialise_table(Table_Symboles * table);
* @param init * @param init
* @return if symbole added successfully, -1 if the table is full and -2 if the varaible already exists in the table * @return if symbole added successfully, -1 if the table is full and -2 if the varaible already exists in the table
*/ */
int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type type , enum Initialised_Variable init); int add_symbole_top(Table_Symboles * table, char * varName, enum Symbole_Type type , enum Initialised_Variable init, int depth);
/** /**
* Adds a symbole at the bottom (temp variables) * Adds a symbole at the bottom (temp variables)
@ -57,6 +56,23 @@ int add_symbole_bottom(Table_Symboles * table, char * varName, enum Symbole_Type
*/ */
int variable_exists(Table_Symboles * table, char * varName); int variable_exists(Table_Symboles * table, char * varName);
/**
* Removes symbole from table having certain depth
* @param table
* @param depth
* @return -1 if the symbole isn't in the table, 0 otherwise
*/
int remove_symboles(Table_Symboles * table, int depth);
/**
* Initialises an already exisiting symbole
* @param table
* @param varName
* @return -1 if the symbole isn't in the table, 0 otherwise
*/
int initialise_symbole(Table_Symboles * table, char * varName);
/** /**
* Prints a symbole with this format * Prints a symbole with this format
* varName | Type | Address | Initialised/Not_Initialised * varName | Type | Address | Initialised/Not_Initialised
@ -70,4 +86,4 @@ void print_symbole(Symbole * symbole);
*/ */
void print_table(Table_Symboles * table); void print_table(Table_Symboles * table);
#endif TABLE_SYMBOLES_H #endif