Changes for William's Analysis
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
1 package IR.Tree;
2 import IR.*;
3 import java.util.*;
4
5
6 public class BuildIR {
7     State state;
8     public BuildIR(State state) {
9         this.state=state;
10     }
11     public void buildtree() {
12         for(Iterator it=state.parsetrees.iterator();it.hasNext();) {
13             ParseNode pn=(ParseNode)it.next();
14             parseFile(pn);
15         }
16     }
17
18     /** Parse the classes in this file */
19     public void parseFile(ParseNode pn) {
20         NameDescriptor packages;
21         Vector singleimports=new Vector();
22         Vector multiimports=new Vector();
23
24         ParseNode ipn=pn.getChild("imports").getChild("import_decls_list");
25         if (ipn!=null) {
26             ParseNodeVector pnv=ipn.getChildren();
27             for(int i=0;i<pnv.size();i++) {
28                 ParseNode pnimport=pnv.elementAt(i);
29                 NameDescriptor nd=parseName(pnimport.getChild("name"));
30                 if (isNode(pnimport,"import_single"))
31                     singleimports.add(nd);
32                 else
33                     multiimports.add(nd);
34             }
35         }
36         ParseNode ppn=pn.getChild("packages").getChild("package");
37         if (ppn!=null) {
38             packages=parseName(pn.getChild("name"));
39         }
40         ParseNode tpn=pn.getChild("type_declaration_list");
41         if (tpn!=null) {
42             ParseNodeVector pnv=tpn.getChildren();
43             for(int i=0;i<pnv.size();i++) {
44                 ParseNode type_pn=pnv.elementAt(i);
45                 if (isEmpty(type_pn)) /* Skip the semicolon */
46                     continue;
47                 if (isNode(type_pn,"class_declaration")) {
48                     ClassDescriptor cn=parseTypeDecl(type_pn);
49                     state.addClass(cn);
50                 } else if (isNode(type_pn,"task_declaration")) {
51                     TaskDescriptor td=parseTaskDecl(type_pn);
52                     state.addTask(td);
53                 } else {
54                     throw new Error(type_pn.getLabel());
55                 }
56             }
57         }
58     }
59
60     public TaskDescriptor parseTaskDecl(ParseNode pn) {
61         TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
62         ParseNode bodyn=pn.getChild("body");
63         BlockNode bn=parseBlock(bodyn);
64         parseParameterList(td, pn);
65         state.addTreeCode(td,bn);
66         if (pn.getChild("flag_effects_list")!=null)
67             td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
68         return td;
69     }
70
71     public Vector parseFlags(ParseNode pn) {
72         Vector vfe=new Vector();
73         ParseNodeVector pnv=pn.getChildren();
74         for(int i=0;i<pnv.size();i++) {
75             ParseNode fn=pnv.elementAt(i);
76             FlagEffects fe=parseFlagEffects(fn);
77             vfe.add(fe);
78         }
79         return vfe;
80     }
81
82     public FlagEffects parseFlagEffects(ParseNode pn) {
83         if (isNode(pn,"flag_effect")) {
84             String flagname=pn.getChild("name").getTerminal();
85             FlagEffects fe=new FlagEffects(flagname);
86             if (pn.getChild("flag_list")!=null)
87                 parseFlagEffect(fe, pn.getChild("flag_list"));
88             if (pn.getChild("tag_list")!=null)
89                 parseTagEffect(fe, pn.getChild("tag_list"));
90             return fe;
91         } else throw new Error();
92     }
93     
94     public void parseTagEffect(FlagEffects fes, ParseNode pn) {
95         ParseNodeVector pnv=pn.getChildren();
96         for(int i=0;i<pnv.size();i++) {
97             ParseNode pn2=pnv.elementAt(i);
98             boolean status=true;
99             if (isNode(pn2,"not")) {
100                 status=false;
101                 pn2=pn2.getChild("name");
102             }
103             String name=pn2.getTerminal();
104             fes.addTagEffect(new TagEffect(name,status));
105         }
106     }
107
108     public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
109         ParseNodeVector pnv=pn.getChildren();
110         for(int i=0;i<pnv.size();i++) {
111             ParseNode pn2=pnv.elementAt(i);
112             boolean status=true;
113             if (isNode(pn2,"not")) {
114                 status=false;
115                 pn2=pn2.getChild("name");
116             }
117             String name=pn2.getTerminal();
118             fes.addEffect(new FlagEffect(name,status));
119         }
120     }
121
122     public FlagExpressionNode parseFlagExpression(ParseNode pn) {
123         if (isNode(pn,"or")) {
124             ParseNodeVector pnv=pn.getChildren();
125             ParseNode left=pnv.elementAt(0);
126             ParseNode right=pnv.elementAt(1);
127             return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
128         } else if (isNode(pn,"and")) {
129             ParseNodeVector pnv=pn.getChildren();
130             ParseNode left=pnv.elementAt(0);
131             ParseNode right=pnv.elementAt(1);
132             return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
133         } else if (isNode(pn, "not")) {
134             ParseNodeVector pnv=pn.getChildren();
135             ParseNode left=pnv.elementAt(0);
136             return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));           
137
138         } else if (isNode(pn,"name")) {
139             return new FlagNode(pn.getTerminal());
140         } else {
141             throw new Error();
142         }
143     }
144
145     public Vector parseChecks(ParseNode pn) {
146         Vector ccs=new Vector();
147         ParseNodeVector pnv=pn.getChildren();
148         for(int i=0;i<pnv.size();i++) {
149             ParseNode fn=pnv.elementAt(i);
150             ConstraintCheck cc=parseConstraintCheck(fn);
151             ccs.add(cc);
152         }
153         return ccs;
154     }
155
156     public ConstraintCheck parseConstraintCheck(ParseNode pn) {
157         if (isNode(pn,"cons_check")) {
158             String specname=pn.getChild("name").getChild("identifier").getTerminal();
159             Vector[] args=parseConsArgumentList(pn);
160             ConstraintCheck cc=new ConstraintCheck(specname);
161             for(int i=0;i<args[0].size();i++) {
162                 cc.addVariable((String)args[0].get(i));
163                 cc.addArgument((ExpressionNode)args[1].get(i));
164             }
165             return cc;
166         } else throw new Error();
167     }
168
169     public void parseParameterList(TaskDescriptor td, ParseNode pn) {
170         
171         boolean optional;
172         ParseNode paramlist=pn.getChild("task_parameter_list");
173         if (paramlist==null)
174             return;
175         ParseNodeVector pnv=paramlist.getChildren();
176         for(int i=0;i<pnv.size();i++) {
177             ParseNode paramn=pnv.elementAt(i);
178             if(paramn.getChild("optional")!=null){
179                 optional = true;
180                 paramn = paramn.getChild("optional").getFirstChild();
181                 System.out.println("OPTIONAL FOUND!!!!!!!");
182             }
183             else { optional = false;
184             System.out.println("NOT OPTIONAL");
185             }
186
187             TypeDescriptor type=parseTypeDescriptor(paramn);
188             
189             String paramname=paramn.getChild("single").getTerminal();
190             FlagExpressionNode fen=null;
191             if (paramn.getChild("flag")!=null)
192                 fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
193             
194             ParseNode tagnode=paramn.getChild("tag");
195             
196             TagExpressionList tel=null;
197             if (tagnode!=null) {
198                 tel=parseTagExpressionList(tagnode);
199             }
200             
201             td.addParameter(type,paramname,fen, tel, optional);
202         }
203     }
204     
205     public TagExpressionList parseTagExpressionList(ParseNode pn) {
206         //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
207         //To test, feed in any input program that uses tags
208         ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
209         TagExpressionList tel=new TagExpressionList();
210         for(int i=0;i<pnv.size();i++) {
211             ParseNode tn=pnv.elementAt(i);
212             String type=tn.getChild("type").getTerminal();
213             String name=tn.getChild("single").getTerminal();
214             tel.addTag(type, name);
215         }
216         return tel;
217     }
218
219     public ClassDescriptor parseTypeDecl(ParseNode pn) {
220         ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
221         if (!isEmpty(pn.getChild("super").getTerminal())) {
222             /* parse superclass name */
223             ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
224             NameDescriptor nd=parseName(snn);
225             cn.setSuper(nd.toString());
226         } else {
227             if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
228                   cn.getSymbol().equals(TypeUtil.TagClass)))
229                 cn.setSuper(TypeUtil.ObjectClass);
230         }
231         cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
232         parseClassBody(cn, pn.getChild("classbody"));
233         return cn;
234     }
235
236     private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
237         ParseNode decls=pn.getChild("class_body_declaration_list");
238         if (decls!=null) {
239             ParseNodeVector pnv=decls.getChildren();
240             for(int i=0;i<pnv.size();i++) {
241                 ParseNode decl=pnv.elementAt(i);
242                 if (isNode(decl,"member")) {
243                     parseClassMember(cn,decl);
244                 } else if (isNode(decl,"constructor")) {
245                     parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
246                 } else if (isNode(decl,"block")) {
247                 } else throw new Error();
248             }
249         }
250     }
251
252     private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
253         ParseNode fieldnode=pn.getChild("field");
254
255         if (fieldnode!=null) {
256             parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
257             return;
258         }
259         ParseNode methodnode=pn.getChild("method");
260         if (methodnode!=null) {
261             parseMethodDecl(cn,methodnode.getChild("method_declaration"));
262             return;
263         }
264         ParseNode flagnode=pn.getChild("flag");
265         if (flagnode!=null) {
266             parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
267             return;
268         }
269         throw new Error();
270     }
271
272     private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
273         ParseNode tn=pn.getChild("type");
274
275         String type_st=tn.getTerminal();
276         if(type_st.equals("byte")) {
277             return state.getTypeDescriptor(TypeDescriptor.BYTE);
278         } else if(type_st.equals("short")) {
279             return state.getTypeDescriptor(TypeDescriptor.SHORT);
280         } else if(type_st.equals("boolean")) {
281             return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
282         } else if(type_st.equals("int")) {
283             return state.getTypeDescriptor(TypeDescriptor.INT);
284         } else if(type_st.equals("long")) {
285             return state.getTypeDescriptor(TypeDescriptor.LONG);
286         } else if(type_st.equals("char")) {
287             return state.getTypeDescriptor(TypeDescriptor.CHAR);
288         } else if(type_st.equals("float")) {
289             return state.getTypeDescriptor(TypeDescriptor.FLOAT);
290         } else if(type_st.equals("double")) {
291             return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
292         } else if(type_st.equals("class")) {
293             ParseNode nn=tn.getChild("class");
294             return state.getTypeDescriptor(parseName(nn.getChild("name")));
295         } else if(type_st.equals("array")) {
296             ParseNode nn=tn.getChild("array");
297             TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
298             Integer numdims=(Integer)nn.getChild("dims").getLiteral();
299             for(int i=0;i<numdims.intValue();i++)
300                 td=td.makeArray(state);
301             return td;
302         } else {
303             throw new Error();
304         }
305     }
306
307     private NameDescriptor parseName(ParseNode nn) {
308         ParseNode base=nn.getChild("base");
309         ParseNode id=nn.getChild("identifier");
310         if (base==null)
311             return new NameDescriptor(id.getTerminal());
312         return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
313         
314     }
315
316     private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
317         String name=pn.getChild("name").getTerminal();
318         FlagDescriptor flag=new FlagDescriptor(name);
319         if (pn.getChild("external")!=null)
320             flag.makeExternal();
321         cn.addFlag(flag);
322     }
323
324     private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
325         ParseNode mn=pn.getChild("modifier");
326         Modifiers m=parseModifiersList(mn);
327
328         ParseNode tn=pn.getChild("type");
329         TypeDescriptor t=parseTypeDescriptor(tn);
330         ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
331         ParseNodeVector pnv=vn.getChildren();
332         for(int i=0;i<pnv.size();i++) {
333             ParseNode vardecl=pnv.elementAt(i);
334
335             
336             ParseNode tmp=vardecl;
337             TypeDescriptor arrayt=t;
338             while (tmp.getChild("single")==null) {
339                 arrayt=arrayt.makeArray(state);
340                 tmp=tmp.getChild("array");
341             }
342             String identifier=tmp.getChild("single").getTerminal();
343
344             ParseNode epn=vardecl.getChild("initializer");
345             
346             ExpressionNode en=null;
347             if (epn!=null)
348                 en=parseExpression(epn.getFirstChild());
349   
350             cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
351         }
352         
353     }
354
355     private ExpressionNode parseExpression(ParseNode pn) {
356         if (isNode(pn,"assignment"))
357             return parseAssignmentExpression(pn);
358         else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
359                  isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
360                  isNode(pn,"bitwise_and")||isNode(pn,"equal")||
361                  isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
362                  isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
363                  isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
364                  isNode(pn,"rightshift")||isNode(pn,"sub")||
365                  isNode(pn,"add")||isNode(pn,"mult")||
366                  isNode(pn,"div")||isNode(pn,"mod")) {
367             ParseNodeVector pnv=pn.getChildren();
368             ParseNode left=pnv.elementAt(0);
369             ParseNode right=pnv.elementAt(1);
370             Operation op=new Operation(pn.getLabel());
371             return new OpNode(parseExpression(left),parseExpression(right),op);
372         } else if (isNode(pn,"unaryplus")||
373                    isNode(pn,"unaryminus")||
374                    isNode(pn,"not")) {
375             ParseNode left=pn.getFirstChild();
376             Operation op=new Operation(pn.getLabel());
377             return new OpNode(parseExpression(left),op);
378         } else if (isNode(pn,"postinc")||
379                    isNode(pn,"postdec")) {
380             ParseNode left=pn.getFirstChild();
381             AssignOperation op=new AssignOperation(pn.getLabel());
382             return new AssignmentNode(parseExpression(left),null,op);
383
384         } else if (isNode(pn,"preinc")||
385                    isNode(pn,"predec")) {
386             ParseNode left=pn.getFirstChild();
387             AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
388             return new AssignmentNode(parseExpression(left),
389                                       new LiteralNode("integer",new Integer(1)),op);
390         } else if (isNode(pn,"literal")) {
391             String literaltype=pn.getTerminal();
392             ParseNode literalnode=pn.getChild(literaltype);
393             Object literal_obj=literalnode.getLiteral();
394             return new LiteralNode(literaltype, literal_obj);
395         } else if (isNode(pn,"createobject")) {
396             TypeDescriptor td=parseTypeDescriptor(pn);
397             Vector args=parseArgumentList(pn);
398             CreateObjectNode con=new CreateObjectNode(td);
399             for(int i=0;i<args.size();i++) {
400                 con.addArgument((ExpressionNode)args.get(i));
401             }
402             /* Could have flag set or tag added here */
403             if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
404                 FlagEffects fe=new FlagEffects(null);
405                 if (pn.getChild("flag_list")!=null)
406                     parseFlagEffect(fe, pn.getChild("flag_list"));
407
408                 if (pn.getChild("tag_list")!=null)
409                     parseTagEffect(fe, pn.getChild("tag_list"));
410                 con.addFlagEffects(fe);
411             }
412             
413             return con;
414         } else if (isNode(pn,"createarray")) {
415             //System.out.println(pn.PPrint(3,true));
416             TypeDescriptor td=parseTypeDescriptor(pn);
417             Vector args=parseDimExprs(pn);
418             int num=0;
419             if (pn.getChild("dims_opt").getLiteral()!=null)
420                 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
421             for(int i=0;i<(args.size()+num);i++)
422                 td=td.makeArray(state);
423             CreateObjectNode con=new CreateObjectNode(td);
424             for(int i=0;i<args.size();i++) {
425                 con.addArgument((ExpressionNode)args.get(i));
426             }
427             return con;
428         } else if (isNode(pn,"name")) {
429             NameDescriptor nd=parseName(pn);
430             return new NameNode(nd);
431         } else if (isNode(pn,"this")) {
432             NameDescriptor nd=new NameDescriptor("this");
433             return new NameNode(nd);
434         } else if (isNode(pn,"methodinvoke1")) {
435             NameDescriptor nd=parseName(pn.getChild("name"));
436             Vector args=parseArgumentList(pn);
437             MethodInvokeNode min=new MethodInvokeNode(nd);
438             for(int i=0;i<args.size();i++) {
439                 min.addArgument((ExpressionNode)args.get(i));
440             }
441             return min;
442         } else if (isNode(pn,"methodinvoke2")) {
443             String methodid=pn.getChild("id").getTerminal();
444             ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
445             Vector args=parseArgumentList(pn);
446             MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
447             for(int i=0;i<args.size();i++) {
448                 min.addArgument((ExpressionNode)args.get(i));
449             }
450             return min;
451         } else if (isNode(pn,"fieldaccess")) { 
452             ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());         String fieldname=pn.getChild("field").getTerminal();
453             return new FieldAccessNode(en,fieldname);
454         } else if (isNode(pn,"arrayaccess")) { 
455             ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
456             ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
457             return new ArrayAccessNode(en,index);
458         } else if (isNode(pn,"cast1")) { 
459             return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
460         } else if (isNode(pn,"cast2")) { 
461             return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
462         } else {
463             System.out.println("---------------------");
464             System.out.println(pn.PPrint(3,true));
465             throw new Error();
466         }
467     }
468
469     private Vector parseDimExprs(ParseNode pn) {
470         Vector arglist=new Vector();
471         ParseNode an=pn.getChild("dim_exprs");
472         if (an==null)   /* No argument list */
473             return arglist;
474         ParseNodeVector anv=an.getChildren();
475         for(int i=0;i<anv.size();i++) {
476             arglist.add(parseExpression(anv.elementAt(i)));
477         }
478         return arglist;
479     }
480
481     private Vector parseArgumentList(ParseNode pn) {
482         Vector arglist=new Vector();
483         ParseNode an=pn.getChild("argument_list");
484         if (an==null)   /* No argument list */
485             return arglist;
486         ParseNodeVector anv=an.getChildren();
487         for(int i=0;i<anv.size();i++) {
488             arglist.add(parseExpression(anv.elementAt(i)));
489         }
490         return arglist;
491     }
492
493     private Vector[] parseConsArgumentList(ParseNode pn) {
494         Vector arglist=new Vector();
495         Vector varlist=new Vector();
496         ParseNode an=pn.getChild("cons_argument_list");
497         if (an==null)   /* No argument list */
498             return new Vector[] {varlist, arglist};
499         ParseNodeVector anv=an.getChildren();
500         for(int i=0;i<anv.size();i++) {
501             ParseNode cpn=anv.elementAt(i);
502             ParseNode var=cpn.getChild("var");
503             ParseNode exp=cpn.getChild("exp").getFirstChild();
504             varlist.add(var.getTerminal());
505             arglist.add(parseExpression(exp));
506         }
507         return new Vector[] {varlist, arglist};
508     }
509
510     private ExpressionNode parseAssignmentExpression(ParseNode pn) {
511         AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
512         ParseNodeVector pnv=pn.getChild("args").getChildren();
513         
514         AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
515         return an;
516     }
517
518
519     private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
520         ParseNode headern=pn.getChild("method_header");
521         ParseNode bodyn=pn.getChild("body");
522         MethodDescriptor md=parseMethodHeader(headern);
523         BlockNode bn=parseBlock(bodyn);
524         cn.addMethod(md);
525         state.addTreeCode(md,bn);
526     }
527
528     private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
529         ParseNode mn=pn.getChild("modifiers");
530         Modifiers m=parseModifiersList(mn);
531         ParseNode cdecl=pn.getChild("constructor_declarator");
532         String name=cdecl.getChild("name").getChild("identifier").getTerminal();
533         MethodDescriptor md=new MethodDescriptor(m, name);
534         ParseNode paramnode=cdecl.getChild("parameters");
535         parseParameterList(md,paramnode);
536         ParseNode bodyn0=pn.getChild("body");
537         ParseNode bodyn=bodyn0.getChild("constructor_body");
538         cn.addMethod(md);
539         BlockNode bn=parseBlock(bodyn);
540         state.addTreeCode(md,bn);
541     }
542
543     public BlockNode parseBlock(ParseNode pn) {
544         if (pn==null||isEmpty(pn.getTerminal()))
545             return new BlockNode();
546         ParseNode bsn=pn.getChild("block_statement_list");
547         return parseBlockHelper(bsn);
548     }
549     
550     private BlockNode parseBlockHelper(ParseNode pn) {
551         ParseNodeVector pnv=pn.getChildren();
552         BlockNode bn=new BlockNode();
553         for(int i=0;i<pnv.size();i++) {
554             Vector bsv=parseBlockStatement(pnv.elementAt(i));
555             for(int j=0;j<bsv.size();j++) {
556                 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
557             }
558         }
559         return bn;
560     }
561
562     public BlockNode parseSingleBlock(ParseNode pn) {
563         BlockNode bn=new BlockNode();
564         Vector bsv=parseBlockStatement(pn);
565         for(int j=0;j<bsv.size();j++) {
566             bn.addBlockStatement((BlockStatementNode)bsv.get(j));
567         }
568         bn.setStyle(BlockNode.NOBRACES);
569         return bn;
570     }
571
572     public Vector parseBlockStatement(ParseNode pn) {
573         Vector blockstatements=new Vector();
574         if (isNode(pn,"tag_declaration")) {
575             String name=pn.getChild("single").getTerminal();
576             String type=pn.getChild("type").getTerminal();
577             
578             blockstatements.add(new TagDeclarationNode(name, type));
579         } else if (isNode(pn,"local_variable_declaration")) {
580             TypeDescriptor t=parseTypeDescriptor(pn);
581             ParseNode vn=pn.getChild("variable_declarators_list");
582             ParseNodeVector pnv=vn.getChildren();
583             for(int i=0;i<pnv.size();i++) {
584                 ParseNode vardecl=pnv.elementAt(i);
585
586             
587                 ParseNode tmp=vardecl;
588                 TypeDescriptor arrayt=t;
589                 while (tmp.getChild("single")==null) {
590                     arrayt=arrayt.makeArray(state);
591                     tmp=tmp.getChild("array");
592                 }
593                 String identifier=tmp.getChild("single").getTerminal();
594                 
595                 ParseNode epn=vardecl.getChild("initializer");
596             
597
598                 ExpressionNode en=null;
599                 if (epn!=null)
600                     en=parseExpression(epn.getFirstChild());
601                 
602                 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
603             }
604         } else if (isNode(pn,"nop")) {
605             /* Do Nothing */
606         } else if (isNode(pn,"expression")) {
607             blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
608         } else if (isNode(pn,"ifstatement")) {
609             blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
610                                        parseSingleBlock(pn.getChild("statement").getFirstChild()),
611                                        pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
612         } else if (isNode(pn,"taskexit")) {
613             Vector vfe=null;
614             if (pn.getChild("flag_effects_list")!=null)
615                 vfe=parseFlags(pn.getChild("flag_effects_list"));
616             Vector ccs=null;
617             if (pn.getChild("cons_checks")!=null)
618                 ccs=parseChecks(pn.getChild("cons_checks"));
619             
620             blockstatements.add(new TaskExitNode(vfe, ccs));
621         } else if (isNode(pn,"return")) {
622             if (isEmpty(pn.getTerminal()))
623                 blockstatements.add(new ReturnNode());
624             else {
625                 ExpressionNode en=parseExpression(pn.getFirstChild());
626                 blockstatements.add(new ReturnNode(en));
627             }
628         } else if (isNode(pn,"block_statement_list")) {
629             BlockNode bn=parseBlockHelper(pn);
630             blockstatements.add(new SubBlockNode(bn));
631         } else if (isNode(pn,"empty")) {
632             /* nop */
633         } else if (isNode(pn,"statement_expression_list")) {
634             ParseNodeVector pnv=pn.getChildren();
635             BlockNode bn=new BlockNode();
636             for(int i=0;i<pnv.size();i++) {
637                 ExpressionNode en=parseExpression(pnv.elementAt(i));
638                 blockstatements.add(new BlockExpressionNode(en));
639             }
640             bn.setStyle(BlockNode.EXPRLIST);
641         } else if (isNode(pn,"forstatement")) {
642             BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
643             BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
644             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
645             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
646             blockstatements.add(new LoopNode(init,condition,update,body));
647         } else if (isNode(pn,"whilestatement")) {
648             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
649             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
650             blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
651         } else if (isNode(pn,"dowhilestatement")) {
652             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
653             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
654             blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
655         } else {
656             System.out.println("---------------");
657             System.out.println(pn.PPrint(3,true));
658             throw new Error();
659         }
660         return blockstatements;
661     }
662
663     public MethodDescriptor parseMethodHeader(ParseNode pn) {
664         ParseNode mn=pn.getChild("modifiers");
665         Modifiers m=parseModifiersList(mn);
666         
667         ParseNode tn=pn.getChild("returntype");
668         TypeDescriptor returntype;
669         if (tn!=null) 
670             returntype=parseTypeDescriptor(tn);
671         else
672             returntype=new TypeDescriptor(TypeDescriptor.VOID);
673
674         ParseNode pmd=pn.getChild("method_declarator");
675         String name=pmd.getChild("name").getTerminal();
676         MethodDescriptor md=new MethodDescriptor(m, returntype, name);
677        
678         ParseNode paramnode=pmd.getChild("parameters");
679         parseParameterList(md,paramnode);
680         return md;
681     }
682
683     public void parseParameterList(MethodDescriptor md, ParseNode pn) {
684         ParseNode paramlist=pn.getChild("formal_parameter_list");
685         if (paramlist==null)
686             return;
687          ParseNodeVector pnv=paramlist.getChildren();
688          for(int i=0;i<pnv.size();i++) {
689              ParseNode paramn=pnv.elementAt(i);
690
691              if (isNode(paramn, "tag_parameter")) {
692                  String paramname=paramn.getChild("single").getTerminal();
693                  TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
694                  md.addTagParameter(type, paramname);
695              } else {
696                  TypeDescriptor type=parseTypeDescriptor(paramn);
697                  
698                  ParseNode tmp=paramn;
699                  while (tmp.getChild("single")==null) {
700                      type=type.makeArray(state);
701                      tmp=tmp.getChild("array");
702                  }
703                  String paramname=tmp.getChild("single").getTerminal();
704                  
705                  md.addParameter(type, paramname);
706              }
707          }
708     }
709
710     public Modifiers parseModifiersList(ParseNode pn) {
711         Modifiers m=new Modifiers();
712         ParseNode modlist=pn.getChild("modifier_list");
713         if (modlist!=null) {
714             ParseNodeVector pnv=modlist.getChildren();
715             for(int i=0;i<pnv.size();i++) {
716                 ParseNode modn=pnv.elementAt(i);
717                 if (isNode(modn,"public"))
718                     m.addModifier(Modifiers.PUBLIC);
719                 else if (isNode(modn,"protected"))
720                     m.addModifier(Modifiers.PROTECTED);
721                 else if (isNode(modn,"private"))
722                     m.addModifier(Modifiers.PRIVATE);
723                 else if (isNode(modn,"static"))
724                     m.addModifier(Modifiers.STATIC);
725                 else if (isNode(modn,"final"))
726                     m.addModifier(Modifiers.FINAL);
727                 else if (isNode(modn,"native"))
728                     m.addModifier(Modifiers.NATIVE);
729                 else if (isNode(modn,"synchronized"))
730                     m.addModifier(Modifiers.SYNCHRONIZED);
731                 else throw new Error("Unrecognized Modifier");
732             }
733         }
734         return m;
735     }
736
737     private boolean isNode(ParseNode pn, String label) {
738         if (pn.getLabel().equals(label))
739             return true;
740         else return false;
741     }
742
743     private static boolean isEmpty(ParseNode pn) {
744         if (pn.getLabel().equals("empty"))
745             return true;
746         else
747             return false;
748     }
749
750     private static boolean isEmpty(String s) {
751         if (s.equals("empty"))
752             return true;
753         else
754             return false;
755     }
756
757     /** Throw an exception if something is unexpected */
758     private void check(ParseNode pn, String label) {
759         if (pn == null) {
760             throw new Error(pn+ "IE: Expected '" + label + "', got null");
761         }
762         if (! pn.getLabel().equals(label)) {
763             throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
764         }
765     }
766 }