Bonjour,
je suis entrain de faire un petit Compilateur en utilisant flex et bison qui doit compiler du PASCAL.
Voila je voudrai vous demandé si il y a pas une grammaire en LALR(1) pour du PASCAL que je pourrait utiliser
Merci d'avance.
Bonjour,
je suis entrain de faire un petit Compilateur en utilisant flex et bison qui doit compiler du PASCAL.
Voila je voudrai vous demandé si il y a pas une grammaire en LALR(1) pour du PASCAL que je pourrait utiliser
Merci d'avance.
Les MP ne sont pas là pour les questions techniques, les forums sont là pour ça.
Sinon, Niklaus Wirth n'a jamais utilisé de méthode LALR(1), N. Wirth a toujours utilisé une méthode manuelle LL(1), que ce soit pour Pascal ou ensuite pour Modula, Modula 2 et Oberon.
D'ailleurs on héberge son cours de compilation plus deux tutoriels pour l'analyse LL(1) du Pascal :
Du même auteur: mon projet, le dernier article publié, le blog dvp et le jeu vidéo.
Avant de poser une question je lis les règles du forum.
Bonjour
Alors oui je suis obligé d'utiliser flex et bison dans le cadre d'un TP de Compile
(J’utilise linux avec Ubunto)
Voila ce que j'ai Réaliser jusqu'la
Fichier lex.l
fichier yacc.y
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 %{ #include "yacc.tab.h" int mot_r(char *s); int type_smp(char *s); int yywrap(void); %} signe [+-] blanc [" "\n\t] lettre [A-Za-z] idf {lettre}("_"|{lettre}|{chiffre})* chiffre [0-9] entier {signe}?{chiffre}+ exposant [eE][+-]?{entier} reel {entier}("."{entier})?{exposant}? nombre {reel}|{entier} comant ("(*"({idf}|({blanc})?)*"*)")* type_simple "INT"|"BOOLEAN"|"REAL"|"CHAR" mot_reserves "IF"|"THEN"|"ELSE"|"PROGRAM"|"BEGIN"|"END"|"VAR"|"CONST" %% {nombre} { ECHO; printf("c'est du Nombre\n"); return NOMBRE;} {mot_reserves} { ECHO; return(mot_r(yytext)); } [,;.=] { /*ECHO; printf("c'est du ','ou';'ou'.'ou'=':\n");*/ return(yytext[0]);} {type_simple} { /*ECHO;*/ return(type_smp(yytext)); } ">" { /*ECHO; printf("c'est du OP_RELATION\n");*/ return SUP;} ">=" { /*ECHO; printf("c'est du OP_RELATION\n");*/ return SUPEG;} "<=" { /*ECHO; printf("c'est du OP_RELATION\n");*/ return INFEG;} "<" { /*ECHO; printf("c'est du OP_RELATION\n");*/ return INF;} "==" { /*ECHO; printf("c'est du OP_RELATION\n");*/ return EGAL;} "<>" { /*ECHO; printf("c'est du OP_RELATION\n");*/ return DIF;} "AND"|"and" { /*ECHO; printf("c'est du OP_LOGIQUE\n");*/ return(OP_AND);} "OR"|"or" { /*ECHO; printf("c'est du OP_LOGIQUE\n");*/ return(OP_OR);} "not"|"NOT" { /*ECHO; printf("c'est du OP_LOGIQUE\n");*/ return(OP_NOT);} "+" { /*ECHO;*/ return(OP_PLUS);} "-" { /*ECHO;*/ return(OP_MOINS);} "*" { /*ECHO;*/ return(OP_MULT);} "/" { /*ECHO;*/ return(OP_DIV);} "MOD"|"mod" { /*ECHO;*/ return(OP_MOD);} ":=" { /*ECHO;*/ return(AFFECT);} {blanc} { /*ECHO;*/ /* rien */ /*printf("c'est du Blanc\n");*/ } {comant} { /*ECHO; ?(* 7idf *)!?*/ /*printf("c'est du COMANTER\n");*/ return COMANT; } {idf} { ECHO; /*printf("c'est du IDF\n");*/ return IDF; } . { ECHO; printf("c'est le SINON de tout le Réste OU Erreur Lexiquale\n"); return(yytext[0]); } %% int mot_r(char *s) { if(strcmp(s,"IF")==0) return(IF); else if(strcmp(s,"ELSE")==0) return(ELSE); else if(strcmp(s,"THEN")==0) return(THEN); else if(strcmp(s,"PROGRAM")==0) return(PROGRAM); else if(strcmp(s,"BEGIN")==0) {/*fin_decl=1*/;return(Begin);} else if(strcmp(s,"END")==0) return(END); else if(strcmp(s,"VAR")==0) return(VAR); else return(CONST); } int type_smp(char *s) {if(strcmp(s,"INT")==0) return(INT); else if(strcmp(s,"CHAR")==0) return(CHAR); else if(strcmp(s,"REAL")==0) return(REAL); else return(BOOLEAN); } int yywrap(void) { return 1; }
Si c'est possible de me dire ou je doit modifier la grammaire
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 %{ #include <stdio.h> int yyerror(char *); %} %token NOMBRE %token IF %token THEN %token ELSE %token PROGRAM %token Begin %token END %token VAR %token CONST %token DO %token INT %token CHAR %token REAL %token BOOLEAN %token SUP %token SUPEG %token INFEG %token INF %token EGAL %token DIF %token OP_AND %token OP_OR %token OP_NOT %token OP_PLUS %token OP_MOINS %token OP_MULT %token OP_DIV %token OP_MOD %token AFFECT %token COMANT %token IDF %start programme %union {char chaine[256]; double valeur;} %type <chaine> corp_de_programme suite_d_declarations suite_d_instructions %type <chaine> declaration instruction inst_if liste_daffectation %type <chaine> liste_var liste_const liste_identificateur %type <chaine> affectation expression condition %type <chaine> opl op %type <valeur> terme %type <valeur> A B %left OP_PLUS OP_MOINS %left OP_MULT OP_DIV OP_MOD %% programme:PROGRAM IDF corp_de_programme {printf("ADD0");} ; corp_de_programme :suite_d_declarations Begin suite_d_instructions END '.' {printf("ADD1");} | {/*Vide MOI ajouter*/} ; suite_d_declarations :declaration {printf("ADD2");} | declaration suite_d_declarations {printf("ADD3");} ; suite_d_instructions:instruction ';' suite_d_instructions {printf("ADD4");} |instruction ';' {printf("ADD5");} ; instruction:inst_if {printf("ADD6");} |liste_daffectation {printf("ADD7");} ; declaration :VAR liste_var {printf("ADD8");} |CONST liste_const {printf("ADD9");} ; liste_const :liste_const ','IDF '=' terme {printf("ADD10");} |IDF '=' terme {printf("ADD11");} ; liste_var : INT liste_identificateur {printf("ADD12");} | REAL liste_identificateur {printf("ADD13");} | CHAR liste_identificateur {printf("ADD14");} | BOOLEAN liste_identificateur {printf("ADD15");} ; liste_identificateur :IDF ',' liste_identificateur {printf("ADD16");} | IDF ';' {printf("ADD17");} ; liste_daffectation : affectation liste_daffectation {printf("ADD18");} |affectation {printf("ADD19");} ; affectation :IDF AFFECT expression ';' {printf("ADD20");} ; inst_if :A THEN suite_d_instructions {printf("ADD21");} |B ELSE suite_d_instructions {printf("ADD22");} ; B :A THEN suite_d_instructions {printf("ADD23");} ; A :IF condition {printf("ADD24");} ; condition : IDF opl IDF {printf("ADD25");} opl :SUP {printf("ADD26");} |SUPEG {printf("ADD27");} |INFEG {printf("ADD28");} |INF {printf("ADD29");} |EGAL {printf("ADD30");} |DIF {printf("ADD31");} |OP_OR {printf("ADD32");} |OP_NOT {printf("ADD33");} |OP_AND {printf("ADD34");} ; expression :IDF op IDF {printf("ADD35");} ; op :OP_PLUS {printf("ADD36");} |OP_MOINS {printf("ADD37");} |OP_MULT {printf("ADD38");} |OP_DIV {printf("ADD39");} |OP_MOD {printf("ADD40");} |OP_OR {printf("ADD41");} |OP_NOT {printf("ADD42");} |OP_AND {printf("ADD43");} |SUP {printf("ADD44");} |SUPEG {printf("ADD45");} |INFEG {printf("ADD46");} |INF {printf("ADD47");} |EGAL {printf("ADD48");} |DIF {printf("ADD49");} ; terme :NOMBRE {printf("ADD50");} |IDF {printf("ADD51");} ; %% int yyerror(char *s) { printf("<<< \n %s \n", s); } int main(void) { if (yyparse() == 0) printf(" Expression correcte\n"); }
pour que elle reconnecté du PASCAL sur tout au nivaux des ";"
Cette grammaire reconnais le langage suivant: (qui est mélange de C et PASCAL)
Pour l'exécution:
Et aussi c'est possible quelque indication sur la Sémantique que je n'est pas comancer.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 bison -d yacc.y flex lex.l gcc lex.yy.c yacc.tab.c -o lexyacc ./lexyacc
Merci d'avance.
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager