Boolean Expression in Compiler Design

Boolean Expression

The translation of conditional statements such as if-else statements and while-do statements is associated with Boolean expression's translation. The main use of the Boolean expression is the following:

  • Boolean expressions are used as conditional expressions in statements that alter the flow of control.
  • A Boolean expression can compute logical values, true or false.

Boolean expression is composed of Boolean operators like &&, ||, !, etc. applied to the elements that are Boolean or relational expressions. E1 rel E2 is the form of relational expressions.

Let us consider the following grammars:

B => B1 | | B2

B => B1 && B2 |

B => !B1

B => (B)

B => E1 rel E2

B => true

B => false

If we compute that B1 is true in the first expression, then the entire expression will be true. We don’t need to compute B2. In the second expression, if B1 is false, then the entire expression is false.

The comparison operators <, <=, =, !=, >, or => is represented by rel.op.

We also assume that || and && are left-associative. || has the lowest precedence and then &&, and !.

PRODUCTIONSEMANTIC R RULES
B => B1 | | B2B1.true = B.true B1.false = newlabel () B2.true = B.true B2.false = B.false B.code = B1.code || label(B1.false) ||    B2.code
B => B1 && B2B1.true = newlabel () B1.false = B.false B2.true = B.true B2.false = B.false        B.code = B1.code | | label( B1.true) | |    B2.code  
B => !B1B1.true = B.false B1.false = B.true B.code = B1.code
B =>  E1 rel E2B.code = E1.code | | E2.code | | gen(‘if’ E1.addr rel.op E2.addr         ‘goto’ B.true) | | gen(‘goto’ B.false)
B => trueB.code = gen(‘goto’ B.true )
B => falseB.code = gen(‘goto’ B.false )

The below example can generate the three address code using the above translation scheme:

if ( x < 100 || x > 200 && x ! = y ) x = 0;

           if x < 100 goto L2

           goto L3                

L3:     if x > 200 goto L4

          goto L1                 

L4:     if x != y goto L 2 

           goto L1               

L2:     x = 0                     

L1:                                 


Related Topics

Code Generation

Code Generation The last phase of the compiler is code generation. It is the compiler's back-end that makes multiple passes over the IR before generating the target program. The code generator's...

4 minutes read.

LR Parser in Compiler Design

LR Parser The most popular type of bottom-up parsing is LR(K) parsing. The LR() parser scans the input from left – to – right, which is the actual abbreviation of L...

2 minutes read.

Run-Time Environments

Run-Time Environments Storage Organization Every target program has its own logical address, and an executable program runs in it. The logical address space has the location for each program value. The...

2 minutes read.

Three-Address Code

Three-Address Code If there is at most one operator on the right side of the instruction, then the instruction will be the three-address code so that no arithmetic expressions are permitted....

2 minutes read.

LR Parser Compiler Design

LR Parser LR parsing is a type of bottom-up parsing that is used to parse the large class of grammars. Here "L" stands for left-to-right scanning of the input "R" stands...

6 minutes read.

Translation of Array References in Compiler Design

Translation of Array References We can access the elements of an array stored in consecutive blocks very easily and quickly. In a programming language like C and Java, the size of...

2 minutes read.

Run-Time Storage Management

Run-Time Storage Management Every executing program has its own logical address space. Logical address space is partitioned into: Code: It is responsible for storing the executable target code. Static: It is used to...

3 minutes read.

Parsing in Compiler Design

Parsing is the term used to describe the process of converting data between different formats. The parser can carry out this operation. The parser is a part of the translator...

4 minutes read.

Syntax-Directed Translation

Syntax-Directed Translation A context-free-grammar with some additional rules is known as a syntax-directed definition. In SDT, attributes are associated with grammar symbols and rules are associated with productions. The attributes can...

3 minutes read.

CLR Parsing Compiler Design

CLR Parsing CLR parsing refers to the canonical lookahead. We will use the canonical collection of LR(1) items for the construction of the CLR(1) parsing table. Generally, CLR(1) parsing has more...

6 minutes read.

S-attributed and L-attributed SDTs

S-attributed and L-attributed SDTs STD stands for Syntax Directed Translation. When we associate some informal notations called semantic rules and the grammar, they are known as STD. So we can say...

2 minutes read.

Intermediate-Code Generator Compiler Design

Intermediate-Code Generator The process of translating a source language into machine code for a given target machine is done by intermediate-code. It lies between the high-level language and the machine language....

2 minutes read.

Evolution of Programming Languages in Compiler Design

The Evolution of Programming Languages The first computer came in the 1940s and was programmed in a binary language that told the computer what operations are to be performed and in...

4 minutes read.

YACC in Compiler Design

YACC  YACC is known as Yet Another Compiler Compiler. It is used to produce the source code of the syntactic analyzer of the language produced by LALR (1) grammar. The input...

5 minutes read.

Stack Allocation of Space

Stack Allocation of Space Almost all compilers for languages that use procedure, functions, or methods manage their run-time memory as a stack. Whenever a procedure is called, the local variable's space...

2 minutes read.

Optimization of Basic Blocks in Compiler Design

Optimization of Basic Blocks We can apply the optimization process on a basic block. While optimization, there is no need to change the set of expressions computed by the block. The basic...

3 minutes read.

Finite State Machine

Finite State Machine A finite state machine is a simple machine to recognize patterns. It takes a string of symbols as the input and changes its state to another state, but...

3 minutes read.

Shift Reduce Parsing in Compiler Design

Like the bottom-up parsing, the shift reduce parser also builds the parse tree from the leaves (bottom) to the root (up). The LR parser is a more versatile variation of...

4 minutes read.

SLR 1 Parsing Compiler Design

SLR(1) Parsing It is a simple LR parsing. Most of the function of this parsing is the same as LR(0) parsing. The parsing table for both the parser vary. The SLR(1)...

5 minutes read.

Storage Allocation

Storage Allocation The storage allocation represents memory management. The allocation of memory can be done in the following ways: Static AllocationStack AllocationHeap Management Static Allocation: It is a procedure used for the allocation of...

1 minute read.