Skip to content
Snippets Groups Projects
Commit 5af6f4fd authored by lucien.noel's avatar lucien.noel
Browse files

push du travail de chris + début analyse semantique

parent 0fb4d072
Branches
No related tags found
No related merge requests found
...@@ -15,8 +15,8 @@ public class DeclarationVariable extends Declaration{ ...@@ -15,8 +15,8 @@ public class DeclarationVariable extends Declaration{
} }
} }
public Idf getIdentifier(){ public Idf getIdentifier(int i){
return listeId.get(0); return listeId.get(i);
} }
......
...@@ -12,6 +12,7 @@ public class Entree{ ...@@ -12,6 +12,7 @@ public class Entree{
public String getNom() { public String getNom() {
return this.varName; return this.varName;
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
......
This diff is collapsed.
programme Program programme Program
entier z; entier x, y;
entier x; booleen z;
booleen p;
debutprg debutprg
c = 1; x = 24;
si c alors c = 2;sinon c = 0; z = vrai;
finsi y = x + z;
ecrire "::: loop0";
pour x allantde 0 a 10 faire
ecrire x;
finpour
finprg finprg
...@@ -11,14 +11,28 @@ ...@@ -11,14 +11,28 @@
public class SemanticAnalyzer implements ASTVisitor { public class SemanticAnalyzer implements ASTVisitor {
public Object visit(Addition node) { public Object visit(Addition node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
if (!(node.getGauche() instanceof Nombre)) {
throw new RuntimeException("Impossible d'effectuer l'addition car la valeur de gauche n'est pas un nombre !");
}
if (!(node.getDroite() instanceof Nombre)) {
throw new RuntimeException("Impossible d'effectuer l'addition car la valeur de droite n'est pas un nombre !");
}
return null; return null;
} }
public Object visit(Affectation node) { public Object visit(Affectation node) {
node.getDestination().accept(this);
node.getSource().accept(this);
return null; return null;
} }
public Object visit(Bloc node) { public Object visit(Bloc node) {
for (Instruction inst : node.getInstructions()) {
inst.accept(this);
}
return null; return null;
} }
...@@ -26,75 +40,161 @@ public class SemanticAnalyzer implements ASTVisitor { ...@@ -26,75 +40,161 @@ public class SemanticAnalyzer implements ASTVisitor {
return null; return null;
} }
public Object visit(Condition node) { return null; } public Object visit(Condition node) {
node.getCondition().accept(this);
node.getThenInstruction().accept(this);
if (node.hasElse()) {
node.getElseInstruction().accept(this);
}
return null;
}
public Object visit(DeclarationConstant node) { return null; } public Object visit(DeclarationConstant node) {
node.getIdentifier().accept(this);
node.getConstantExpression().accept(this);
return null;
}
public Object visit(DeclarationProgramme node) { public Object visit(DeclarationProgramme node) {
node.getIdentifier().accept(this);
node.getDeclaration().accept(this);
node.getInstructions().accept(this);
return null;
}
public Object visit(DeclarationVariable node) {
for (int i = 0; i < node.listeId.size(); i++) {
node.getIdentifier(i).accept(this);
}
return null; return null;
} }
public Object visit(DeclarationVariable node) { return null; } public Object visit(Diff node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
public Object visit(Diff node) { return null; } return null;
}
public Object visit(Division node) { public Object visit(Division node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
return null; return null;
} }
public Object visit(Ecrire node) { return null; } public Object visit(Ecrire node) {
node.getSource().accept(this);
return null;
}
public Object visit(Egal node) { public Object visit(Egal node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
return null; return null;
} }
public Object visit(Et node) { public Object visit(Et node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
return null; return null;
} }
public Object visit(Faux node) { return null; } public Object visit(Faux node) {
return null;
}
public Object visit(Idf node) { public Object visit(Idf node) {
return null; return node.getNom();
} }
public Object visit(InfEgal node) { return null; } public Object visit(InfEgal node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
return null;
}
public Object visit(Inferieur node) { return null; } public Object visit(Inferieur node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
return null;
}
public Object visit(Lire node) { return null; } public Object visit(Lire node) {
node.getDestination().accept(this);
return null;
}
public Object visit(Moins node) { return null; } public Object visit(Moins node) {
node.getOperand().accept(this);
return null;
}
public Object visit(Nombre node) { public Object visit(Nombre node) {
return node.getValeur();
}
public Object visit(Non node) {
node.getOperand().accept(this);
return null; return null;
} }
public Object visit(Non node) { return null; } public Object visit(Ou node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
return null;
}
public Object visit(Ou node) { return null; } public Object visit(Parentheses node) {
node.getExpression().accept(this);
return null;
}
public Object visit(Parentheses node) { return null; } public Object visit(Pour node) {
node.getIteratorName().accept(this);
node.getFrom().accept(this);
node.getTo().accept(this);
node.getInstruction().accept(this);
public Object visit(Pour node) { return null; } return null;
}
public Object visit(Produit node) { public Object visit(Produit node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
return null; return null;
} }
public Object visit(Relation node) { return null; } public Object visit(Relation node) {
return null;
}
public Object visit(Soustraction node) { public Object visit(Soustraction node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
return null; return null;
} }
public Object visit(SupEgal node) { return null; } public Object visit(SupEgal node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
return null;
}
public Object visit(Superieur node) { return null; } public Object visit(Superieur node) {
node.getGauche().accept(this);
node.getDroite().accept(this);
return null;
}
public Object visit(Tantque node) { return null; } public Object visit(Tantque node) {
return null;
}
public Object visit(Unaire node) { return null; } public Object visit(Unaire node) {
return null;
}
public Object visit(Vrai node) { return null; } public Object visit(Vrai node) {
return null;
}
} }
...@@ -107,12 +107,22 @@ public class SourceCodeGenerator implements ASTVisitor { ...@@ -107,12 +107,22 @@ public class SourceCodeGenerator implements ASTVisitor {
} }
public Object visit(DeclarationVariable node) { public Object visit(DeclarationVariable node) {
Symbole sym = TDS.getInstance().identifier(new Entree(node.getIdentifier().getNom())); for (int i = 0; i < node.listeId.size(); i++) {
Symbole sym = TDS.getInstance().identifier(new Entree(node.listeId.get(i).getNom()));
code += sym + " "; code += sym + " ";
node.getIdentifier().accept(this); //code += node.listeId.get(i).getNom();
node.getIdentifier(i).accept(this);
if (!isParameterDeclaration) if (!isParameterDeclaration)
code += ";"; code += ";";
if (!(i == node.listeId.size() - 1)) {
code += "\n";
addTabulation();
}
}
return null; return null;
} }
......
hepial.cup 100755 → 100644
...@@ -73,8 +73,9 @@ declar_var ::= type:t l_ident:l SEMICOLON {: RESULT = new DeclarationVari ...@@ -73,8 +73,9 @@ declar_var ::= type:t l_ident:l SEMICOLON {: RESULT = new DeclarationVari
l_ident ::= IDENT:id {: ArrayList<Idf> liste = new ArrayList<Idf>(); l_ident ::= IDENT:id {: ArrayList<Idf> liste = new ArrayList<Idf>();
liste.add(new Idf(id,"",idleft,idright)); liste.add(new Idf(id,"",idleft,idright));
RESULT = liste;:} RESULT = liste;:}
| l_ident:idList COMMA IDENT:id {: ArrayList<Idf> idl = new ArrayList<Idf>((Collection<? extends Idf>) idList); | l_ident:idList COMMA IDENT:id {: ArrayList<Idf> idl = new ArrayList<Idf>((idList));
idl.add(new Idf(id,"",idleft,idright)); idl.add(new Idf(id,"",idleft,idright));
RESULT = idl;:}; RESULT = idl;:};
type ::= TINTEGER {: RESULT = (new entier());:} type ::= TINTEGER {: RESULT = (new entier());:}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment