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

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...

3 minutes read.

Regular Expression | Compiler Design

Regular Expression A regular expression is a set of patterns that can match a character or string. It can also match alternative characters or strings. The grammar defined by the regular...

3 minutes read.

Ambiguity Elimination Compiler Design

Ambiguity Elimination Ambiguity elimination makes the sentence clear and readable. A sentence is grammatically ambiguous if it can produce more than one parse tree for a particular grammar. In this article,...

3 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.

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.

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.

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.

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.

Bottom-Up Parsing in Compiler Design

Bottom-Up Parsing A bottom-up parsing constructs the parse tree for an input string beginning from the bottom (the leaves) and moves to work towards the top (the root). Bottom-up parsing is...

6 minutes read.

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.

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.

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.

Errors in Compiler Design

Introduction Errors in compiler design refer to mistakes or issues that arise during the process of execution of the program. A compiler is a program that translates source code written in...

4 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.

Machine-Independent Optimizations in Compiler Design

Machine-Independent Optimizations The main aim of machine-independent optimization is to improve the generated intermediate code so that compiler can get better target code. Eliminating unwanted code from the object code or replacing...

8 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.

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.

Derivation and Parse Tree in Compiler Design

Derivation and Parse Tree In this article, we will learn Derivation and Parse Tree. Derivations The parse tree can be constructed by taking a derivational view in which production is treated as rewriting...

4 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.

Data Flow Analysis in Compiler Design

Data Flow Analysis All the optimization techniques we have learned earlier depend on data flow analysis. DFA is a technique used to know about how the data is flowing in any...

3 minutes read.