Syntax Trees Syntax-Tree – an intermediate representation of the compiler’s input. – A condensed form of the parse tree. – Syntax tree shows the syntactic structure of the program while omitting irrelevant details. – Operators and keywords are associated with the interior nodes. – Chains of simple productions are collapsed. Syntax directed translation can be based on syntax tree as well as parse tree. 1 Syntax Tree-Examples Expression: if B then S1 else S2 if - then - else + 5 * 3 4 • Leaves: identifiers or constants • Internal nodes: labelled with operations • Children: of a node are its operands B S1 S2 Statement: Node’s label indicates what kind of a statement it is Children of a node correspond to the components of the statement 2 Constructing Syntax Tree for Expressions Each node can be implemented as a record with several fields. Operator node: one field identifies the operator (called label of the node) and remaining fields contain pointers to operands. The nodes may also contain fields to hold the values (pointers to values) of attributes attached to the nodes. Functions used to create nodes of syntax tree for expressions with binary operator are given below. mknode(op,left,right) mkleaf(id,entry) mkleaf(num,val) Each function returns a pointer to a newly created node. 3 Syntax Tree Forms of three address instructions x = y op z x = op y x=y goto L if x goto L and if False x goto L if x relop y goto L Procedure calls using: param x call p,n y = call p,n x = y[i] and x[i] = y x = &y and x = *y and *x =y Forms of three address instructions x = y op z x = op y x = y goto L if x goto L and if False x goto L if x relop y goto L Procedure calls using: param x call p,n y = call p,n x = y[i] and x[i] = y x = &y and x = *y and *x =y ASTs and DAGs: a := b *-c + b*-c := a := + * a * b - (uni) b - (uni) c + * b - (uni) c c 12 Forms of three address instructions x = y op z x = op y x = y goto L if x goto L and if False x goto L if x relop y goto L Procedure calls using: param x call p,n y = call p,n x = y[i] and x[i] = y x = &y and x = *y and *x =y THREE-ADDRESS CODES are a form of IR similar to assembler for an imaginary machine. Each three address code instruction has the form x := y op z x,y,z are names (identifiers), constants, or temporaries (names generated by the compiler) op is an operator, from a limited set defined by the IR. Complicated arithmetic expressions are represented as a sequence of 3-address statements, using temporaries for intermediate values. For instance the expression x + y + z becomes t1 := y + z t2 := x + t1 Three address code is a linearized representation of a syntax tree, where the names of the temporaries correspond to the nodes. The use of names for intermediate values allows three-address code to be easily rearranged which is convenient for optimization. Postfix notation does not have this feature. The reason for the term three-address code is that each statement usually contain three addresses, two for the operands and one for the result. Assignments. Two possible forms: x:= y op z where op is a binary arithmetic or logical operation, x:= op y where op is a unary operation (minus, negation, conversion operator) Copy statements. They have the form x := y. Inconditional jumps. They have the form goto L where L is a symbolic label of a statement. Unconditional jumps. They have the form if x relop y goto L where statement L is executed if x and y are in relation relop. Procedure calls. They have the form param x1 param x2 param xn call p, n corresponding to the procdure call p(x1, x2, ..., xn) Return statement. They have the form return y where y representing a returned value is optional. Indexed assignments. They have the form x := y[i] or x[i] := y. Address assignments. They have the form x := &y which sets x to the location of y. Pointer assignments. They have the form x := *y where y is a pointer and which sets x to the value pointed to by y *x := y which changes the location of the value pointed to by x. Array Variables
© Copyright 2026 Paperzz