- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is translation of control statements in compiler design?
Control statements are the statements that change the flow of execution of statements.
Consider the Grammar
S → if E then S1
|if E then S1 else S2
|while E do S1
In this grammar, E is the Boolean expression depending upon which S1 or S2 will be executed.
Following representation shows the order of execution of an instruction of if-then, ifthen-else, & while do.
- 𝐒 → 𝐢𝐟 𝐄 𝐭𝐡𝐞𝐧 𝐒𝟏
E.CODE & S.CODE are a sequence of statements which generate three address code.
E.TRUE is the label to which control flow if E is true.
E.FALSE is the label to which control flow if E is false.
The code for E generates a jump to E.TRUE if E is true and a jump to S.NEXT if E is false.
∴ E.FALSE=S.NEXT in the following table.
In the following table, a new label is allocated to E.TRUE.
When S1.CODE will be executed, and the control will be jumped to statement following S, i.e., to S1.NEXT.
∴ S1. NEXT = S. NEXT.
Syntax Directed Translation for "If E then S1."
|𝐒 → 𝐢𝐟 𝐄 𝐭𝐡𝐞𝐧 𝐒𝟏||E. TRUE = newlabel;
E. FALSE = S. NEXT;
S1. NEXT = S. NEXT;
S. CODE = E. CODE | |
GEN (E. TRUE '− ') | | S1. CODE
- 𝐒 → 𝐈𝐟 𝐄 𝐭𝐡𝐞𝐧 𝐒𝟏 𝐞𝐥𝐬𝐞 𝐒𝟐
If E is true, control will go to E.TRUE, i.e., S1.CODE will be executed and after that S.NEXT appears after S1.CODE.
If E.CODE will be false, then S2.CODE will be executed.
Initially, both E.TRUE & E.FALSE are taken as new labels. Hen S1.CODE at label E.TRUE is executed, control will jump to S.NEXT.
Therefore, after S1, control will jump to the next statement of complete statement S.
Similarly, after S2.CODE, the next statement of S will be executed.
Syntax Directed Translation for "If E then S1 else S2."
|𝐒 → 𝐢𝐟 𝐄 𝐭𝐡𝐞𝐧 𝐒𝟏 𝐞𝐥𝐬𝐞 𝐒𝟐||E. TRUE = newlabel;
E. FALSE = newlabel;
S1. NEXT = S. NEXT;
S2. NEXT = S. NEXT;
S. CODE = E. CODE | | GEN (E. TRUE '− ') | | S1. CODE
GEN(goto S. NEXT) | |
GEN (E. FALSE −) | | S2. CODE
- 𝐒 → 𝐰𝐡𝐢𝐥𝐞 𝐄 𝐝𝐨 𝐒𝟏
Another important control statement is while E do S1, i.e., statement S1 will be executed till Expression E is true. Control will arrive out of the loop as the expression E will become false.
A Label S. BEGIN is created which points to the first instruction for E. Label E. TRUE is attached with the first instruction for S1. If E is true, control will jump to the label E. TRUE & S1. CODE will be executed. If E is false, control will jump to E. FALSE. After S1. CODE, again control will jump to S. BEGIN, which will again check E. CODE for true or false.
∴ S1. NEXT = S. BEGIN
If E. CODE is false, control will jump to E. FALSE, which causes the next statement after S to be executed.
∴ E. FALSE = S. NEXT
Syntax Directed Translation for " 𝐒 → 𝐰𝐡𝐢𝐥𝐞 𝐄 𝐝𝐨 𝐒𝟏 "
|𝐒 → 𝐰𝐡𝐢𝐥𝐞 𝐄 𝐝𝐨 𝐒𝟏||S. BEGIN = newlabel;
E. TRUE = newlabel;
E. FALSE = S. NEXT;
S1. NEXT = S. BEGIN;
S. CODE = GEN(S. BEGIN '− ') | |
E. CODE | | GEN(E. TRUE '− ')| |
S1. CODE | | GEN('goto' S. BEGIN)
- What is Backpatching of Boolean Expressions and Control Statements in compiler design?
- What is syntax-directed translation schemes in compiler design?
- What is assignment statements with Integer types in compiler design?
- What is Compiler Design?
- What is Control Statements?
- What is Design of Lexical Analysis in Compiler Design?
- What is minimizing of DFA in compiler design?
- What is Chomsky Hierarchy in compiler design?
- What is error handling in compiler design?
- What is Input Buffering in Compiler Design?
- What is Finite Automata in Compiler Design?
- What is the Representation of DFA in compiler design?
- What is Components of LR Parsers in compiler design?
- What is types of LR Parser in compiler design?
- What is techniques of storage allocation in compiler design?