CS计算机代考程序代写 grammar VarLang;

grammar VarLang;

import ArithLang; //Import all rules from Arithlang grammar.

// New elements in the Grammar of this Programming Language
// – grammar rules start with lowercase

exp returns [Exp ast]:
v=varexp { $ast = $v.ast; }
| n=numexp { $ast = $n.ast; }
| a=addexp { $ast = $a.ast; }
| s=subexp { $ast = $s.ast; }
| m=multexp { $ast = $m.ast; }
| d=divexp { $ast = $d.ast; }
| l=letexp { $ast = $l.ast; }
;

varexp returns [VarExp ast]:
id=Identifier { $ast = new VarExp($id.text); }
;

letexp returns [LetExp ast]
locals [ArrayList names, ArrayList value_exps]
@init { $names = new ArrayList(); $value_exps = new ArrayList(); } :
‘(‘ Let
‘(‘ ( ‘(‘ id=Identifier e=exp ‘)’ { $names.add($id.text); $value_exps.add($e.ast); } )+ ‘)’
body=exp
‘)’ { $ast = new LetExp($names, $value_exps, $body.ast); }
;

// Lexical Specification of this Programming Language
// – lexical specification rules start with uppercase