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
| %{
#include "declarations.h"
#include "unistd.h" /* pour le unlink */
#include "errno.h" /* pour les perror */
#include "string.h"
%}
%start programmes
%token PROGRAMME
%token DEBUT
%token FIN
%token DECLARATION
%token AFFECTATION
%token SI
%token ALORS
%token SINON
%token TANT_QUE
%token FAIRE
%token NON
%token FONCTION
%token PROCEDURE
%token OPERATION
%token COMPARAISON
%token AJOUT
%token <chaine> IDENT
%token <chaine> NBRE
%token <chaine> TYPE
%union
{
char chaine[256];
symbole * liste;
}
%%
programmes : PROGRAMME IDENT ';' déclaration instruction_composée'.'
;
déclaration : déclaration_var déclaration_sous_programmes
;
déclaration_var : déclaration_var DECLARATION liste_identificateurs ':' TYPE ';'
|
;
liste_identificateurs : IDENT
| liste_identificateurs ',' IDENT
;
déclarations_sous_programmes : déclarations_sous_programmes déclarations_sous_programme ';'
|
;
déclarations_sous_programme :entete_sous_programme déclaration instruction_composée
;
entete_sous_programme : FONCTION IDENT arguments ':' TYPE ';'
| PROCEDURE IDENT arguments ';'
;
arguments :'('liste_paramètres')'
|
;
liste_paramètres: paramètre
| listes_paramètres ';' paramètre
;
paramètre : IDENT ':' TYPE
| DECLARATION IDENT ':' TYPE
;
instruction_composée : DEBUT instructions_optionnelles FIN
;
instructions_optionnelles : liste_instructions
|
;
liste_instructions : instruction
| liste_instructions ';' instruction
;
instruction : variable AFFECTATION expression
| appel_de_procédure
| instruction_composée
| SI expression ALORS instruction SINON instruction
| TANT_QUE expression FAIRE instruction
;
variable : IDENT
;
appel_de_procédure : IDENT
| IDENT'('liste_expressions')'
;
liste_expressions : expression
| liste_expressions ',' expression
;
expression: expression_simple
| expression_simple COMPARAISON expression_simple
;
expression_simple : terme
| signe terme
| expression_simple AJOUT terme
;
terme : facteur
| terme OPERATION facteur
;
facteur : IDENT
| IDENT'('liste_expressions')''
| NBRE
|'('expression')'
| NON facteur
;
signe : '+'
| '-'
;
%%
int main (int argc, char * argv[])
{
symbole * pt_sym, * p;
FILE * fic_source;
char * fic_dest, * table_quad = "table_quad", * table_symb = "table_symb";
int i, l;
/* Vérification du nombre de paramètres */
if (--argc!=1 || !strcmp(argv[1], "-h"))
{
fprintf (stderr, "Usage: %s <fichier>\n", argv[0]);
exit (1);
}
else
{
fic_source = fopen (argv[1], "r");
if (!fic_source)
{
perror(argv[1]);
fprintf (stderr, "%s: Fichier \"%s\" illisible !\n", argv[0], argv[1]);
exit (2);
}
yyin = fic_source;
} |
Partager