+/EE379K
programming paradigms
git clone https://git.julia.blue/+/EE379K
34c38a9
master
1options { 2 NODE_CLASS="MyNode"; 3 MULTI=true; 4 NODE_PREFIX=""; 5 TRACK_TOKENS=true; 6 VISITOR=true; 7 VISITOR_RETURN_TYPE="void"; 8 VISITOR_EXCEPTION="InvalidProgramException"; 9} 10 11PARSER_BEGIN(MyParser) 12 13public class MyParser { 14 15 public static void main(String args[]) { 16 System.out.println("Reading from standard input..."); 17 MyParser p = new MyParser(System.in); 18 try { 19 Program n = (Program)p.Program(); 20 // n.dump(""); 21 System.out.println("Parsing complete"); 22 Visitor v = new Visitor(); 23 v.visit(n, null); 24 System.out.println("Program is valid"); 25 } catch (InvalidProgramException e) { 26 System.out.println("Semantic Error:"); 27 System.out.println(e.getMessage()); 28 e.printStackTrace(); 29 } catch (Exception e) { 30 System.out.println("Parse Error:"); 31 System.out.println(e.getMessage()); 32 e.printStackTrace(); 33 } 34 } 35 36} 37 38PARSER_END(MyParser) 39 40SKIP : { 41 " " 42 | "\t" | "\n" | "\r" 43 | <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")> // Comments 44} 45 46MyNode Program() : {} { 47 "program" <IDENT> (ConstDecl() | VarDecl() | ClassDecl () | EnumDecl() 48 | InterfaceDecl())* "{" (MethodDecl())* "}" <EOF> 49 { return jjtThis; } 50} 51 52void ConstDecl() : {} { 53 "const" Type() Ident() "=" (<NUMBER> | <CHAR_CONST> | <BOOL_CONST>) 54 ("," Ident() "=" (<NUMBER> | <CHAR_CONST> | <BOOL_CONST>))* ";" 55} 56 57void EnumDecl() : {} { 58 "enum" Ident() "{" Ident() ["=" <NUMBER>] ("," Ident() ["=" <NUMBER>])* "}" 59} 60 61void VarDecl() : {} { Type() Ident() ["[" "]"] ("," Ident() ["[" "]"])* ";" } 62 63// void VarDecl() : {Type type; List<String> vars = new ArrayList();} { 64// type=Type() Ident(vars) ["[" "]"] ("," Ident(vars) ["[" "]"])* ";" 65// { 66// jjtThis.type = type.getFirstToken().image; 67// jjtThis.names = vars; 68// } 69// } 70 71void ClassDecl() : {} { 72 "class" Ident() ["extends" Type()] ["implements" Type() ("," Type())*] 73 "{" (VarDecl())* ["{" (MethodDecl())* "}"] "}" 74} 75 76void InterfaceDecl() : {} { 77 "interface" Ident() "{" (InterfaceMethodDecl())* "}" 78} 79 80void InterfaceMethodDecl() : {} { 81 (Type() | "void") Ident() "(" [ParamDecl()] ")" ";" 82} 83 84void MethodDecl() : {} { 85 (Type() | "void") Ident() "(" [ParamDecl()] ")" 86 (VarDecl())* "{" (Statement())* "}" 87} 88 89void ParamDecl() : {} { 90 Type() Ident() ["[" "]"] ("," Type() Ident() ["[" "]"])* 91} 92 93void Statement() : {} { 94 DesignatorStatement() ";" 95| "if" "(" Condition() ")" Statement() [LOOKAHEAD(1) "else" Statement()] 96| "for" "(" [DesignatorStatement()] ";" [Condition()] ";" 97 [DesignatorStatement()] ")" Statement() 98| "break" ";" 99| "continue" ";" 100| "return" [Expr()] ";" 101| "read" "(" Designator() ")" ";" 102| "print" "(" Expr() ["," <NUMBER>] ")" ";" 103| "{" (Statement())* "}" 104} 105 106void DesignatorStatement() : {} { 107 Designator() ("=" Expr() | "(" [Params()] ")" | "++" | "--") 108} 109 110void Condition() : {} { CompareAnd() ("||" CompareAnd())* } 111 112void CompareAnd() : {} { Comparison() ("&&" Comparison())* } 113 114void Comparison() : {} { Expr() [<REL_OP> Expr()] } 115 116void Params() : {} { Expr() ("," Expr())* } 117 118void Expr() : {} { ["-"] Term() (("+"|"-") Term())* } 119 120void Term() : {} { Factor() (("*"|"/"|"%") Factor())* } 121 122void Factor() : {} { 123 Designator() ["(" [Params()] ")"] 124| <NUMBER> 125| <CHAR_CONST> 126| <BOOL_CONST> 127| "new" Type() ["[" Expr() "]"] 128| "(" Expr() ")" 129} 130 131// void Ident(List<String> l) : {Token t;} { 132// t=<IDENT> { 133// String s=t.image;jjtThis.name=s; 134// if(l != null) l.add(s); 135// } 136// } 137 138void Ident() : {Token t;} { 139 t=<IDENT> { jjtThis.name=t.image; } 140} 141 142void Type() : {} { Ident() } 143 144void Designator() : {} { Ident() ("." Ident() | ("[" Expr() "]"))* } 145 146TOKEN : { 147 <IDENT: ["a"-"z","A"-"Z"] (["a"-"z","A"-"Z","0"-"9","_"])*> 148| <NUMBER: (["0"-"9"])+> 149| <CHAR_CONST: "'" ~[] "'"> 150| <BOOL_CONST: "true"|"false"> 151| <REL_OP: "=="|"!="|">"|">="|"<"|"<="> 152} 153