-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar
149 lines (105 loc) · 5.43 KB
/
grammar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// A grammar for j--, suitable for parsing by recursive descent
// Copyright 2013 Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
compilationUnit ::= [PACKAGE qualifiedIdentifier SEMI]
{IMPORT qualifiedIdentifier SEMI}
{typeDeclaration} EOF
qualifiedIdentifier ::= IDENTIFIER {DOT IDENTIFIER}
typeDeclaration ::= modifiers (classDeclaration | interfaceBody)
modifiers ::= {PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT}
classDeclaration ::= CLASS IDENTIFIER
[EXTENDS qualifiedIdentifier]
[IMPLEMENTS qualifiedIdentifier {COMMA qualifiedIdentifier}]
classBody
interfaceDeclaration ::= INTERFACE IDENTIFIER
[EXTENDS qualifiedIdentifier {COMMA qualifiedIdentifier}]
interfaceBody
classBody ::= LCURLY {({modifiers memberDecl} | [STATIC] block)} RCURLY
interfaceBody ::= LCURLY {(VOID | type) IDENTIFIER formalParameters SEMI} RCURLY
memberDecl ::= IDENTIFIER // constructor
formalParameters
[THROWS qualifiedIdentifier {COMMA qualifiedIdentifier}]
block
| (VOID | type) IDENTIFIER // method
formalParameters
[THROWS qualifiedIdentifier {COMMA qualifiedIdentifier}]
(block | SEMI)
| type variableDeclarators SEMI // field
block ::= LCURLY {blockStatement} RCURLY
blockStatement ::= localVariableDeclarationStatement
| statement
statement ::= block
| IF parExpression statement [ELSE statement]
| WHILE parExpression statement
| FOR forExpression | forEachExpression statement
| RETURN [expression] SEMI
| SEMI
| TRY block {CATCH LPARAN type {BITWISE_OR type} IDENTIFIER RPARAN block} [FINALLY block]
| THROW expression SEMI
| statementExpression SEMI
formalParameters ::= LPAREN
[formalParameter {COMMA formalParameter}]
RPAREN
formalParameter ::= type IDENTIFIER
parExpression ::= LPAREN expression RPAREN
localVariableDeclarationStatement ::= type variableDeclarators SEMI
variableDeclarators ::= variableDeclarator {COMMA variableDeclarator}
variableDeclarator ::= IDENTIFIER [ASSIGN variableInitializer]
variableInitializer ::= arrayInitializer | expression
arrayInitializer ::= LCURLY
[variableInitializer {COMMA variableInitializer}]
RCURLY
arguments ::= LPAREN [expression {COMMA expression}] RPAREN
type ::= referenceType | basicType
basicType ::= BOOLEAN | CHAR | INT | DOUBLE
referenceType ::= basicType LBRACK RBRACK {LBRACK RBRACK}
| qualifiedIdentifier {LBRACK RBRACK}
conditionalExpression ::= expression COLON expression QUESTION_MARK expression
forInit ::= statementExpression {COMMA statementExpression} | [final] type variableDeclarators
forUpdate ::= statementExpression {COMMA statementExpression}
forExpression ::= LPAREN [forInit] SEMI expression SEMI [forUpdate] RPAREN
forEachExpression ::= LPAREN type IDENTIFIER COLON IDENTIFIER RPAREN
statementExpression ::= expression // but must have side-effect, eg i++
expression ::= assignmentExpression | conditionalExpression
assignmentExpression ::= conditionalAndExpression // must be a valid lhs
[(ASSIGN | PLUS_ASSIGN) assignmentExpression]
conditionalAndExpression ::= equalityExpression // level 10
{LAND equalityExpression}
equalityExpression ::= relationalExpression // level 6
{EQUAL relationalExpression}
relationalExpression ::= additiveExpression // level 5
[(GT | LE) additiveExpression
| INSTANCEOF referenceType]
additiveExpression ::= multiplicativeExpression // level 3
{(PLUS | MINUS) multiplicativeExpression}
multiplicativeExpression ::= unaryExpression // level 2
{(STAR|DIVIDE|REMAINDER|LSHIFT|RSHIFT|RSHIFT_ZERO|BITWISE_AND|BITWISE_OR|BITWISE_XOR) unaryExpression}
unaryExpression ::= INC unaryExpression // level 1
| MINUS unaryExpression
| BITWISE_NOT unaryExpression
| simpleUnaryExpression
simpleUnaryExpression ::= LNOT unaryExpression
| LPAREN basicType RPAREN unaryExpression //casted
| LPAREN // casted
referenceType
RPAREN simpleUnaryExpression
| postfixExpression
postfixExpression ::= primary {selector} {DEC}
selector ::= DOT qualifiedIdentifier [arguments]
| LBRACK expression RBRACK
primary ::= parExpression
| THIS [arguments]
| SUPER ( arguments
| DOT IDENTIFIER [arguments]
)
| literal
| NEW creator
| qualifiedIdentifier [arguments]
creator ::= (basicType | qualifiedIdentifier)
( arguments
| LBRACK RBRACK {LBRACK RBRACK} [arrayInitializer]
| newArrayDeclarator
)
newArrayDeclarator ::= LBRACK expression RBRACK
{LBRACK expression RBRACK} {LBRACK RBRACK}
literal ::= INT_LITERAL | CHAR_LITERAL | STRING_LITERAL | DOUBLE_LITERAL
| TRUE | FALSE | NULL