diff --git a/ArbreAbstrait/Condition.java b/ArbreAbstrait/Condition.java new file mode 100644 index 0000000000000000000000000000000000000000..578e8da173b23c0f55ca716dd72d8f29c26de01a --- /dev/null +++ b/ArbreAbstrait/Condition.java @@ -0,0 +1,57 @@ +/* + * Represent an equal comparaison expression node inside the AST. + */ + + + +public class Condition extends Instruction { + private Expression condition; + private Instruction thenInstruction; + private Instruction elseInstruction; + + /** + * Constructor + */ + public Condition(Expression condition, Instruction thenInstruction, Instruction elseInstruction, String fl, int line, int col) { + super(fl, line, col); + this.condition = condition; + this.elseInstruction = elseInstruction; + this.thenInstruction = thenInstruction; + } + + /** + * Return the expression of the if condition + */ + public Expression getCondition(){ + return this.condition; + } + + /** + * Return the instruction in the then statement + */ + public Instruction getThenInstruction(){ + return this.thenInstruction; + } + + /** + * Return the instruction in the else statement + */ + public Instruction getElseInstruction(){ + return this.elseInstruction; + } + + /** + * Check if there is an else instruction + */ + public boolean hasElse(){ + return this.elseInstruction != null; + } + + + /** + * Accepts a AST visitor + */ + Object accept(ASTVisitor visitor){ + return visitor.visit(this); + } +} diff --git a/ArbreAbstrait/Parentheses.java b/ArbreAbstrait/Parentheses.java new file mode 100755 index 0000000000000000000000000000000000000000..c56ada7ec5a12e35c5e9a87e3c286ef766935c18 --- /dev/null +++ b/ArbreAbstrait/Parentheses.java @@ -0,0 +1,32 @@ +/* + * Represent an identifier node inside the AST. + */ + +public class Parentheses extends Expression { + /** + * Name of the + */ + private Expression exp; + + /** + * Constructor + */ + public Parentheses(Expression exp, String fl, int line, int col) { + super(fl, line, col); + this.exp = exp; + } + + /** + * Get the expression + */ + public Expression getExpression() { + return this.exp; + } + + /** + * Accepts a AST visitor + */ + Object accept(ASTVisitor visitor){ + return visitor.visit(this); + } +} diff --git a/Tests/test_base_with_sementic_issue.input b/Tests/test_base_with_sementic_issue.input index 126236c086ba36e63550184a7795a265d56f18c5..af87024dfe35a20bdbec1672a5cef0cc927b39db 100755 --- a/Tests/test_base_with_sementic_issue.input +++ b/Tests/test_base_with_sementic_issue.input @@ -1,4 +1,6 @@ programme Program debutprg - c = -2; + c = 1; + si c alors c = 2;sinon c = 0; + finsi finprg diff --git a/Visitors/ASTVisitor.java b/Visitors/ASTVisitor.java index 1acb5759996eb3528d09903cd6995e3f13dc51a6..a2eaa4c86a1aa9cfe1a889afee7088aee410415d 100755 --- a/Visitors/ASTVisitor.java +++ b/Visitors/ASTVisitor.java @@ -18,7 +18,7 @@ public interface ASTVisitor { Object visit(Chaine node); - // Object visit(Condition node); + Object visit(Condition node); // Object visit(DeclarationConstant node); Object visit(DeclarationProgramme node); @@ -42,7 +42,7 @@ public interface ASTVisitor { Object visit(Non node); Object visit(Ou node); - // Object visit(Parentheses node); + Object visit(Parentheses node); // Object visit(Pour node); Object visit(Produit node); diff --git a/Visitors/ByteCodeGenerator.java b/Visitors/ByteCodeGenerator.java index 4f0b76b897704db1c9a5ec4ed21f24bf021d7679..c577626de9e2fb72e6206e1a653b5e30c2502cc3 100755 --- a/Visitors/ByteCodeGenerator.java +++ b/Visitors/ByteCodeGenerator.java @@ -24,7 +24,7 @@ public class ByteCodeGenerator implements ASTVisitor { return null; } - // public Object visit(Condition node) { return null; } + public Object visit(Condition node) { return null; } // public Object visit(DeclarationConstant node) { return null; } @@ -72,7 +72,7 @@ public class ByteCodeGenerator implements ASTVisitor { public Object visit(Ou node) { return null; } - // public Object visit(Parentheses node) { return null; } + public Object visit(Parentheses node) { return null; } // public Object visit(Pour node) { return null; } diff --git a/Visitors/SemanticAnalyzer.java b/Visitors/SemanticAnalyzer.java index e659598a151c6c81f7cc895ce9b18ea7a9be0b0e..ccbf7119a8df62a08a526f7edbc95a872cd191b6 100755 --- a/Visitors/SemanticAnalyzer.java +++ b/Visitors/SemanticAnalyzer.java @@ -26,7 +26,7 @@ public class SemanticAnalyzer implements ASTVisitor { return null; } - // public Object visit(Condition node) { return null; } + public Object visit(Condition node) { return null; } // public Object visit(DeclarationConstant node) { return null; } @@ -74,7 +74,7 @@ public class SemanticAnalyzer implements ASTVisitor { public Object visit(Ou node) { return null; } - // public Object visit(Parentheses node) { return null; } + public Object visit(Parentheses node) { return null; } // public Object visit(Pour node) { return null; } diff --git a/Visitors/SourceCodeGenerator.java b/Visitors/SourceCodeGenerator.java index 52aa723a6660e880e10631497f8010e6b221f536..9d7c98f27cd62e11f2cdaaf68be5a09c26d3ddca 100755 --- a/Visitors/SourceCodeGenerator.java +++ b/Visitors/SourceCodeGenerator.java @@ -65,25 +65,25 @@ public class SourceCodeGenerator implements ASTVisitor { return null; } - // public Object visit(Condition node){ - // code += "si "; - // node.getCondition().accept(this); - // code += " alors"; - // level += 1; - // node.getThenInstruction().accept(this); - // if (node.hasElse()){ - // code += "\n"; - // addTabulation(level - 1); - // code += "sinon"; - // node.getElseInstruction().accept(this); - // } - // level -= 1; - // code += "\n"; - // addTabulation(); - // code += "finsi"; - // return null; - // } - // + public Object visit(Condition node) { + code += "si "; + node.getCondition().accept(this); + code += " alors"; + level += 1; + node.getThenInstruction().accept(this); + if (node.hasElse()) { + code += "\n"; + addTabulation(level - 1); + code += "sinon"; + node.getElseInstruction().accept(this); + } + level -= 1; + code += "\n"; + addTabulation(); + code += "finsi"; + return null; + } + // public Object visit(DeclarationConstant node){ // Symbole sym = TDS.getInstance().identifier(new // Entree(node.getIdentifier().getNom())); @@ -131,12 +131,12 @@ public class SourceCodeGenerator implements ASTVisitor { node.getDroite().accept(this); return null; } - - public Object visit(Ecrire node){ - code += "ecrire "; - node.getSource().accept(this); - code += ";"; - return null; + + public Object visit(Ecrire node) { + code += "ecrire "; + node.getSource().accept(this); + code += ";"; + return null; } public Object visit(Egal node) { @@ -176,18 +176,18 @@ public class SourceCodeGenerator implements ASTVisitor { node.getDroite().accept(this); return null; } - - public Object visit(Lire node){ - code += "lire "; - node.getDestination().accept(this); - code += ";"; - return null; + + public Object visit(Lire node) { + code += "lire "; + node.getDestination().accept(this); + code += ";"; + return null; } - - public Object visit(Moins node){ - code += node.operateur(); - node.getOperand().accept(this); - return null; + + public Object visit(Moins node) { + code += node.operateur(); + node.getOperand().accept(this); + return null; } public Object visit(Nombre node) { @@ -196,7 +196,7 @@ public class SourceCodeGenerator implements ASTVisitor { } public Object visit(Non node) { - code += node.operateur()+" "; + code += node.operateur() + " "; node.getOperand().accept(this); return null; } @@ -208,13 +208,13 @@ public class SourceCodeGenerator implements ASTVisitor { return null; } - // - // public Object visit(Parentheses node){ - // code += "("; - // node.getExpression().accept(this); - // code += ")"; - // return null; - // } + public Object visit(Parentheses node) { + code += "("; + node.getExpression().accept(this); + code += ")"; + return null; + } + // // public Object visit(Pour node){ // code += "pour "; diff --git a/hepial.cup b/hepial.cup index 596467f396d0536cad5567bceeb3a47f3dfea73b..3249edc60b22b3b013ce732b7b851c2a483431f3 100755 --- a/hepial.cup +++ b/hepial.cup @@ -23,7 +23,7 @@ non terminal declar_const; non terminal Expression expr, access, operand; non terminal for_instr; non terminal while_instr; -non terminal cond_instr; +non terminal Condition cond_instr; non terminal Ecrire write_instr; non terminal Lire read_instr; non terminal Instruction instr; @@ -104,7 +104,7 @@ expr ::= op_bin:exp {: RESULT = exp; :} | relation:exp {: RESULT = exp; :} | op_una:exp {: RESULT = exp; :} | operand:operand {: RESULT = operand; :} - | OPENPARENT expr CLOSEPARENT {: :}; + | OPENPARENT expr:exp CLOSEPARENT {: RESULT = new Parentheses(exp, "", expleft, expright); :}; body ::= instr_lst:instructions {: RESULT = new Bloc(instructions, "", instructionsleft, instructionsright); :}; @@ -118,10 +118,10 @@ instr_lst ::= {: RESULT = new ArrayList(); :} instr ::= assign:inst {: RESULT = inst; :} | write_instr:e {: RESULT = e; :} - | read_instr {: :} - | cond_instr {: :} - | while_instr {: :} - | for_instr {: :}; + | read_instr:r {: RESULT = r;:} + | cond_instr:c {: RESULT = c;:} + | while_instr:w {: :} + | for_instr:f {: :}; assign ::= access:dest EQUAL:e expr:src SEMICOLON {: RESULT = new Affectation(dest, src, "", eleft, eright); :}; @@ -130,9 +130,9 @@ write_instr ::= WRITE expr:a SEMICOLON {: RESULT = new Ecrire(a, "", aleft read_instr ::= READ IDENT:a SEMICOLON {: RESULT = new Lire(new Idf(a, "", aleft, aright), "", aleft, aright); :}; -cond_instr ::= IF expr THEN body ELSE body ENDIF {: :} - | IF expr THEN body ENDIF {: :}; +cond_instr ::= IF expr:condition THEN body:then_instr ELSE body:else_instr ENDIF {: RESULT = new Condition(condition, then_instr, else_instr, "", conditionleft, conditionright); :} + | IF expr:condition THEN body:then_instr ENDIF {: RESULT = new Condition(condition, then_instr, null, "", conditionleft, conditionright); :}; while_instr ::= WHILE expr DO body ENDWHILE {: :}; -for_instr ::= FOR IDENT FROM expr TO expr DO body ENDFOR {: :}; +for_instr ::= FOR IDENT FROM expr TO expr DO body ENDFOR {: :}; \ No newline at end of file