options { NODE_CLASS="MyNode"; MULTI=true; NODE_PREFIX=""; TRACK_TOKENS=true; VISITOR=true; VISITOR_RETURN_TYPE="void"; VISITOR_EXCEPTION="InvalidProgramException"; } PARSER_BEGIN(MyParser) public class MyParser { public static void main(String args[]) { System.out.println("Reading from standard input..."); MyParser p = new MyParser(System.in); try { Program n = (Program)p.Program(); // n.dump(""); System.out.println("Parsing complete"); Visitor v = new Visitor(); v.visit(n, null); System.out.println("Program is valid"); } catch (InvalidProgramException e) { System.out.println("Semantic Error:"); System.out.println(e.getMessage()); e.printStackTrace(); } catch (Exception e) { System.out.println("Parse Error:"); System.out.println(e.getMessage()); e.printStackTrace(); } } } PARSER_END(MyParser) SKIP : { " " | "\t" | "\n" | "\r" | <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")> // Comments } MyNode Program() : {} { "program" (ConstDecl() | VarDecl() | ClassDecl () | EnumDecl() | InterfaceDecl())* "{" (MethodDecl())* "}" { return jjtThis; } } void ConstDecl() : {} { "const" Type() Ident() "=" ( | | ) ("," Ident() "=" ( | | ))* ";" } void EnumDecl() : {} { "enum" Ident() "{" Ident() ["=" ] ("," Ident() ["=" ])* "}" } void VarDecl() : {} { Type() Ident() ["[" "]"] ("," Ident() ["[" "]"])* ";" } // void VarDecl() : {Type type; List vars = new ArrayList();} { // type=Type() Ident(vars) ["[" "]"] ("," Ident(vars) ["[" "]"])* ";" // { // jjtThis.type = type.getFirstToken().image; // jjtThis.names = vars; // } // } void ClassDecl() : {} { "class" Ident() ["extends" Type()] ["implements" Type() ("," Type())*] "{" (VarDecl())* ["{" (MethodDecl())* "}"] "}" } void InterfaceDecl() : {} { "interface" Ident() "{" (InterfaceMethodDecl())* "}" } void InterfaceMethodDecl() : {} { (Type() | "void") Ident() "(" [ParamDecl()] ")" ";" } void MethodDecl() : {} { (Type() | "void") Ident() "(" [ParamDecl()] ")" (VarDecl())* "{" (Statement())* "}" } void ParamDecl() : {} { Type() Ident() ["[" "]"] ("," Type() Ident() ["[" "]"])* } void Statement() : {} { DesignatorStatement() ";" | "if" "(" Condition() ")" Statement() [LOOKAHEAD(1) "else" Statement()] | "for" "(" [DesignatorStatement()] ";" [Condition()] ";" [DesignatorStatement()] ")" Statement() | "break" ";" | "continue" ";" | "return" [Expr()] ";" | "read" "(" Designator() ")" ";" | "print" "(" Expr() ["," ] ")" ";" | "{" (Statement())* "}" } void DesignatorStatement() : {} { Designator() ("=" Expr() | "(" [Params()] ")" | "++" | "--") } void Condition() : {} { CompareAnd() ("||" CompareAnd())* } void CompareAnd() : {} { Comparison() ("&&" Comparison())* } void Comparison() : {} { Expr() [ Expr()] } void Params() : {} { Expr() ("," Expr())* } void Expr() : {} { ["-"] Term() (("+"|"-") Term())* } void Term() : {} { Factor() (("*"|"/"|"%") Factor())* } void Factor() : {} { Designator() ["(" [Params()] ")"] | | | | "new" Type() ["[" Expr() "]"] | "(" Expr() ")" } // void Ident(List l) : {Token t;} { // t= { // String s=t.image;jjtThis.name=s; // if(l != null) l.add(s); // } // } void Ident() : {Token t;} { t= { jjtThis.name=t.image; } } void Type() : {} { Ident() } void Designator() : {} { Ident() ("." Ident() | ("[" Expr() "]"))* } TOKEN : { | | | | "|">="|"<"|"<="> }