options { JAVA_UNICODE_ESCAPE = true; } PARSER_BEGIN(Parser) import edu.du.cs.comp3351.p5.visitors.TreeDumper; import edu.du.cs.comp3351.p5.visitors.TreeFormatter; public class Parser { public static void main(String args[]){ Parser parser = new Parser(System.in); try { CompilationUnit root = parser.CompilationUnit(); new TreeFormatter().visit(root); new TreeDumper(System.out).visit(root); } catch (ParseException e) { System.err.println("Parse error."); } catch (TokenMgrError tme) { System.err.println("Parse error."); } } } PARSER_END(Parser) /* WHITE SPACE */ SKIP : { " " | "\t" | "\n" | "\r" | "\f" } /* COMMENTS */ MORE : { "//" : IN_SINGLE_LINE_COMMENT | <"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT | "/*" : IN_MULTI_LINE_COMMENT } SPECIAL_TOKEN : { : DEFAULT } SPECIAL_TOKEN : { : DEFAULT } SPECIAL_TOKEN : { : DEFAULT } MORE : { < ~[] > } /* RESERVED WORDS AND LITERALS */ TOKEN : { < ABSTRACT: "abstract" > | < BOOLEAN: "boolean" > | < BREAK: "break" > | < CASE: "case" > | < CATCH: "catch" > | < CLASS: "class" > | < CONST: "const" > | < CONTINUE: "continue" > | < _DEFAULT: "default" > | < ELSE: "else" > | < EXTENDS: "extends" > | < FALSE: "false" > | < FLOAT: "float" > | < IF: "if" > | < INSTANCEOF: "instanceof" > | < INT: "int" > | < NEW: "new" > | < NULL: "null" > | < RETURN: "return" > | < STATIC: "static" > | < SUPER: "super" > | < SWITCH: "switch" > | < THIS: "this" > | < THROW: "throw" > | < TRUE: "true" > | < TRY: "try" > | < VOID: "void" > | < WHILE: "while" > | < PRINT: "print" > | < CB_REFERENCE: "ref" > | < CB_VALUE: "val" > | < CB_RESULT: "result" > | < CB_NEED: "need" > } /* LITERALS */ TOKEN : { < INTEGER_LITERAL: > | < #DECIMAL_LITERAL: ["0"-"9"] (["0"-"9"])* > | < FLOATING_POINT_LITERAL: (["0"-"9"])+ "." (["0"-"9"])* ()? (["f","F"])? | "." (["0"-"9"])+ ()? (["f","F"])? | (["0"-"9"])+ (["f","F"])? | (["0"-"9"])+ ()? ["f","F"] > | < #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > } /* IDENTIFIERS */ TOKEN : { < IDENTIFIER: (|)* > | < #LETTER: [ "\u0024", "\u0041"-"\u005a", "\u005f", "\u0061"-"\u007a", "\u00c0"-"\u00d6", "\u00d8"-"\u00f6", "\u00f8"-"\u00ff", "\u0100"-"\u1fff", "\u3040"-"\u318f", "\u3300"-"\u337f", "\u3400"-"\u3d2d", "\u4e00"-"\u9fff", "\uf900"-"\ufaff" ] > | < #DIGIT: [ "\u0030"-"\u0039", "\u0660"-"\u0669", "\u06f0"-"\u06f9", "\u0966"-"\u096f", "\u09e6"-"\u09ef", "\u0a66"-"\u0a6f", "\u0ae6"-"\u0aef", "\u0b66"-"\u0b6f", "\u0be7"-"\u0bef", "\u0c66"-"\u0c6f", "\u0ce6"-"\u0cef", "\u0d66"-"\u0d6f", "\u0e50"-"\u0e59", "\u0ed0"-"\u0ed9", "\u1040"-"\u1049" ] > } /* SEPARATORS */ TOKEN : { < LPAREN: "(" > | < RPAREN: ")" > | < LBRACE: "{" > | < RBRACE: "}" > | < SEMICOLON: ";" > | < COMMA: "," > | < DOT: "." > } /* OPERATORS */ TOKEN : { < ASSIGN: "=" > | < GT: ">" > | < LT: "<" > | < BANG: "!" > | < TILDE: "~" > | < HOOK: "?" > | < COLON: ":" > | < EQ: "==" > | < LE: "<=" > | < GE: ">=" > | < NE: "!=" > | < SC_OR: "||" > | < SC_AND: "&&" > | < PLUS: "+" > | < MINUS: "-" > | < STAR: "*" > | < SLASH: "/" > | < BIT_AND: "&" > | < BIT_OR: "|" > | < XOR: "^" > | < REM: "%" > | < LSHIFT: "<<" > | < RSIGNEDSHIFT: ">>" > | < RUNSIGNEDSHIFT: ">>>" > } /************************************ * THE LANGUAGE GRAMMAR STARTS HERE * ************************************/ void CompilationUnit() : {} { ClassDeclaration() } void ClassDeclaration() : {} { "class" "{" ( ClassBodyDeclaration() )* "}" } void ClassBodyDeclaration() : {} { MethodDeclaration() } void MethodDeclaration() : {} { "static" ResultType() FormalParameters() ( Block() | ";" ) } void FormalParameters() : {} { "(" [ FormalParameter() ( "," FormalParameter() )* ] ")" } void FormalParameter() : {} { CallingConvention() Type() } void CallingConvention() : {} { "val" | "need" } void Type() : {} { "boolean" | "int" | "float" } void ResultType() : {} { "void" | "boolean" | "int" | "float" } void Expression() : {} { ConditionalOrExpression() } void ConditionalOrExpression() : {} { ConditionalAndExpression() ( "||" ConditionalAndExpression() )* } void ConditionalAndExpression() : {} { InclusiveOrExpression() ( "&&" InclusiveOrExpression() )* } void InclusiveOrExpression() : {} { ExclusiveOrExpression() ( "|" ExclusiveOrExpression() )* } void ExclusiveOrExpression() : {} { AndExpression() ( "^" AndExpression() )* } void AndExpression() : {} { EqualityExpression() ( "&" EqualityExpression() )* } void EqualityExpression() : {} { RelationalExpression() ( ( "==" | "!=" ) RelationalExpression() )* } void RelationalExpression() : {} { ShiftExpression() [ ( "<" | ">" | "<=" | ">=" ) ShiftExpression() ] } void ShiftExpression() : {} { AdditiveExpression() ( ( "<<" | ">>" | ">>>" ) AdditiveExpression() )* } void AdditiveExpression() : {} { MultiplicativeExpression() ( ( "+" | "-" ) MultiplicativeExpression() )* } void MultiplicativeExpression() : {} { UnaryExpression() ( ( "*" | "/" | "%" ) UnaryExpression() )* } void UnaryExpression() : {} { ( "+" | "-" ) UnaryExpression() | UnaryExpressionNotPlusMinus() } void UnaryExpressionNotPlusMinus() : {} { ( "~" | "!" ) UnaryExpression() | PrimaryExpression() } void PrimaryExpression() : {} { Literal() | "(" Expression() ")" | LOOKAHEAD(2) StaticMethodCall() | LOOKAHEAD(2) } void StaticMethodCall() : {} { Arguments() } void Literal() : {} { | | BooleanLiteral() } void BooleanLiteral() : {} { "true" | "false" } void Arguments() : {} { "(" [ ArgumentList() ] ")" } void ArgumentList() : {} { Expression() ( "," Expression() )* } /* * Statement syntax follows. */ void Statement() : {} { Block() | EmptyStatement() | StatementExpression() ";" | SwitchStatement() | IfStatement() | WhileStatement() | BreakStatement() | ContinueStatement() | ReturnStatement() | PrintStatement() } void PrintStatement() : {} { "print" "(" Expression() ")" ";" } void Block() : {} { "{" ( BlockStatement() )* "}" } void BlockStatement() : {} { LOOKAHEAD(Type() ) LocalVariableDeclaration() ";" | Statement() } void LocalVariableDeclaration() : {} { Type() "=" Expression() } void EmptyStatement() : {} { ";" } void StatementExpression() : {} { "=" Expression() } void SwitchStatement() : {} { "switch" "(" Expression() ")" "{" ( SwitchClause() )* "}" } void SwitchClause() : {} { SwitchLabel() ( BlockStatement() )* } void SwitchLabel() : {} { CaseLabel() | "default" ":" } void CaseLabel() : {} { "case" Expression() ":" } void IfStatement() : {} { "if" "(" Expression() ")" Statement() "else" Statement() } void WhileStatement() : {} { "while" "(" Expression() ")" Statement() } void BreakStatement() : {} { "break" ";" } void ContinueStatement() : {} { "continue" ";" } void ReturnStatement() : {} { "return" [ Expression() ] ";" }