📄 antlrhelloworld.mht
字号:
<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'>“Hello
World”<o:p></o:p></span></p>
</div>
<![if !mso]></td>
</tr>
</table>
<![endif]></v:textbox>
</v:shape><v:line id=3D"_x0000_s1070" style=3D'position:absolute;left:0;
text-align:left;flip:x;z-index:27' from=3D"159.6pt,34.1pt" to=3D"177.6pt,5=
7.5pt"/><v:line
id=3D"_x0000_s1071" style=3D'position:absolute;left:0;text-align:left;z-in=
dex:28'
from=3D"196.35pt,33.35pt" to=3D"214.35pt,56.75pt"/><![endif]--><![if !vml]=
><span
style=3D'mso-ignore:vglayout'>
<table cellpadding=3D0 cellspacing=3D0 align=3Dleft>
<tr>
<td width=3D182 height=3D15></td>
</tr>
<tr>
<td></td>
<td><img width=3D195 height=3D92 src=3D"Chapter1.files/image003.gif" v:sh=
apes=3D"_x0000_s1064 _x0000_s1065 _x0000_s1066 _x0000_s1067 _x0000_s1068 _x=
0000_s1069 _x0000_s1070 _x0000_s1071"></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>
<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.2<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>“=3D”</span>做为根节୅=
7;变量<span
lang=3DEN-US>str</span>作为左子树, =
780;字符串表达式<span
lang=3DEN-US>“Hello World” </span>作为右é=
76;树。了解了语法树后=
我们开始录入文法源=
5991;件。<span
lang=3DEN-US>ANTLR</span>中文法文件是&#=
25193;展名为<span
lang=3DEN-US>“.g”</span>的文本文件=
;,<span
lang=3DEN-US>“.g”</span>文件就是我=
;们的源文件。这里新&#=
24314;一个叫<span
lang=3DEN-US>“E.g”</span>的文法文ߥ=
4;,在文件中输入如下&=
#25991;法定义:<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'><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:3.0pt;text-indent:24.1pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><b style=3D'mso-bidi-f=
ont-weight:
normal'><span lang=3DEN-US style=3D'font-size:12.0pt;font-family:NSimSun'>g=
rammar
E;<o:p></o:p></span></b></p>
<p class=3DMsoNormal style=3D'margin-bottom:3.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun;background:aqua;mso-highlight=
:aqua'>options{
output=3DAST;}</span><span lang=3DEN-US style=3D'font-size:12.0pt;font-fami=
ly:NSimSun'><o:p></o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:3.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun'>program : statement + ;<o:p>=
</o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:3.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun'>statement: (expression | VAR=
'=3D' expression)
';' ;<o:p></o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:3.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun'>expression : (multExpr (('+'=
|'-'
) multExpr)*) | STRING;<o:p></o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:3.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun'>multExpr : atom ('*' atom)*;=
<o:p></o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:3.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun'>atom : INT | '(' expression =
')';<o:p></o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:3.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun;background:aqua;mso-highlight=
:aqua'>VAR
: ('a'..'z' |'A'..'Z' )+ ;<o:p></o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:3.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun;background:aqua;mso-highlight=
:aqua'>INT
: '0'..'9' + ;<o:p></o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:3.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun;background:aqua;mso-highlight=
:aqua'>STRING
: '"' (('A'..'Z' | 'a'..'z' | ' ') +) '"' ;<o:p></o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:3.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun;background:aqua;mso-highlight=
:aqua'>WS
: (' ' |'\t' |'\n' |'\r' )+ {skip();} ;</span><span lang=3DEN-US
style=3D'font-size:12.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'><span style=3D'mso-bid=
i-font-size:
10.5pt;font-family:NSimSun'>文件的第一行=
;<span
lang=3DEN-US>grammar E</span>的<span lang=3DEN-US>E</span>为=
5991;法的名称它与文件ࡧ=
7;一致。第二行是文法&=
#30340;设置部分<span
lang=3DEN-US>options{ output=3DAST;}</span>,<span lang=3DEN-US>outpu=
t=3DAST</span>表示让语法分析Ý=
20;返回包含语法树的信=
息。第三行开始是文=
7861;定义部分,文法是ஷ=
2;<span
lang=3DEN-US>EBNF</span></span><a style=3D'mso-footnote-id:ftn3' href=3D"#_=
ftn3"
name=3D"_ftnref3" title=3D""><span lang=3DEN-US style=3D'font-size:6.5pt;fo=
nt-family:
NSimSun'>1</span></a><span style=3D'mso-bidi-font-size:10.5pt;font-family:N=
SimSun'>推导式来描述的(=
6377;关<span
lang=3DEN-US>EBNF</span>会在后面章节=
0013;讲解),文法定义ߑ=
3;分两大部分以小写字&=
#27597;开头的语法描述和=
840;大写的词法描述。其=
;中每一行都是一个规&#=
21017;(<span
lang=3DEN-US>rule</span>)或叫做推导=
4335;、产生式,每个规࠵=
7;的左边是文法中的一&=
#20010;名字,代表文法中=
340;一个抽象概念。中间=
;用一个<span
lang=3DEN-US>“:”</span>表示推导关=
系,右边是该名字推=
3548;出的文法形式。下༣=
4;逐行介绍文法的规则&=
#23450;义:<span
lang=3DEN-US><o:p></o:p></span></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun'>statement : (expression | VA=
R '=3D' expression)
';'<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'>statement</span><sp=
an
style=3D'mso-bidi-font-size:10.5pt;font-family:NSimSun'>代表=
4920;达式语句,前面说ߚ=
2;语句有两种,在推导&=
#24335;中以<span
lang=3DEN-US>“|”</span>分隔代表并=
列可选的关系。表达=
4335;本身是合法的语句ᦁ=
2;表达式也可以出现在&=
#36171;值表达式中组成赋=
540;语句,两种语句都以=
;<span
lang=3DEN-US>“;”</span>字符结束。=
<span
lang=3DEN-US><o:p></o:p></span></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun;color:red;background:yellow;
mso-highlight:yellow'>expression</span><span lang=3DEN-US style=3D'mso-bidi=
-font-size:
10.5pt;font-family:NSimSun;color:red;background:yellow;mso-highlight:yellow=
'>: </span><span
lang=3DEN-US style=3D'font-size:12.0pt;font-family:NSimSun;color:red;backgr=
ound:
yellow;mso-highlight:yellow'>(multExpr (('+' |'-' ) multExpr)*) | STRING <s=
pan
style=3D'mso-spacerun:yes'> </span></span><span style=3D'font-size:12.=
0pt;
font-family:NSimSun;color:fuchsia;background:yellow;mso-highlight:yellow'>&=
#65288;有两个<span
lang=3DEN-US>|</span>,不明确)</span><span
lang=3DEN-US style=3D'font-size:12.0pt;font-family:NSimSun;color:red;backgr=
ound:
yellow;mso-highlight:yellow'><o:p></o:p></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun;color:red;background:yellow;
mso-highlight:yellow'>expression</span><span style=3D'mso-bidi-font-size:10=
.5pt;
font-family:NSimSun;color:red;background:yellow;mso-highlight:yellow'>É=
95;表表达式,<span
lang=3DEN-US>“|”</span>的左边是算=
术表达式的形式,<span
lang=3DEN-US>“|”</span>的右边是字=
符串表达式。</span><span
style=3D'mso-bidi-font-size:10.5pt;font-family:NSimSun'>我们=
6890;过规则的推导顺序ࡤ=
7;以看出,规则按操作&=
#31526;的优先级首先推导=
248;先级最低的运算<span
lang=3DEN-US>“+”</span>,<span lang=3DEN-US>“-̶=
1;</span>运算。<span
lang=3DEN-US><o:p></o:p></span></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun'>multExpr : atom ('*' atom)*;=
<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 style=3D'mso-bid=
i-font-size:
10.5pt;font-family:NSimSun'>然后是<span lang=3DEN-US>'*'<=
/span>的运算。表达式中Ė=
09;有除法运算。<span
lang=3DEN-US><o:p></o:p></span></span></p>
<p class=3DMsoNormal style=3D'margin-bottom:6.0pt;text-indent:24.0pt;mso-ch=
ar-indent-count:
2.0;line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3DEN-US
style=3D'font-size:12.0pt;font-family:NSimSun'>atom : '(' expression ')' | =
INT;<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 style=3D'mso-bid=
i-font-size:
10.5pt;font-family:NSimSun'>最后是<span lang=3DEN-US>R=
20;</span>()<span
lang=3DEN-US>”</span>运算,括号中=
又可以是一个表达式=
5292;这样也就实现了括ࡥ=
5;的嵌套关系。以<span
lang=3DEN-US>“|” </span>分隔与括号=
;并列的可以出现参与&#=
36816;算的整型数。<span
lang=3DEN-US><o:p></o:p></span></span></p>
<p class=3DMsoNormal style=3D'text-indent:24.0pt;mso-char-indent-count:2.0;
line-height:20.0pt;mso-line-height-rule:exactly'><span lang=3D
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -