2081 3

CSC376 · 2081 · 10 marks

Illustrate the concept of backpatching with an example. Convert the regular expression a(a + b)a# to DFA.

Illustrate the concept of backpatching with an example. Convert the regular expression a(a + b)a# to DFA.[10]

Compiler Design and ConstructionChapter 4: Intermediate Code Generation, Code Generation and OptimizationBack patchingNumerical: backpatching, regex to DFA

Backpatching and RE to DFA Conversion

Part 1: Backpatching (5 marks)

Concept

Backpatching is a technique used during single-pass code generation for handling forward jumps whose target addresses are not yet known when the jump instruction is generated.

When we emit a conditional or unconditional jump, the destination label may still be undefined. Instead of making a second pass, we:

  1. Generate the jump with the target field left blank.
  2. Keep the instruction number in a list.
  3. When the target address becomes known, we go back and fill in all instructions in that list. This "going back to fill in" is backpatching.

Data Structures and Helper Functions

  • truelist: jumps taken when a boolean is true.
  • falselist: jumps taken when a boolean is false.
  • nextlist: unconditional jumps to the statement following the current one.
FunctionMeaning
makelist(i)creates a new list with only instruction i
merge(p1,p2)concatenates two lists
backpatch(p,i)inserts i as the target of every jump in list p

Example: if (a < b) then x = 1 else x = 2

Code is emitted with blank targets:

AddrEmitted codeList
100if a < b goto ___truelist = {100}
101goto ___falselist = {101}
102x = 1(then-part)
103goto ___nextlist = {103}
104x = 2(else-part)
105...next statement

Backpatching actions:

  • then-part begins at 102 → backpatch(truelist,102) gives 100: if a<b goto 102
  • else-part begins at 104 → backpatch(falselist,104) gives 101: goto 104
  • exit is 105 → backpatch(nextlist,105) gives 103: goto 105

Final patched code:

100: if a < b goto 102
101: goto 104
102: x = 1
103: goto 105
104: x = 2
105: ...

Backpatching therefore lets the compiler resolve forward addresses in a single pass.


Part 2: Convert a(a + b)a# to DFA (Direct / Syntax-Tree Method)

Step 1: Number the leaves

The augmented expression is $a \cdot (a+b) \cdot a \cdot #$. Numbering symbol positions:

$$a_1 \cdot (a_2 + b_3) \cdot a_4 \cdot #_5$$

Step 2: Syntax tree

              . (root)
             / \
            .    #(5)
           / \
          .    a(4)
         / \
      a(1)   +
            / \
         a(2) b(3)

Step 3: nullable / firstpos / lastpos

No leaf is nullable, so every node here is non-nullable.

Nodenullablefirstposlastpos
1 (a)F{1}{1}
2 (a)F{2}{2}
3 (b)F{3}{3}
2+3F{2,3}{2,3}
1·(2+3)F{1}{2,3}
4 (a)F{4}{4}
(…)·4F{1}{4}
5 (#)F{5}{5}
rootF{1}{5}

Step 4: followpos

Rule for concatenation $c_1 \cdot c_2$: for each $i \in lastpos(c_1)$, $followpos(i) \mathrel{+}= firstpos(c_2)$.

Concat nodelastpos(left)firstpos(right)effect
1·(2+3){1}{2,3}followpos(1)={2,3}
(…)·4{2,3}{4}followpos(2)={4}, followpos(3)={4}
(…)·5{4}{5}followpos(4)={5}

followpos table:

PositionSymbolfollowpos
1a{2,3}
2a{4}
3b{4}
4a{5}
5#∅

Step 5: Construct the DFA

  • Start state $A = firstpos(\text{root}) = {1}$.
  • The accepting states are those containing position 5 (the #).

From $A={1}$ (position 1 is a):

  • on a: union of followpos of positions in $A$ with symbol a = followpos(1) = {2,3} → state $B={2,3}$.
  • on b: none.

From $B={2,3}$ (2 is a, 3 is b):

  • on a: followpos(2) = {4} → state $C={4}$.
  • on b: followpos(3) = {4} → state $C={4}$.

From $C={4}$ (position 4 is a):

  • on a: followpos(4) = {5} → state $D={5}$.
  • on b: none.

From $D={5}$ (position 5 is #, contains #, so $D$ is the accepting state):

  • followpos(5) = ∅ → no outgoing transitions.

DFA Transition Table

StatePositionson aon b
→A{1}B-
B{2,3}CC
C{4}D-
D*{5}--

DFA Diagram

        a          a / b          a
 →(A) ------> (B) --------> (C) -------> ((D))

Accepting state: $D={5}$. The DFA accepts strings matching $a(a,|,b)a$, i.e. exactly aaa and aba, before the endmarker #.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.