-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathc0-grammar.txt
176 lines (137 loc) · 4.73 KB
/
c0-grammar.txt
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<digit> ::=
'0'|<nonzero-digit>
<nonzero-digit> ::=
'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'
<hexadecimal-digit> ::=
<digit>|'a'|'b'|'c'|'d'|'e'|'f'|'A'|'B'|'C'|'D'|'E'|'F'
<integer-literal> ::=
<decimal-literal>|<hexadecimal-literal>
<decimal-literal> ::=
'0'|<nonzero-digit>{<digit>}
<hexadecimal-literal> ::=
('0x'|'0X')<hexadecimal-digit>{<hexadecimal-digit>}
<nondigit> ::= 'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'|'j'|'k'|'l'|'m'|'n'|'o'|'p'|'q'|'r'|'s'|'t'|'u'|'v'|'w'|'x'|'y'|'z'|'A'|'B'|'C'|'D'|'E'|'F'|'G'|'H'|'I'|'J'|'K'|'L'|'M'|'N'|'O'|'P'|'Q'|'R'|'S'|'T'|'U'|'V'|'W'|'X'|'Y'|'Z'
<identifier> ::=
<nondigit>{<nondigit>|<digit>}
<reserved-word> ::=
'const'
|'void' |'int' |'char' |'double'
|'struct'
|'if' |'else'
|'switch' |'case' |'default'
|'while' |'for' |'do'
|'return' |'break' |'continue'
|'print' |'scan'
<char-liter> ::=
"'" (<c-char>|<escape-seq>) "'"
<string-literal> ::=
'"' {<s-char>|<escape-seq>} '"'
<escape-seq> ::=
'\\' | "\'" | '\"' | '\n' | '\r' | '\t'
| '\x'<hexadecimal-digit><hexadecimal-digit>
<sign> ::=
'+'|'-'
<digit-seq> ::=
<digit>{<digit>}
<floating-literal> ::=
[<digit-seq>]'.'<digit-seq>[<exponent>]
|<digit-seq>'.'[<exponent>]
|<digit-seq><exponent>
<exponent> ::=
('e'|'E')[<sign>]<digit-seq>
<unary-operator> ::= '+' | '-'
<additive-operator> ::= '+' | '-'
<multiplicative-operator> ::= '*' | '/'
<relational-operator> ::= '<' | '<=' | '>' | '>=' | '!=' | '=='
<assignment-operator> ::= '='
<single-line-comment> ::=
'//'{<any-char>}<LF>
<multi-line-comment> ::=
'/*'{<any-char>}'*/'
<type-specifier> ::= <simple-type-specifier>
<simple-type-specifier> ::= 'void'|'int'|'char'|'double'
<const-qualifier> ::= 'const'
<C0-program> ::=
{<variable-declaration>}{<function-definition>}
<variable-declaration> ::=
[<const-qualifier>]<type-specifier><init-declarator-list>';'
<init-declarator-list> ::=
<init-declarator>{','<init-declarator>}
<init-declarator> ::=
<identifier>[<initializer>]
<initializer> ::=
'='<expression>
<function-definition> ::=
<type-specifier><identifier><parameter-clause><compound-statement>
<parameter-clause> ::=
'(' [<parameter-declaration-list>] ')'
<parameter-declaration-list> ::=
<parameter-declaration>{','<parameter-declaration>}
<parameter-declaration> ::=
[<const-qualifier>]<type-specifier><identifier>
<compound-statement> ::=
'{' {<variable-declaration>} <statement-seq> '}'
<statement-seq> ::=
{<statement>}
<statement> ::=
<compound-statement>
|<condition-statement>
|<loop-statement>
|<jump-statement>
|<print-statement>
|<scan-statement>
|<assignment-expression>';'
|<function-call>';'
|';'
<condition> ::=
<expression>[<relational-operator><expression>]
<condition-statement> ::=
'if' '(' <condition> ')' <statement> ['else' <statement>]
|'switch' '(' <expression> ')' '{' {<labeled-statement>} '}'
<labeled-statement> ::=
'case' (<integer-literal>|<char-literal>) ':' <statement>
|'default' ':' <statement>
<loop-statement> ::=
'while' '(' <condition> ')' <statement>
|'do' <statement> 'while' '(' <condition> ')' ';'
|'for' '('<for-init-statement> [<condition>]';' [<for-update-expression>]')' <statement>
<for-init-statement> ::=
[<assignment-expression>{','<assignment-expression>}]';'
<for-update-expression> ::=
(<assignment-expression>|<function-call>){','(<assignment-expression>|<function-call>)}
<jump-statement> ::=
'break' ';'
|'continue' ';'
|<return-statement>
<return-statement> ::= 'return' [<expression>] ';'
<scan-statement> ::=
'scan' '(' <identifier> ')' ';'
<print-statement> ::=
'print' '(' [<printable-list>] ')' ';'
<printable-list> ::=
<printable> {',' <printable>}
<printable> ::=
<expression> | <string-literal>
<assignment-expression> ::=
<identifier><assignment-operator><expression>
<expression> ::=
<additive-expression>
<additive-expression> ::=
<multiplicative-expression>{<additive-operator><multiplicative-expression>}
<multiplicative-expression> ::=
<cast-expression>{<multiplicative-operator><cast-expression>}
<cast-expression> ::=
{'('<type-specifier>')'}<unary-expression>
<unary-expression> ::=
[<unary-operator>]<primary-expression>
<primary-expression> ::=
'('<expression>')'
|<identifier>
|<integer-literal>
|<char-literal>
|<floating-literal>
|<function-call>
<function-call> ::=
<identifier> '(' [<expression-list>] ')'
<expression-list> ::=
<expression>{','<expression>}