編譯原理課件lesson_第1頁
編譯原理課件lesson_第2頁
編譯原理課件lesson_第3頁
編譯原理課件lesson_第4頁
編譯原理課件lesson_第5頁
已閱讀5頁,還剩21頁未讀, 繼續(xù)免費閱讀

付費下載

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論