Chapter 5:Semantic Analysis

DAG for Expressions

Expression a + a * ( b - c ) + ( b - c ) * d make a leaf or node if not present, otherwise return pointer to the existing node

P 1 = makeleaf(id,a)
P 2 = makeleaf(id,a)
P 3 = makeleaf(id,b)
P 4 = makeleaf(id,c)
P 5 = makenode(-,P 3 ,P 4 )
P 6 = makenode(*,P 2 ,P 5 )
P 7 = makenode(+,P 1 ,P 6 )
P 8 = makeleaf(id,b)
P 9 = makeleaf(id,c)
P 10 = makenode(-,P 8 ,P 9 )
P 11 = makeleaf(id,d)
P 12 = makenode(*,P 10 ,P 11 )
P 13 = makenode(+,P 7 ,P 12 )
 

A directed acyclic graph (DAG) for the expression : a + a * (b V c) + (b V c) * d All the function calls are made as in the order shown. Whenever the required node is already present, a pointer to it is returned so that a pointer to the old node itself is obtained. A new node is made if it did not exist before. The function calls can be explained as:

P1 = makeleaf(id,a)

A new node for identifier Qa R made and pointer P1 pointing to it is returned.

P2 = makeleaf(id,a)

Node for Qa R already exists so a pointer to that node i.e. P1 returned.

P3 = makeleaf(id,b)

A new node for identifier Qb R made and pointer P3 pointing to it is returned.

P4 = makeleaf(id,c)

A new node for identifier Qc R made and pointer P4 pointing to it is returned.

P5 = makenode(-,P3,P4)

A new node for operator Q- R made and pointer P5 pointing to it is returned. This node becomes the parent of P3,P4.

P6 = makenode(*,P2,P5)

A new node for operator Q- R made and pointer P6 pointing to it is returned. This node becomes the parent of P2,P5.

P7 = makenode(+,P1,P6)

A new node for operator Q+ R made and pointer P7 pointing to it is returned. This node becomes the parent of P1,P6.

P8 = makeleaf(id,b)

Node for Qb R already exists so a pointer to that node i.e. P3 returned.

P9 = makeleaf(id,c)

Node for Qc R already exists so a pointer to that node i.e. P4 returned.

P10 = makenode(-,P8,P9)

A new node for operator Q- R made and pointer P10 pointing to it is returned. This node becomes the parent of P8,P9.

P11 = makeleaf(id,d)

A new node for identifier Qd R made and pointer P11 pointing to it is returned.

P12 = makenode(*,P10,P11)

A new node for operator Q* R made and pointer P12 pointing to it is returned. This node becomes the parent of P10,P11.

P13 = makenode(+,P7,P12)

A new node for operator Q+ R made and pointer P13 pointing to it is returned. This node becomes the parent of P7, P12.