summaryrefslogtreecommitdiffstats
path: root/src/main/javassist/compiler/Parser.java
blob: ed6e9dd301d20ff915b82fc9cfd439998d890ea3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
/*
 * Javassist, a Java-bytecode translator toolkit.
 * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License.  Alternatively, the contents of this file may be used under
 * the terms of the GNU Lesser General Public License Version 2.1 or later.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 */

package javassist.compiler;

import javassist.compiler.ast.*;

public final class Parser implements TokenId {
    private Lex lex;

    public Parser(Lex lex) {
        this.lex = lex;
    }

    public boolean hasMore() { return lex.lookAhead() >= 0; }

    /* member.declaration
     * : method.declaration | field.declaration
     */
    public ASTList parseMember(SymbolTable tbl) throws CompileError {
        ASTList mem = parseMember1(tbl);
        if (mem instanceof MethodDecl)
            return parseMethod2(tbl, (MethodDecl)mem);
        else
            return mem;
    }

    /* A method body is not parsed.
     */
    public ASTList parseMember1(SymbolTable tbl) throws CompileError {
        ASTList mods = parseMemberMods();
        Declarator d;
        boolean isConstructor = false;
        if (lex.lookAhead() == Identifier && lex.lookAhead(1) == '(') {
            d = new Declarator(VOID, 0);
            isConstructor = true;
        }
        else
            d = parseFormalType(tbl);

        if (lex.get() != Identifier)
            throw new SyntaxError(lex);

        String name;
        if (isConstructor)
            name = MethodDecl.initName;
        else
            name = lex.getString();

        d.setVariable(new Symbol(name));
        if (isConstructor || lex.lookAhead() == '(')
            return parseMethod1(tbl, isConstructor, mods, d);
        else
            return parseField(tbl, mods, d);
    }

    /* field.declaration
     *  : member.modifiers
     *    formal.type Identifier
     *    [ "=" expression ] ";"
     */
    private FieldDecl parseField(SymbolTable tbl, ASTList mods,
                                Declarator d) throws CompileError
    {
        ASTree expr = null;
        if (lex.lookAhead() == '=') {
            lex.get();
            expr = parseExpression(tbl);
        }

        int c = lex.get();
        if (c == ';')
            return new FieldDecl(mods, new ASTList(d, new ASTList(expr)));
        else if (c == ',')
            throw new CompileError(
                "only one field can be declared in one declaration", lex);
        else
            throw new SyntaxError(lex);
    }

    /* method.declaration
     *  : member.modifiers
     *    [ formal.type ]
     *    Identifier "(" [ formal.parameter ( "," formal.parameter )* ] ")"
     *    array.dimension
     *    [ THROWS class.type ( "," class.type ) ]
     *    ( block.statement | ";" )
     *
     * Note that a method body is not parsed.
     */
    private MethodDecl parseMethod1(SymbolTable tbl, boolean isConstructor,
                                    ASTList mods, Declarator d)
        throws CompileError
    {
        if (lex.get() != '(')
            throw new SyntaxError(lex);

        ASTList parms = null;
        if (lex.lookAhead() != ')')
            while (true) {
                parms = ASTList.append(parms, parseFormalParam(tbl));
                int t = lex.lookAhead();
                if (t == ',')
                    lex.get();
                else if (t == ')')
                    break;
            }

        lex.get();      // ')'
        d.addArrayDim(parseArrayDimension());
        if (isConstructor && d.getArrayDim() > 0)
            throw new SyntaxError(lex);

        ASTList throwsList = null;
        if (lex.lookAhead() == THROWS) {
            lex.get();
            while (true) {
                throwsList = ASTList.append(throwsList, parseClassType(tbl));
                if (lex.lookAhead() == ',')
                    lex.get();
                else
                    break;
            }
        }

        return new MethodDecl(mods, new ASTList(d,
                                ASTList.make(parms, throwsList, null)));
    }

    /* Parses a method body.
     */
    public MethodDecl parseMethod2(SymbolTable tbl, MethodDecl md)
        throws CompileError
    {
        Stmnt body = null;
        if (lex.lookAhead() == ';')
            lex.get();
        else {
            body = parseBlock(tbl);
            if (body == null)
                body = new Stmnt(BLOCK);
        }

        md.sublist(4).setHead(body);
        return md;
    }

    /* member.modifiers
     *  : ( FINAL | SYNCHRONIZED | ABSTRACT
     *    | PUBLIC | PROTECTED | PRIVATE | STATIC
     *    | VOLATILE | TRANSIENT | STRICT )*
     */
    private ASTList parseMemberMods() {
        int t;
        ASTList list = null;
        while (true) {
            t = lex.lookAhead();
            if (t == ABSTRACT || t == FINAL || t == PUBLIC || t == PROTECTED
                || t == PRIVATE || t == SYNCHRONIZED || t == STATIC
                || t == VOLATILE || t == TRANSIENT || t == STRICT)
                list = new ASTList(new Keyword(lex.get()), list);
            else
                break;
        }

        return list;
    }

