|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
Java的桌面程序开发在java程序员里通常叫swing开发,主要用的swing包里的类开发的,也就是通常说的c/s架构开发
上一篇我们利用ANTLR来形貌了Jerry言语的基础语法,并经由过程ANTLRWorks来实行该语法对样本代码生成的剖析树。但好像上一篇最初所述,如许失掉的剖析树中有太多对后续处置来讲无用的冗余信息。我们必要打消这些冗余信息,失掉笼统语法树(AST)。
本篇将以之前做的语法为基本,经由过程增加树重写划定规矩来将ANTLR默许天生的剖析树简化收拾为笼统语法树。
本文触及的源码和运转时库打包在附件里了,懒得复制粘贴的话就间接下载附件的版本,用ANTLRWorks来检察和编纂语法文件吧~
修正后的语法文件以下:
Jerry.g(ANTLR3.1语法文件,以Java为天生方针言语)
Java代码
1.grammarJerry;
2.
3.options{
4.language=Java;
5.output=AST;
6.ASTLabelType=CommonTree;
7.}
8.
9.tokens{
10.//imaginarytokens
11.VAR_DECL;
12.SIMPLE_TYPE;
13.ARRAY_TYPE;
14.ARRAY_LITERAL;
15.SIMPLE_VAR_ACCESS;
16.ARRAY_VAR_ACCESS;
17.UNARY_MINUS;
18.BLOCK;
19.EXPR_STMT;
20.}
21.
22.//parserrules
23.
24.program:statementListEOF!
25.{
26.System.out.println(
27.null==$statementList.tree?
28."null":
29.$statementList.tree.toStringTree());
30.}
31.;
32.
33.statementList
34.:statement*
35.;
36.
37.statement
38.:expressionStatement
39.|variableDeclaration
40.|blockStatement
41.|ifStatement
42.|whileStatement
43.|breakStatement
44.|readStatement
45.|writeStatement
46.;
47.
48.expressionStatement
49.:expressionSEMICOLON
50.->^(EXPR_STMTexpression)
51.;
52.
53.variableDeclaration
54.:typeSpecifier
55.(Identifier
56.(->^(VAR_DECL^(SIMPLE_TYPEtypeSpecifier)Identifier)
57.|(LBRACKIntegerRBRACK)+
58.->^(VAR_DECL^(ARRAY_TYPEtypeSpecifierInteger+)Identifier)
59.|EQexpression
60.->^(VAR_DECL^(SIMPLE_TYPEtypeSpecifier)Identifierexpression)
61.|(LBRACKIntegerRBRACK)+EQarrayLiteral
62.->^(VAR_DECL^(ARRAY_TYPEtypeSpecifierInteger+)IdentifierarrayLiteral)
63.)
64.)
65.(COMMAid=Identifier
66.(->$variableDeclaration^(VAR_DECL^(SIMPLE_TYPEtypeSpecifier)$id)
67.|(LBRACKdim1+=IntegerRBRACK)+
68.->$variableDeclaration^(VAR_DECL^(ARRAY_TYPEtypeSpecifier$dim1+)$id)
69.|EQexp=expression
70.->$variableDeclaration^(VAR_DECL^(SIMPLE_TYPEtypeSpecifier)$id$exp)
71.|(LBRACKdim2+=IntegerRBRACK)+EQal=arrayLiteral
72.->$variableDeclaration^(VAR_DECL^(ARRAY_TYPEtypeSpecifier$dim2+)$id$al)
73.)
74.{if(null!=$dim1)$dim1.clear();if(null!=$dim2)$dim2.clear();}
75.)*
76.SEMICOLON
77.;
78.
79.typeSpecifier
80.:INT|REAL
81.;
82.
83.arrayLiteral
84.:LBRACE
85.arrayLiteralElement(COMMAarrayLiteralElement)*
86.RBRACE
87.->^(ARRAY_LITERALarrayLiteralElement+)
88.;
89.
90.arrayLiteralElement
91.:expression
92.|arrayLiteral
93.;
94.
95.blockStatement
96.:LBRACEstatementListRBRACE
97.->^(BLOCKstatementList)
98.;
99.
100.ifStatement
101.:IF^LPAREN!expressionRPAREN!statement(ELSE!statement)?
102.;
103.
104.whileStatement
105.:WHILE^LPAREN!expressionRPAREN!statement
106.;
107.
108.breakStatement
109.:BREAKSEMICOLON!
110.;
111.
112.readStatement
113.:READ^variableAccessSEMICOLON!
114.;
115.
116.writeStatement
117.:WRITE^expressionSEMICOLON!
118.;
119.
120.variableAccess
121.:Identifier
122.(->^(SIMPLE_VAR_ACCESSIdentifier)
123.|(LBRACKIntegerRBRACK)+
124.->^(ARRAY_VAR_ACCESSIdentifierInteger+)
125.)
126.;
127.
128.expression
129.:assignmentExpression
130.|logicalOrExpression
131.;
132.
133.assignmentExpression
134.:variableAccessEQ^expression
135.;
136.
137.logicalOrExpression
138.:logicalAndExpression(OROR^logicalAndExpression)*
139.;
140.
141.logicalAndExpression
142.:relationalExpression(ANDAND^relationalExpression)*
143.;
144.
145.relationalExpression
146.:additiveExpression(relationalOperator^additiveExpression)?
147.|BANG^relationalExpression
148.;
149.
150.additiveExpression
151.:multiplicativeExpression(additiveOperator^multiplicativeExpression)*
152.;
153.
154.multiplicativeExpression
155.:primaryExpression(multiplicativeOperator^primaryExpression)*
156.;
157.
158.primaryExpression
159.:variableAccess
160.|Integer
161.|RealNumber
162.|LPAREN!expressionRPAREN!
163.|MINUSprimaryExpression
164.->^(UNARY_MINUSprimaryExpression)
165.;
166.
167.relationalOperator
168.:LT|GT|EQEQ|LE|GE|NE
169.;
170.
171.additiveOperator
172.:PLUS|MINUS
173.;
174.
175.multiplicativeOperator
176.:MUL|DIV
177.;
178.
179.//lexerrules
180.
181.LPAREN:(
182.;
183.
184.RPAREN:)
185.;
186.
187.LBRACK:[
188.;
189.
190.RBRACK:]
191.;
192.
193.LBRACE:{
194.;
195.
196.RBRACE:}
197.;
198.
199.COMMA:,
200.;
201.
202.SEMICOLON
203.:;
204.;
205.
206.PLUS:+
207.;
208.
209.MINUS:-
210.;
211.
212.MUL:*
213.;
214.
215.DIV:/
216.;
217.
218.EQEQ:==
219.;
220.
221.NE:!=
222.;
223.
224.LT:<
225.;
226.
227.LE:<=
228.;
229.
230.GT:>
231.;
232.
233.GE:>=
234.;
235.
236.BANG:!
237.;
238.
239.ANDAND:&&
240.;
241.
242.OROR:||
243.;
244.
245.EQ:=
246.;
247.
248.IF:if
249.;
250.
251.ELSE:else
252.;
253.
254.WHILE:while
255.;
256.
257.BREAK:break
258.;
259.
260.READ:read
261.;
262.
263.WRITE:write
264.;
265.
266.INT:int
267.;
268.
269.REAL:real
270.;
271.
272.Identifier
273.:LetterOrUnderscore(LetterOrUnderscore|Digit)*
274.;
275.
276.Integer:Digit+
277.;
278.
279.RealNumber
280.:Digit+.Digit+
281.;
282.
283.fragment
284.Digit:0..9
285.;
286.
287.fragment
288.LetterOrUnderscore
289.:Letter|_
290.;
291.
292.fragment
293.Letter:(a..z|A..Z)
294.;
295.
296.WS:(| ||
)+{$channel=HIDDEN;}
297.;
298.
299.Comment
300.:/*(options{greedy=false;}:.)**/{$channel=HIDDEN;}
301.;
302.
303.LineComment
304.://~(
|)*?
{$channel=HIDDEN;}
305.;
<p>
为什么外国人还要写那些框架进行代码封装,他们不就是为了别人使用时可以更简单么!如果要达到一个企业级项目的不用框架是很难的。小一些的项目还行,大的光是MVC模式的设计的编码量就够大的了。还有性能方面,单轮windows,这个工具是微软写的,。 |
|