1 / 14

语义分析

语义分析. 主要内容: 语义分析概述(必要性、功能、描述方法) 符号表 类型表达式 声明和程序体的语义分析. 语义分析的必要性. 语法和语义的区别; 语法: 关于什么样的字符串才是该语言 在组成结构上合法的程序的法规。 语义: 关于结构上合法的程序的意义的 法则。. 语义分析的功能. 语义种类 静态语义: 在编译阶段(从程序文本上)可 以检查的语义。 动态语义: 通过程序的执行才能检查的语 义。. 语义的描述 语义形式化方法:

kermit-burt
Download Presentation

语义分析

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 语义分析 主要内容: • 语义分析概述(必要性、功能、描述方法) • 符号表 • 类型表达式 • 声明和程序体的语义分析

  2. 语义分析的必要性 • 语法和语义的区别; 语法:关于什么样的字符串才是该语言 在组成结构上合法的程序的法规。 语义:关于结构上合法的程序的意义的 法则。

  3. 语义分析的功能 • 语义种类 静态语义:在编译阶段(从程序文本上)可 以检查的语义。 动态语义:通过程序的执行才能检查的语 义。

  4. 语义的描述 语义形式化方法: 1. 操作语义 2. 指称语义 3. 公理语义 4. 代数语义

  5. 语义分析的内容: 类型分析; 标识符相关信息; • 语义分析的功能: 检查语义错误 构造标识符属性表(符号表) • 语义分析的实现: 与语法分析相结合

  6. 语义分析的功能图示 语法分析树 语义分析 符号表 TokenList 语义定义 判定 自然语言描述规定

  7. 三种内部表示 • 标识符的内部表示 • 类型的内部表示 • 值的内部表示

  8. 标识符的内部表示 • 标识符种类: 常量名、类型名、变量名、函数名、过程名、域名。 TYPE idkind=( consKind, typeKind, varKind, fieldKind, procKind,funcKind ) • 内部表示(AttributeIR): 常量: 类型: 变量: 域名*: 过函: TypePtr Kind Value TypePtr Kind Forward TypePtr Kind Access Level Off TypePtr Kind Off HostType Off TypePtr Kind Level Parm Class Code Size Forward

  9. 例有声明如下: CONST pai= 3.14 ; TYPE vector=ARRAY[1..10] OF integer; VAR x, y : real ; r, s : vector ; 设当前层数和可用offset值分别为L和0,构造标识符 pai, vector, x, y, r 和s 的属性表示。

  10. 类型的内部表示 • 类型的种类:标准、子界、枚举、数组、记录、 集合、文件、指针类型等等。 TypeKind=(intTy,boolTy,charTy,realTy,enumTy, subTy,arrayTy,recordTy,setTy,fileTy,pointerTy) • 内部表示:(TypeIR) 标准类型: sub: enum: array: Size Kind Size Kind HostType Low Up Size Kind Elems Leng Size Kind IndexType ElemType

  11. id CaseType Off Size Kind FixBody VariBody id FixUnitType Off Next record: FixBody: VariBody: set: file: pointer: FixBody VariBody Next CaseUnit VariUnits Size Kind BaseType Size Kind CompType Size Kind TypeName

  12. 例有如下的类型定义: at = ARRAY [1..10] OF ARRAY[1..100] OF integer; rt = RECORD x : real ; a : at; CASE u: boolean OF false:(k : integer); true:(y: real; b: boolean) END 构造类型的内部表示。

  13. 值的内部表示 • 非结构类型值的内部表示: 实型: 指针: 有序类型:整数形式

  14. 有序类型的常量表示: • 整型常量:ord(N) = N • 布尔常量:ord(false)=0, ord(true) = 1 • 字符常量:ord(C) = ASCⅡ (C) • 枚举常量:设有枚举类型(D,A,B),则有 ord(D)=0,ord(A)=1,ord(B)=2 • 子界常量:设有子界类型C1..C2,则值空间 为[ord(C1)...ord(C2)]

More Related