    /* formal.type : ( build-in-type | class.type ) array.dimension
     */
    private Declarator parseFormalType(SymbolTable tbl) throws CompileError {
        int t = lex.lookAhead();
        if (isBuiltinType(t) || t == VOID) {
            lex.get();  // primitive type
            int dim = parseArrayDimension();
            return new Declarator(t, dim);
        }
        else {
            ASTList name = parseClassType(tbl);
            int dim = parseArrayDimension();
            return new Declarator(name, dim);
        }
    }

    private static boolean isBuiltinType(int t) {
        return (t == BOOLEAN || t == BYTE || t == CHAR || t == SHORT
                || t == INT || t == LONG || t == FLOAT || t == DOUBLE);
    }

    /* formal.parameter : formal.type Identifier array.dimension
     */
    private Declarator parseFormalParam(SymbolTable tbl)
        throws CompileError
    {
        Declarator d = parseFormalType(tbl);
        if (lex.get() != Identifier)
            throw new SyntaxError(lex);

        String name = lex.getString();
        d.setVariable(new Symbol(name));
        d.addArrayDim(parseArrayDimension());
        tbl.append(name, d);
        return d;
    }

    /* statement : [ label ":" ]* labeled.statement
     *
     * labeled.statement
     *          : block.statement
     *          | if.statement
     *          | while.statement
     *          | do.statement
     *          | for.statement
     *          | switch.statement
     *          | try.statement
     *          | return.statement
     *          | thorw.statement
     *          | break.statement
     *          | continue.statement
     *          | declaration.or.expression
     *          | ";"
     *
     * This method may return null (empty statement).
     */
    public Stmnt parseStatement(SymbolTable tbl)
        throws CompileError
    {
        int t = lex.lookAhead();
        if (t == '{')
            return parseBlock(tbl);
        else if (t == ';') {
            lex.get();
            return new Stmnt(BLOCK);    // empty statement
        }
        else if (t == Identifier && lex.lookAhead(1) == ':') {
            lex.get();  // Identifier
            String label = lex.getString();
            lex.get();  // ':'
            return Stmnt.make(LABEL, new Symbol(label), parseStatement(tbl));
        }
        else if (t == IF)
            return parseIf(tbl);
        else if (t == WHILE)
            return parseWhile(tbl);
        else if (t == DO)
            return parseDo(tbl);
        else if (t == FOR)
            return parseFor(tbl);
        else if (t == TRY)
            return parseTry(tbl);
        else if (t == SWITCH)
            return parseSwitch(tbl);
        else if (t == SYNCHRONIZED)
            return parseSynchronized(tbl);
        else if (t == RETURN)
            return parseReturn(tbl);
        else if (t == THROW)
            return parseThrow(tbl);
        else if (t == BREAK)
            return parseBreak(tbl);
        else if (t == CONTINUE)
            return parseContinue(tbl);
        else
            return parseDeclarationOrExpression(tbl, false);
    }

    /* block.statement : "{" statement* "}"
     */
    private Stmnt parseBlock(SymbolTable tbl) throws CompileError {
        if (lex.get() != '{')
            throw new SyntaxError(lex);

        Stmnt body = null;
        SymbolTable tbl2 = new SymbolTable(tbl);
        while (lex.lookAhead() != '}') {
            Stmnt s = parseStatement(tbl2);
            if (s != null)
                body = (Stmnt)ASTList.concat(body, new Stmnt(BLOCK, s));
        }

        lex.get();      // '}'
        if (body == null)
            return new Stmnt(BLOCK);    // empty block
        else
            return body;
    }

    /* if.statement : IF "(" expression ")" statement
     *                [ ELSE statement ]
     */
    private Stmnt parseIf(SymbolTable tbl) throws CompileError {
        int t = lex.get();      // IF
        ASTree expr = parseParExpression(tbl);
        Stmnt thenp = parseStatement(tbl);
        Stmnt elsep;
        if (lex.lookAhead() == ELSE) {
            lex.get();
            elsep = parseStatement(tbl);
        }
        else
            elsep = null;

        return new Stmnt(t, expr, new ASTList(thenp, new ASTList(elsep)));
    }

    /* while.statement : WHILE "(" expression ")" statement
     */
    private Stmnt parseWhile(SymbolTable tbl)
        throws CompileError
    {
        int t = lex.get();      // WHILE
        ASTree expr = parseParExpression(tbl);
        Stmnt body = parseStatement(tbl);
        return new Stmnt(t, expr, body);
    }

    /* do.statement : DO statement WHILE "(" expression ")" ";"
     */
    private Stmnt parseDo(SymbolTable tbl) throws CompileError {
        int t = lex.get();      // DO
        Stmnt body = parseStatement(tbl);
        if (lex.get() != WHILE || lex.get() != '(')
            throw new SyntaxError(lex);

        ASTree expr = parseExpression(tbl);
        if (lex.get() != ')' || lex.get() != ';')
            throw new SyntaxError(lex);

        return new Stmnt(t, expr, body);
    }

    /* for.statement : FOR "(" decl.or.expr expression ";" expression ")"
     *                 statement
     */
    private Stmnt parseFor(SymbolTable tbl) throws CompileError {
        Stmnt expr1, expr3;
        ASTree expr2;
        int t = lex.get();      // FOR

        SymbolTable tbl2 = new SymbolTable(tbl);

        if (lex.get() != '(')
            throw new SyntaxError(lex);

        if (lex.lookAhead() == ';') {
            lex.get();
            expr1 = null;
        }
        else
            expr1 = parseDeclarationOrExpression(tbl2, true);

        if (lex.lookAhead() == ';')
            expr2 = null;
        else
            expr2 = parseExpression(tbl2);

        if (lex.get() != ';')
            throw new CompileError("; is missing", lex);

        if (lex.lookAhead() == ')')
            expr3 = null;
        else
            expr3 = parseExprList(tbl2);

        if (lex.get() != ')')
            throw new CompileError(") is missing", lex);

        Stmnt body = parseStatement(tbl2);
        return new Stmnt(t, expr1, new ASTList(expr2,
                                               new ASTList(expr3, body)));
    }

    /* switch.statement : SWITCH "(" expression ")" "{" switch.block "}"
     *
     * swtich.block : ( switch.label statement* )*
     *
     * swtich.label : DEFAULT ":"
     *              | CASE const.expression ":"
     */
    private Stmnt parseSwitch(SymbolTable tbl) throws CompileError {
        int t = lex.get();	// SWITCH
        ASTree expr = parseParExpression(tbl);
        Stmnt body = parseSwitchBlock(tbl);
        return new Stmnt(t, expr, body);
    }

    private Stmnt parseSwitchBlock(SymbolTable tbl) throws CompileError {
        if (lex.get() != '{')
            throw new SyntaxError(lex);

        SymbolTable tbl2 = new SymbolTable(tbl);
        Stmnt s = parseStmntOrCase(tbl2);
        if (s == null)
            throw new CompileError("empty switch block", lex);

        int op = s.getOperator();
        if (op != CASE && op != DEFAULT)
            throw new CompileError("no case or default in a switch block",
                                   lex);

        Stmnt body = new Stmnt(BLOCK, s);
        while (lex.lookAhead() != '}') {
            Stmnt s2 = parseStmntOrCase(tbl2);
            if (s2 != null) {
                int op2 = s2.getOperator();
                if (op2 == CASE || op2 == DEFAULT) {
                    body = (Stmnt)ASTList.concat(body, new Stmnt(BLOCK, s2));
                    s = s2;
                }
                else
                    s = (Stmnt)ASTList.concat(s, new Stmnt(BLOCK, s2));
            }
        }

        lex.get();      // '}'
        return body;
    }

    private Stmnt parseStmntOrCase(SymbolTable tbl) throws CompileError {
        int t = lex.lookAhead();
        if (t != CASE && t != DEFAULT)
            return parseStatement(tbl);

        lex.get();
        Stmnt s;
        if (t == CASE)
            s = new Stmnt(t, parseExpression(tbl));
        else
            s = new Stmnt(DEFAULT);

        if (lex.get() != ':')
            throw new CompileError(": is missing", lex);

        return s;
    }

    /* synchronized.statement :
     *     SYNCHRONIZED "(" expression ")" block.statement
     */
    private Stmnt parseSynchronized(SymbolTable tbl) throws CompileError {
        int t = lex.get();	// SYNCHRONIZED
        if (lex.get() != '(')
            throw new SyntaxError(lex);

        ASTree expr = parseExpression(tbl);
        if (lex.get() != ')')
            throw new SyntaxError(lex);

        Stmnt body = parseBlock(tbl);
        return new Stmnt(t, expr, body);
    }

    /* try.statement
     * : TRY block.statement
     *   [ CATCH "(" class.type Identifier ")" block.statement ]*
     *   [ FINALLY block.statement ]*
     */
    private Stmnt parseTry(SymbolTable tbl) throws CompileError {
        lex.get();      // TRY
        Stmnt block = parseBlock(tbl);
        ASTList catchList = null;
        while (lex.lookAhead() == CATCH) {
            lex.get();  // CATCH
            if (lex.get() != '(')
                throw new SyntaxError(lex);

            SymbolTable tbl2 = new SymbolTable(tbl);
            Declarator d = parseFormalParam(tbl2);
            if (d.getArrayDim() > 0 || d.getType() != CLASS)
                throw new SyntaxError(lex);

            if (lex.get() != ')')
                throw new SyntaxError(lex);

            Stmnt b = parseBlock(tbl2);
            catchList = ASTList.append(catchList, new Pair(d, b));
        }

        Stmnt finallyBlock = null;
        if (lex.lookAhead() == FINALLY) {
            lex.get();  // FINALLY
            finallyBlock = parseBlock(tbl);
        }

        return Stmnt.make(TRY, block, catchList, finallyBlock);
    }

    /* return.statement : RETURN [ expression ] ";"
     */
    private Stmnt parseReturn(SymbolTable tbl) throws CompileError {
        int t = lex.get();      // RETURN
        Stmnt s = new Stmnt(t);
        if (lex.lookAhead() != ';')
            s.setLeft(parseExpression(tbl));

        if (lex.get() != ';')
            throw new CompileError("; is missing", lex);

        return s;
    }

    /* throw.statement : THROW expression ";"
     */
    private Stmnt parseThrow(SymbolTable tbl) throws CompileError {
        int t = lex.get();      // THROW
        ASTree expr = parseExpression(tbl);
        if (lex.get() != ';')
            throw new CompileError("; is missing", lex);

        return new Stmnt(t, expr);
    }

    /* break.statement : BREAK [ Identifier ] ";"
     */
    private Stmnt parseBreak(SymbolTable tbl)
        throws CompileError
    {
        return parseContinue(tbl);
    }

    /* continue.statement : CONTINUE [ Identifier ] ";"
     */
    private Stmnt parseContinue(SymbolTable tbl)
        throws CompileError
    {
        int t = lex.get();      // CONTINUE
        Stmnt s = new Stmnt(t);
        int t2 = lex.get();
        if (t2 == Identifier) {
            s.setLeft(new Symbol(lex.getString()));
            t2 = lex.get();
        }

        if (t2 != ';')
            throw new CompileError("; is missing", lex);

        return s;
    }

    /* declaration.or.expression
     *      : [ FINAL ] built-in-type array.dimension declarators
     *      | [ FINAL ] class.type array.dimension declarators
     *      | expression ';'
     *      | expr.list ';'             if exprList is true
     *
     * Note: FINAL is currently ignored.  This must be fixed
     * in future.
     */
    private Stmnt parseDeclarationOrExpression(SymbolTable tbl,
                                               boolean exprList)
        throws CompileError
    {
        int t = lex.lookAhead();
        while (t == FINAL) {
            lex.get();
            t = lex.lookAhead();
        }

        if (isBuiltinType(t)) {
            t = lex.get();
            int dim = parseArrayDimension();
            return parseDeclarators(tbl, new Declarator(t, dim));
        }
        else if (t == Identifier) {
            int i = nextIsClassType(0);
            if (i >= 0)
                if (lex.lookAhead(i) == Identifier) {
                    ASTList name = parseClassType(tbl);
                    int dim = parseArrayDimension();
                    return parseDeclarators(tbl, new Declarator(name, dim));
                }
        }

        Stmnt expr;
        if (exprList)
            expr = parseExprList(tbl);
        else
            expr = new Stmnt(EXPR, parseExpression(tbl));

        if (lex.get() != ';')
            throw new CompileError("; is missing", lex);

        return expr;
    }

    /* expr.list : ( expression ',')* expression
     */
    private Stmnt parseExprList(SymbolTable tbl) throws CompileError {
        Stmnt expr = null;
        for (;;) {
            Stmnt e = new Stmnt(EXPR, parseExpression(tbl));
            expr = (Stmnt)ASTList.concat(expr, new Stmnt(BLOCK, e));
            if (lex.lookAhead() == ',')
                lex.get();
            else
                return expr;
        }
    }

    /* declarators : declarator [ ',' declarator ]* ';'
     */
    private Stmnt parseDeclarators(SymbolTable tbl, Declarator d)
        throws CompileError
    {
        Stmnt decl = null;
        for (;;) {
            decl = (Stmnt)ASTList.concat(decl,
                                new Stmnt(DECL, parseDeclarator(tbl, d)));
            int t = lex.get();
            if (t == ';')
                return decl;
            else if (t != ',')
                throw new CompileError("; is missing", lex);
        }
    }

    /* declarator : Identifier array.dimension [ '=' initializer ]
     */
    private Declarator parseDeclarator(SymbolTable tbl, Declarator d)
        throws CompileError
    {
        if (lex.get() != Identifier || d.getType() == VOID)
            throw new SyntaxError(lex);

        String name = lex.getString();
        Symbol symbol = new Symbol(name);
        int dim = parseArrayDimension();
        ASTree init = null;
        if (lex.lookAhead() == '=') {
            lex.get();
            init = parseInitializer(tbl);
        }

        Declarator decl = d.make(symbol, dim, init);
        tbl.append(name, decl);
        return decl;
    }

    /* initializer : expression | array.initializer
     */
    private ASTree parseInitializer(SymbolTable tbl) throws CompileError {
        if (lex.lookAhead() == '{')
            return parseArrayInitializer(tbl);
        else
            return parseExpression(tbl);
    }

    /* array.initializer :
     *  '{' (( array.initializer | expression ) ',')* '}'
     */
    private ASTree parseArrayInitializer(SymbolTable tbl)
        throws CompileError
    {
        lex.get();      // '{'
        throw new CompileError("array initializer is not supported", lex);
    }

    /* par.expression : '(' expression ')'
     */
    private ASTree parseParExpression(SymbolTable tbl) throws CompileError {
        if (lex.get() != '(')
            throw new SyntaxError(lex);

        ASTree expr = parseExpression(tbl);
        if (lex.get() != ')')
            throw new SyntaxError(lex);

        return expr;
    }

    /* expression : conditional.expr
     *            | conditional.expr assign.op expression (right-to-left)
     */
    public ASTree parseExpression(SymbolTable tbl) throws CompileError {
        ASTree left = parseConditionalExpr(tbl);
        if (!isAssignOp(lex.lookAhead()))
            return left;

        int t = lex.get();
        ASTree right = parseExpression(tbl);
        return AssignExpr.makeAssign(t, left, right);
    }

    private static boolean isAssignOp(int t) {
        return t == '=' || t == MOD_E || t == AND_E
                || t == MUL_E || t == PLUS_E || t == MINUS_E || t == DIV_E
                || t == EXOR_E || t == OR_E || t == LSHIFT_E
                || t == RSHIFT_E || t == ARSHIFT_E;
    }

    /* conditional.expr                 (right-to-left)
     *     : logical.or.expr [ '?' expression ':' conditional.expr ]
     */
    private ASTree parseConditionalExpr(SymbolTable tbl) throws CompileError {
        ASTree cond = parseBinaryExpr(tbl);
        if (lex.lookAhead() == '?') {
            lex.get();
            ASTree thenExpr = parseExpression(tbl);
            if (lex.get() != ':')
                throw new CompileError(": is missing", lex);

            ASTree elseExpr = parseExpression(tbl);
            return new CondExpr(cond, thenExpr, elseExpr);
        }
        else
            return cond;
    }

    /* logical.or.expr          10 (operator precedence)
     * : logical.and.expr
     * | logical.or.expr OROR logical.and.expr          left-to-right
     *
     * logical.and.expr         9
     * : inclusive.or.expr
     * | logical.and.expr ANDAND inclusive.or.expr
     *
     * inclusive.or.expr        8
     * : exclusive.or.expr
     * | inclusive.or.expr "|" exclusive.or.expr
     *
     * exclusive.or.expr        7
     *  : and.expr
     * | exclusive.or.expr "^" and.expr
     *
     * and.expr                 6
     * : equality.expr
     * | and.expr "&" equality.expr
     *
     * equality.expr            5
     * : relational.expr
     * | equality.expr (EQ | NEQ) relational.expr
     *
     * relational.expr          4
     * : shift.expr
     * | relational.expr (LE | GE | "<" | ">") shift.expr
     * | relational.expr INSTANCEOF class.type ("[" "]")*
     *
     * shift.expr               3
     * : additive.expr
     * | shift.expr (LSHIFT | RSHIFT | ARSHIFT) additive.expr
     *
     * additive.expr            2
     * : multiply.expr
     * | additive.expr ("+" | "-") multiply.expr
     *
     * multiply.expr            1
     * : unary.expr
     * | multiply.expr ("*" | "/" | "%") unary.expr
     */
    private ASTree parseBinaryExpr(SymbolTable tbl) throws CompileError {
        ASTree expr = parseUnaryExpr(tbl);
        for (;;) {
            int t = lex.lookAhead();
            int p = getOpPrecedence(t);
            if (p == 0)
                return expr;
            else
                expr = binaryExpr2(tbl, expr, p);
        }
    }

    private ASTree parseInstanceOf(SymbolTable tbl, ASTree expr)
        throws CompileError
    {
        int t = lex.lookAhead();
        if (isBuiltinType(t)) {
            lex.get();  // primitive type
            int dim = parseArrayDimension();
            return new InstanceOfExpr(t, dim, expr);
        }
        else {
            ASTList name = parseClassType(tbl);
            int dim = parseArrayDimension();
            return new InstanceOfExpr(name, dim, expr);
        }
    }

    private ASTree binaryExpr2(SymbolTable tbl, ASTree expr, int prec)
        throws CompileError
    {
        int t = lex.get();
        if (t == INSTANCEOF)
            return parseInstanceOf(tbl, expr);

        ASTree expr2 = parseUnaryExpr(tbl);
        for (;;) {
            int t2 = lex.lookAhead();
            int p2 = getOpPrecedence(t2);
            if (p2 != 0 && prec > p2)
                expr2 = binaryExpr2(tbl, expr2, p2);
            else
                return BinExpr.makeBin(t, expr, expr2);
        }
    }

    // !"#$%&'(    )*+,-./0    12345678    9:;<=>?
    private static final int[] binaryOpPrecedence
        =  { 0, 0, 0, 0, 1, 6, 0, 0,
             0, 1, 2, 0, 2, 0, 1, 0,
             0, 0, 0, 0, 0, 0, 0, 0,
             0, 0, 0, 4, 0, 4, 0 };

    private int getOpPrecedence(int c) {
        if ('!' <= c && c <= '?')
            return binaryOpPrecedence[c - '!'];
        else if (c == '^')
            return 7;
        else if (c == '|')
            return 8;
        else if (c == ANDAND)
            return 9;
        else if (c == OROR)
            return 10;
        else if (c == EQ || c == NEQ)
            return 5;
        else if (c == LE || c == GE || c == INSTANCEOF)
            return 4;
        else if (c == LSHIFT || c == RSHIFT || c == ARSHIFT)
            return 3;
        else
            return 0;   // not a binary operator
    }

    /* unary.expr : "++"|"--" unary.expr
                  | "+"|"-" unary.expr
                  | "!"|"~" unary.expr
                  | cast.expr
                  | postfix.expr

       unary.expr.not.plus.minus is a unary expression starting without
       "+", "-", "++", or "--".
     */
    private ASTree parseUnaryExpr(SymbolTable tbl) throws CompileError {
        int t;
        switch (lex.lookAhead()) {
        case '+' :
        case '-' :
        case PLUSPLUS :
        case MINUSMINUS :
        case '!' :
        case '~' :
            t = lex.get();
            return Expr.make(t, parseUnaryExpr(tbl));
        case '(' :
            return parseCast(tbl);
        default :
            return parsePostfix(tbl);
        }
    }

    /* cast.expr : "(" builtin.type ("[" "]")* ")" unary.expr
                 | "(" class.type ("[" "]")* ")" unary.expr2

       unary.expr2 is a unary.expr begining with "(", NULL, StringL,
       Identifier, THIS, SUPER, or NEW.

       Either "(int.class)" or "(String[].class)" is a not cast expression.
     */
    private ASTree parseCast(SymbolTable tbl) throws CompileError {
        int t = lex.lookAhead(1);
        if (isBuiltinType(t) && nextIsBuiltinCast()) {
            lex.get();  // '('
            lex.get();  // primitive type
            int dim = parseArrayDimension();
            if (lex.get() != ')')
                throw new CompileError(") is missing", lex);

            return new CastExpr(t, dim, parseUnaryExpr(tbl));
        }
        else if (t == Identifier && nextIsClassCast()) {
            lex.get();  // '('
            ASTList name = parseClassType(tbl);
            int dim = parseArrayDimension();
            if (lex.get() != ')')
                throw new CompileError(") is missing", lex);

            return new CastExpr(name, dim, parseUnaryExpr(tbl));
        }
        else
            return parsePostfix(tbl);
    }

    private boolean nextIsBuiltinCast() {
        int t;
        int i = 2;
        while ((t = lex.lookAhead(i++)) == '[')
            if (lex.lookAhead(i++) != ']')
                return false;

        return lex.lookAhead(i - 1) == ')';
    }

    private boolean nextIsClassCast() {
        int i = nextIsClassType(1);
        if (i < 0)
            return false;

        int t = lex.lookAhead(i);
        if (t != ')')
            return false;

        t = lex.lookAhead(i + 1);
        return t == '(' || t == NULL || t == StringL
               || t == Identifier || t == THIS || t == SUPER || t == NEW
               || t == TRUE || t == FALSE || t == LongConstant
               || t == IntConstant || t == CharConstant
               || t == DoubleConstant || t == FloatConstant;
    }

    private int nextIsClassType(int i) {
        int t;
        while (lex.lookAhead(++i) == '.')
            if (lex.lookAhead(++i) != Identifier)
                return -1;

        while ((t = lex.lookAhead(i++)) == '[')
            if (lex.lookAhead(i++) != ']')
                return -1;

        return i - 1;
    }

    /* array.dimension : [ "[" "]" ]*
     */
    private int parseArrayDimension() throws CompileError {
        int arrayDim = 0;
        while (lex.lookAhead() == '[') {
            ++arrayDim;
            lex.get();
            if (lex.get() != ']')
                throw new CompileError("] is missing", lex);
        }

        return arrayDim;
    }

    /* class.type : Identifier ( "." Identifier )*
     */
    private ASTList parseClassType(SymbolTable tbl) throws CompileError {
        ASTList list = null;
        for (;;) {
            if (lex.get() != Identifier)
                throw new SyntaxError(lex);

            list = ASTList.append(list, new Symbol(lex.getString()));
            if (lex.lookAhead() == '.')
                lex.get();
            else
                break;
        }

        return list;
    }

    /* postfix.expr : number.literal
     *              | primary.expr
     *              | method.expr
     *              | postfix.expr "++" | "--"
     *              | postfix.expr "[" array.size "]"
     *              | postfix.expr "." Identifier
     *              | postfix.expr ( "[" "]" )* "." CLASS
     *              | postfix.expr "#" Identifier
     *
     * "#" is not an operator of regular Java.  It separates
     * a class name and a member name in an expression for static member
     * access.  For example,
     *     java.lang.Integer.toString(3)        in regular Java
     * must be written like this:
     *     java.lang.Integer#toString(3)        for this compiler.
     */
    private ASTree parsePostfix(SymbolTable tbl) throws CompileError {
        int token = lex.lookAhead();
        switch (token) {
        case LongConstant :
        case IntConstant :
        case CharConstant :
            lex.get();
            return new IntConst(lex.getLong(), token);
        case DoubleConstant :
        case FloatConstant :
            lex.get();
            return new DoubleConst(lex.getDouble(), token);
        default :
            break;
        }

        String str;
        ASTree index;
        ASTree expr = parsePrimaryExpr(tbl);
        int t;
        while (true) {
            switch (lex.lookAhead()) {
            case '(' :
                expr = parseMethodCall(tbl, expr);
                break;
            case '[' :
                if (lex.lookAhead(1) == ']') {
                    int dim = parseArrayDimension();
                    if (lex.get() != '.' || lex.get() != CLASS)
                        throw new SyntaxError(lex);

                    expr = parseDotClass(expr, dim);
                }
                else {
                    index = parseArrayIndex(tbl);
                    if (index == null)
                        throw new SyntaxError(lex);

                    expr = Expr.make(ARRAY, expr, index);
                }
                break;
            case PLUSPLUS :
            case MINUSMINUS :
                t = lex.get();
                expr = Expr.make(t, null, expr);
                break;
            case '.' :
                lex.get();
                t = lex.get();
                if (t == CLASS) {
                    expr = parseDotClass(expr, 0);
                }
                else if (t == Identifier) {
                    str = lex.getString();
                    expr = Expr.make('.', expr, new Member(str));
                }
                else
                    throw new CompileError("missing member name", lex);
                break;
            case '#' :
                lex.get();
                t = lex.get();
                if (t != Identifier)
                    throw new CompileError("missing static member name", lex);

                str = lex.getString();
                expr = Expr.make(MEMBER, new Symbol(toClassName(expr)),
                                 new Member(str));
                break;
            default :
                return expr;
            }
        }
    }

    /* Parse a .class expression on a class type.  For example,
     * String.class   => ('.' "String" "class")
     * String[].class => ('.' "[LString;" "class")
     */
    private ASTree parseDotClass(ASTree className, int dim)
        throws CompileError
    {
        String cname = toClassName(className);
        if (dim > 0) {
            StringBuffer sbuf = new StringBuffer();
            while (dim-- > 0)
                sbuf.append('[');

            sbuf.append('L').append(cname.replace('.', '/')).append(';');
            cname = sbuf.toString();
        }

        return Expr.make('.', new Symbol(cname), new Member("class"));
    }

    /* Parses a .class expression on a built-in type.  For example,
     * int.class   => ('#' "java.lang.Integer" "TYPE")
     * int[].class => ('.' "[I", "class")
     */
    private ASTree parseDotClass(int builtinType, int dim)
        throws CompileError
    {
        if (dim > 0) {
            String cname = CodeGen.toJvmTypeName(builtinType, dim);
            return Expr.make('.', new Symbol(cname), new Member("class"));
        }
        else {
            String cname;
            switch(builtinType) {
            case BOOLEAN :
                cname = "java.lang.Boolean";
                break;
            case BYTE :
                cname = "java.lang.Byte";
                break;
            case CHAR :
                cname = "java.lang.Character";
                break;
            case SHORT :
                cname = "java.lang.Short";
                break;
            case INT :
                cname = "java.lang.Integer";
                break;
            case LONG :
                cname = "java.lang.Long";
                break;
            case FLOAT :
                cname = "java.lang.Float";
                break;
            case DOUBLE :
                cname = "java.lang.Double";
                break;
            case VOID :
                cname = "java.lang.Void";
                break;
            default :
                throw new CompileError("invalid builtin type: "
                                       + builtinType);
            }

            return Expr.make(MEMBER, new Symbol(cname), new Member("TYPE"));
        }
    }

    /* method.call : method.expr "(" argument.list ")"
     * method.expr : THIS | SUPER | Identifier
     *             | postfix.expr "." Identifier
     *             | postfix.expr "#" Identifier
     */
    private ASTree parseMethodCall(SymbolTable tbl, ASTree expr)
        throws CompileError
    {
        if (expr instanceof Keyword) {
            int token = ((Keyword)expr).get();
            if (token != THIS && token != SUPER)
                throw new SyntaxError(lex);
        }
        else if (expr instanceof Symbol)        // Identifier
            ;
        else if (expr instanceof Expr) {
            int op = ((Expr)expr).getOperator();
            if (op != '.' && op != MEMBER)
                throw new SyntaxError(lex);
        }

        return CallExpr.makeCall(expr, parseArgumentList(tbl));
    }

    private String toClassName(ASTree name)
        throws CompileError
    {
        StringBuffer sbuf = new StringBuffer();
        toClassName(name, sbuf);
        return sbuf.toString();
    }

    private void toClassName(ASTree name, StringBuffer sbuf)
        throws CompileError
    {
        if (name instanceof Symbol) {
            sbuf.append(((Symbol)name).get());
            return;
        }
        else if (name instanceof Expr) {
            Expr expr = (Expr)name;
            if (expr.getOperator() == '.') {
                toClassName(expr.oprand1(), sbuf);
                sbuf.append('.');
                toClassName(expr.oprand2(), sbuf);
                return;
            }
        }

        throw new CompileError("bad static member access", lex);
    }

    /* primary.expr : THIS | SUPER | TRUE | FALSE | NULL
     *              | StringL
     *              | Identifier
     *              | NEW new.expr
     *              | "(" expression ")"
     *              | builtin.type ( "[" "]" )* "." CLASS
     *
     * Identifier represents either a local variable name, a member name,
     * or a class name.
     */
    private ASTree parsePrimaryExpr(SymbolTable tbl) throws CompileError {
        int t;
        String name;
        Declarator decl;
        ASTree expr;

        switch (t = lex.get()) {
        case THIS :
        case SUPER :
        case TRUE :
        case FALSE :
        case NULL :
            return new Keyword(t);
        case Identifier :
            name = lex.getString();
            decl = tbl.lookup(name);
            if (decl == null)
                return new Member(name);        // this or static member
            else
                return new Variable(name, decl); // local variable
        case StringL :
            return new StringL(lex.getString());
        case NEW :
            return parseNew(tbl);
        case '(' :
            expr = parseExpression(tbl);
            if (lex.get() == ')')
                return expr;
            else
                throw new CompileError(") is missing", lex);
        default :
            if (isBuiltinType(t) || t == VOID) {
                int dim = parseArrayDimension();
                if (lex.get() == '.' && lex.get() == CLASS)
                    return parseDotClass(t, dim);
            }

            throw new SyntaxError(lex);
        }
    }

    /* new.expr : class.type "(" argument.list ")"
     *          | class.type     array.size [ array.initializer ]
     *          | primitive.type array.size [ array.initializer ]
     */
    private NewExpr parseNew(SymbolTable tbl) throws CompileError {
        ASTree init = null;
        int t = lex.lookAhead();
        if (isBuiltinType(t)) {
            lex.get();
            ASTList size = parseArraySize(tbl);
            if (lex.lookAhead() == '{')
                init = parseArrayInitializer(tbl);

            return new NewExpr(t, size, init);
        }
        else if (t == Identifier) {
            ASTList name = parseClassType(tbl);
            t = lex.lookAhead();
            if (t == '(') {
                ASTList args = parseArgumentList(tbl);
                return new NewExpr(name, args);
            }
            else if (t == '[') {
                ASTList size = parseArraySize(tbl);
                if (lex.lookAhead() == '{')
                    init = parseArrayInitializer(tbl);

                return NewExpr.makeObjectArray(name, size, init);
            }
        }

        throw new SyntaxError(lex);
    }

    /* array.size : [ array.index ]*
     */
    private ASTList parseArraySize(SymbolTable tbl) throws CompileError {
        ASTList list = null;
        while (lex.lookAhead() == '[')
            list = ASTList.append(list, parseArrayIndex(tbl));

        return list;
    }

    /* array.index : "[" [ expression ] "]"
     */
    private ASTree parseArrayIndex(SymbolTable tbl) throws CompileError {
        lex.get();      // '['
        if (lex.lookAhead() == ']') {
            lex.get();
            return null;
        }
        else {
            ASTree index = parseExpression(tbl);
            if (lex.get() != ']')
                throw new CompileError("] is missing", lex);

            return index;
        }
    }

    /* argument.list : "(" [ expression [ "," expression ]* ] ")"
     */
    private ASTList parseArgumentList(SymbolTable tbl) throws CompileError {
        if (lex.get() != '(')
            throw new CompileError("( is missing", lex);

        ASTList list = null;
        if (lex.lookAhead() != ')')
            for (;;) {
                list = ASTList.append(list, parseExpression(tbl));
                if (lex.lookAhead() == ',')
                    lex.get();
                else
                    break;
            }

        if (lex.get() != ')')
            throw new CompileError(") is missing", lex);

        return list;
    }
}