From 19af482a32a318e6cb6bf2bfa88169f1ac43ff87 Mon Sep 17 00:00:00 2001 From: Vu Tuan Minh Date: Mon, 28 Apr 2025 06:20:11 +0200 Subject: [PATCH] error LL1 in function and proto in Parser. too weird. --- src/main/antlr/VSLParser.g | 20 +++++++++++-- src/main/java/TP2/asd/SymTable.java | 34 +++++++++++------------ src/main/java/TP2/asd/toLLVM_Visitor.java | 11 ++++++-- tests/aLaMain.vsl | 8 +++--- 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/main/antlr/VSLParser.g b/src/main/antlr/VSLParser.g index 5b2531f..568fa6b 100644 --- a/src/main/antlr/VSLParser.g +++ b/src/main/antlr/VSLParser.g @@ -52,17 +52,31 @@ functions returns [ArrayList out] ; proto returns [Function out]: - PROTOTYPE t=type i=ident ParO v=liste_param ParF + PROTOTYPE t=type i=ident ParO + ( + v=liste_param ParF { $out=new PrototypeImp($t.return_type,$i.out, $v.out); } + | ParF + { + $out=new PrototypeImp($t.return_type,$i.out, new ArrayList<>()); + } + ) ; function returns [Function out]: - FUNCTION t=type i=ident ParO v=liste_param ParF instr= instruction + FUNCTION t=type i=ident ParO + ( + v=liste_param ParF instr= instruction { $out=new FunctionImp($t.return_type, $i.out, $v.out, $instr.out); } + | ParF instr= instruction + { + $out=new FunctionImp($t.return_type, $i.out, new ArrayList<>(), $instr.out); + } + ) ; list_decls returns [ArrayList out] @@ -207,7 +221,7 @@ lit returns [Expression out]: } | t=ident ( - ParO (para=list_expression) ParF {$out = new AppealImp($t.out,$para.out);} + ParO (para=list_expression)? ParF {$out = new AppealImp($t.out,$para.out);} | {$out=new VarImp($t.out);} ) ; diff --git a/src/main/java/TP2/asd/SymTable.java b/src/main/java/TP2/asd/SymTable.java index 35b856f..c58c823 100644 --- a/src/main/java/TP2/asd/SymTable.java +++ b/src/main/java/TP2/asd/SymTable.java @@ -8,18 +8,18 @@ import TP2.llvm.ProgramLLVM.DefineLLVMImp; public class SymTable { private PStack> varMap; - private PMap fuctionsMap; + private PMap functionsMap; private int id=1; private int idLabel = 1; public SymTable(){ this.varMap= ConsPStack.singleton(HashTreePMap.empty()); - this.fuctionsMap = HashTreePMap.empty(); + this.functionsMap = HashTreePMap.empty(); } - public SymTable(PStack> varMap, PMap fuctionsMap, int id, int idLabel){ + public SymTable(PStack> varMap, PMap functionsMap, int id, int idLabel){ this.varMap= varMap; this.id = id; - this.fuctionsMap = fuctionsMap; + this.functionsMap = functionsMap; this.idLabel = idLabel; } @@ -34,12 +34,12 @@ public class SymTable { } public SymTable newBlock() { - return new SymTable(varMap.plus(HashTreePMap.empty()), fuctionsMap, id, idLabel); + return new SymTable(varMap.plus(HashTreePMap.empty()), functionsMap, id, idLabel); } public SymTable outBlock() { if (varMap.size() > 1) { - return new SymTable(varMap.minus(0), fuctionsMap, id, idLabel); + return new SymTable(varMap.minus(0), functionsMap, id, idLabel); } else { System.err.println("Vide"); return this; @@ -73,9 +73,9 @@ public class SymTable { public SymTable addFunction(DefineLLVMImp function, Boolean isProto){ - ValueFunMap value = this.fuctionsMap.get(function.name()); + ValueFunMap value = this.functionsMap.get(function.name()); if(value == null || (value!=null && value.isProto && !isProto)){ - return new SymTable(this.varMap, this.fuctionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id, this.idLabel); + return new SymTable(this.varMap, this.functionsMap.plus(function.name(),new ValueFunMap(function,isProto)), this.id, this.idLabel); } else{ if(value.isProto) System.err.println("[VSL compile error] : Le prototype "+function.name()+" existe déjà"); @@ -86,7 +86,7 @@ public class SymTable { } public ValueFunMap getFunction(String name){ - return this.fuctionsMap.get(name); + return this.functionsMap.get(name); } public int getId(){ @@ -129,7 +129,7 @@ public class SymTable { } String realName = nomVar + id; top = top.plus(nomVar, new ValueVarMap(new Type_intImp(), id, false)); - SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), fuctionsMap, id+1, idLabel); + SymTable newSym = new SymTable(varMap.minus(0).plus(0, top), functionsMap, id+1, idLabel); return new Result(newSym, realName); } @@ -150,12 +150,10 @@ public class SymTable { } public Boolean isPresentVar(String nomVar){ - boolean x= false; - while(!x){ - for (PMap scope : varMap){ - x=scope.containsKey(nomVar); + for (PMap scope : varMap) { + if (scope.containsKey(nomVar)) { + return true; } - if (x) return true; } System.err.println(nomVar+" n'est pas trouvé"); return false; @@ -189,7 +187,7 @@ public class SymTable { return this; } top = top.plus(nomVar, new ValueVarMap(type, id, isParam)); - return new SymTable(varMap.minus(0).plus(0, top), fuctionsMap, id+1, idLabel); + return new SymTable(varMap.minus(0).plus(0, top), functionsMap, id+1, idLabel); } public String print_all(){ @@ -199,7 +197,7 @@ public class SymTable { int scopeLevel = varMap.size(); for (PMap scope : varMap) { - str.append("Scope Level ").append(scopeLevel--).append(":\n"); + str.append("Block Level ").append(scopeLevel--).append(":\n"); for (String varName : scope.keySet()) { ValueVarMap value = scope.get(varName); str.append("Name: ").append(varName) @@ -211,7 +209,7 @@ public class SymTable { } str.append("FUNCTIONS:\n"); - for (String funcName : fuctionsMap.keySet()) { + for (String funcName : functionsMap.keySet()) { str.append(" Name: ").append(funcName).append("\n"); } diff --git a/src/main/java/TP2/asd/toLLVM_Visitor.java b/src/main/java/TP2/asd/toLLVM_Visitor.java index feca761..06b6a36 100644 --- a/src/main/java/TP2/asd/toLLVM_Visitor.java +++ b/src/main/java/TP2/asd/toLLVM_Visitor.java @@ -336,15 +336,22 @@ public class toLLVM_Visitor implements ProgramVisitor, ArrayList paramsLLVM = new ArrayList<>(); for(Expression param : instr.params()){ InstrAndVal result = param.accept(this,h); - l.add((InstructionLLVM) result.instrs); + l.addAll( result.instrs); paramsLLVM.add(result.val); } ValueFunMap fLLVM = h.getFunction(instr.fName()); //on récupère la fonction LLVM dans la table des Symboles if(fLLVM == null){ System.err.println("[VSL compile error] : la fonction n'existe pas, veuillez vous assurer de l'avoir déclarée avant l'appel"); + return new InstrAndVal(l, null); } + + //Pour c=func(x,y) + Result res = h.addNewTempVar(); + h = res.symTable; + VarLLVMImp var = new VarLLVMImp(fLLVM.define.type(), res.var); + l.add(new CallLLVMImp(fLLVM.define,paramsLLVM,"")); - return new InstrAndVal(l, null); + return new InstrAndVal(l, var); } @Override diff --git a/tests/aLaMain.vsl b/tests/aLaMain.vsl index af2188a..27c938e 100644 --- a/tests/aLaMain.vsl +++ b/tests/aLaMain.vsl @@ -1,8 +1,8 @@ -PROTO INT add(x,y) +PROTO INT add() -FUNC INT add(x,y) { +FUNC INT add() { INT z - z := x+y + z := 1 RETURN z } @@ -11,7 +11,7 @@ FUNC INT main(x,y) { x := 5 minh := x * y b:=3 - c:=add(x,b) + c:=add() PRINT "coucou, tu peux réparer le visitPrint dans LLVM stp","il manque virgule au milieu", c*5+b WHILE b - 1 DO{