770 likes | 872 Views
● Purpose 目的. compute additional information needed for compilation that is beyond the capabilities of context-free grammars and standard parsing algorithms;. 5. Chapter Syntax-Directed Translation. 5.1 Attributes and attribute grammars
E N D
● Purpose 目的 compute additional information needed for compilation that is beyond the capabilities of context-free grammars and standard parsing algorithms; 5 Chapter Syntax-Directed Translation 5.1 Attributes and attribute grammars 5.2 Algorithms for attribute computation 5.3 翻译模式
● Classification 分类 analysis of a program required by the rules of the programming language to establish its correctness and to guarantee proper execution; analysis performed by a compiler to enhance the efficiency of execution of the translated program ● Static semantic analysis 静态语义分析 Take place prior to execution (Such as building a symbol table、performing type inference and type checking) ;
● Binding of the attribute 属性的联编 the process of computing an attribute and associating its computed value with the language construct in question. ● Binding time联编时间 the time during the compilation/execution process when the binding of an attribute occurs. 5.1 Attributes and attribute grammars 属性和属性文法 • any property of a programming language construct such as • The data type of a variable • The value of an expression • The location of a variable in memory • The object code of a procedure • The number of significant digits in a number 数的有效位数 ● Attributes 属性
● Example ◆ type checker 类型检查器 ◆the values of expressions表达式的值 usually dynamic and the be computed during execution; but sometime can also be evaluated during compilation(constant folding 常量合并). ◆ The allocation of a variable 变量的分配 either static or dynamic,sometimes it is a mixture of static and dynamic depending on the language and properties of the variable itself. ◆ Object code of a procedure程序的目标代码 a static attribute, which is computed by the code generator. ◆ Number of significant digits in a number数的有效位数 often not explicitly treated during compilation. 5.1 Attributes and attribute grammars 属性和属性文法 Based on the difference of the binding time, attributes is divided into Static attributes ( be bound prior to execution) and Dynamic attributes (be bound during execution). ● Static attributes / Dynamic attributes
5.1.1 Attribute grammars 属性文法 X.a means the value of a associated to X (X is a grammar symbol and a is an attribute associated to X). ● syntax-directed semantics 语法制导语义 attributes are associated directly with the grammar symbols of the language. Given a collection of attributes a1,…,ak, it implies that for each grammar rule X0X1X2…Xn (X0 is a nonterminal), the values of the attributes Xi.aj of each grammar symbol Xi are related to the values of the attributes of the other symbols in the rule.
● attribute grammars 属性文法 Grammar Rule Semantic Rules Rule 1 Associated attribute equations . . . . Rule n Associated attribute equations 5.1.1 Attribute grammars 属性文法 ● attribute equations or semantic rules 属性等式或语义规则 An attribute grammar for attributes a1, a2, … , ak is the collection of all attribute equations or semantic rules of the following form, for all the grammar rules of the language. Xi.aj = fij(X0.a1,…,X0.ak, …, X1.al, …, Xn-1.a1, …Xn.ak) Where fij is a mathematical function of its arguments
解: grammar rule semantic rules number1number2digitnumber1.val = number2.val*10+digit.val number digit number.val= digit.val digit 0 digit.val = 0 digit 1 digit.val = 1 digit 2 digit.val = 2 digit 3 digit.val = 3 digit 4 digit.val = 4 digit 5 digit.val = 5 digit 6 digit.val = 6 digit 7 digit.val = 7 digit 8 digit.val = 8 digit 9 digit.val = 9 ●example 1) 已知无符号数文法如下,请改写成值属性文法,并对345进行语义分析。number number digit | digit digit 0|1|2|3|4|5|6|7|8|9
the parse tree showing attribute computations for the number 345 number (val=34*10+5=345) number (val=3*10+4=34) digit (val=5) number (val=3) digit (val=4) 5 4 digit (val=3) 3
解: grammar rule semantic rules exp1exp2+term exp1.val=exp2.val+term.val exp1exp2-term exp1.val=exp2.val-term.val exp term exp.val= term.val term1term2*factor term1.val=term2.val*factor.val term factor term.val=factor.val factor (exp) factor.val=exp.val factor number factor.val=number.val ●example 2) 已知简单的整数算术表达式文法如下,请改写成值属性文法,并对 (34-3)*42 进行语义分析。 exp exp + term | exp-term | term term term*factor | factor factor (exp) | number
exp (val = 1302) the parse tree (34-3)*42 term (val = 31*42=1302) factor (val = 42) term (val = 31) * number (val = 42) factor (val = 31) exp (val = 34-3=31) ( ) term (val = 3) exp (val = 34) - term (val = 34) factor (val =3) factor (val = 34) number (val = 3) number (val = 34)
解: grammar rule semantic rules decltype var-list var-list.dtype = type.dtype typeint type.dtype = integer type float type.dtype = real var-list1id,var-list2 id.dtype = var-list1.dtype var-list2.dtype= var-list1.dtype var-listid id.dtype = var-list.dtype ●example 3) 已知变量声明文法如下,请对float x, y进行语义分析。decl type var-list typeint|float var-listid,var-list|id
the parse tree (float x,y) decl type (dtype = real) var-list (dtype = real) var-list (dtype = real) float id (x) (dtype = real) , id (y) (dtype = real)
解: grammar rule semantic rules based-numnum basechar based-num.val = num.val num.base = basechar.base basechar o basechar.base = 8 basechar d basechar.base = 10 num1num2 digit num1.val = if digit.val = error or num2.val = error then error else num2.val*num1.base+digit.val num2.base = num1.base digit.base = num1.base ●example 4) 已知数文法如下,请对345o进行语义分析。 based-num num basechar basechar o|d num num digit | digit digit 0|1|2|3|4|5|6|7|8|9
●example 4) 已知数文法如下,请对345o进行语义分析。 based-num num basechar basechar o|d num num digit | digit digit 0|1|2|3|4|5|6|7|8|9 解: grammar rule semantic rules num digit num.val = digit.val digit.base = num.base digit 0 digit.val = 0 digit 1 digit.val = 1 … … digit 7 digit.val = 7 digit 8 digit.val = if digit.base = 8 then error else 8 digit 9 digit.val = if digit.base = 8 then error else 9
the parse tree showing attribute computations for the number 345o based-num (val = 229) num (val = 28*8+5=229) (base = 8) basechar (base = 8) num (val = 3*8+4=28)(base=8) o digit (val = 5) (base = 8) 5 num (val = 3)(base = 8) digit (val = 4) (base = 8) digit (val = 3) (base = 8) 4 3
解: grammar rule semantic rules exp1exp2+exp3 exp1.val=exp2.val+exp3.val exp1exp2-exp3 exp1.val=exp2.val-exp3.val exp1exp2*exp3 exp1.val=exp2.val*exp3.val exp1(exp2) exp1.val=exp2.val expnumber exp.val=number.val 5.1.2 Simplifications and Extensions to Attribute Grammars 属性文法的简化和扩充 ●example 1)ambiguous grammar: exp exp + exp|exp – exp|exp * exp|(exp)|number
解: Grammar Rule Semantic Rules exp1exp2+term exp1.tree=mkOpNode(+,exp2.tree,term.tree) exp1exp2-term exp1.tree=mkOpNode(-,exp2.tree,term.tree) exp1 term exp1.tree= term.tree term1term2*factor term1.tree=mkOpNode(*,term2.tree,factor.tree) termfactor term.tree=factor.tree factor(exp) factor.tree=exp.tree factornumber factor.tree=mkNumNode(number.lexval) ●example 2)define an abstract syntax tree for simple integer arithmetic expressions by the attribute grammar as follows:
5.2 Algorithms for attribute computation 属性计算方法 5.2.1 dependency graphs and evaluation order相关图和赋值顺序 dependency graph of the string is the union of the dependency graphs of the grammar rule choices representing each node(nonleaf) of the parse tree of the string. Xi.aj = fij(…, Xm.ak, …) An edge from each node Xm.ak to Xi.aj the node expressing the dependency of Xi.aj on Xm.ak.
解:属性等式number1.val = number2.val*10+digit.val 文法规则number1number2digit 的依赖图: number1 .val number2 .val digit.val ●example 1) 已知无符号数属性文法,请画出属性等式及345的依赖图。 grammar rule semantic rules number1number2digitnumber1.val = number2.val*10+digit.val number digit number.val= digit.val
字符串345对应的语法树的依赖图: number.val number.val digit.val digit.val number.val digit.val number.val 属性等式number.val = digit.val 文法规则numberdigit 的依赖图: digit.val
解:属性等式= var-list1.dtype 文法规则id.dtype = var-list1.dtype var-list2.dtype var-list1id,var-list2的依赖图: var-list1.dtype id.dtype var-list1.dtype ●example 2) 已知声明属性文法,请画出属性等式及float x,y的依赖图。 grammar rule semantic rules decltype var-list var-list.dtype = type.dtype typeint type.dtype = integer type float type.dtype = real var-list1id,var-list2 id.dtype = var-list1.dtype var-list2.dtype= var-list1.dtype var-listid id.dtype = var-list.dtype
属性等式var-list.dtype = type.dtype 文法规则decltype var-list 的依赖图: decl type dtype dtype var-list 字符串float x,y的依赖图: decl dtype var-list type dtype dtype var-list , float dtype id (x) dtype id (y) 属性等式id.dtype = var-list.dtype 文法规则var-listid的依赖图: var-list.dtype id.dtype
3) 已知无符号数属性文法,请画出属性等式及345o的依赖图。 ●example • grammar rule semantic rules • based-numnum basechar based-num.val = num. val • num.base = basechar.base • basechar o basechar.base = 8 • basechar d basechar.base = 10 • num1num2 digit num1.val = • if digit.val = error or num2.val = error • then error • else num2.val*num1.base+digit.val • num2.base = num1.base • digit.base = num1.base • num digit num.val = digit.val • digit.base = num.base • digit 0 digit.val = 0 • digit 1 digit.val = 1 • … … • digit 7 digit.val = 7 • digit 8 digit.val = if digit.base = 8 then error else 8 • digit 9 digit.val = if digit.base = 8 then error else 9
解:属性等式based-num.val = num.bval num.base = basechar.base 文法规则based-numnum basechar的依赖图: based-num val base num val basechar base
属性等式num1.val = if digit.val = error or num2.val = error • then error • else num2.val*num1.base+digit.val • num2.base = num1.base • digit.base = num1.base • 文法规则num1num2 digit 的依赖图: base num 1 val base digit val base num2 val
属性等式digit.val = if digit.base = 8 then error else 9 文法规则digit 9的依赖图: base digit val 9 属性等式num.val = digit.val digit.base = num.base 文法规则numdigit 的依赖图: base num val base digit val
● topological sort 拓扑排序 ● directed acyclic graphs. or dags 确定非循环图(有向无环图) based-num val 字符串345o的依赖图: base num val basechar base o base num val base digit val 5 base num val base digit val base digit val 4 3
●example 4) 已知依赖图是一个dag ,请计算其属性值。 based-num val 14 1 base num val 2 13 basechar base o 3 base num val 10 11 12 base digit val 5 4 base num val base digit val 7 8 9 base digit val 5 6 4 3
● synthesized attributes An attribute is synthesized if all its dependencies pointform child to parent in the parse tree. Equivalently, an attribute a is synthesized if, given a grammar ruleAX1X2… Xn, the only associated attribute equation with an a on the left-hand side is of the form: A.a = f(X1.a1,…X1.ak,…Xn.a1,… Xn.ak) 5.2.2 Synthesized and inherited attributes 综合和继承属性 ● Classification of the attributes 属性的分类 1. synthesized attributes 合成属性(综合属性) 2. Inherited attributes 继承属性 An attribute grammar in which all the attributes are synthesized is called an S-attributed grammar.
●example 1) 无符号数属性文法为S属性文法,其中val属性为综合属性。 grammar rule semantic rules number1number2digitnumber1.val = number2.val*10+digit.val number digit number.val= digit.val digit 0 digit.val = 0 digit 1 digit.val = 1 digit 2 digit.val = 2 digit 3 digit.val = 3 digit 4 digit.val = 4 digit 5 digit.val = 5 digit 6 digit.val = 6 digit 7 digit.val = 7 digit 8 digit.val = 8 digit 9 digit.val = 9
●example 2) 简单的整数算术表达式属性文法为S属性文法,其中val属性为综合属性。 grammar rule semantic rules exp1exp2+term exp1.val=exp2.val+term.val exp1exp2-term exp1.val=exp2.val-erm.val exp term exp.val= term.val term1term2*factor term1.val=term2.val*factor.val term factor term.val=factor.val factor (exp) factor.val=exp.val factor number factor.val=number.val
◆综合属性值的计算 The attribute values of an S-attributed grammar can be computed by a signal bottom-up, or post-order, traversal of the parse or syntax tree. Just as follows: Procedure PostEval (T : treenode); Begin For each child C of T do PostEval(C); Compute all synthesized attributes of T; End
◆The structure for a syntax tree语法树的结构 Typedef enum {Plus, Minus,Times} opkind; Typedef enum {opkind, constkind} expkind; Typedef struct streenode { expkind kind; opkind op; struct streenode *lchild, *rchild; int val; } Streenode; typedef Streenode *Syntaxtree;
◆the PostEval pseudocode 后序属性赋值 void postEval(Syntaxtree t) { int temp; if (t->kind == opkind) { postEval(t->lchild); postEval(t_rchild); switch (t->op) { case Plus: t->val = t->lchild->val + t->rchild->val; break; case Minus: t->val = t->lchild->val - t->rchild->val; break; case Times: t->val = t->lchild->val * t->rchild->val; break; } } }
● two basic kinds of dependency of inherited attributes a A A a B a C a B a C Inheritance form parent to siblings inheritance from sibling to sibling ● sibling inheritance via sibling pointers 同属指针的同属继承 A a B a C ● inherited attribute An attribute that is not synthesized is called an inherited attribute.
grammar rule semantic rules decltype var-list var-list.dtype = type.dtype typeint type.dtype = integer type float type.dtype = real var-list1id,var-list2 id.dtype = var-list1.dtype var-list2.dtype= var-list1.dtype var-listid id.dtype = var-list.dtype ●example 3) 变量声明属性文法,其中dtype属性为继承属性。
◆继承属性值的计算 Inherited attributes can be computed by a preorder traversal , or combined preorder/inorder traversal of the parse or syntax tree, represented by the following pseudocode: Procedure PreEval(T: treenode); Begin For each child C of T do Compute all inherited attributes of C; PreEval(C); End;
In the procedure above, preorder and inorder operations are mixed. Inorder: decl node Preorder: var-list node ◆dtype属性的递归程序 Procedure EvalType(T: treenode); Begin Case nodekind of T of Decl: EvalType(type child of T); Assign dtype of type child of T to var-list child of T; EvalType(var-list child of T); Type: If child of T = int then T.dtype := integer Else T.dtype :=real; Var-list: Assign T.dtype to first child of T; If third child of T is not nil then Assign T.dtype to third child; EvalType(third child of T); End case; End EvalType;
字符串float x,y的依赖图: decl dtype var-list type dtype 1 2 dtype var-list , 4 float dtype id (x) 3 5 dtype id (y)
◆The syntax tree structure 语法树的结构(C语言) Typedef enum {decl, type,id} nodekind; Typedef enum {integer, real} typekind; Typedef struct treenode {nodekind kind; struct treenode *lchild, *rchild , * sibling; typekind dtype; char *name; } *Syntaxtree; decl id id float
◆The EvalType procedure (C语言) void evaltype (syntaxtree t) { switch (t->kind) {case decl: t->rchild->dtype = t->lchild->dtype evaltype(t->rchild); break; case id: if(t->sibling != NULL) { t->sibling->dtype = t->dtype; evaltype(t->sibling); } break; } }
◆The EvalType nonrecursive procedure (C语言) void evaltype(syntaxtree t) { if(t->kind = = decl) { syntaxtree p = t->rchild; p->dtype = t->lchlild->dtype; while (p->sibling !=NULL) { p->sibling->dtype = p->dtype; p = p->sibling; } } }
3)无符号数属性文法,其中val属性为综合属性,base为继承属性。 3)无符号数属性文法,其中val属性为综合属性,base为继承属性。 ●example • grammar rule semantic rules • based-numnum basechar based-num.val = num. val • num.base = basechar.base • basechar o basechar.base = 8 • basechar d basechar.base = 10 • num1num2 digit num1.val = • if digit.val = error or num2.val = error • then error • else num2.val*num1.base+digit.val • num2.base = num1.base • digit.base = num1.base • num digit num.val = digit.val • digit.base = num.base • digit 0 digit.val = 0 • digit 1 digit.val = 1 • … … • digit 7 digit.val = 7 • digit 8 digit.val = if digit.base = 8 then error else 8 • digit 9 digit.val = if digit.base = 8 then error else 9
Base is computed in preorder and val in postorder during a single pass as follows. ◆val , btype属性的计算程序 Procedure EvalWithBase(T: treenode); Begin Case nodekind of T of Based-num: EvalWithBase(right child of T); Assign base of right child of T to base of left child; EvalWithBase (left child of T); Assign val of left child of T to T.val; Num: Assign T.base to base of left child of T; EvalWith Base(left child of T);
If right child of T is not nil then Assign T.base to base of right child of T; EvalWithBase(right child of T); If vals of left and right children != error then T.val :=T.base*(val of left child) +val of right child Else T.val := error; Else T.val := val of left child; Basechar: If child of T = o then T.base :=8; Else T.base :=10; Digit: If T.base=8 and child of T=8 or 9 then T.val := error Else T.val :=numbal(child of T); End case; End EvalWithBase;
To compute all the attributes in a single pass over the parse or syntax tree, the inherited attributes shouldn’t depend on any synthesized attributes, the synthesized attributes could depend on the inherited attributes (as well as other synthesized attributes). ◆ Procedure CombinedEval(T:treenode); Begin For each child C of T do Compute all inherited attributes of C; CombinedEval(C); Compute all synthesized attributes of T. End;
●example 4) consider the following simple version of an expression grammar: Expexp/exp | num | num.num Operations may be interpreted differently depending on whether they are floating-point or strictly integer operations. For instance: 5/2/2.0 = 1.25 5/2/2 = 1 the attributes needed to express the corresponding semantic: 1.isFloat : boolean, indicates if any part of an expression has a floating-point value (synthesized) 2.etype: gives the type of each subexpression and depends on isFloat (inherited), here is int or float 3.val : gives the numeric value of each subexpression , depends on etype.
grammar rule semantic rules Sexp exp.etype = if exp.isFloat then float else int S.val = exp.val Exp1exp2/exp3 exp1.isFloat = exp2.isFloat or exp3.isFloat Exp2.etype = exp1.etype Exp3.etype = exp1.etype Exp1.val = if exp1.etype=int then exp2.val div exp3.val else exp2.val / exp3.val expnum exp.isFloat = false exp.val = if exp.etype = int then num.val else Float(num.val) expnum.num exp.isFloat = true exp.val = num.num.val
5.2.3 Attributes as parameters and returned values 作为参数和返回值的属性 Many attributes are the same or are only used temporarily to compute other attribute values, needn’t be stored as fields in a syntax tree record structure. inherited attributes: be computed in preorder, often be treated as parameters of the call. Synthesized attributes: be computed in postorder, often be treated as returned values of the call.
1) consider the recursive procedure EvalWithBase of example, we can turn base into a parameter and val into a returned val. ●example Function EvalWithBase(T: treenode; base:integer):integer; Var temp, temp2 : integer; Begin Case nodekind of T of Based-num: Temp := EvalWithBase(right child of T); Return EvalWithBase(left child of T, temp); Num: Temp:= EvalWithBase(left child of T, base); If right child of T is not nil then Temp2 := EvalWithBase(right child of T, base); If temp != error and temp2 !=error then Return base*temp + temp2 Else return error; Else return temp;