Commit 5387786c authored by LucasDANIELE's avatar LucasDANIELE
Browse files

ajout des tokens, skip, methodes:'valeur','identifiant','terme','multiplication', 'somme'

parent 5f2862d7
......@@ -6,10 +6,196 @@ options {
PARSER_BEGIN(AnalyseurSyntaxique)
public class AnalyseurSyntaxique {}
package fr.insarouen.asi.compilation.basic.analyseurs;
import fr.insarouen.asi.compilation.basic.analyseurs.*;
import fr.insarouen.asi.compilation.basic.moteur.arbreSyntaxiqueAbstrait.*;
import fr.insarouen.asi.compilation.basic.moteur.*;
import fr.insarouen.asi.compilation.basic.moteur.expression.*;
import fr.insarouen.asi.compilation.basic.moteur.TypeIncompatibleException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class AnalyseurSyntaxique {
private Programme prog;
private Memoire mem;
public Programme programme() throws ParseException, IdentifiantException, TypeIncompatibleException,VariableInconnueException,TypeIncompatibleException{
mem = new Memoire();
prog = new Programme();
lire();
return prog;
}
}
PARSER_END(AnalyseurSyntaxique)
SKIP : {}
SKIP : {
" "
| "\t"
| "\r"
| "\n"
}
TOKEN : {
//INSTRUCTIONS
<LIRE:["l","L"]["i","I"]["r","R"]["e","E"]>
| <ECRIRE:["e","E"]["c","C"]["r","R"]["i","I"]["r","R"]["e","E"]>
| <ALLERA:["a","A"]["l","L"]["l","L"]["e","E"]["r","R"]["a","A"]>
| <RIEN:["r","R"]["i","I"]["e","E"]["n","N"]>
| <LABEL:":">
| <SI:["s","S"]["i","I"]>
| <ALORS:["a","A"]["l","L"]["o","O"]["r","R"]["s","S"]>
//OPERATIONS
//Opérations arihtmétiques
| <PLUS:"+">
| <MOINS:"-">
| <MUL:"*">
| <DIV:"/">
| <AFF:"=">
//Pour les booléens
| <NON:["n","N"]["o","O"]["n","N"]>
| <ET:["e","E"]["t","T"]>
| <OU:["o","O"]["u","U"]>
//Comparaison
| <EST_EGAL:"==">
| <DIFF:"!=">
| <SINF:"<">
| <INF:"<=">
| <SSUP:">">
| <SUP:">=">
//Prioriser opérations
| <PARENG:"(">
| <PAREND:")">
//EXPRESSIONS
| <ENTIER:(["0"-"9"])+>
| <REEL:((["0"-"9"])*"."(["0"-"9"])+) | ((["0"-"9"])+ "." (["0"-"9"])*) >
| <ID:["A"-"Z","a"-"z","_"](["A"-"Z","a"-"z","_","0"-"9"])*>
| <VRAI:["v","V"]["r","R"]["a","A"]["i","I"]>
| <FAUX:["f","F"]["a","A"]["u","U"]["x","X"]>
}
// void lire() throws IdentifiantException :
// {
// Identifiant id = null ;
// Instruction instruction;
// }
// {
// (
// ((id = identifiant()) < LABEL >) ? instruction = instruction()
// {
// if (instruction == null)
// prog.addInstruction(instruction);
// else
// prog.addInstructionAvecLabel(instruction, id);
// id = null;
// }
// )*
// < EOF >
// }
void lire() throws IdentifiantException,TypeIncompatibleException,TypeIncompatibleException,VariableInconnueException :
{
Expression e;
Memoire mem=new Memoire();
}
{
(
e=somme()
)
{
System.out.println(e.evaluer(mem));
}
}
Expression somme() throws IdentifiantException:
{
Expression g,d;
Token t;
}
{
g=multiplication()
(
(t=<PLUS> | t=<MOINS>)
d = somme(){
switch (t.kind) {
case PLUS:
return new Addition(g,d);
break;
case MOINS:
return new Soustraction(g,d);
break;
}
}
)?
{
return g;
}
}
Expression multiplication() throws IdentifiantException:
{
Expression g,d;
Token t;
}
{
g=terme()
(
(t=<MUL> | t=<DIV>)
d=multiplication(){
switch (t.kind) {
case MUL:
return new Multiplication(g,d);
case DIV:
return new Division(g,d);
}
}
)?
{return g;}
}
Expression terme() throws IdentifiantException :
{
Token t = null;
Valeur v;
Identifiant i;
Expression e;
}
{
(t=<MOINS>)?
(
( (v=valeur()){ e=new Constante(v); } )
| ( (i=identifiant()){ e=new IdentifiantDeVariable(i); } )
| ( <PARENG> (e=terme()) <PAREND> )
)
{
if (t == null)
return e;
else
return new Moins(e);
}
}
Identifiant identifiant() throws IdentifiantException :
{ Token t ; }
{
(t = < ID >)
{
return new Identifiant(t.image);
}
}
TOKEN : {}
Valeur valeur() :
{Token t ; }
{
( t = < VRAI > { return new Valeur(Boolean.valueOf(t.image)); } )
| ( t = < FAUX > { return new Valeur(Boolean.valueOf(t.image)); } )
| ( t = < ENTIER > { return new Valeur(Integer.valueOf(t.image)); } )
| ( t = < REEL > { return new Valeur(Float.valueOf(t.image)); } )
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment