6.11 THE PROCEDURE CALL


6.10 SWITCH/CASE

To capture the syntactic structure of the switch statement, we add the following productions to the grammar. Here, break is assumed to be a part of statement that is derivable from a nonterminal S .

  S    switch  E  { caselist} caselist   caselist case  V  :  S  caselist   case  V  :  S  caselist   default:  S  caselist   caselist default:  S  

A switch statement is comprised of two components : an expression E , which is used to select a particular case from the list of cases; and a caselist, which is a list of n number of cases, each of which corresponds to one of the possible values of the expression E , perhaps including a default case.

Note  

A case statement can be implemented in a variety of different ways. If the number of cases is not too great, then a case statement can be implemented by generating a sequence of conditional jumps , each of which tests for an individual value and transfers to the code for the corresponding statement. If the number of cases is large, then it is more efficient to construct a hash table for the case values with the labels of the various statements as entries.

A syntax-directed translation scheme that translates a case statement into a sequence of conditional jumps, each of which tests for an individual value and transfers to the code for the corresponding statement, is considered below. We begin with a typical switch statement:

 switch (  E  )   {     case  V  1:  S  1     case  V  2:  S  2     .     .     .     case  Vn  :  Sn  } 

The generated three-address that is required for the statement is shown in Figure 6.17. Here, next is the label of the code for the statement that comes next in the switch statement execution order.

click to expand
Figure 6.17: A switch/case three-address translation.

Therefore, switch statement translation involves generating an unconditional jump after the code of every S 1, S 2, , Sn statement in order to transfer control to the next element of the switch statement, as well as to remember the code start of S 1, S 2, , Sn , and to generate the conditional jumps. Each of these jumps tests for an individual value and transfers to the code for the corresponding statement. This requires introducing nullable nonterminals before S 1, as shown in Figure 6.18.

click to expand
Figure 6.18: Nullable nonterminals are introduced into a switch statement translation.
EXAMPLE 6.1
start example

Consider the following switch statement:

 switch (  i  +  j  )   {     case 1:  x  =  y  +  z  default:  p  =  q  +  r  case 2:  u  =  v  +  w  } 
end example
 

The above translation scheme translates into the following three-address code, which is also shown in Figure 6.19:

click to expand
Figure 6.19: Contents of queue during the translation.
EXAMPLE 6.2
start example

Using the above translation scheme translates the following switch statement:

 switch (  a  +  b  )   {            case 2: {  x  =  y  ; break; }            case 5: {switch  x  {                            case 0: {  a  =  b  + 1; break; }                            case 1: {  a  =  b  + 3; break; }                            default: {  a  = 2; break; }                    }                    break;            case 9: {  x  =  y  - 1; break; }            default: {  a  = 2; break; }   } 
end example
 

The three address code is:

  1. t 1 = a + b

  2. goto(23)

  3. x = y

  4. goto NEXT

  5. goto(14)

  6. t 3 = b + 1

  7. a = t 3

  8. goto NEXT

  9. t 4 = b + 3

  10. a = t 4

  11. goto NEXT

  12. a = 2

  13. goto NEXT

  14. if x = 0 goto(6)

  15. if x = 1 goto(9)

  16. goto(12)

  17. goto NEXT

  18. t 5 = y ˆ’ 1

  19. x = t 5

  20. goto NEXT

  21. a = 2

  22. goto NEXT

  23. if t 1 = 2 goto(3)

  24. if t 1 = 5 goto(5)

  25. if t 1 = 9 goto(18)

  26. goto(21)




Algorithms for Compiler Design
Algorithms for Compiler Design (Electrical and Computer Engineering Series)
ISBN: 1584501006
EAN: 2147483647
Year: 2005
Pages: 108
Authors: O G Kakde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net