|
1 | | -Program ::= MainClass { ClassDeclaration } ; |
2 | | - |
3 | | -MainClass ::= "class" Identifier "{" |
4 | | - "public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{" |
5 | | - Statement |
6 | | - "}" |
7 | | - "}" ; |
8 | | - |
9 | | -ClassDeclaration ::= "class" Identifier [ "extends" Identifier ] "{" |
10 | | - { VarDeclaration } |
11 | | - { MethodDeclaration } |
12 | | - "}" ; |
13 | | - |
14 | | -VarDeclaration ::= Type Identifier ";" ; |
15 | | - |
16 | | -MethodDeclaration ::= "public" Type Identifier "(" [ Type Identifier { "," Type Identifier } ] ")" "{" |
17 | | - { VarDeclaration } |
18 | | - { Statement } |
19 | | - "return" Expression ";" |
20 | | - "}" ; |
21 | | - |
22 | | -Type ::= ( "int" "[" "]" ) |
23 | | - | "boolean" |
24 | | - | "int" |
25 | | - | Identifier |
26 | | - ; |
27 | | - |
28 | | -Statement ::= ( "{" { Statement } "}" ) |
29 | | - | ( "if" "(" Expression ")" Statement "else" Statement ) |
30 | | - | ( "while" "(" Expression ")" Statement ) |
31 | | - | ( "System.out.println" "(" Expression ")" ";" ) |
32 | | - | ( Identifier "=" Expression ";" ) |
33 | | - | ( Identifier "[" Expression "]" "=" Expression ";" ) |
34 | | - ; |
35 | | - |
36 | | -Expression ::= AndExpression ; |
37 | | -AndExpression ::= LessThanExpression { "&&" LessThanExpression } ; |
38 | | -LessThanExpression ::= AdditiveExpression [ "<" AdditiveExpression ] ; |
39 | | -AdditiveExpression ::= TimesExpression { [ "+" | "-" ] TimesExpression } ; |
40 | | -TimesExpression ::= PrefixExpression { "*" PrefixExpression } ; |
41 | | -PrefixExpression ::= NotExpression | PostfixExpression; |
42 | | -NotExpression ::= "!" { "!" } PostfixExpression ; |
43 | | -PostfixExpression ::= PrimaryExpression { ( "[" Expression "]" ) |
44 | | - | ( "." Identifier "(" [ Expression { "," Expression } ] ")" ) |
45 | | - | ( "." "length" ) } ; |
46 | | -PrimaryExpression ::= Integer |
47 | | - | "true" |
48 | | - | "false" |
49 | | - | "this" |
50 | | - | ( "(" Expression ")" ) |
51 | | - | ( "new" "int" "[" Expression "]" ) |
52 | | - | ( "new" Identifier "(" ")" ) |
53 | | - | Identifier |
54 | | - ; |
55 | | - |
56 | | -Identifier ::= ? /([A-Za-z_][A-Za-z0-9-_]*)/ ? ; |
57 | | -Integer ::= ? /(-?[1-9][0-9]*)/ ? ; |
| 1 | +(* HPG Grammar for Mini Java *) |
| 2 | +(* https://github.com/christophevg/human-parser-generator/wiki/Tutorial-MiniJava *) |
| 3 | + |
| 4 | +program = main-class { class-declaration } ; |
| 5 | + |
| 6 | +main-class = "class" class-name @ identifier "{" |
| 7 | + "public" "static" "void" "main" "(" "String" "[" "]" args @ identifier ")" "{" |
| 8 | + statement |
| 9 | + "}" |
| 10 | + "}" ; |
| 11 | + |
| 12 | +class-declaration = "class" class-name @ identifier [ "extends" extends @ identifier ] "{" |
| 13 | + { var-declaration } |
| 14 | + { method-declaration } |
| 15 | + "}" ; |
| 16 | + |
| 17 | +var-declaration = type variable @ identifier ";" ; |
| 18 | + |
| 19 | +method-declaration = "public" return-type @ type method-name @ identifier |
| 20 | + "(" [ type identifier { "," type identifier } ] ")" "{" |
| 21 | + { var-declaration } |
| 22 | + { statement } |
| 23 | + "return" returns @ expression ";" |
| 24 | + "}" ; |
| 25 | + |
| 26 | +type = primitive-type |
| 27 | + | list-type |
| 28 | + | complex-type |
| 29 | + ; |
| 30 | + |
| 31 | +primitive-type = integer-type |
| 32 | + | boolean-type |
| 33 | + ; |
| 34 | + |
| 35 | +integer-type = "int" ; |
| 36 | +boolean-type = "boolean" ; |
| 37 | + |
| 38 | +list-type = integer-type "[" "]"; |
| 39 | + |
| 40 | +complex-type = type @ identifier ; |
| 41 | + |
| 42 | +statement = block-statement |
| 43 | + | if-statement |
| 44 | + | while-statement |
| 45 | + | print-statement |
| 46 | + | assignment-statement |
| 47 | + ; |
| 48 | + |
| 49 | +block-statement = "{" { statement } "}" ; |
| 50 | +if-statement = "if" "(" condition @ expression ")" |
| 51 | + true-statement @ statement |
| 52 | + "else" |
| 53 | + false-statement @ statement |
| 54 | + ; |
| 55 | +while-statement = "while" "(" expression ")" statement ; |
| 56 | +print-statement = "System.out.print" [ "ln" ] "(" expression ")" ";" ; |
| 57 | +assignment-statement = variable @ identifier [ "[" index @ expression "]" ] |
| 58 | + "=" expression ";" |
| 59 | + ; |
| 60 | + |
| 61 | +expression = postfix-expression |
| 62 | + | non-postfix-expression |
| 63 | + ; |
| 64 | + |
| 65 | +postfix-expression = indexed-expression |
| 66 | + | method-call-expression |
| 67 | + | length-expression |
| 68 | + ; |
| 69 | + |
| 70 | +indexed-expression = object @ non-postfix-expression "[" expression "]" ; |
| 71 | +method-call-expression = object @ non-postfix-expression "." identifier |
| 72 | + "(" [ argument @ expression { "," argument @ expression } ] ")" ; |
| 73 | +length-expression = object @ non-postfix-expression "." "length" ; |
| 74 | + |
| 75 | +non-postfix-expression = prefix-expression |
| 76 | + | non-prefix-expression |
| 77 | + ; |
| 78 | + |
| 79 | +prefix-expression = not-expression ; |
| 80 | + |
| 81 | +not-expression = "!" prefix-expression ; |
| 82 | + |
| 83 | +non-prefix-expression = times-expression |
| 84 | + | non-times-expression |
| 85 | + ; |
| 86 | + |
| 87 | +times-expression = non-times-expression "*" non-prefix-expression ; |
| 88 | + |
| 89 | +non-times-expression = additive-expression |
| 90 | + | non-additive-expression |
| 91 | + ; |
| 92 | + |
| 93 | +additive-expression = non-additive-expression [ "+" | "-" ] non-times-expression ; |
| 94 | + |
| 95 | +non-additive-expression = less-than-expression |
| 96 | + | non-less-than-expression |
| 97 | + ; |
| 98 | + |
| 99 | +less-than-expression = non-less-than-expression "<" non-additive-expression ; |
| 100 | + |
| 101 | +non-less-than-expression = and-expression |
| 102 | + | non-and-expression |
| 103 | + ; |
| 104 | + |
| 105 | +and-expression = non-and-expression "&&" non-less-than-expression ; |
| 106 | + |
| 107 | + |
| 108 | +non-and-expression = group-expression |
| 109 | + | primary-expression |
| 110 | + ; |
| 111 | + |
| 112 | +group-expression = "(" expression ")" ; |
| 113 | + |
| 114 | +primary-expression = integer-expression |
| 115 | + | boolean-expression |
| 116 | + | this-expression |
| 117 | + | constructor-expression |
| 118 | + | identifier-expression |
| 119 | + ; |
| 120 | + |
| 121 | +integer-expression = value @ integer ; |
| 122 | +boolean-expression = value @ boolean ; |
| 123 | +this-expression = "this" ; |
| 124 | +constructor-expression = "new" "int" "[" size @ expression "]" |
| 125 | + | "new" class-name @ identifier "(" ")" |
| 126 | + ; |
| 127 | +identifier-expression = name @ identifier ; |
| 128 | + |
| 129 | +identifier = ? /([A-Za-z_][A-Za-z0-9-_]*)/ ? ; |
| 130 | +integer = ? /(-?[1-9][0-9]*)/ ? ; |
| 131 | +boolean = ? /(true|false)/ ? ; |
0 commit comments