编译原理第6章-语义分析

  • 格式:pdf
  • 大小:446.98 KB
  • 文档页数:49

College of Computer Science & Technology
语义分析的功能
抽象语法树 语义分析程序 符号表 语义错误
抽象语法树
TokenList 语义分析程序
TokenList 符号表 语义错误
Compiler Construction Principles & Implementation Techniques
变量的层数和偏移 (包括形式参数)
College of Computer Science & Technology
• 层数(level)
– 某些程序语言,过程/函数的定义可以嵌套 – 通常规定主程序的层数为0;
• 偏移量(offset)
– 执行过程/函数的调用时, 需要为其中的变量分配空间; – 偏移量指的是变量针对其所在过程/函数的空间的首地址 的偏移量;
Compiler Construction Principles & Implementation Techniques
-8-
基于Tokenlist的语义分析
College of Computer Science & Technology
int x,y; int f();
($int, -) ($id, x) ($comma, -) ($id, y) ($semi,-) ($int, -) ($id, f) ($lparen, -) ($rparen,-) ($semi,-) xentry: yentry: fentry: (x,varkind,intptr,……) (y,varkind,intptr,……) (f,routkind,intptr,……)
($int, -) ($id, xentry) ($comma, -) ($id, yentry) ($semi,-) ($int, -) ($id, fentry) ($lparen, -) ($rparen,-) ($semi,-)
Compiler Construction Principles & Implementation Techniques
College of Computer Science & Technology
第6章 语义分析
6.1 6.2 6.3 6.4
语义分析概述 符号表 符号表的局部化 语义分析的方法
Compiler Construction Principles & Implementation Techniques
-1-
• Kind
– 标识符的种类, 所有变量标识符的Kind = varKind;
• TypePtr
– 指向变量类型的内部表示;
• Access: (dir, indir); • Level: 层数 • Offset: 偏移量 • Value: 初始化时赋给变量的值的内部表示 • 例:int x = 10; • float y; Compiler Construction Principles & Implementation Techniques • float* z;
Compiler Construction Principles & Implementation Techniques
变量标识符的内部表示
College of Computer Science & Technology
Kind varKind
TypePtr
Access
Level
Offset Value
过程或者函数的属性
Kind routKind TypePtr Class Level Off Param Code Size Forward
College of Computer Science & Technology
• • • • • • • • • • • •
Kind: 标识符的种类,此处为 routKind; TypePtr: 指向函数返回值类型的内部表示,过程情形为NULL; Class: 当前函数是实在函数时(有自己函数体)取actual; 当前函数是形参函数时( 作为其他函数的参数)取formal; Level: 函数标识符被定义的层数; Off:当前函数是形参函数时,为其分配的地址偏移; Param: 指向当前函数的形式参数列表的指针; Code: 指向函数对应的目标代码的起始地址; Size: 目标代码的大小; Forward: 该声明为函数原型时取值 true,否则取值为false; 例:int f(int x, float*y, int inc()) { … } Compiler Construction Principles & Implementation void p(int x, float*y) { …. } Techniques
Compiler Construction Principles & Implementation Techniques
College of Computer Science & Technology
Program A Var x, y: integer;
层数为0 层数为1
x : (0, 0) y : (0, 1)
Kind
College of Computer Science & Technology
TypePtr
typeKind
• Kind

标识符的种类,所有类型标识符的 kind = typeKind;
• TypePtr
– 指向类型内部表示的指针;
• 例:typedef int t1; • typedef int t2[10]; • t1 a; //t1是类型标识符,a是变量标识符 • t2 b; //t2是类型标识符,b是变量标识符
College of Computer Science & Technology
6.1 语义分析概述
• 语法 vs.语义 • 语义分析的功能 • 语义分析的一般过程
Compiler Construction Principles & Implementation Techniques
-2-
College of Computer Science & Technology
程序Root 节点 Node2 …… 常量声明 类型声明 节点 Noden 函数声明 形参 声明 局部量 声明
节点 Node1
name value name type name type
语句
节点 Node 变量声明 函数声明
Compiler Construction Principles & Implementation Techniques
• switch语句的分支常量表达式不能有重复; • 枚举类型的元素不能重复; Compiler Construction Principles & Implementation Techniques • 结构类型的域名不能重复等
-10-
• 其他语义错误
College of Computer Science & Technology
常见的语义错误
College of Computer Science & Technology
• 与标识符声明和使用相关的语义错误
–标识符没有声明; –重复声明; 建符号表时检查
• 与类型相关的语义错误
–各种条件表达式的类型不是布尔类型; –运算符的分量类型不相容; 表建好后,分析语 –…… 句时检查 –唯一性检查
过程体 StmLK 形参 1
……
形参 n 语句 1
……
语句 n
类型声明
变量声明
过程声明
Compiler Construction Principles & Implementation Techniques
类C语言的抽象语法树
College of Computer Science & Technology
6.1 语义分析概述
• 语法:关于语法结构组成的规则 • 语义: 关于语法结构含义及使用的规则
• • • • • • 例: <函数调用语句> → id( <实参表达式>) int x; float f(){……} 语法正确的程序并不 能保证语义一定正确 x(); !因此,有必要进行 x=f(); 语义分析!!
Compiler Construction Principles & Implementation Techniques -13-
常量标识符的内部表示
College of Computer Science & Technology
Kind constKind
• Kind
TypePtr
Value
– 标识符的种类,所有常量的 kind = constKind;
Compiler Construction Principles & Implementation Techniques
-3-
College of Computer Science & Technology
6.1 语义分析概述
• 静态语义:是指编译时就可以确定的语义
• 例:
float x,y; int z; z = x+y;
• 例:
标识符未声明、重 复声明、类型不符 、种类不符等
• 动态语义: 是指运行时才可以确定的语义
除零溢出错误 x/(y-i); 当 y = i时 数组下标越界 int a[5]; a[5] =0; 无效指针 int*p; p =NULL; *p = 3;
Compiler Construction Principles & Implementation Techniques -4-