parser grammar VSLParser; options { language = Java; tokenVocab = VSLLexer; k = 1; } @header { package TP2; import TP2.Program.Program; } // On syntax error, raise exception rather than silently recovery @rulecatch { catch (RecognitionException ex) { reportError(ex) ; throw(ex) ; } } program returns [Program.Program p] : func=function EOF {$p = new Program.Program($func.p);} ; function returns [Program.List out] @init{ SymTable sym_table = new SymTable(); }: FUNCTION type ident ParO ParF list_instr[sym_table] ; prototype returns [Program.Program p]: ; list_instr [SymTable table] returns [Program.List out] @init{ List instructions = new List(); }: instruction [table]{ instructions.add($instruction.out); $out=instructions; } ; instruction [SymTable table] returns [List out]: RETURN expression[SymTable table] ; expression [SymTable table] returns [Type return_Type, Expression out]: //Binop(op,exp,exp) et this.return_type est meme que left ou right left_exp=expression[table] op=(PLUS| MINUS | DIV | TIMES) right_exp=expression[table]{ switch($op.getType()){ case PLUS: $out = new Program.BinopExpression("PLUS",$left_exp,$right_exp); break; case MINUS: $out = new Program.BinopExpression("MINUS", $l.out, $r.out); break; case TIMES: $out = new Program.BinopExpression("TIMES", $l.out, $r.out); break; case DIV: $out = new Program.BinopExpression("DIV", $l.out, $r.out); break; } $return_Type = $right_exp.return_Type } | //Const(lit) et peut-etre var aussi ( dans le futur )? lit{ $out=lit.out; $return_Type= $lit.return_Type } ; lit returns [Expression out, return_Type retType]: NUMBER { $return_Type = new Program.IntType(); $out = new Program.IntergerNum($NUMBER.int); } ; ident returns [String ident]: TEXT { $ident = $TEXT.getText(); } ; type returns [Type return_type]: TYPE_INT { $return_type = new Program.Type.Type_int(); } |TYPE_VOID { $return_type = new Program.Type.Type_void(); } ;