📄 antlrhelloworld.mht
字号:
5292;将字符串赋给一个ࡢ=
4;量。后面我们要开发&=
#19968;个语法分析器来分=
512;这两条语句。在开发=
;之前先简单提一下语&#=
27861;树的概念,在语法Ñ=
98;析中一般用树来表示=
语法结构,表达式的=
5821;法树是以操作符为੬=
1;节点操作数为子节点&=
#30340;树形结构,<span
lang=3DEN-US>23+4*(5+1)</span>的语法树根þ=
54;<span
style=3D'color:red;background:yellow;mso-highlight:yellow'>操作=
符的优先级如下。</span><span
style=3D'color:fuchsia'>(应明确指出=
6159;</span></span><span
style=3D'font-size:9.0pt;font-family:NSimSun;color:fuchsia'>图<span
lang=3DEN-US>1.1</span></span><span style=3D'mso-bidi-font-size:10.5pt;font=
-family:
NSimSun;color:fuchsia'>)</span><span lang=3DEN-US style=3D'font-size=
:9.0pt;
font-family:NSimSun'><o:p></o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:21.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><!--[if gte vml 1]><v:=
oval
id=3D"_x0000_s1029" style=3D'position:absolute;left:0;text-align:left;
margin-left:151.5pt;margin-top:4.05pt;width:27pt;height:23.4pt;z-index:1'/=
><v:shape
id=3D"_x0000_s1030" type=3D"#_x0000_t202" style=3D'position:absolute;left:=
0;
text-align:left;margin-left:162pt;margin-top:7.8pt;width:9pt;height:15.6pt;
text-indent:0;z-index:2' stroked=3D"f">
<v:textbox inset=3D"0,0,0,0">
<![if !mso]>
<table cellpadding=3D0 cellspacing=3D0 width=3D"100%">
<tr>
<td><![endif]>
<div>
<p class=3DMsoNormal><span lang=3DEN-US style=3D'mso-bidi-font-size:10.=
5pt'>+<o:p></o:p></span></p>
</div>
<![if !mso]></td>
</tr>
</table>
<![endif]></v:textbox>
</v:shape><v:oval id=3D"_x0000_s1039" style=3D'position:absolute;left:0;
text-align:left;margin-left:99pt;margin-top:35.25pt;width:27pt;height:23.4=
pt;
z-index:3'/><v:shape id=3D"_x0000_s1040" type=3D"#_x0000_t202" style=3D'po=
sition:absolute;
left:0;text-align:left;margin-left:108pt;margin-top:39pt;width:10.5pt;
height:15.6pt;text-indent:0;z-index:4' stroked=3D"f">
<v:textbox style=3D'mso-next-textbox:#_x0000_s1040' inset=3D"0,0,0,0">
<![if !mso]>
<table cellpadding=3D0 cellspacing=3D0 width=3D"100%">
<tr>
<td><![endif]>
<div>
<p class=3DMsoNormal><span lang=3DEN-US style=3D'mso-bidi-font-size:10.=
5pt'>23<o:p></o:p></span></p>
</div>
<![if !mso]></td>
</tr>
</table>
<![endif]></v:textbox>
</v:shape><v:oval id=3D"_x0000_s1041" style=3D'position:absolute;left:0;
text-align:left;margin-left:196.5pt;margin-top:34.5pt;width:27pt;height:23=
.4pt;
z-index:5'/><v:shape id=3D"_x0000_s1042" type=3D"#_x0000_t202" style=3D'po=
sition:absolute;
left:0;text-align:left;margin-left:207pt;margin-top:39pt;width:9pt;height:=
15.6pt;
text-indent:0;z-index:6' stroked=3D"f">
<v:textbox style=3D'mso-next-textbox:#_x0000_s1042' inset=3D"0,0,0,0">
<![if !mso]>
<table cellpadding=3D0 cellspacing=3D0 width=3D"100%">
<tr>
<td><![endif]>
<div>
<p class=3DMsoNormal><span lang=3DEN-US style=3D'mso-bidi-font-size:10.=
5pt'>*<o:p></o:p></span></p>
</div>
<![if !mso]></td>
</tr>
</table>
<![endif]></v:textbox>
</v:shape><v:oval id=3D"_x0000_s1043" style=3D'position:absolute;left:0;
text-align:left;margin-left:150pt;margin-top:64.95pt;width:27pt;height:23.=
4pt;
z-index:7'/><v:shape id=3D"_x0000_s1044" type=3D"#_x0000_t202" style=3D'po=
sition:absolute;
left:0;text-align:left;margin-left:160.5pt;margin-top:68.7pt;width:9pt;
height:15.6pt;text-indent:0;z-index:8' stroked=3D"f">
<v:textbox style=3D'mso-next-textbox:#_x0000_s1044' inset=3D"0,0,0,0">
<![if !mso]>
<table cellpadding=3D0 cellspacing=3D0 width=3D"100%">
<tr>
<td><![endif]>
<div>
<p class=3DMsoNormal><span lang=3DEN-US style=3D'mso-bidi-font-size:10.=
5pt'>4<o:p></o:p></span></p>
</div>
<![if !mso]></td>
</tr>
</table>
<![endif]></v:textbox>
</v:shape><v:oval id=3D"_x0000_s1045" style=3D'position:absolute;left:0;
text-align:left;margin-left:241.5pt;margin-top:66.45pt;width:27pt;height:2=
3.4pt;
z-index:9'/><v:shape id=3D"_x0000_s1046" type=3D"#_x0000_t202" style=3D'po=
sition:absolute;
left:0;text-align:left;margin-left:252pt;margin-top:70.2pt;width:9pt;heigh=
t:15.6pt;
text-indent:0;z-index:10' stroked=3D"f">
<v:textbox style=3D'mso-next-textbox:#_x0000_s1046' inset=3D"0,0,0,0">
<![if !mso]>
<table cellpadding=3D0 cellspacing=3D0 width=3D"100%">
<tr>
<td><![endif]>
<div>
<p class=3DMsoNormal><span lang=3DEN-US style=3D'mso-bidi-font-size:10.=
5pt'>+<o:p></o:p></span></p>
</div>
<![if !mso]></td>
</tr>
</table>
<![endif]></v:textbox>
</v:shape><v:oval id=3D"_x0000_s1047" style=3D'position:absolute;left:0;
text-align:left;margin-left:196.5pt;margin-top:97.65pt;width:27pt;height:2=
3.4pt;
z-index:11'/><v:shape id=3D"_x0000_s1048" type=3D"#_x0000_t202" style=3D'p=
osition:absolute;
left:0;text-align:left;margin-left:207pt;margin-top:101.4pt;width:9pt;
height:15.6pt;text-indent:0;z-index:12' stroked=3D"f">
<v:textbox style=3D'mso-next-textbox:#_x0000_s1048' inset=3D"0,0,0,0">
<![if !mso]>
<table cellpadding=3D0 cellspacing=3D0 width=3D"100%">
<tr>
<td><![endif]>
<div>
<p class=3DMsoNormal><span lang=3DEN-US style=3D'mso-bidi-font-size:10.=
5pt'>5<o:p></o:p></span></p>
</div>
<![if !mso]></td>
</tr>
</table>
<![endif]></v:textbox>
</v:shape><v:oval id=3D"_x0000_s1049" style=3D'position:absolute;left:0;
text-align:left;margin-left:286.5pt;margin-top:98.4pt;width:27pt;height:23=
.4pt;
z-index:13'/><v:shape id=3D"_x0000_s1050" type=3D"#_x0000_t202" style=3D'p=
osition:absolute;
left:0;text-align:left;margin-left:297pt;margin-top:102.15pt;width:9pt;
height:15.6pt;text-indent:0;z-index:14' stroked=3D"f">
<v:textbox style=3D'mso-next-textbox:#_x0000_s1050' inset=3D"0,0,0,0">
<![if !mso]>
<table cellpadding=3D0 cellspacing=3D0 width=3D"100%">
<tr>
<td><![endif]>
<div>
<p class=3DMsoNormal><span lang=3DEN-US style=3D'mso-bidi-font-size:10.=
5pt'>1<o:p></o:p></span></p>
</div>
<![if !mso]></td>
</tr>
</table>
<![endif]></v:textbox>
</v:shape><v:line id=3D"_x0000_s1051" style=3D'position:absolute;left:0;
text-align:left;flip:x;z-index:15' from=3D"123.75pt,21.9pt" to=3D"153pt,38=
.25pt"/><v:line
id=3D"_x0000_s1052" style=3D'position:absolute;left:0;text-align:left;z-in=
dex:16'
from=3D"174pt,25.2pt" to=3D"198pt,39pt"/><v:line id=3D"_x0000_s1053" style=
=3D'position:absolute;
left:0;text-align:left;flip:x;z-index:17' from=3D"172.5pt,53.1pt" to=3D"19=
9.5pt,68.7pt"/><v:line
id=3D"_x0000_s1054" style=3D'position:absolute;left:0;text-align:left;z-in=
dex:18'
from=3D"221.25pt,52.35pt" to=3D"243.75pt,70.2pt"/><v:line id=3D"_x0000_s10=
55"
style=3D'position:absolute;left:0;text-align:left;flip:x;z-index:19' from=
=3D"215.25pt,83.55pt"
to=3D"242.25pt,99.15pt"/><v:line id=3D"_x0000_s1056" style=3D'position:abs=
olute;
left:0;text-align:left;z-index:20' from=3D"267.75pt,84.3pt" to=3D"294.75pt=
,99.9pt"/><![endif]--><![if !vml]><span
style=3D'mso-ignore:vglayout'>
<table cellpadding=3D0 cellspacing=3D0 align=3Dleft>
<tr>
<td width=3D131 height=3D4></td>
</tr>
<tr>
<td></td>
<td><img width=3D288 height=3D159 src=3D"Chapter1.files/image002.gif" v:s=
hapes=3D"_x0000_s1029 _x0000_s1030 _x0000_s1039 _x0000_s1040 _x0000_s1041 _=
x0000_s1042 _x0000_s1043 _x0000_s1044 _x0000_s1045 _x0000_s1046 _x0000_s104=
7 _x0000_s1048 _x0000_s1049 _x0000_s1050 _x0000_s1051 _x0000_s1052 _x0000_s=
1053 _x0000_s1054 _x0000_s1055 _x0000_s1056"></td>
</tr>
</table>
</span><![endif]><span lang=3DEN-US style=3D'mso-bidi-font-size:10.5pt;font=
-family:
NSimSun'><o:p> </o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:21.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'mso-bidi-font-size:10.5pt;font-family:NSimSun'><o:p> </o:p></=
span></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:21.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'mso-bidi-font-size:10.5pt;font-family:NSimSun'><o:p> </o:p></=
span></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:21.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'mso-bidi-font-size:10.5pt;font-family:NSimSun'><o:p> </o:p></=
span></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:21.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'mso-bidi-font-size:10.5pt;font-family:NSimSun'><o:p> </o:p></=
span></p>
<br style=3D'mso-ignore:vglayout' clear=3DALL>
<p class=3DMsoNormal align=3Dcenter style=3D'margin-bottom:6.0pt;text-align=
:center;
line-height:20.0pt;mso-line-height-rule:exactly'><span style=3D'font-size:9=
.0pt;
font-family:NSimSun'>图<span lang=3DEN-US>1.1<o:p></o:p></span></span=
></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:21.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span style=3D'mso-bid=
i-font-size:
10.5pt;font-family:NSimSun'>算术表达式先=
;计算<span
lang=3DEN-US>5+1</span>,<span lang=3DEN-US>5+1</span>在括=
;号中操作符的优先级&#=
26368;高在语法树中的深ò=
30;最大,然后是<span
lang=3DEN-US>4*(5+1)</span>,最后是<span lang=3DEN-=
US>23+4*(5+1)</span>。可以看出语法=
;树的求值顺序是从下&#=
21521;上的,先计算深度ä=
23;的操作符<span
lang=3DEN-US>5+1</span>结果为<span lang=3DEN-US>6</span>&=
#65292;然后是<span
lang=3DEN-US>4*6</span>结果为<span lang=3DEN-US>24</span>=
,然后是<span
lang=3DEN-US>23+24</span>表达式的结果&#=
20026;<span
lang=3DEN-US>47</span>。下面再看一Ç=
79;赋值表达式的语法树=
结构:<span
lang=3DEN-US><o:p></o:p></span></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:21.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><!--[if gte vml 1]><v:=
oval
id=3D"_x0000_s1064" style=3D'position:absolute;left:0;text-align:left;
margin-left:173.1pt;margin-top:12.25pt;width:27pt;height:23.4pt;z-index:21=
'/><v:shape
id=3D"_x0000_s1065" type=3D"#_x0000_t202" style=3D'position:absolute;left:=
0;
text-align:left;margin-left:183.6pt;margin-top:16pt;width:9pt;height:15.6p=
t;
text-indent:0;z-index:22' stroked=3D"f">
<v:textbox inset=3D"0,0,0,0">
<![if !mso]>
<table cellpadding=3D0 cellspacing=3D0 width=3D"100%">
<tr>
<td><![endif]>
<div>
<p class=3DMsoNormal><span lang=3DEN-US style=3D'mso-bidi-font-size:10.=
5pt'>=3D<o:p></o:p></span></p>
</div>
<![if !mso]></td>
</tr>
</table>
<![endif]></v:textbox>
</v:shape><v:oval id=3D"_x0000_s1066" style=3D'position:absolute;left:0;
text-align:left;margin-left:137.1pt;margin-top:55.8pt;width:27pt;height:23=
.4pt;
z-index:23'/><v:shape id=3D"_x0000_s1067" type=3D"#_x0000_t202" style=3D'p=
osition:absolute;
left:0;text-align:left;margin-left:142.35pt;margin-top:59.55pt;width:18pt;
height:15.6pt;text-indent:0;z-index:24' stroked=3D"f">
<v:textbox inset=3D"0,0,0,0">
<![if !mso]>
<table cellpadding=3D0 cellspacing=3D0 width=3D"100%">
<tr>
<td><![endif]>
<div>
<p class=3DMsoNormal><span lang=3DEN-US style=3D'mso-bidi-font-size:10.=
5pt'>str<o:p></o:p></span></p>
</div>
<![if !mso]></td>
</tr>
</table>
<![endif]></v:textbox>
</v:shape><v:oval id=3D"_x0000_s1068" style=3D'position:absolute;left:0;
text-align:left;margin-left:190.35pt;margin-top:55.8pt;width:91.5pt;height=
:23.1pt;
z-index:25'/><v:shape id=3D"_x0000_s1069" type=3D"#_x0000_t202" style=3D'p=
osition:absolute;
left:0;text-align:left;margin-left:204.6pt;margin-top:60.3pt;width:63pt;
height:11.85pt;text-indent:0;z-index:26' stroked=3D"f">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -