What is Backpatching?


While generating three address codes for the given expression, it can specify the address of the Label in goto statements. It is very difficult to assign locations of these label statements in one pass so, two passes are used. In the first pass, it can leave these addresses unspecified & in the next pass, and it can fill these addresses. Therefore filling of incomplete transformation is called Backpatching.

One-pass code generation using backpatching

Backpatching can be used to generate a program for boolean expressions and the flow of control statements in one pass. In this, synthesized attributes truelist and falselist of non-terminal B are used to handle labels in jumping code for Boolean expressions.

In specific, B.truelist will be a list of the jump or conditional jump instructions into which it should add the label to which control goes if B is true. B.falselist similarly is the list of instructions that finally get the label to which control goes when B is false.

As the program is produced for B, jumps to the true and false exists are left incomplete, with the label field unfilled. These preliminary jumps are located on lists pointed to by B.truelist and B.falselist as applicable.

Likewise, a statement S has a synthesized attribute S.nextlist, indicating a list of jumps to the instruction directly following the code for S. It can produce instructions into an instruction array, and labels will be indices into this array. To manipulate the list of jumps, we use three functions −

  • Makelist (i)− Create a new list including only i, an index into the array of instructions; makelist returns a pointer to the newly generated list.

  • Merge(p1,p2)− Concatenates the lists pointed to by p1, and p2 and returns a pointer to the concatenated list.

  • Backpatch (p, i)− Inserts i as the target label for each of the instructions on the record pointed to by p.

Backpatching for Boolean Expressions

It can create a translation scheme suitable for generating code for Boolean expressions during bottom-up parsing. A non-terminal marker M in the grammar generate a semantic action to pick up, at suitable times, the index of the next instruction to be created. The grammar is as follows−

B → B1| | MB2|B1&& MB2|! B1|(B1)|E1rel E2|True|False

M → ϵ

Flow of Control Statements

Control statements are the statements that change the flow of execution of statements. For example, If, If-else, Switch-Case, while-do statements. In programming languages, Boolean expressions are often used to

  • Alter the flow of control− Boolean expressions are used as conditional expressions in a statement that alter the flow of control. The value of such Boolean expression is implicit in a position reached in a program. For example, if (E) S, the expression E must be true if statement S is reached.

  • Compute logical values− A Boolean expression can describe true or false values. Such Boolean expressions can be computed in parallel to arithmetic expressions using three address instruction with logical operators.

Updated on: 05-Nov-2021

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements