版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
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.正規(guī)式是用于說明詞法單元如何到對應(yīng)到詞法記號的模式。與非形式化的描述相比,正規(guī)式更具形式化,更加精確。
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(chǎn)’(a|b)* allthestringsconsistingof‘a(chǎn)’and‘b’2.2.3NamesofREForbrevity,wecannamesomeREs。d1
r1d2
r2...dn
rndimustbedifferentfromeachotherEveryrimustbeaFEover{d1,d2,…,di-1}這樣就保證了,每個名字對應(yīng)的正規(guī)式中使用的各種符號已經(jīng)在前面定義了,從而可以避免遞歸定義的情況。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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026廣東廣州市黃埔區(qū)人民政府黃埔街道辦事處政府聘員招聘1人參考考試題庫附答案解析
- 2026青海海南州衛(wèi)生健康系統(tǒng)面向社會招聘80人備考考試題庫附答案解析
- 2026河南鄭州地鐵招聘安檢員參考考試題庫附答案解析
- 2026年河北張家口赤城縣農(nóng)業(yè)農(nóng)村局公開招聘特聘農(nóng)技員4名備考考試試題附答案解析
- 2026山東事業(yè)單位統(tǒng)考威海市榮成市招聘初級綜合類崗位84人參考考試試題附答案解析
- 醫(yī)藥中間體生產(chǎn)車間制度
- 2026廣東湛江市體育學(xué)校(湛江市體育運動學(xué)校)招聘4人(編制)備考考試題庫附答案解析
- 2026湖南長沙開福區(qū)國防科技大學(xué)理學(xué)院社會招聘2人備考考試試題附答案解析
- 2026年普洱市孟連縣民政局公益性崗位招聘(3人)備考考試題庫附答案解析
- 2026年濟寧魚臺縣事業(yè)單位公開招聘初級綜合類崗位人員(9人)參考考試試題附答案解析
- 2025下半年中學(xué)教師資格證英語《學(xué)科知識與教學(xué)能力》真題卷(帶答案)
- 漁獲物船上保鮮技術(shù)規(guī)范(DB3309-T 2004-2024)
- 車輛使用協(xié)議合同2025年
- 東北大學(xué)2015年招生簡章
- 資金管理辦法實施細則模版(2篇)
- IATF16949-質(zhì)量手冊(過程方法無刪減版)
- 河南省安陽市滑縣2024-2025學(xué)年高二數(shù)學(xué)上學(xué)期期末考試試題文
- 客房服務(wù)員:高級客房服務(wù)員考試資料
- GB/T 6974.5-2023起重機術(shù)語第5部分:橋式和門式起重機
- 心臟血管檢查課件
- 運用PDCA循環(huán)管理提高手衛(wèi)生依從性課件
評論
0/150
提交評論