Master 1 – Compilation

publicité
Master 1 – Compilation
Correction facture
Fichier jflex
import java_cup.runtime.*;
%%
%unicode
%line
%cup
%eof{
System.out.println("\nTerminé ");
%eof}
chiffre
entier
bl
lettre
lib
=
=
=
=
=
[0-9]
{chiffre}+
[ \t]
[a-z]
{lettre}+
%%
{bl}
{;}
\n
{;}
\,
{ return new Symbol(sym.VIRG); }
\.
{ return new Symbol (sym.PT); }
FACTURE
{ return new Symbol(sym.FACTURE); }
TOTAL
{ return new Symbol(sym.TOTAL); }
NO{chiffre}{3}
{ return new Symbol(sym.NO); }
{lib}
{ return new Symbol(sym.LIB); }
{entier}
{ return new Symbol(sym.ENTIER, new Integer(yytext())); }
.
{ return new Symbol(sym.error); }
fichier cup
// section 1: setup et code utilisateur
import java_cup.runtime.*;
action code {:
int somme = 0;
int quantite, prix;
:}
// section 2: terminaux et non terminaux
// les terminaux sont fournis par jflex
terminal FACTURE, NO, LIB, ENTIER, TOTAL, VIRG, PT;
non terminal facture, lignes, ligne;
start with facture;
// règles de précédences
// section4 : les règles
facture ::= FACTURE
{: System.out.println("Lecture d'une facture"); :}
NO lignes TOTAL ENTIER:n
{:
int total = ((Integer) n).intValue();
System.out.println("Facture terminée");
System.out.println("Somme calculée : " + somme + "\ttotal annoncé : " + total);
if (total == somme)
System.out.println("facture correcte");
else
System.out.println("facture incorrecte");
:}
PT
;
lignes ::= ligne
|
ligne lignes ;
ligne ::= LIB ENTIER:k
{:
quantite = ((Integer)k).intValue();
System.out.print("quantite " + quantite + "\t");
:}
ENTIER:n
{:
prix = ((Integer) n).intValue();
System.out.print("\tprix" + " " + prix + " ");
somme += prix*quantite;
System.out.println("\t" + somme);
:}
VIRG
;
Téléchargement