change lexer and parser. tested

This commit is contained in:
Vu Tuan Minh
2025-04-07 19:17:07 +02:00
parent e78c531c56
commit 425e2d4088
3 changed files with 40 additions and 64 deletions

View File

@@ -38,7 +38,7 @@ PRINT : 'PRINT'
; ;
RETURN: 'RETURN' RETURN: 'RETURN'
; ;
TEXT : '"' (ASCII)+ '"' {getText().substring(1,getText().length()-1);} TEXT : '"' (ASCII)+ '"'
; ;
NUMBER : (DIGIT)+ NUMBER : (DIGIT)+
; ;

View File

@@ -22,53 +22,51 @@ options {
} }
program returns [ProgramImp p] : program returns [ProgramImp p] :
func=function func=functions
EOF EOF
{$p = new ProgramImp($func.out);} {$p = new ProgramImp($func.out);}
; ;
function returns [ArrayList<Function> out] functions returns [ArrayList<Function> out]
@init{ @init{
SymTable sym_table = new SymTable();
sym_table.next_layer();
$out = new ArrayList<Function>(); $out = new ArrayList<Function>();
}: }:
(FUNCTION t=type i=ident ParO ParF (function {
$out.add($function.out);
})+
;
function returns [Function out]:
FUNCTION t=type i=ident ParO ParF
( (
BacO instrs=list_instr[sym_table] BacF BacO instrs=list_instr BacF
{ {
$out.add(new FunctionImp($t.return_type, $i.out, $instrs.out)); $out=new FunctionImp($t.return_type, $i.out, $instrs.out);
} }
| instr= instruction[sym_table] | instr= instruction
{ {
ArrayList<Instruction> single = new ArrayList<Instruction>(); $out=new FunctionImp($t.return_type, $i.out, $instr.out);
single.add($instr.out);
$out.add(new FunctionImp($t.return_type, $i.out, single));
} }
)* )
)
; ;
prototype returns [Program p]: list_instr returns [ArrayList<Instruction> out]
;
list_instr [SymTable table] returns [ArrayList<Instruction> out]
@init{ @init{
$out = new ArrayList<>(); $out = new ArrayList<>();
}: }:
(instruction [table] { (instruction {
$out.add($instruction.out); $out.add($instruction.out);
})+ })+
; ;
instruction [SymTable table] returns [Instruction out]: instruction returns [Instruction out]:
//RETURN //RETURN
RETURN e=expression [table] RETURN e=expression
{ {
$out =new Return_instrImp($e.out);} $out =new Return_instrImp($e.out);}
| //ASSIGN | //ASSIGN
i=ident ASSIGN e=expression [table] i=ident ASSIGN e=expression
{ {
$out = new AssignImp($i.out, $e.out); $out = new AssignImp($i.out, $e.out);
} }
@@ -78,73 +76,51 @@ instruction [SymTable table] returns [Instruction out]:
{ {
ArrayList<String> declare= new ArrayList<String>(); ArrayList<String> declare= new ArrayList<String>();
declare.add($i.out); declare.add($i.out);
$table.addVar($i.out,$t.return_type);
}(VIRGULE i2=ident }(VIRGULE i2=ident
{ {
$table.addVar($i2.out,$t.return_type);
declare.add($i2.out); declare.add($i2.out);
})* })*
{$out = new DeclarationImp($t.return_type, declare);} {$out = new DeclarationImp($t.return_type, declare);}
; ;
//Priorité lit(val, const ou paranthese) -> td_exp (*/%) -> exp (+-) //Priorité lit(val, const ou paranthese) -> td_exp (*/%) -> exp (+-)
expression [SymTable table] returns [Type return_Type, Expression out]: expression returns [Expression out]:
//Fix LL1 and compare add and sub first //Fix LL1 and compare add and sub first
left=td_expression[table] { left=td_expression {
$out = $left.out; $out = $left.out;
$return_Type = $left.return_Type;
} }
//maybe it will be binop(s) with add/sub here //maybe it will be binop(s) with add/sub here
//if it is mul or div, they are all gonna be left=td_expression[table] to handle //if it is mul or div, they are all gonna be left=td_expression[table] to handle
// have to make $out,$right.out to make a loop // have to make $out,$right.out to make a loop
(op=(PLUS | MINUS) right=td_expression[table] { (
$out = new BinopExpressionImp( (PLUS right=td_expression{$out = new BinopExpressionImp(Op.PLUS,$out,$right.out);})
switch($op.getType()) { | //MINUS
case PLUS -> Op.PLUS; (MINUS right=td_expression{$out = new BinopExpressionImp(Op.MINUS,$out,$right.out);})
case MINUS -> Op.MINUS; )*
default -> throw new IllegalArgumentException("Unknown operator");
},
$out,
$right.out
);
$return_Type = $left.return_Type;
})*
; ;
td_expression [SymTable table] returns [Type return_Type, Expression out]: td_expression returns [Expression out]:
left=lit[table] { left=lit {
$out = $left.out; $out = $left.out;
$return_Type = $left.return_Type;
} }
(op=(TIMES | DIV| MOD) right=lit [table] { (
$out = new Program.BinopExpressionImp( (DIV right=lit{$out = new BinopExpressionImp(Op.DIV,$out,$right.out);})
switch($op.getType()) { | //MOD
case TIMES -> Op.TIMES; (MOD right=lit{$out = new BinopExpressionImp(Op.MOD,$out,$right.out);})
case DIV -> Op.DIV; | //TIMES
case MOD -> Op.MOD; (TIMES right=lit{$out = new BinopExpressionImp(Op.TIMES,$out,$right.out);})
default -> throw new IllegalArgumentException("Unknown operator"); )*
},
$out,
$right.out
);
$return_Type = $left.return_Type;
})*
; ;
lit [SymTable table] returns [Expression out, Type return_Type]: lit returns [Expression out]:
NUMBER { NUMBER {
$return_Type = new Type_intImp();
$out = new ConstImp($NUMBER.int); $out = new ConstImp($NUMBER.int);
} }
| ParO e=expression[table] ParF{ | ParO e=expression ParF{
$out=$e.out; $out=$e.out;
$return_Type=$e.return_Type;
} }
| t=ident{ | t=ident{
if($table.searchVar($t.out)){ $out=new VarImp($t.out);
$out=new VarImp($t.out);
$return_Type=$table.getvar_Type($t.out);
}
} }
; ;

View File

@@ -55,7 +55,7 @@ public class Main {
// Generate the intermediate representation // Generate the intermediate representation
//System.out.println("todo"); //System.out.println("todo");
//ProgramLLVMImpl astLLVM = ast.toLLVM(); ProgramLLVMImpl astLLVM = ast.toLLVM();
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n"); //System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");
System.out.println(astLLVM.prettyprinter()); System.out.println(astLLVM.prettyprinter());
//System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n"); //System.out.println("\n\n PRETTYPRINTER LLVM : \n--------------\n");