第3章_词法分析和词法分析程序.ppt

  1. 1、本文档共57页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第3章_词法分析和词法分析程序

编 译 原 理 第 三 章 词法分析和词法分析程序 Principles of Compiler Design Chapter 3. Lexical Analysis 3.1 设计扫描器时应考虑的问题 词法分析程序亦称为扫描器 任务:扫描程序,识别单词 扫描器的输出是语法分析程序的输入 3.1 How to construct a lexical analyzer A lexical analyzer is also called Scanner The main task of the lexical analyzer is to read the input characters of the source program, and group them into lexemes. The lexical analyzer outputs a sequence of tokens for each lexeme in the source program to the parser for syntax analysis. 3.1.1 词法分析的必要性 描述单词的结构比其它语法结构简单,仅用3型文法就够了; 将单词识别从语法分析识别分离出来,可采用更有效的工具实现; 有些语言的单词识别与前后文相关,不宜将其与语法分析合并; 使编译程序各部分独立出来,有利于设计、调试和维护 3.1.1 Lexical Analysis Versus Parsing The separation of lexical and syntactic analysis often leads to a cleaner overall design. Compiler efficiency is improved. A separate lexical analyzer allows us to apply specialized techniques that serve only the lexical task, not the job of parsing. Compiler portability is enhanced. Input-device-specific peculiarities can be restricted to the lexical analyzer. 3.1.2 单词符号的内部表示 常用的内部表示方法: (class,value) 为便于阅读,常用助记符(或常量标识符、宏定义等)表示class。 单词的分类方法:可一词一类(+、-、begin、end等)或多词一类(如关键字类、操作符类、分隔符类、变量名类、常数类等)。 在识别出变量名、函数(过程)名时,还应进行查填符号表的工作。 3.1.2 Tokens A token is a pair consisting of a token name and an optional attribute value (class,value) The token name is an abstract symbol representing a kind of lexical unit, e.g., a particular keyword, or a sequence of input characters denoting an identifier. The token names are the input symbols that the parser processes. We will often refer to a token by its token name. 3.1.3 识别标识符的若干约定和策略 一般来说,单词的长度是有限制的。 在允许长度下,应按最长匹配原则进行识别 有时需要超前扫描来进行单词识别。 在进行超前扫描时,还应注意“回退”字符,即将多吃掉的字符退还回输入缓冲区。 3.1.3 Token Recognition All tokens have finite length. Tokens are recognized when all possible symbols are scanned. Sometimes we need to look at least one additional character ahead. 3.2 正规文法和状态转换图 3.2 Regular Expressions Transition Diagrams 3.2.1 由正规文法构造状态转换图 程序设计语言的单词都能用正规文法描述 一般说来,凡能用正规文法描述的语言,均可由某种有限状态算法——状态转换图进行分析 状态转换图: 有向图(一个初态

文档评论(0)

ligennv1314 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档