Initial Commit
This commit is contained in:
20
Makefile
Normal file
20
Makefile
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
CC = gcc
|
||||||
|
|
||||||
|
LIBS = -lm
|
||||||
|
CCFLAGS = -Wall -ggdb
|
||||||
|
|
||||||
|
OBJ = analyseur_lexical.o util.o
|
||||||
|
|
||||||
|
all: test_yylex
|
||||||
|
|
||||||
|
test_yylex: test_yylex.c $(OBJ)
|
||||||
|
$(CC) $(CCFLAGS) -o test_yylex test_yylex.c $(OBJ)
|
||||||
|
|
||||||
|
analyseur_lexical.o: analyseur_lexical.c
|
||||||
|
$(CC) $(CCFLAGS) -c $^
|
||||||
|
|
||||||
|
.PHONY : clean
|
||||||
|
|
||||||
|
clean:
|
||||||
|
- rm -f $(OBJ)
|
||||||
|
- rm -f test_yylex
|
||||||
10
header/analyseur_lexical.h
Normal file
10
header/analyseur_lexical.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef __LIRE_UNITE__
|
||||||
|
#define __LIRE_UNITE__
|
||||||
|
|
||||||
|
#include "stdio.h"
|
||||||
|
|
||||||
|
int yylex(void);
|
||||||
|
void nom_token( int token, char *nom, char *valeur );
|
||||||
|
void test_yylex_internal( FILE *yyin );
|
||||||
|
|
||||||
|
#endif
|
||||||
84
header/symboles.h
Normal file
84
header/symboles.h
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
/* symboles non terminaux */
|
||||||
|
|
||||||
|
#define EPSILON 0
|
||||||
|
|
||||||
|
#define NB_NON_TERMINAUX 42
|
||||||
|
|
||||||
|
#define _listeDecVariables_ 1
|
||||||
|
#define _listeDecFonctions_ 2
|
||||||
|
#define _declarationVariable_ 3
|
||||||
|
#define _declarationFonction_ 4
|
||||||
|
#define _listeParam_ 5
|
||||||
|
#define _listeInstructions_ 6
|
||||||
|
#define _instruction_ 8
|
||||||
|
#define _instructionAffect_ 9
|
||||||
|
#define _instructionBloc_ 10
|
||||||
|
#define _instructionSi_ 11
|
||||||
|
#define _instructionTantque_ 12
|
||||||
|
#define _instructionAppel_ 13
|
||||||
|
#define _instructionRetour_ 14
|
||||||
|
#define _instructionEcriture_ 15
|
||||||
|
#define _instructionVide_ 16
|
||||||
|
#define _var_ 17
|
||||||
|
#define _expression_ 18
|
||||||
|
#define _appelFct_ 19
|
||||||
|
#define _conjonction_ 20
|
||||||
|
#define _negation_ 21
|
||||||
|
#define _comparaison_ 22
|
||||||
|
#define _expArith_ 23
|
||||||
|
#define _terme_ 24
|
||||||
|
#define _facteur_ 25
|
||||||
|
#define _argumentsEffectifs_ 26
|
||||||
|
#define _listeExpressions_ 27
|
||||||
|
#define _listeExpressionsBis_ 7
|
||||||
|
#define _programme_ 28
|
||||||
|
#define _conjonctionBis_ 29
|
||||||
|
#define _optTailleTableau_ 30
|
||||||
|
#define _expArithBis_ 31
|
||||||
|
#define _optSinon_ 32
|
||||||
|
#define _comparaisonBis_ 33
|
||||||
|
#define _optDecVariables_ 34
|
||||||
|
#define _optIndice_ 35
|
||||||
|
#define _listeDecVariablesBis_ 36
|
||||||
|
#define _termeBis_ 38
|
||||||
|
#define _expressionBis_ 39
|
||||||
|
#define _instructionFaire_ 40
|
||||||
|
#define _optListeDecVariables_ 41
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* symboles terminaux */
|
||||||
|
#define NB_TERMINAUX 31
|
||||||
|
|
||||||
|
#define POINT_VIRGULE 1
|
||||||
|
#define PLUS 2
|
||||||
|
#define MOINS 3
|
||||||
|
#define FOIS 4
|
||||||
|
#define DIVISE 5
|
||||||
|
#define PARENTHESE_OUVRANTE 6
|
||||||
|
#define PARENTHESE_FERMANTE 7
|
||||||
|
#define CROCHET_OUVRANT 8
|
||||||
|
#define CROCHET_FERMANT 9
|
||||||
|
#define ACCOLADE_OUVRANTE 10
|
||||||
|
#define ACCOLADE_FERMANTE 11
|
||||||
|
#define EGAL 12
|
||||||
|
#define INFERIEUR 13
|
||||||
|
#define ET 14
|
||||||
|
#define OU 15
|
||||||
|
#define NON 16
|
||||||
|
#define SI 17
|
||||||
|
#define ALORS 18
|
||||||
|
#define SINON 19
|
||||||
|
#define TANTQUE 20
|
||||||
|
#define FAIRE 21
|
||||||
|
#define ENTIER 22
|
||||||
|
#define RETOUR 23
|
||||||
|
#define LIRE 24
|
||||||
|
#define ECRIRE 25
|
||||||
|
#define ID_VAR 26
|
||||||
|
#define ID_FCT 27
|
||||||
|
#define NOMBRE 28
|
||||||
|
#define FIN 29
|
||||||
|
#define VIRGULE 30
|
||||||
|
|
||||||
|
|
||||||
12
header/util.h
Normal file
12
header/util.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef __UTIL__
|
||||||
|
#define __UTIL__
|
||||||
|
|
||||||
|
char *duplique_chaine(char *s);
|
||||||
|
void erreur(char *message);
|
||||||
|
void erreur_1s(char *message, char *s);
|
||||||
|
void affiche_balise_ouvrante(const char *fct_, int trace_xml);
|
||||||
|
void affiche_balise_fermante(const char *fct_, int trace_xml);
|
||||||
|
void affiche_element(char *fct_, char *texte_, int trace_xml);
|
||||||
|
void affiche_texte(char *texte_, int trace_xml);
|
||||||
|
|
||||||
|
#endif
|
||||||
154
src/analyseur_lexical.c
Normal file
154
src/analyseur_lexical.c
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "symboles.h"
|
||||||
|
#include "analyseur_lexical.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#define YYTEXT_MAX 100
|
||||||
|
#define is_num(c)(('0' <= (c)) && ((c) <= '9'))
|
||||||
|
#define is_maj(c)(('A' <= (c)) && ((c) <= 'Z'))
|
||||||
|
#define is_min(c)(('a' <= (c)) && ((c) <= 'z'))
|
||||||
|
#define is_alpha(c)(is_maj(c) || is_min(c) || (c) == '_' || (c) == '$')
|
||||||
|
#define is_alphanum(c)(is_num((c)) || is_alpha((c)))
|
||||||
|
|
||||||
|
extern FILE *yyin;
|
||||||
|
|
||||||
|
char *tableMotsClefs[] = {
|
||||||
|
"si",
|
||||||
|
};
|
||||||
|
|
||||||
|
int codeMotClefs[] = {
|
||||||
|
SI,
|
||||||
|
};
|
||||||
|
|
||||||
|
char yytext[YYTEXT_MAX];
|
||||||
|
int yyleng;
|
||||||
|
int nbMotsClefs = 9;
|
||||||
|
/* Compter les lignes pour afficher les messages d'erreur avec numero ligne */
|
||||||
|
int nb_ligne = 1;
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
* Fonction qui ignore les espaces et commentaires.
|
||||||
|
* Renvoie -1 si arrivé à la fin du fichier, 0 si tout va bien
|
||||||
|
******************************************************************************/
|
||||||
|
int mangeEspaces()
|
||||||
|
{
|
||||||
|
char c = fgetc(yyin);
|
||||||
|
int comment = 0;
|
||||||
|
while( comment || (c == ' ') || (c == '\n') || (c == '\t') || (c == '#' ) ) {
|
||||||
|
if( c == '#' ) {
|
||||||
|
comment = 1;
|
||||||
|
}
|
||||||
|
if( c == '\n' ) {
|
||||||
|
nb_ligne++;
|
||||||
|
comment = 0;
|
||||||
|
}
|
||||||
|
c = fgetc(yyin);
|
||||||
|
}
|
||||||
|
if ( feof(yyin) ) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ungetc(c, yyin);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
* Lit un caractère et le stocke dans le buffer yytext
|
||||||
|
******************************************************************************/
|
||||||
|
char lireCar(void)
|
||||||
|
{
|
||||||
|
yytext[yyleng++] = fgetc(yyin);
|
||||||
|
yytext[yyleng] = '\0';
|
||||||
|
return yytext[yyleng - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
* Remet le dernier caractère lu au buffer clavier et enlève du buffer yytext
|
||||||
|
******************************************************************************/
|
||||||
|
void delireCar()
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
c = yytext[yyleng - 1];
|
||||||
|
yytext[--yyleng] = '\0';
|
||||||
|
ungetc(c, yyin);
|
||||||
|
}
|
||||||
|
/*******************************************************************************
|
||||||
|
* Fonction principale de l'analyseur lexical, lit les caractères de yyin et
|
||||||
|
* renvoie les tokens sous forme d'entier. Le code de chaque unité est défini
|
||||||
|
* dans symboles.h sinon (mot clé, idententifiant, etc.). Pour les tokens de
|
||||||
|
* type ID_FCT, ID_VAR et NOMBRE la valeur du token est dans yytext, visible
|
||||||
|
* dans l'analyseur syntaxique.
|
||||||
|
******************************************************************************/
|
||||||
|
int yylex(void)
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
int i;
|
||||||
|
yytext[yyleng = 0] = '\0';
|
||||||
|
// COMPLÉTER
|
||||||
|
}
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
* Fonction auxiliaire appelée par l'analyseur syntaxique tout simplement pour
|
||||||
|
* afficher des messages d'erreur et l'arbre XML
|
||||||
|
******************************************************************************/
|
||||||
|
void nom_token( int token, char *nom, char *valeur ) {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
strcpy( nom, "symbole" );
|
||||||
|
if(token == POINT_VIRGULE) strcpy( valeur, "POINT_VIRGULE");
|
||||||
|
else if(token == PLUS) strcpy(valeur, "PLUS");
|
||||||
|
else if(token == MOINS) strcpy(valeur, "MOINS");
|
||||||
|
else if(token == FOIS) strcpy(valeur, "FOIS");
|
||||||
|
else if(token == DIVISE) strcpy(valeur, "DIVISE");
|
||||||
|
else if(token == PARENTHESE_OUVRANTE) strcpy(valeur, "PARENTHESE_OUVRANTE");
|
||||||
|
else if(token == PARENTHESE_FERMANTE) strcpy(valeur, "PARENTHESE_FERMANTE");
|
||||||
|
else if(token == CROCHET_OUVRANT) strcpy(valeur, "CROCHET_OUVRANT");
|
||||||
|
else if(token == CROCHET_FERMANT) strcpy(valeur, "CROCHET_FERMANT");
|
||||||
|
else if(token == ACCOLADE_OUVRANTE) strcpy(valeur, "ACCOLADE_OUVRANTE");
|
||||||
|
else if(token == ACCOLADE_FERMANTE) strcpy(valeur, "ACCOLADE_FERMANTE");
|
||||||
|
else if(token == EGAL) strcpy(valeur, "EGAL");
|
||||||
|
else if(token == INFERIEUR) strcpy(valeur, "INFERIEUR");
|
||||||
|
else if(token == ET) strcpy(valeur, "ET");
|
||||||
|
else if(token == OU) strcpy(valeur, "OU");
|
||||||
|
else if(token == NON) strcpy(valeur, "NON");
|
||||||
|
else if(token == FIN) strcpy(valeur, "FIN");
|
||||||
|
else if(token == VIRGULE) strcpy(valeur, "VIRGULE");
|
||||||
|
|
||||||
|
else if( token == ID_VAR ) {
|
||||||
|
strcpy( nom, "id_variable" );
|
||||||
|
strcpy( valeur, yytext );
|
||||||
|
}
|
||||||
|
else if( token == ID_FCT ) {
|
||||||
|
strcpy( nom, "id_fonction" );
|
||||||
|
strcpy( valeur, yytext );
|
||||||
|
}
|
||||||
|
else if( token == NOMBRE ) {
|
||||||
|
strcpy( nom, "nombre" );
|
||||||
|
strcpy( valeur, yytext );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
strcpy( nom, "mot_clef" );
|
||||||
|
for(i=0; i < nbMotsClefs; i++){
|
||||||
|
if( token == codeMotClefs[i] ){
|
||||||
|
strcpy( valeur, tableMotsClefs[i] );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*******************************************************************************
|
||||||
|
* Fonction auxiliaire appelée par le compilo en mode -l, pour tester l'analyseur
|
||||||
|
* lexical et, étant donné un programme en entrée, afficher la liste des tokens.
|
||||||
|
******************************************************************************/
|
||||||
|
|
||||||
|
void test_yylex_internal(FILE *yyin) {
|
||||||
|
int uniteCourante;
|
||||||
|
char nom[100];
|
||||||
|
char valeur[100];
|
||||||
|
do {
|
||||||
|
uniteCourante = yylex();
|
||||||
|
nom_token( uniteCourante, nom, valeur );
|
||||||
|
printf("%s\t%s\t%s\n", yytext, nom, valeur);
|
||||||
|
} while (uniteCourante != FIN);
|
||||||
|
}
|
||||||
17
src/test_yylex.c
Normal file
17
src/test_yylex.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "analyseur_lexical.h"
|
||||||
|
#include "symboles.h"
|
||||||
|
|
||||||
|
char yytext[100];
|
||||||
|
FILE *yyin;
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
yyin = fopen(argv[1], "r");
|
||||||
|
if(yyin == NULL){
|
||||||
|
fprintf(stderr, "impossible d'ouvrir le fichier %s\n", argv[1]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
test_yylex_internal(yyin);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
102
src/util.c
Normal file
102
src/util.c
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
extern int nb_ligne;
|
||||||
|
int indent_xml = 0;
|
||||||
|
int indent_step = 1; // set to 0 for no indentation
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
void erreur(char *message) {
|
||||||
|
fprintf (stderr, "Ligne %d : ", nb_ligne);
|
||||||
|
fprintf (stderr, "%s\n", message);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
void erreur_1s(char *message, char *s) {
|
||||||
|
fprintf( stderr, "Ligne %d : ", nb_ligne );
|
||||||
|
fprintf( stderr, message, s );
|
||||||
|
fprintf( stderr, "\n" );
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
char *duplique_chaine(char *src) {
|
||||||
|
char *dest = malloc(sizeof(char) * strlen(src));
|
||||||
|
strcpy(dest, src);
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
void indent() {
|
||||||
|
int i;
|
||||||
|
for( i = 0; i < indent_xml; i++ ) {
|
||||||
|
printf( " " );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
void affiche_balise_ouvrante(const char *fct_, int trace_xml) {
|
||||||
|
if( trace_xml ) {
|
||||||
|
indent();
|
||||||
|
indent_xml += indent_step ;
|
||||||
|
fprintf (stdout, "<%s>\n", fct_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
void affiche_balise_fermante(const char *fct_, int trace_xml) {
|
||||||
|
if(trace_xml) {
|
||||||
|
indent_xml -= indent_step ;
|
||||||
|
indent();
|
||||||
|
fprintf (stdout, "</%s>\n", fct_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
void affiche_texte(char *texte_, int trace_xml) {
|
||||||
|
if(trace_xml) {
|
||||||
|
indent();
|
||||||
|
fprintf (stdout, "%s\n", texte_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
void affiche_xml_texte( char *texte_ ) {
|
||||||
|
int i = 0;
|
||||||
|
while( texte_[ i ] != '\0' ) {
|
||||||
|
if( texte_[ i ] == '<' ) {
|
||||||
|
fprintf( stdout, "<" );
|
||||||
|
}
|
||||||
|
else if( texte_[ i ] == '>' ) {
|
||||||
|
fprintf( stdout, ">" );
|
||||||
|
}
|
||||||
|
else if( texte_[ i ] == '&' ) {
|
||||||
|
fprintf( stdout, "&" );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
putchar( texte_[i] );
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
void affiche_element(char *fct_, char *texte_, int trace_xml) {
|
||||||
|
if(trace_xml) {
|
||||||
|
indent();
|
||||||
|
fprintf (stdout, "<%s>", fct_ );
|
||||||
|
affiche_xml_texte( texte_ );
|
||||||
|
fprintf (stdout, "</%s>\n", fct_ );
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
support/checklist.pdf
Normal file
BIN
support/checklist.pdf
Normal file
Binary file not shown.
BIN
support/cm01_intro_compil.pdf
Normal file
BIN
support/cm01_intro_compil.pdf
Normal file
Binary file not shown.
7
test/affect-err.l
Normal file
7
test/affect-err.l
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
entier $a;
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
$a = 5;
|
||||||
|
ecrire( $a );
|
||||||
|
}
|
||||||
|
$extra = 0;
|
||||||
22
test/affect-err.lex
Normal file
22
test/affect-err.lex
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
entier mot_clef entier
|
||||||
|
$a id_variable $a
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
main id_fonction main
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
$a id_variable $a
|
||||||
|
= symbole EGAL
|
||||||
|
5 nombre 5
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
ecrire mot_clef ecrire
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
$a id_variable $a
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
$extra id_variable $extra
|
||||||
|
= symbole EGAL
|
||||||
|
0 nombre 0
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
symbole FIN
|
||||||
6
test/affect.l
Normal file
6
test/affect.l
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
entier $a;
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
$a = 1;
|
||||||
|
ecrire($a);
|
||||||
|
}
|
||||||
18
test/affect.lex
Normal file
18
test/affect.lex
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
entier mot_clef entier
|
||||||
|
$a id_variable $a
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
main id_fonction main
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
$a id_variable $a
|
||||||
|
= symbole EGAL
|
||||||
|
1 nombre 1
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
ecrire mot_clef ecrire
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
$a id_variable $a
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
symbole FIN
|
||||||
11
test/boucle.l
Normal file
11
test/boucle.l
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
entier $i, entier $carre;
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
$i = 0;
|
||||||
|
tantque $i < 10 faire
|
||||||
|
{
|
||||||
|
$carre = $i * $i;
|
||||||
|
ecrire( $carre );
|
||||||
|
$i = $i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
40
test/boucle.lex
Normal file
40
test/boucle.lex
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
entier mot_clef entier
|
||||||
|
$i id_variable $i
|
||||||
|
, symbole VIRGULE
|
||||||
|
entier mot_clef entier
|
||||||
|
$carre id_variable $carre
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
main id_fonction main
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
$i id_variable $i
|
||||||
|
= symbole EGAL
|
||||||
|
0 nombre 0
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
tantque mot_clef tantque
|
||||||
|
$i id_variable $i
|
||||||
|
< symbole INFERIEUR
|
||||||
|
10 nombre 10
|
||||||
|
faire mot_clef faire
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
$carre id_variable $carre
|
||||||
|
= symbole EGAL
|
||||||
|
$i id_variable $i
|
||||||
|
* symbole FOIS
|
||||||
|
$i id_variable $i
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
ecrire mot_clef ecrire
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
$carre id_variable $carre
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$i id_variable $i
|
||||||
|
= symbole EGAL
|
||||||
|
$i id_variable $i
|
||||||
|
+ symbole PLUS
|
||||||
|
1 nombre 1
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
symbole FIN
|
||||||
5
test/expression.l
Normal file
5
test/expression.l
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
entier $a;
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
ecrire(5 * 2);
|
||||||
|
}
|
||||||
16
test/expression.lex
Normal file
16
test/expression.lex
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
entier mot_clef entier
|
||||||
|
$a id_variable $a
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
main id_fonction main
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
ecrire mot_clef ecrire
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
5 nombre 5
|
||||||
|
* symbole FOIS
|
||||||
|
2 nombre 2
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
symbole FIN
|
||||||
23
test/max.l
Normal file
23
test/max.l
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
max( entier $a, entier $b )
|
||||||
|
{
|
||||||
|
si $a < $b alors {
|
||||||
|
retour $b;
|
||||||
|
}
|
||||||
|
retour $a;
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
entier $v_1, entier $v_2;
|
||||||
|
{
|
||||||
|
$v_1 = lire();
|
||||||
|
$v_2 = lire();
|
||||||
|
si max( $v_1, $v_2 ) = $v_1 alors
|
||||||
|
{
|
||||||
|
ecrire( $v_1 );
|
||||||
|
}
|
||||||
|
sinon
|
||||||
|
{
|
||||||
|
ecrire( $v_2 );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
72
test/max.lex
Normal file
72
test/max.lex
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
max id_fonction max
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
entier mot_clef entier
|
||||||
|
$a id_variable $a
|
||||||
|
, symbole VIRGULE
|
||||||
|
entier mot_clef entier
|
||||||
|
$b id_variable $b
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
si mot_clef si
|
||||||
|
$a id_variable $a
|
||||||
|
< symbole INFERIEUR
|
||||||
|
$b id_variable $b
|
||||||
|
alors mot_clef alors
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
retour mot_clef retour
|
||||||
|
$b id_variable $b
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
retour mot_clef retour
|
||||||
|
$a id_variable $a
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
main id_fonction main
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
entier mot_clef entier
|
||||||
|
$v_1 id_variable $v_1
|
||||||
|
, symbole VIRGULE
|
||||||
|
entier mot_clef entier
|
||||||
|
$v_2 id_variable $v_2
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
$v_1 id_variable $v_1
|
||||||
|
= symbole EGAL
|
||||||
|
lire mot_clef lire
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$v_2 id_variable $v_2
|
||||||
|
= symbole EGAL
|
||||||
|
lire mot_clef lire
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
si mot_clef si
|
||||||
|
max id_fonction max
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
$v_1 id_variable $v_1
|
||||||
|
, symbole VIRGULE
|
||||||
|
$v_2 id_variable $v_2
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
= symbole EGAL
|
||||||
|
$v_1 id_variable $v_1
|
||||||
|
alors mot_clef alors
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
ecrire mot_clef ecrire
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
$v_1 id_variable $v_1
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
sinon mot_clef sinon
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
ecrire mot_clef ecrire
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
$v_2 id_variable $v_2
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
symbole FIN
|
||||||
57
test/tri.l
Normal file
57
test/tri.l
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
entier $tab[ 10 ];
|
||||||
|
|
||||||
|
initialiser()
|
||||||
|
{
|
||||||
|
$tab[0] = 8; $tab[1] = 6; $tab[2] = 9;
|
||||||
|
$tab[3] = 9; $tab[4] = 4; $tab[5] = 2;
|
||||||
|
$tab[6] = 3; $tab[7] = 1; $tab[8] = 4;
|
||||||
|
$tab[9] = 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
afficher( entier $n )
|
||||||
|
entier $i;
|
||||||
|
{
|
||||||
|
$i = 0;
|
||||||
|
tantque $i < $n faire {
|
||||||
|
ecrire( $tab[ $i ] );
|
||||||
|
$i = $i + 1;
|
||||||
|
}
|
||||||
|
ecrire( 0 ); # marqueur fin de tableau
|
||||||
|
}
|
||||||
|
|
||||||
|
echanger( entier $i, entier $j )
|
||||||
|
entier $temp;
|
||||||
|
{
|
||||||
|
$temp = $tab[ $j ];
|
||||||
|
$tab[ $j ] = $tab[ $i ];
|
||||||
|
$tab[ $i ] = $temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
trier( entier $n )
|
||||||
|
entier $echange, entier $j, entier $m;
|
||||||
|
{
|
||||||
|
$m = $n;
|
||||||
|
$echange = 1;
|
||||||
|
tantque $echange = 1 faire
|
||||||
|
{
|
||||||
|
$echange = 0;
|
||||||
|
$j = 0;
|
||||||
|
tantque $j < $m - 1 faire
|
||||||
|
{
|
||||||
|
si $tab[ $j + 1 ] < $tab[ $j ] alors {
|
||||||
|
echanger( $j, $j + 1 );
|
||||||
|
$echange = 1;
|
||||||
|
}
|
||||||
|
$j = $j + 1;
|
||||||
|
}
|
||||||
|
$m = $m - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
{
|
||||||
|
initialiser();
|
||||||
|
afficher( 10 );
|
||||||
|
trier( 10 );
|
||||||
|
afficher( 10 );
|
||||||
|
}
|
||||||
271
test/tri.lex
Normal file
271
test/tri.lex
Normal file
@@ -0,0 +1,271 @@
|
|||||||
|
entier mot_clef entier
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
10 nombre 10
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
initialiser id_fonction initialiser
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
0 nombre 0
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
8 nombre 8
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
1 nombre 1
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
6 nombre 6
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
2 nombre 2
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
9 nombre 9
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
3 nombre 3
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
9 nombre 9
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
4 nombre 4
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
4 nombre 4
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
5 nombre 5
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
2 nombre 2
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
6 nombre 6
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
3 nombre 3
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
7 nombre 7
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
1 nombre 1
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
8 nombre 8
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
4 nombre 4
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
9 nombre 9
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
5 nombre 5
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
afficher id_fonction afficher
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
entier mot_clef entier
|
||||||
|
$n id_variable $n
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
entier mot_clef entier
|
||||||
|
$i id_variable $i
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
$i id_variable $i
|
||||||
|
= symbole EGAL
|
||||||
|
0 nombre 0
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
tantque mot_clef tantque
|
||||||
|
$i id_variable $i
|
||||||
|
< symbole INFERIEUR
|
||||||
|
$n id_variable $n
|
||||||
|
faire mot_clef faire
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
ecrire mot_clef ecrire
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
$i id_variable $i
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$i id_variable $i
|
||||||
|
= symbole EGAL
|
||||||
|
$i id_variable $i
|
||||||
|
+ symbole PLUS
|
||||||
|
1 nombre 1
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
ecrire mot_clef ecrire
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
0 nombre 0
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
echanger id_fonction echanger
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
entier mot_clef entier
|
||||||
|
$i id_variable $i
|
||||||
|
, symbole VIRGULE
|
||||||
|
entier mot_clef entier
|
||||||
|
$j id_variable $j
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
entier mot_clef entier
|
||||||
|
$temp id_variable $temp
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
$temp id_variable $temp
|
||||||
|
= symbole EGAL
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
$j id_variable $j
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
$j id_variable $j
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
$i id_variable $i
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
$i id_variable $i
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
= symbole EGAL
|
||||||
|
$temp id_variable $temp
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
trier id_fonction trier
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
entier mot_clef entier
|
||||||
|
$n id_variable $n
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
entier mot_clef entier
|
||||||
|
$echange id_variable $echange
|
||||||
|
, symbole VIRGULE
|
||||||
|
entier mot_clef entier
|
||||||
|
$j id_variable $j
|
||||||
|
, symbole VIRGULE
|
||||||
|
entier mot_clef entier
|
||||||
|
$m id_variable $m
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
$m id_variable $m
|
||||||
|
= symbole EGAL
|
||||||
|
$n id_variable $n
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$echange id_variable $echange
|
||||||
|
= symbole EGAL
|
||||||
|
1 nombre 1
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
tantque mot_clef tantque
|
||||||
|
$echange id_variable $echange
|
||||||
|
= symbole EGAL
|
||||||
|
1 nombre 1
|
||||||
|
faire mot_clef faire
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
$echange id_variable $echange
|
||||||
|
= symbole EGAL
|
||||||
|
0 nombre 0
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$j id_variable $j
|
||||||
|
= symbole EGAL
|
||||||
|
0 nombre 0
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
tantque mot_clef tantque
|
||||||
|
$j id_variable $j
|
||||||
|
< symbole INFERIEUR
|
||||||
|
$m id_variable $m
|
||||||
|
- symbole MOINS
|
||||||
|
1 nombre 1
|
||||||
|
faire mot_clef faire
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
si mot_clef si
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
$j id_variable $j
|
||||||
|
+ symbole PLUS
|
||||||
|
1 nombre 1
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
< symbole INFERIEUR
|
||||||
|
$tab id_variable $tab
|
||||||
|
[ symbole CROCHET_OUVRANT
|
||||||
|
$j id_variable $j
|
||||||
|
] symbole CROCHET_FERMANT
|
||||||
|
alors mot_clef alors
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
echanger id_fonction echanger
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
$j id_variable $j
|
||||||
|
, symbole VIRGULE
|
||||||
|
$j id_variable $j
|
||||||
|
+ symbole PLUS
|
||||||
|
1 nombre 1
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
$echange id_variable $echange
|
||||||
|
= symbole EGAL
|
||||||
|
1 nombre 1
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
$j id_variable $j
|
||||||
|
= symbole EGAL
|
||||||
|
$j id_variable $j
|
||||||
|
+ symbole PLUS
|
||||||
|
1 nombre 1
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
$m id_variable $m
|
||||||
|
= symbole EGAL
|
||||||
|
$m id_variable $m
|
||||||
|
- symbole MOINS
|
||||||
|
1 nombre 1
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
main id_fonction main
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
{ symbole ACCOLADE_OUVRANTE
|
||||||
|
initialiser id_fonction initialiser
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
afficher id_fonction afficher
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
10 nombre 10
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
trier id_fonction trier
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
10 nombre 10
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
afficher id_fonction afficher
|
||||||
|
( symbole PARENTHESE_OUVRANTE
|
||||||
|
10 nombre 10
|
||||||
|
) symbole PARENTHESE_FERMANTE
|
||||||
|
; symbole POINT_VIRGULE
|
||||||
|
} symbole ACCOLADE_FERMANTE
|
||||||
|
symbole FIN
|
||||||
Reference in New Issue
Block a user