-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleC.g4
159 lines (104 loc) · 3.56 KB
/
simpleC.g4
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
grammar simpleC;
prog :(include)* (initialBlock|arrayInitBlock|structInitBlock|mStructDef|mFunction)*;
//prog : (forBlock)*;
//-------------语法规则----------------------------------------------
include : '#include' '<' mLIB '>';
//结构体
mStructDef : mStruct '{' (structParam)+ '}'';';
//结构体中参数
structParam : (mType|mStruct) (mID|mArray) (',' (mID|mArray))* ';';
//函数
mFunction : (mType|mVoid|mStruct) mID '(' params ')' '{' funcBody '}';
//函数参数
params : param (','param)* |;
param : mType mID;
//函数体
funcBody : body returnBlock;
//语句块/函数快
body : (block | func';')*;
//语句块
block : initialBlock | arrayInitBlock | structInitBlock | assignBlock | ifBlocks | whileBlock | forBlock | returnBlock;
//初始化语句
initialBlock : (mType) mID ('=' expr)? (',' mID ('=' expr)?)* ';';
arrayInitBlock : mType mID '[' mINT ']'';';
structInitBlock : mStruct (mID|mArray)';';
//赋值语句
assignBlock : ((arrayItem|mID|structMember) '=')+ expr ';';
//if 语句
ifBlocks : ifBlock (elifBlock)* (elseBlock)?;
ifBlock : 'if' '('condition')' '{' body '}';
elifBlock : 'else' 'if' '(' condition ')' '{' body '}';
elseBlock : 'else' '{' body '}';
condition : expr;
//while 语句
whileBlock : 'while' '(' condition ')' '{' body '}';
//for 语句
forBlock : 'for' '(' for1Block ';' condition ';' for3Block ')' ('{' body '}'|';');
for1Block : mID '=' expr (',' for1Block)?|;
for3Block : mID '=' expr (',' for3Block)?|;
//return 语句
returnBlock : 'return' (mINT|mID)? ';';
expr
: '(' expr ')' #parens
| op='!' expr #Neg
| expr op=('*' | '/' | '%') expr #MulDiv
| expr op=('+' | '-') expr #AddSub
| expr op=('==' | '!=' | '<' | '<=' | '>' | '>=') expr #Judge
| expr '&&' expr # AND
| expr '||' expr # OR
| arrayItem #arrayitem
| structMember #structmember
| (op='-')? mINT #int
| (op='-')? mDOUBLE #double
| mCHAR #char
| mSTRING #string
| mID #identifier
| func #function
;
mType : 'int'| 'double'| 'char'| 'string';
mArray : mID '[' mINT ']';
mVoid : 'void';
mStruct : 'struct' mID;
structMember: (mID | arrayItem)'.'(mID | arrayItem);
arrayItem : mID '[' expr ']';
//函数
func : (strlenFunc | atoiFunc | printfFunc | scanfFunc | getsFunc | selfDefinedFunc);
//strlen
strlenFunc : 'strlen' '(' mID ')';
//atoi
atoiFunc : 'atoi' '(' mID ')' ;
//printf
printfFunc
: 'printf' '(' (mSTRING | mID) (','expr)* ')';
//scanf
scanfFunc : 'scanf' '(' mSTRING (','('&')?(mID|arrayItem|structMember))* ')';
//gets
getsFunc : 'gets' '(' mID ')';
//Selfdefined
selfDefinedFunc : mID '('((argument|mID)(','(argument|mID))*)? ')';
argument : mINT | mDOUBLE | mCHAR | mSTRING;
//mID
mID : ID;
//mINT
mINT : INT;
//mDOUBLE
mDOUBLE : DOUBLE;
//mCHAR
mCHAR : CHAR;
//mSTRING
mSTRING : STRING;
//mLIB
mLIB : LIB;
//-------------词法规则----------------------------------------------
ID : [a-zA-Z_][0-9A-Za-z_]*;
INT : [0-9]+;
DOUBLE : [0-9]+'.'[0-9]+;
CHAR : '\''.'\'';
STRING : '"'.*?'"';
LIB : [a-zA-Z]+'.h'?;
Conjunction : '&&' | '||';
Operator : '!' | '+' | '-' | '*' | '/' | '==' | '!=' | '<' | '<=' | '>' | '>=';
//UnaryOperator : '&' | '*' | '+' | '-' | '~' | '!';
LineComment: '//'.*?'\r'?'\n' -> skip;
BlockComment: '/*'.*?'*/' -> skip;
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines