




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
LastLectureRevisitedWhatandWhytostudythiscourse?Def.oftranslatorandcompilerStructureofcompiler,front-end,back-end“Pass”Chapter2.LexicalAnalysis
LexicalAnalyzerSyntaxAnalyzerSymbolTableTokenGetnexttokenSourceCode
词法分析器语法分析器符号表记号取下一个记号源程序Whatlexicalanalysisdoes:LexicalanalyzerTokensSourcecode2.1Tokenandattributes2.1.1Token、pattern、lexeme
Lexeme:stringsinsourcecode。Token:atokenrepresentatypeofstringssubjecttosomerules(patterns)。Pattern:determineshowatokenrelatestolexemesletterssequenceslexemetokenpatternAnexampleeg:var
count
:
integer
;
count
=
5
;lexemeletterssequenceslexemetokenpatternToken
Examples
DescriptionofPatterns
var var varfor for forrelation <,<=,=,… <or<=or=or…id sum,count,D5 letterandnumberstringstartingwithaletternum 3.1,10,2.8E12 anynumericalvalueliteral “seg.error” thestringwithin“”
TokensandpatternsToken
Examples
Patterndescriptionrelation <,<=,=,… <or<=or=or
…id sum,count,D5 letterandnumberstringstartingwithaletternoun
Dalian,Sea
namesofthingsToken
Examples
PatterndescriptionToken
Examples
PatterndescriptionChinese
ZeminJiang male/femalewithChinanationalityAmerican
Clinton,Obama male/femalewithUSAnationality 2.1词法记号及属性SomeissuesinthehistoryShouldweomittheblanksornot?
DO8I3.75 DO8I3.75DO8I3,75Shouldwereservethekeywordsornot?
IFTHENTHENTHEN=ELSE;ELSE…2.1.2Attributesoftokens
position:=initial+rate*60
id,pointertothepositioniteminthesymbletable assign_op,
id,pointertotheinitialiteminthesymbletable add_op,+
id,pointertotherateiteminthesymbletable
mul_op,* num,60
Exerciseofsymboltable:Writeapieceofcodetocalculatethenumberofwords,thenumberofdistinctwords.
eg:IloveDalianandIloveDLUTWordnumber:7Distinctwordnumber:52.1.3LexicalErrorsIt’shardtofindthefollowingerror,sincelexicalanalysisislocallyinsightful.
fi(a==f(x))
…Anexception: 123.2.1.3LexicalErrorsRecovery“emergencymodel”
“recoverytry”deleteanadditionalcharacter
insertamissingcharacter
replaceaninvalidcharacterwithanvalidone
swaptwoadjacentcharacters2.2DescriptionandIdentificationofTokens2.2.1StringandLanguageAlphabettable:setofsymbol,eg.={0,1}String:sequenceofsymbols,eg.0110,Language:setofstrings {,0,00,000,…},{},Sentence:stringinalanguage字母组合串语言集合集合字母表词法单元记号模式emptystring2.2.1StringandLanguageOperatorsoverstringsconcatenation:
xy,s
=s=s
Product(exponent):
s0=,si=si-1s(i>0)
Operatorsoverlanguagesplus:L∪M={s|s
L或s
M}concatenation:LM={st|s
L且t
M}exponent:L0是{
},Li是Li-1L
closure:L=L0∪L1∪L2∪…positiveclosure:L+=L1∪L2∪…e.g.2.2(p14)L:{A,B,…,Z,a,b,…,z},D:{0,1,…,9}L∪D,LD,L6,L*,L(L∪D)*,D+
2.2.2RegularExpression(RE)Regularexpression:Accordingtoasetofrules,formalregulationsshowhowtoformlanguagesfromscratch.正规式是用于说明词法单元如何到对应到词法记号的模式。与非形式化的描述相比,正规式更具形式化,更加精确。
RE
languagedefined
description
{}
a {a} a (r)|(s)L(r)∪L(s) randsareFEs (r)(s)
L(r)L(s) randsareFEs
(r)*
(L(r))* risaFE
(r) L(r) risaFEPrioritiesofoperators:*>concatenation
>|
SomeexamplesofRE={a,b}a|b
{a,b}(a|b)(a|b)
{aa,ab,ba,bb}aa|ab|ba|bb
{aa,ab,ba,bb}a* allthestrings
consistingof‘a’(a|b)* allthestringsconsistingof‘a’and‘b’2.2.3NamesofREForbrevity,wecannamesomeREs。d1
r1d2
r2...dn
rndimustbedifferentfromeachotherEveryrimustbeaFEover{d1,d2,…,di-1}这样就保证了,每个名字对应的正规式中使用的各种符号已经在前面定义了,从而可以避免递归定义的情况。ExamplesofRE
IDsinPascal letter
A|B|…|Z|a|b|…
|z
digit
0
|1|…|9 id
letter(letter|digit)*
UnsignednumbersinPascal1946,11.28,63.6E8,1.99E6
digit
0
|1|…|9
digits
digit
digit*
optional_fraction
.digits|
optional_exponent
(E(+||)digits)|
numdigitsoptional_fractionoptional_exponentSimplifiednotations: num
digit+(.digit+)?(E(+|)?digit+)?
Simplifiednotations:r+=rr*r?=r|[a-z]=a|b|c|…|z whilewhile dodo relop<|<=|=|<>|>|>= idletter(letter|
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年计算机二级考试重点知识试题及答案
- 2024年精算师高级考试复习与考前准备试题及答案
- 深层次的地理文化研究试题及答案
- 投资者行为分析试题及答案
- 全国人教版初中信息技术七年级上册第二单元第6课一、《加工图片素材》教学设计
- 2024年监理工程师面试技巧试题及答案
- 孩子心理疏导试题及答案
- 2025年第一季度可燃冰开采EPC总承包合同甲烷泄漏预案
- 投资咨询与管理的最佳实践试题及答案
- 安全用电教育主题班会
- GB/T 26480-2011阀门的检验和试验
- 案例:收球器盲板伤人事故
- 《员工思想培训》课件
- 网络主题 大锁孙天宇小品《时间都去哪儿了》台词
- 精神科症状学演示课件
- 文学类文本聂志红《在那桃花盛开的地方》阅读练习与答案
- DB13T 5080-2019 SBS改性沥青生产过程动态质量监控规范
- 义务教育物理课程标准(2022年版word版)
- 《CSS样式表的使用》教学设计
- 外环长安大道、东方大道段天然气管道工程管道试压吹扫方案资料(共13页)
- 中国花鸟画简史-共60页PPT课件
评论
0/150
提交评论