package IC.Parser; import IC.Lexer.*; import java_cup.runtime.*; import IC.Hierarchy.*; /* IC parser for CUP. * Created by Ilya Ganusov */ //Terminals returned by the lexer terminal String IDENTIFIER; terminal PLUS; terminal MINUS; terminal STAR; terminal FORWARDSLASH; terminal PERCENT; terminal AND; terminal OR; terminal LESS; terminal LESSEQUAL; terminal GREATEREQUAL; terminal GREATER; terminal EQUAL; terminal ASSIGN; terminal NOTEQUAL; terminal BANG; terminal COMMENTDOUBLESLASH; terminal COMMENTSTARSLASH; terminal QUOTE; terminal BACKSLASH; terminal QUOTEESCAPE; terminal BACKSLASHESCAPE; terminal TAB; terminal NEWLINE; terminal CLASS; terminal RETURN; terminal THIS; terminal EXTENDS; terminal IF; terminal NEW; terminal VOID; terminal ELSE; terminal LENGTH; terminal INT; terminal WHILE; terminal TRUE; terminal BOOLEAN; terminal BREAK; terminal FALSE; terminal STRING; terminal CONTINUE; terminal NULL; terminal RPAREN; terminal LPAREN; terminal RBRACKET; terminal LBRACKET; terminal RBRACE; terminal LBRACE; terminal COMMA; terminal SEMICOLON; terminal PERIOD; terminal Long NUMBER; terminal String TEXT; terminal ERROR; //Non-terminals non terminal Node program; non terminal Node classDecls_opt; non terminal Node classDecls; non terminal Node classDecl; non terminal Node super_opt; non terminal Node super; non terminal Node class_body; non terminal Node class_body_decls_opt; non terminal Node class_body_decls; non terminal Node class_body_decl; non terminal Node fieldDecl; non terminal Node field_variable_declarators_opt; non terminal Node field_variable_declarators; non terminal Node field_variable_declarator; non terminal Node methDecl; non terminal Node meth_header; non terminal Node meth_body; non terminal Node method_declarator; non terminal Node formals_opt; non terminal Node formals; non terminal Node formals_list_opt; non terminal Node formals_list; non terminal Node formal_parameter; non terminal Node type; non terminal Node primitive_type; non terminal Node reference_type; non terminal Node array_type; non terminal Node block; non terminal Node block_statements_opt; non terminal Node block_statements; non terminal Node block_statement; non terminal Node varDecl; non terminal Node variable_declarators; non terminal Node variable_declarator; non terminal Node stmt; non terminal Node stmt_without_trailing_substatement; non terminal Node continue_statement; non terminal Node break_statement; non terminal Node return_statement; non terminal Node expr_opt; non terminal Node assign_statement; non terminal Node if_then_statement; non terminal Node if_then_else_statement; non terminal Node stmt_no_short_if; non terminal Node if_then_else_statement_no_short_if; non terminal Node while_statement; non terminal Node while_statement_no_short_if; non terminal Node assign; non terminal Node assignment_expression; //non terminal Node location; non terminal Node field_access; non terminal Node array_access; non terminal Node call; non terminal Node actuals_opt; //non terminal Node method; non terminal Node actuals; non terminal Node expr; non terminal Node no_new_array_expr; non terminal Node array_creation_expr; non terminal Node binary_expr; non terminal Node unary_expr; non terminal Node literal; //Newly added non terminal Node assignment; non terminal Node primary_expr; non terminal Node unary_expr_not_minus_bang; non terminal Node name; non terminal Node simple_name; non terminal Node qualified_name; non terminal Node call_statement; non terminal Node varDecls_opt; non terminal Node varDecls; non terminal Node stmts_opt; non terminal Node stmts; //Precedences //precedence ASSIGN; precedence left OR; precedence left AND; precedence left EQUAL, NOTEQUAL; precedence left LESS, LESSEQUAL, GREATER, GREATEREQUAL; precedence left PLUS, MINUS; precedence left STAR, FORWARDSLASH, PERCENT; precedence left BANG; precedence left LPAREN, LBRACKET, PERIOD; // program ::= classDecls_opt; classDecls_opt ::= | classDecls ; classDecls ::= classDecl | classDecls classDecl ; //Class declaration classDecl ::= CLASS IDENTIFIER super_opt class_body; super_opt ::= | super ; super ::= EXTENDS IDENTIFIER; class_body ::= LBRACE class_body_decls_opt RBRACE; class_body_decls_opt ::= | class_body_decls ; class_body_decls ::= class_body_decl | class_body_decls class_body_decl ; class_body_decl ::= fieldDecl | methDecl ; //Field declaration fieldDecl ::= type IDENTIFIER field_variable_declarators_opt SEMICOLON; field_variable_declarators_opt ::= | field_variable_declarators ; field_variable_declarators ::= field_variable_declarator | field_variable_declarators field_variable_declarator ; field_variable_declarator ::= COMMA IDENTIFIER; //Method Declaration: methDecl ::= meth_header meth_body; meth_header ::= type method_declarator | VOID method_declarator ; method_declarator ::= IDENTIFIER LPAREN formals_opt RPAREN; formals_opt ::= | formals ; meth_body ::= block; formals ::= type IDENTIFIER formals_list_opt; formals_list_opt ::= | formals_list; formals_list ::= formal_parameter | formals_list COMMA formal_parameter; formal_parameter ::= type IDENTIFIER; //Types type ::= primitive_type | reference_type; primitive_type ::= INT | BOOLEAN | STRING; reference_type ::= simple_name | array_type ; array_type ::= type LBRACKET RBRACKET; //Blocks //block ::= LBRACE block_statements_opt RBRACE; // //block_statements_opt ::= // | block_statements // ; //block_statements ::= // block_statement // | block_statements block_statement // ; //block_statement ::= // varDecl // | stmt // ; block ::= LBRACE varDecls stmts_opt RBRACE | LBRACE stmts_opt RBRACE; varDecls ::= varDecl | varDecls varDecl ; stmts_opt ::= | stmts ; stmts ::= stmt | stmts stmt ; //Variable Declaration varDecl ::= type variable_declarators SEMICOLON; variable_declarators ::= variable_declarator | variable_declarators COMMA variable_declarator ; variable_declarator ::= IDENTIFIER | IDENTIFIER EQUAL expr ; //Statements stmt ::= stmt_without_trailing_substatement | if_then_statement | if_then_else_statement | while_statement ; stmt_without_trailing_substatement ::= block | continue_statement | break_statement | return_statement | assign_statement | call_statement ; continue_statement ::= CONTINUE SEMICOLON; break_statement ::= BREAK SEMICOLON; return_statement ::= RETURN expr_opt SEMICOLON; expr_opt ::= | expr ; assign_statement ::= assignment SEMICOLON; call_statement ::= call SEMICOLON; if_then_statement ::= IF LPAREN expr RPAREN stmt; if_then_else_statement ::= IF LPAREN expr RPAREN stmt_no_short_if ELSE stmt; stmt_no_short_if ::= stmt_without_trailing_substatement | if_then_else_statement_no_short_if | while_statement_no_short_if ; if_then_else_statement_no_short_if ::= IF LPAREN expr RPAREN stmt_no_short_if ELSE stmt_no_short_if ; while_statement ::= WHILE LPAREN expr RPAREN stmt ; while_statement_no_short_if ::= WHILE LPAREN expr RPAREN stmt_no_short_if ; //Method Invocation call ::= IDENTIFIER LPAREN actuals_opt RPAREN | primary_expr PERIOD IDENTIFIER LPAREN actuals_opt RPAREN ; actuals_opt ::= | actuals ; actuals ::= expr | actuals COMMA expr ; //Expressions primary_expr ::= no_new_array_expr | array_creation_expr ; no_new_array_expr ::= call | THIS | NEW IDENTIFIER LPAREN RPAREN | primary_expr PERIOD LENGTH | literal | LPAREN expr RPAREN | field_access | array_access ; array_creation_expr ::= NEW type LBRACKET primary_expr RBRACKET ; field_access ::= primary_expr PERIOD IDENTIFIER | IDENTIFIER PERIOD IDENTIFIER ; array_access ::= no_new_array_expr LBRACKET expr RBRACKET | simple_name LBRACKET expr RBRACKET | qualified_name LBRACKET expr RBRACKET ; simple_name ::= IDENTIFIER; qualified_name ::= simple_name PERIOD IDENTIFIER; binary_expr ::= unary_expr | binary_expr PLUS unary_expr | binary_expr MINUS unary_expr | binary_expr STAR unary_expr | binary_expr FORWARDSLASH unary_expr | binary_expr PERCENT unary_expr | binary_expr AND unary_expr | binary_expr OR unary_expr | binary_expr LESS unary_expr | binary_expr LESSEQUAL unary_expr | binary_expr GREATER unary_expr | binary_expr GREATEREQUAL unary_expr | binary_expr EQUAL unary_expr | binary_expr NOTEQUAL unary_expr ; unary_expr ::= MINUS unary_expr | BANG unary_expr | unary_expr_not_minus_bang ; unary_expr_not_minus_bang ::= primary_expr | simple_name | qualified_name ; assignment_expression ::= binary_expr | assignment ; assignment ::= IDENTIFIER ASSIGN assignment_expression | field_access ASSIGN assignment_expression | array_access ASSIGN assignment_expression ; expr ::= assignment_expression; //DONE literal ::= NUMBER:n {: RESULT = new Num(n.longValue()); :} | TEXT:t {: RESULT = new Text(t.toString()); :} | TRUE {: RESULT = new Bool(true);:} | FALSE {: RESULT = new Bool(false);:} | NULL {: RESULT = new Null();:} ;