吉大编译课件scanning-5_第1页
吉大编译课件scanning-5_第2页
吉大编译课件scanning-5_第3页
吉大编译课件scanning-5_第4页
吉大编译课件scanning-5_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Compiler Construction Principles & Implementation TechniquesDr. Zheng XiaojuanProfessorSoftware College of Northeast Normal UniversityMarch. 20101Summary for Last Lecture Regular Expression (RE)Some concepts and operations on strings & set of strings;Definition of RERegular DefinitionGenerating NFA

2、from Regular Expressionrules2Outline2.1 Overview2.1.1 General Function of a Scanner2.1.2 Some Issues about Scanning2.2 Finite Automata 2.2.1 Definition and Implementation of DFA2.2.2 Non-Determinate Finite Automata2.2.3 Transforming NFA into DFA2.2.4 Minimizing DFA2.3 Regular Expressions2.3.1 Defini

3、tion of Regular Expressions2.3.2 Regular Definition2.3.4 From Regular Expression to DFA2.4 Design and Implementation of a Scanner2.4.1 Developing a Scanner from DFA2.4.2 A Scanner Generator Lex32.4 Design and Implementation of a ScannerDeveloping a Scanner ManuallyA Scanner Generator Lex4Developing

4、a Scanner ManuallyDevelop a ScannerLexical Definition inRegular ExpressionNFADFAtransformingtransformingminimized DFAminimizingimplement5Implement Scanner with DFAImplementation of DFAJust checking whether a string is acceptable by the DFA;Implementation of a Scannernot checking;but recognizing an a

5、cceptable string(word) and establish its internal representation 6Implement Scanner with DFASome IssuesIndependent or AttachedSkip these special charactersBlank, Tab, comments, return (line number)When to stop scanningAt the end of the source fileKeywords & identifierHow to know the end of recognizi

6、ng one token?7立即接受状态和延迟接受状态 ;:=otherddother立即接受状态立即接受状态延迟接受状态延迟接受状态 return(SEMI,;) return(ASS,:=) return(COLON,:) BACK() return(NUM,number) BACK()*8Defining Lexical Structure of C0letter = a|z|A|Z digit = 0|9NZ-digit = 1|9Reserved = | | read| writeIdentifier =letter(letter|digit)* Constant: int = NZ

7、-digit digit* | 0 Other symbols: syms = +|*| := | ; Lexical structure: lex = Reserved | identifier |int | syms9DFA for C0startdoneIDIntNumAssignNZ-digitdigitotherother+, *, ; :=letterletterdigitReserved(key)-words will be decided by checking identifier in reserved(key)-words table;10Developing a Sca

8、nner from DFAInput: a sequence of symbols, with a special symbol EOF as the end of the sequence;Output: a sequence of tokens;11Developing a Scanner from DFAToken Type:typedef enum IDE, NUM, ASS /标识符,整数,赋值号 PLUS, MINUS, SEMI / +, *, ; , READ, WRITE , /keywords TkType12Developing a Scanner from DFADat

9、a Structure for TOKEN: struct Token TkType type; char val50; 13Developing a Scanner from DFAGlobal Variables: - char str50; - store the string has been read already; - int len = 0; - the length of the str - Token tk; - current token - Token TokenList100; - the sequence of tokens - int total = 0; - t

10、he number of tokens generated14Developing a Scanner from DFAPredefined Functions: - bool ReadNextchar() - read current symbol to CurrentChar, if current symbol is EOF returns false; else returns true; - int IsKeyword(str) - checking whether str is one of keywords, if str is a keyword, it returns the

11、 number of the keywords; else it returns -1; - void SkipBlank() - skip blank characters & return until read one other character to CurrentChar;15Developing a Scanner from DFASkipBlank();start: case CurrentChar of “1.9”: strlen = CurrentChar; len+; goto IntNum ; “a.z”, “A.Z”: strlen = CurrentChar; le

12、n+; goto ID; “:”: goto Assign; “+” : tk.type =PLUS; SkipBlank() ;goto Done; “*”: tk.type = MINUS; SkipBlank() ;goto Done; “;” :tk.type = SEMI ; SkipBlank() ;goto Done; EOF: exit; other: error();16Developing a Scanner from DFAIntNum: if (not ReadNextchar() if len !=0 tk.type = NUM, strcpy(tk.val, str

13、); goto Done case CurrentChar of “0.9”: strlen = CurrentChar; len+; goto IntNum ; other: tk.type = NUM, strcpy(tk.val, str);17Developing a Scanner from DFAID: if (not ReadNextchar() if len !=0 tk.type = IDE, strcpy(tk.val, str); goto Done case CurrentChar of “0.9”: strlen = CurrentChar; len+; goto I

14、D; “a.z”, “A.Z”: strlen = CurrentChar; len+; goto ID; other: if IsKeyword(str) tk.type = IsKeyword(str) else tk.type = IDE, strcpy(tk.val, str) ; goto done;18Developing a Scanner from DFAAssign: if (not ReadNextchar() if len !=0 error; exit; case CurrentChar of “=”: Tk.type = ASS; goto Done ; other:

15、error();19Developing a Scanner from DFADone: TokenListtotal = tk; / add new token to the token list total +; / len = 0; /start storing new token string strcpy(str, “”); / reset the token string SkipBlank(); / skip blank characters goto start; /start scanning new token20What are problems with this Sc

16、annerstr & TokenList use array, which is not practical;Not deal with errors;Not deal with line number;21A Scanner Generator LexDifferent versions of Lex;flex is distributed by GNU compiler package produced by the Free Software Foundation, which is freely available from Internet;flex.lRE-like definit

17、ionlexyy.c( yylex() )22PracticeGet Lex-similar free software;Get to know its specification language;Try to define the lexeme of C0 with the specification language;Implement Scanner for C0 with the software;Test the Scanner;23Summary for 2. Scanning24What have been introduced2.1 Overview2.1.1 General

18、 Function of a Scanner2.1.2 Some Issues about Scanning2.2 Finite Automata 2.2.1 Definition and Implementation of DFA2.2.2 Non-Determinate Finite Automata2.2.3 Transforming NFA into DFA2.2.4 Minimizing DFA2.3 Regular Expressions2.3.1 Definition of Regular Expressions2.3.2 Regular Definition2.3.4 From

19、 Regular Expression to DFA2.4 Design and Implementation of a Scanner2.4.1 Developing a Scanner from DFA2.4.2 A Scanner Generator Lex25SummaryAbout finite automataDefinition of DFA(, start state, set of states, set of terminate states, f)Definition of NFA(, set of start states, set of states, set of

20、terminate states, f)Differences between NFA and DFANumber of start statesAllows more than one ledges for a state and one same symbol26SummaryAbout finite automataFrom NFA to DFAmain ideasolve problemMinimizing DFAmain ideasolve problemImplementing DFATable basedGraph based27SummaryAbout regular expr

21、essionsDefinition of regular expressionRegular definitionFrom regular expression to NFADefining lexical structure with regular expression28SummaryAbout scannerDefining lexical structure of the programming language with regular expressionTransforming regular expression into NFATransforming NFA into D

22、FAMinimizing DFAImplementing DFA 29SummaryOriginal ProblemDevelop a ScannerRead source program in the form of stream of characters, and recognize tokens with respect to lexical rules of source language;General techniquesUse RE to define Lexical structure;RE - NFA - DFA - minimized DFA - implementGeneral ProblemUse RE/FA to define the structural rulesCheck whether the input meets the structural rules30SummaryApplication in similar problemsUse RE(DFA) to formally describe th

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论