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'
;
TEXT : '"' (ASCII)+ '"' {getText().substring(1,getText().length()-1);}
TEXT : '"' (ASCII)+ '"'
;
NUMBER : (DIGIT)+
;

View File

@@ -22,53 +22,51 @@ options {
}
program returns [ProgramImp p] :
func=function
func=functions
EOF
{$p = new ProgramImp($func.out);}
;
function returns [ArrayList<Function> out]
functions returns [ArrayList<Function> out]
@init{
SymTable sym_table = new SymTable();
sym_table.next_layer();
$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>();
single.add($instr.out);
$out.add(new FunctionImp($t.return_type, $i.out, single));
$out=new FunctionImp($t.return_type, $i.out, $instr.out);
}
)*
)
)
;
prototype returns [Program p]:
;
list_instr [SymTable table] returns [ArrayList<Instruction> out]
list_instr returns [ArrayList<Instruction> out]
@init{
$out = new ArrayList<>();
}:
(instruction [table] {
(instruction {
$out.add($instruction.out);
})+
;
instruction [SymTable table] returns [Instruction out]:
instruction returns [Instruction out]:
//RETURN
RETURN e=expression [table]
RETURN e=expression
{
$out =new Return_instrImp($e.out);}
| //ASSIGN
i=ident ASSIGN e=expression [table]
i=ident ASSIGN e=expression
{
$out = new AssignImp($i.out, $e.out);
}
@@ -78,73 +76,51 @@ instruction [SymTable table] returns [Instruction out]:
{
ArrayList<String> declare= new ArrayList<String>();
declare.add($i.out);
$table.addVar($i.out,$t.return_type);
}(VIRGULE i2=ident
{
$table.addVar($i2.out,$t.return_type);
declare.add($i2.out);
})*
{$out = new DeclarationImp($t.return_type, declare);}
;
//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
left=td_expression[table] {
left=td_expression {
$out = $left.out;
$return_Type = $left.return_Type;
}
//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
// have to make $out,$right.out to make a loop
(op=(PLUS | MINUS) right=td_expression[table] {
$out = new BinopExpressionImp(
switch($op.getType()) {
case PLUS -> Op.PLUS;
case MINUS -> Op.MINUS;
default -> throw new IllegalArgumentException("Unknown operator");
},
$out,
$right.out
);
$return_Type = $left.return_Type;
})*
(
(PLUS right=td_expression{$out = new BinopExpressionImp(Op.PLUS,$out,$right.out);})
| //MINUS
(MINUS right=td_expression{$out = new BinopExpressionImp(Op.MINUS,$out,$right.out);})
)*
;
td_expression [SymTable table] returns [Type return_Type, Expression out]:
left=lit[table] {
td_expression returns [Expression out]:
left=lit {
$out = $left.out;
$return_Type = $left.return_Type;
}
(op=(TIMES | DIV| MOD) right=lit [table] {
$out = new Program.BinopExpressionImp(
switch($op.getType()) {
case TIMES -> Op.TIMES;
case DIV -> Op.DIV;
case MOD -> Op.MOD;
default -> throw new IllegalArgumentException("Unknown operator");
},
$out,
$right.out
);
$return_Type = $left.return_Type;
})*
(
(DIV right=lit{$out = new BinopExpressionImp(Op.DIV,$out,$right.out);})
| //MOD
(MOD right=lit{$out = new BinopExpressionImp(Op.MOD,$out,$right.out);})
| //TIMES
(TIMES right=lit{$out = new BinopExpressionImp(Op.TIMES,$out,$right.out);})
)*
;
lit [SymTable table] returns [Expression out, Type return_Type]:
lit returns [Expression out]:
NUMBER {
$return_Type = new Type_intImp();
$out = new ConstImp($NUMBER.int);
}
| ParO e=expression[table] ParF{
| ParO e=expression ParF{
$out=$e.out;
$return_Type=$e.return_Type;
}
| t=ident{
if($table.searchVar($t.out)){
$out=new VarImp($t.out);
$return_Type=$table.getvar_Type($t.out);
}
$out=new VarImp($t.out);
}
;

View File

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