grammar Decaf; program: classdecl+; //class identifier{...} classdecl: AccessModifierPublic? 'class' Identifier OpenCurlyBracket (constuctorDecl|fieldDecl|methodDecl)*(MainMethodDecl block)? ClosedCurlyBracket; constuctorDecl: AccessModifierPublic? Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; //Method without //Method and FieldVar methodDecl: AccessModifierPublic? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; fieldDecl: AccessModifierPublic? type Identifier Semicolon; //Parameters parameterList: parameter(Comma parameter)*; parameter: type Identifier; //property, object.a, 3+1, a = 3 expression: subExpression | binaryExpr; //subExpression to dissolve left-recusion subExpression: This | assignableExpr | stmtExpr | OpenRoundBracket subExpression ClosedRoundBracket; assignableExpr: Identifier | instVar; instVar: subReceiver? receivingMethod* Identifier; //.trim().toLength().toLowerCase().count ... methodCall: receiver? receivingMethod* Identifier OpenRoundBracket argumentList ClosedRoundBracket; argumentList: expression? | expression (Comma expression)+; subReceiver: ((This | newDecl | Identifier) Dot); receiver: ((This | instVar | newDecl | Identifier) Dot); receivingMethod: Identifier OpenRoundBracket argumentList ClosedRoundBracket Dot; binaryExpr: calcExpr | nonCalcExpr| value | Not binaryExpr; calcExpr: calcExpr LineOperator dotExpr | dotExpr; dotExpr: dotExpr DotOperator dotSubExpr | dotSubExpr; dotSubExpr: IntValue | Identifier | instVar | methodCall | OpenRoundBracket calcExpr ClosedRoundBracket; nonCalcExpr: subExpression nonCalcOperator expression; nonCalcOperator: LogicalOpertor | ComparisonOperator; //Statement but also expression //a = expr, new Object(), method(param1) stmtExpr: assign | newDecl | methodCall; //Statements //int a, {...}, while(a > 10){...}, if(...){...} else if{...} else{...} statement: returnStmt Semicolon | localVarDecl Semicolon | block | whileStmt | ifElseStmt | stmtExpr Semicolon; returnStmt: Return (expression)?; localVarDecl: type Identifier (Assign expression)?; block: OpenCurlyBracket statement* ClosedCurlyBracket; whileStmt: While OpenRoundBracket expression ClosedRoundBracket statement; ifElseStmt: ifStmt elseStmt?; ifStmt: If OpenRoundBracket expression ClosedRoundBracket statement; elseStmt: Else statement; assign: assignableExpr Assign expression; newDecl: New Identifier OpenRoundBracket argumentList ClosedRoundBracket; type: Int | Boolean | Char | Identifier; value: IntValue | BooleanValue | CharValue | NullValue; //Access modifier AccessModifierPublic : 'public' ; MainMethodDecl : 'public static void main(String[] args)'; //Operators DotOperator : Multipilkation | Division | Modulo; LineOperator : Plus | Minus; ComparisonOperator : Greater | Less | GreaterEqual | LessEqual | Equal | NotEqual; LogicalOpertor : And | Or; Assign : '='; Minus : '-'; Plus : '+'; Multipilkation : '*'; Division : '/'; Modulo : '%'; Greater : '>'; Less : '<'; GreaterEqual : '>='; LessEqual : '<='; Equal : '=='; NotEqual : '!='; Not : '!'; And : '&&'; Or : '||'; //Symbols Dot : '.'; OpenRoundBracket : '('; ClosedRoundBracket : ')'; OpenCurlyBracket : '{'; ClosedCurlyBracket : '}'; Semicolon : ';'; Comma : ','; //Keywords Class : 'class'; This : 'this'; While : 'while'; If : 'if'; Else : 'else'; Return : 'return'; New : 'new'; //Identifier fragment Alpabetic : [a-zA-Z]; fragment Numeric: [0-9]; fragment ValidIdentSymbols : Alpabetic|Numeric|'$'|'_'; Identifier: Alpabetic ValidIdentSymbols*; //Types Void : 'void'; Int : 'int'; Boolean : 'bool'; Char : 'char'; //Values IntValue : ('+'|'-')*[0-9]+; CharValue: '\''~[\r\n]?'\''; BooleanValue: 'true'|'false'; NullValue: 'null'; //Whitespace? Right into the trash it gooeesss WS : [ \t\r\n] -> skip;