@ -1,4 +1,5 @@
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.List;
import java_cup.runtime.*;
import java_cup.runtime.*;
@ -113,12 +114,26 @@ action code {:
}
}
return list;
return list;
}
}
<T> List<T> combine(List<T> list, List<T> item) {
if (item != null) {
Iterator<T> it = item.iterator();
while(it.hasNext())
list.add(it.next());
}
return list;
}
/** Return a mutable empty list. */
/** Return a mutable empty list. */
<T> List<T> empty() {
<T> List<T> empty() {
return new ArrayList<T>();
return new ArrayList<T>();
}
}
class FuncBody {
public List<Declaration> fbd;
public List<Stmt> sl;
public FuncBody(List<Declaration> fbd, List<Stmt> sl){
this.fbd = fbd;
this.sl = sl;
}
}
/** Return the leftmost non-whitespace location in NODES, or null if NODES
/** Return the leftmost non-whitespace location in NODES, or null if NODES
* is empty. Assumes that the nodes of NODES are ordered in increasing
* is empty. Assumes that the nodes of NODES are ordered in increasing
* order of location, from left to right. */
* order of location, from left to right. */
@ -228,7 +243,7 @@ terminal String IS;
/* Returned by the lexer for erroneous tokens. Since it does not appear in
/* Returned by the lexer for erroneous tokens. Since it does not appear in
* the grammar, it indicates a syntax error. */
* the grammar, it indicates a syntax error. */
terminal UNRECOGNIZED;
terminal String UNRECOGNIZED;
/* Nonterminal symbols (defined in production rules below).
/* Nonterminal symbols (defined in production rules below).
* As for terminal symbols,
* As for terminal symbols,
@ -236,7 +251,7 @@ terminal UNRECOGNIZED;
* defines the listed nonterminal identifier symbols to have semantic values
* defines the listed nonterminal identifier symbols to have semantic values
* of type <type>. */
* of type <type>. */
non terminal Program program;
non terminal Program program;
non terminal List<Declaration> program_head, class_body, class_body_defs, fun_body_decs;
non terminal List<Declaration> defs, program_head, opt_ program_head, class_body, class_body_defs, fun_body_decs;
non terminal List<Stmt> stmt_list, opt_stmt_list, block, else_body;
non terminal List<Stmt> stmt_list, opt_stmt_list, block, else_body;
non terminal Stmt stmt, simple_stmt;
non terminal Stmt stmt, simple_stmt;
non terminal Expr expr, pexpr, cexpr;
non terminal Expr expr, pexpr, cexpr;
@ -255,7 +270,7 @@ non terminal List<Expr> opt_target, expr_list;
non terminal Expr target;
non terminal Expr target;
non terminal MemberExpr member_expr;
non terminal MemberExpr member_expr;
non terminal IndexExpr index_expr;
non terminal IndexExpr index_expr;
non terminal FuncBody fun_body;
@ -331,6 +346,10 @@ typed_vars ::= typed_var:tv {: RESULT= single(tv
/* fun_body */
/* fun_body */
fun_body ::= fun_body_decs:fbd stmt_list:sl {: RESULT = new FuncBody(fbd, sl);:}
| fun_body_decs:fbd {: RESULT = new FuncBody(fbd, new ArrayList<Stmt>());:}
;
fun_body_decs ::= fun_body_decs:fbd global_decl:gd {: RESULT= combine(fbd, gd); :}
fun_body_decs ::= fun_body_decs:fbd global_decl:gd {: RESULT= combine(fbd, gd); :}
| fun_body_decs:fbd nonlocal_decl:nd {: RESULT= combine(fbd, nd); :}
| fun_body_decs:fbd nonlocal_decl:nd {: RESULT= combine(fbd, nd); :}
| fun_body_decs:fbd var_def:vd {: RESULT= combine(fbd, vd); :}
| fun_body_decs:fbd var_def:vd {: RESULT= combine(fbd, vd); :}