Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. grammar Calculette;
  2.  
  3. @members {
  4.  
  5.     private TablesSymboles tablesSymboles = new TablesSymboles();
  6.    
  7.     private int _label = 0;
  8.     /** générateur de nom d'étiquettes */
  9.     private int nextLabel() { return _label++; }
  10.    
  11.         }
  12.  
  13.  
  14. // lexer
  15. VAR : 'var' ;
  16.  
  17. start
  18.     : a=calcul EOF {System.out.println($a.code);};
  19.  
  20. expr returns [ String code ]
  21. @init { $code = new String(); }
  22.    : '(' k=expr ')' {$code = $k.code;}
  23.    | '-' j=expr {$code = "PUSHI 0\n"+ $j.code + "SUB\n";}
  24.    | a=expr '*' b=expr {$code = $a.code + $b.code + "MUL\n";}
  25.    | c=expr '/' d=expr {$code = $c.code + $d.code + "DIV\n";}
  26.    | g=expr '-' h=expr {$code = $g.code + $h.code + "SUB\n";}
  27.    | e=expr '+' f=expr {$code = $e.code + $f.code + "ADD\n";}
  28.    | id=IDENTIFIANT { $code = "PUSHG " + tablesSymboles.adresseVar($id.text) + "\n";}
  29.    | i=ENTIER {$code = "PUSHI " + $i.text + "\n";}
  30.    ;
  31.    
  32. // lexer
  33. IDENTIFIANT  : (('a'..'z' | 'A'..'Z' | '_')('a'..'z' | 'A'..'Z' | '_' | '0'..'9')*);
  34.  
  35.  
  36.  
  37. finInstruction
  38.     : (';' | NEWLINE)    
  39.     ;
  40.  
  41. assignation returns [ String code ]
  42.     : a=IDENTIFIANT '=' b=expr
  43.         {
  44.             $code = $b.code + "STOREG " + tablesSymboles.adresseVar($a.text) + "\n";
  45.         }
  46.     ;
  47.  
  48. condition returns [String code]
  49.     : orGauche=condition 'ou' orDroite=condition { $code = $orGauche.code + $orDroite.code + "ADD\n"; }
  50.     | andGauche=condition 'et' andDroite=condition { $code = $andGauche.code + $andDroite.code + "MUL\n"; }
  51.     | 'non' notDroite=condition { $code = "PUSHI 0 \n" + $notDroite.code + "SUPEQ\n"; }
  52.     | 'true'  { $code = "PUSHI 1\n"; }
  53.     | 'false' { $code = "PUSHI 0\n"; }
  54.     | infeqGauche=expr '<=' infeqDroite=expr { $code = $infeqGauche.code + $infeqDroite.code + "INFEQ\n"; }
  55.     | supeqGauche=expr '>=' supeqDroite=expr { $code = $supeqGauche.code + $supeqDroite.code + "SUPEQ\n"; }
  56.     | infGauche=expr '<' infDroite=expr { $code = $infGauche.code + $infDroite.code + "INF\n"; }
  57.     | supGauche=expr '>' supDroite=expr { $code = $supGauche.code + $supDroite.code + "SUP\n"; }
  58.     | equalGauche=expr '==' equalDroite=expr { $code = $equalGauche.code + $equalDroite.code + "EQUAL\n"; }
  59.     ;
  60.    
  61. bouclewhile returns [String code]
  62.     : 'while' '(' con=condition ')' ins=instruction
  63.         {  
  64.             int debLabel = nextLabel();
  65.             int finLabel = nextLabel();
  66.             $code = "LABEL " + debLabel + "\n";
  67.             $code += $con.code;
  68.             $code += "JUMPF " + finLabel + "\n";
  69.             $code += $ins.code;
  70.             $code += "JUMP " + debLabel + "\n";
  71.             $code += "LABEL " + finLabel + "\n";
  72.            
  73.         }
  74.     ;
  75.  
  76. repeatuntil returns [ String code ]
  77.     : 'repeat' ins=blocinstructions 'until' con=condition
  78.         {
  79.             int debLabel = nextLabel();
  80.             int finLabel = nextLabel();
  81.             $code = "LABEL " + debLabel + "\n";
  82.             $code += $ins.code;
  83.             $code += "PUSHI 0\n";
  84.             $code += $con.code;
  85.             $code += "EQUAL \n";
  86.             $code += "JUMPF " + finLabel + "\n";
  87.             $code += "JUMP " + debLabel + "\n";
  88.             $code += "LABEL " + finLabel + "\n";
  89.         }
  90.     ;
  91.  
  92. ifelse returns [ String code ]
  93.     @init{ boolean sinon = false;}
  94.     : 'if' '(' con=condition ')' gauche=instruction ('else' droite=instruction {sinon = true;})?
  95.         {
  96.             int debLabel = nextLabel();
  97.             int finLabel = nextLabel();
  98.             $code += $con.code;
  99.             $code += "JUMPF " + debLabel + "\n";
  100.             if(sinon){
  101.                 $code += $droite.code;
  102.                 $code += "JUMP " + finLabel + "\n";
  103.             }
  104.             $code += "LABEL " + debLabel + "\n";
  105.             $code += $gauche.code;
  106.             $code += "LABEL " + finLabel + "\n";
  107.         }
  108.         ;
  109.  
  110.  
  111. blocinstructions returns [ String code ]
  112. @init { $code = new String(); $code = ""; }
  113.     : {$code = "";} '{' (ins=instruction {$code += $ins.code;})* '}'
  114.     ;
  115.  
  116. instruction returns [ String code ]
  117.     : z=expr finInstruction
  118.         {
  119.             $code = $z.code;
  120.         }
  121.     | w=assignation finInstruction
  122.         {
  123.             $code = $w.code;
  124.         }
  125.     | a=bouclewhile finInstruction
  126.         {
  127.             $code = $a.code;
  128.         }
  129.     | rep=repeatuntil finInstruction
  130.         {
  131.             $code = $rep.code;
  132.         }
  133.     | ife=ifelse finInstruction
  134.         {
  135.             $code = $ife.code;
  136.         }
  137.     | finInstruction
  138.         {
  139.             $code = "";
  140.         }
  141.     ;
  142.  
  143. decl returns [ String code ]  
  144.     : VAR a=IDENTIFIANT finInstruction
  145.         {
  146.           tablesSymboles.putAdresse($a.text);
  147.           $code = "PUSHI 0\n";
  148.         }
  149.     ;
  150.  
  151.  
  152. calcul returns [ String code ]
  153. @init{ $code = new String(); }   // On initialise $code, pour ensuite l'utiliser comme accumulateur
  154.     :   (decl { $code += $decl.code; })*        
  155.        
  156.         NEWLINE*
  157.        
  158.         (instruction { $code += $instruction.code; })*
  159.  
  160.         { $code += "HALT\n"; }
  161.     ;
  162.  
  163.  
  164. NEWLINE : '\r'? '\n';
  165.  
  166. WS :   (' '|'\t')+ -> skip  ;
  167.  
  168. ENTIER : ('0'..'9')+  ;
  169.  
  170. UNMATCH : . -> skip ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement