00001: package clogs.parser;
00002:
00003: import java_cup.runtime.*;
00004: import clogs.ast.*;
00005: import clogs.util.List;
00006: import clogs.util.Optional;
00007:
00008: /* TO PRINT LIST OF TOKENS DURING READ, UNCOMMENT FOLLOWING LINE */
00009: /* scan with {: return ((ClogsLexer) getScanner ()).debug_next_token (); :}; */
00010:
00011: terminal IF, LOWER_THAN_ELSE, ELSE, WHILE, GOTO, RETURN, SKIP, NEW;
00012: terminal VOID, INT;
00013: terminal LBRACK, RBRACK, LBRACE, RBRACE, LPAREN, RPAREN;
00014: terminal COMMA, SEMI, COLON, COLONCOLON, EQUALS, BANG, EQUALSEQUALS, BANGEQUALS;
00015: terminal LESSTHAN, GREATERTHAN, LEQ, GEQ, BAR, CARET, AMP, BARBAR, AMPAMP;
00016: terminal PLUS, MINUS, TIMES, DIVIDE, MOD;
00017:
00018: terminal String IDENTIFIER;
00019: terminal Integer INTEGER_CONSTANT;
00020: terminal Character CHARACTER_CONSTANT;
00021: terminal String STRING_CONSTANT;
00022:
00023: non terminal List<ExtDecl> translation_unit;
00024: non terminal ExtDecl external_declaration;
00025: non terminal FunDef function_definition;
00026: non terminal Decl declaration;
00027: non terminal List<Decl> declaration_list_opt;
00028: non terminal List<Decl> declaration_list;
00029: non terminal Type type_specifier;
00030: non terminal InitDecl init_declarator;
00031: non terminal List<Decl> parameter_type_list_opt;
00032: non terminal List<Decl> parameter_type_list;
00033: non terminal Decl parameter_declaration;
00034: non terminal Exp initializer;
00035: non terminal Stat statement;
00036: non terminal Stat labeled_statement;
00037: non terminal Stat expression_statement;
00038: non terminal StatCompound compound_statement;
00039: non terminal List<Stat> statement_list_opt;
00040: non terminal List<Stat> statement_list;
00041: non terminal Stat selection_statement;
00042: non terminal Stat iteration_statement;
00043: non terminal Stat jump_statement;
00044: non terminal Optional<Exp> expression_opt;
00045: non terminal Exp expression;
00046: non terminal Exp assignment_expression;
00047: non terminal Exp assignment_operator;
00048: non terminal Exp conditional_expression;
00049: non terminal Exp logical_or_expression;
00050: non terminal Exp logical_and_expression;
00051: non terminal Exp inclusive_or_expression;
00052: non terminal Exp exclusive_or_expression;
00053: non terminal Exp and_expression;
00054: non terminal Exp equality_expression;
00055: non terminal Exp relational_expression;
00056: non terminal Exp shift_expression;
00057: non terminal Exp additive_expression;
00058: non terminal Exp multiplicative_expression;
00059: non terminal Exp cast_expression;
00060: non terminal Exp unary_expression;
00061: non terminal ExpUnOp.UnOp unary_operator;
00062: non terminal Exp postfix_expression;
00063: non terminal Exp primary_expression;
00064: non terminal Optional<Type> type_assertion_opt;
00065: non terminal List<Exp> argument_expression_list_opt;
00066: non terminal List<Exp> argument_expression_list;
00067: non terminal Exp constant;
00068:
00069: precedence nonassoc LOWER_THAN_ELSE;
00070: precedence nonassoc ELSE;
00071:
00072: translation_unit
00073: ::= external_declaration:ed
00074: {: RESULT = List.singleton (ed); :}
00075: |
00076: translation_unit:l external_declaration:ed
00077: {: RESULT = l.snoc (ed); :}
00078: ;
00079:
00080: external_declaration
00081: ::= function_definition:fd
00082: {: RESULT = fd; :}
00083: |
00084: declaration:d
00085: {: RESULT = d; :}
00086: ;
00087:
00088: function_definition
00089: ::= type_specifier:t IDENTIFIER:name LPAREN parameter_type_list_opt:l RPAREN compound_statement:s
00090: {: RESULT = new FunDef (t, name, l, s); :}
00091: ;
00092:
00093: declaration
00094: ::= type_specifier:t init_declarator:id SEMI
00095: {: RESULT = new Decl (t, id.name, id.eo); :}
00096: ;
00097:
00098: declaration_list_opt
00099: ::= declaration_list:l
00100: {: RESULT = l; :}
00101: |
00102: {: RESULT = List.nil (); :}
00103: ;
00104:
00105: declaration_list
00106: ::= declaration:d
00107: {: RESULT = List.singleton (d); :}
00108: |
00109: declaration_list:l declaration:d
00110: {: RESULT = l.snoc (d); :}
00111: ;
00112:
00113: type_specifier
00114: ::= VOID
00115: {: RESULT = new TypeVoid (); :}
00116: |
00117: INT
00118: {: RESULT = new TypeInt (); :}
00119: |
00120: type_specifier:t LBRACK RBRACK
00121: {: RESULT = new TypeArray (t); :}
00122: ;
00123:
00124: init_declarator
00125: ::= IDENTIFIER:name
00126: {: RESULT = new InitDecl (name, new Optional<Exp> ()); :}
00127: |
00128: IDENTIFIER:name EQUALS initializer:e
00129: {: RESULT = new InitDecl (name, new Optional<Exp> (e)); :}
00130: ;
00131:
00132: parameter_type_list_opt
00133: ::= parameter_type_list:l
00134: {: RESULT = l; :}
00135: |
00136: {: RESULT = List.nil (); :}
00137: ;
00138:
00139: parameter_type_list
00140: ::= parameter_declaration:d
00141: {: RESULT = List.singleton (d); :}
00142: |
00143: parameter_type_list:l COMMA parameter_declaration:d
00144: {: RESULT = l.snoc (d); :}
00145: ;
00146:
00147: parameter_declaration
00148: ::= type_specifier:t IDENTIFIER:name
00149: {: RESULT = new Decl (t, name, new Optional<Exp> ()); :}
00150: ;
00151:
00152: initializer
00153: ::= assignment_expression:e
00154: {: RESULT = e; :}
00155: ;
00156:
00157: statement
00158: ::= labeled_statement:s
00159: {: RESULT = s; :}
00160: |
00161: expression_statement:s
00162: {: RESULT = s; :}
00163: |
00164: compound_statement:s
00165: {: RESULT = s; :}
00166: |
00167: selection_statement:s
00168: {: RESULT = s; :}
00169: |
00170: iteration_statement:s
00171: {: RESULT = s; :}
00172: |
00173: jump_statement:s
00174: {: RESULT = s; :}
00175: ;
00176:
00177: labeled_statement
00178: ::= IDENTIFIER:label COLON statement:s
00179: {: RESULT = s.addLabel (label); :}
00180: ;
00181:
00182: expression_statement
00183: ::= expression_opt:eo SEMI
00184: {:
00185: if (eo.isEmpty ()) {
00186: RESULT = new StatSkip ();
00187: } else {
00188: RESULT = new StatExp (eo.get ());
00189: }
00190: :}
00191: |
00192: SKIP SEMI
00193: {: RESULT = new StatSkip (); :}
00194: ;
00195:
00196: compound_statement
00197: ::= LBRACE declaration_list_opt:ld statement_list_opt:ls RBRACE
00198: {: RESULT = new StatCompound (ld, ls); :}
00199: ;
00200:
00201: statement_list_opt
00202: ::= statement_list:l
00203: {: RESULT = l; :}
00204: |
00205: {: RESULT = List.nil (); :}
00206: ;
00207:
00208: statement_list
00209: ::= statement:s
00210: {: RESULT = List.singleton (s); :}
00211: |
00212: statement_list:l statement:s
00213: {: RESULT = l.snoc (s); :}
00214: ;
00215:
00216: selection_statement
00217: ::= IF LPAREN expression:e RPAREN statement:sT
00218: {: RESULT = new StatIf (e, sT, new StatSkip ()); :}
00219: %prec LOWER_THAN_ELSE
00220: |
00221: IF LPAREN expression:e RPAREN statement:sT ELSE statement:sF
00222: {: RESULT = new StatIf (e, sT, sF); :}
00223: ;
00224:
00225: iteration_statement
00226: ::= WHILE LPAREN expression:e RPAREN statement:s
00227: {: RESULT = new StatWhile (e, s); :}
00228: ;
00229:
00230: jump_statement
00231: ::= GOTO IDENTIFIER:target SEMI
00232: {: RESULT = new StatGoto (target); :}
00233: |
00234: RETURN expression_opt:eo SEMI
00235: {: RESULT = new StatReturn (eo); :}
00236: ;
00237:
00238: expression_opt
00239: ::= expression:e
00240: {: RESULT = new Optional<Exp> (e); :}
00241: |
00242: {: RESULT = new Optional<Exp> (); :}
00243: ;
00244:
00245: expression
00246: ::= assignment_expression:e
00247: {: RESULT = e; :}
00248: |
00249: expression:e1 COMMA assignment_expression:e2
00250: {: RESULT = new ExpComma (e1, e2); :}
00251: ;
00252:
00253: assignment_expression
00254: ::= conditional_expression:e
00255: {: RESULT = e; :}
00256: |
00257: unary_expression:e1 assignment_operator:op assignment_expression:e2
00258: {: RESULT = new ExpAssign (e1, e2); :}
00259: ;
00260:
00261: assignment_operator
00262: ::= EQUALS;
00263:
00264: conditional_expression
00265: ::= logical_or_expression:e
00266: {: RESULT = e; :}
00267: ;
00268: /* ignored cases */
00269:
00270: logical_or_expression
00271: ::= logical_and_expression:e
00272: {: RESULT = e; :}
00273: |
00274: logical_or_expression:e1 BARBAR logical_and_expression:e2
00275: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.BARBAR, e1, e2); :}
00276: ;
00277:
00278: logical_and_expression
00279: ::= inclusive_or_expression:e
00280: {: RESULT = e; :}
00281: |
00282: logical_and_expression:e1 AMPAMP inclusive_or_expression:e2
00283: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.AMPAMP, e1, e2); :}
00284: ;
00285:
00286: inclusive_or_expression
00287: ::= exclusive_or_expression:e
00288: {: RESULT = e; :}
00289: |
00290: inclusive_or_expression:e1 BAR exclusive_or_expression:e2
00291: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.BAR, e1, e2); :}
00292: ;
00293:
00294: exclusive_or_expression
00295: ::= and_expression:e
00296: {: RESULT = e; :}
00297: |
00298: exclusive_or_expression:e1 CARET and_expression:e2
00299: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.CARET, e1, e2); :}
00300: ;
00301:
00302: and_expression
00303: ::= equality_expression:e
00304: {: RESULT = e; :}
00305: |
00306: and_expression:e1 AMP equality_expression:e2
00307: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.AMP, e1, e2); :}
00308: ;
00309:
00310: equality_expression
00311: ::= relational_expression:e
00312: {: RESULT = e; :}
00313: |
00314: equality_expression:e1 EQUALSEQUALS relational_expression:e2
00315: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.EQUALSEQUALS, e1, e2); :}
00316: |
00317: equality_expression:e1 BANGEQUALS relational_expression:e2
00318: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.BANGEQUALS, e1, e2); :}
00319: ;
00320:
00321: relational_expression
00322: ::= shift_expression:e
00323: {: RESULT = e; :}
00324: |
00325: relational_expression:e1 LESSTHAN shift_expression:e2
00326: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.LESSTHAN, e1, e2); :}
00327: |
00328: relational_expression:e1 GREATERTHAN shift_expression:e2
00329: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.GREATERTHAN, e1, e2); :}
00330: |
00331: relational_expression:e1 LEQ shift_expression:e2
00332: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.LEQ, e1, e2); :}
00333: |
00334: relational_expression:e1 GEQ shift_expression:e2
00335: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.GEQ, e1, e2); :}
00336: ;
00337:
00338: shift_expression
00339: ::= additive_expression:e
00340: {: RESULT = e; :}
00341: ;
00342: /* ignored cases */
00343:
00344: additive_expression
00345: ::= multiplicative_expression:e
00346: {: RESULT = e; :}
00347: |
00348: additive_expression:e1 PLUS multiplicative_expression:e2
00349: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.PLUS, e1, e2); :}
00350: |
00351: additive_expression:e1 MINUS multiplicative_expression:e2
00352: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.MINUS, e1, e2); :}
00353: ;
00354:
00355: multiplicative_expression
00356: ::= cast_expression:e
00357: {: RESULT = e; :}
00358: |
00359: multiplicative_expression:e1 TIMES cast_expression:e2
00360: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.TIMES, e1, e2); :}
00361: |
00362: multiplicative_expression:e1 DIVIDE cast_expression:e2
00363: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.DIVIDE, e1, e2); :}
00364: |
00365: multiplicative_expression:e1 MOD cast_expression:e2
00366: {: RESULT = new ExpBinOp (ExpBinOp.BinOp.MOD, e1, e2); :}
00367: ;
00368:
00369: cast_expression
00370: ::= unary_expression:e
00371: {: RESULT = e; :}
00372: ;
00373: /* ignored cases */
00374:
00375: unary_expression
00376: ::= postfix_expression:e
00377: {: RESULT = e; :}
00378: |
00379: unary_operator:op cast_expression:e
00380: {: RESULT = new ExpUnOp (op, e); :}
00381: ;
00382: /* ignored cases: PLUSPLUS, MINUSMINUS */
00383:
00384: unary_operator
00385: ::= MINUS
00386: {: RESULT = ExpUnOp.UnOp.MINUS; :}
00387: |
00388: BANG
00389: {: RESULT = ExpUnOp.UnOp.BANG; :}
00390: ;
00391:
00392: postfix_expression
00393: ::= primary_expression:e
00394: {: RESULT = e; :}
00395: |
00396: postfix_expression:e1 LBRACK expression:e2 RBRACK
00397: {: RESULT = new ExpArrayAccess (e1, e2); :}
00398: |
00399: IDENTIFIER:name LPAREN argument_expression_list_opt:l RPAREN
00400: {: RESULT = new ExpFunCall (name, l); :}
00401: ;
00402: /* ignored cases: PLUSPLUS, MINUSMINUS */
00403:
00404: primary_expression
00405: ::= IDENTIFIER:e
00406: {: RESULT = new ExpVar (e); :}
00407: |
00408: constant:e
00409: {: RESULT = e; :}
00410: |
00411: NEW type_specifier:t LBRACK expression:e RBRACK
00412: {: RESULT = new ExpNew (t, e); :}
00413: |
00414: LPAREN expression:e type_assertion_opt:to RPAREN
00415: {: RESULT = to.isEmpty () ? e : e.setType (to.get ()); :}
00416: ;
00417:
00418: type_assertion_opt
00419: ::= COLONCOLON type_specifier:t
00420: {: RESULT = new Optional<Type> (t); :}
00421: |
00422: {: RESULT = new Optional<Type> (); :}
00423: ;
00424:
00425: argument_expression_list_opt
00426: ::= argument_expression_list:l
00427: {: RESULT = l; :}
00428: |
00429: {: RESULT = List.<Exp>nil (); :}
00430: ;
00431:
00432: argument_expression_list
00433: ::= assignment_expression:e
00434: {: RESULT = List.<Exp>singleton (e); :}
00435: |
00436: argument_expression_list:l COMMA assignment_expression:e
00437: {: RESULT = l.snoc (e); :}
00438: ;
00439:
00440: constant
00441: ::= INTEGER_CONSTANT:e
00442: {: RESULT = new ExpInt (e.intValue ()); :}
00443: |
00444: CHARACTER_CONSTANT:e
00445: {: RESULT = new ExpInt (0xffff & e.charValue ()); :}
00446: |
00447: STRING_CONSTANT:e
00448: {: RESULT = new ExpString (e); :}
00449: ;
00450:
00451:
|