error LL1 in function and proto in Parser. too weird.

This commit is contained in:
Vu Tuan Minh
2025-04-28 06:20:11 +02:00
parent 85a693acbd
commit 19af482a32
4 changed files with 46 additions and 27 deletions

View File

@@ -52,17 +52,31 @@ functions returns [ArrayList<Function> 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<Declaration> 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);}
)
;

View File

@@ -8,18 +8,18 @@ import TP2.llvm.ProgramLLVM.DefineLLVMImp;
public class SymTable {
private PStack<PMap<String,ValueVarMap>> varMap;
private PMap<String,ValueFunMap> fuctionsMap;
private PMap<String,ValueFunMap> 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<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> fuctionsMap, int id, int idLabel){
public SymTable(PStack<PMap<String,ValueVarMap>> varMap, PMap<String,ValueFunMap> 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<String,ValueVarMap> scope : varMap){
x=scope.containsKey(nomVar);
for (PMap<String,ValueVarMap> 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<String, ValueVarMap> 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");
}

View File

@@ -336,15 +336,22 @@ public class toLLVM_Visitor implements ProgramVisitor<SymTable,ProgramLLVMImp>,
ArrayList<ValLLVM> 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

View File

@@ -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{