while loop using LR(1), Summaries of Computer science

while loop using LR(1) in which steps to create it are demonstrated.

Typology: Summaries

2025/2026

Uploaded on 01/04/2026

why-flies
why-flies 🇮🇳

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
While Loop using LR(1) Parser
This document presents the complete design and analysis of a WHILE loop construct using an
LR(1) parser as part of the Principles of Compiler Design (POCD) assignment.
1. Introduction
A parser is a core component of a compiler that checks the syntactic structure of source code.
LR(1) parsers are powerful bottom-up parsers that use one lookahead symbol and deterministic
parsing tables.
2. Grammar for While Loop
S while_stmt while_stmt WHILE ( C ) S | WHILE ( C ) { SL } SL SL S | S C E relop E
relop < | > | <= | >= | == | != E E + T | T T T * F | F F id | num
3. Augmented Grammar
S' S
4. FIRST and FOLLOW Sets
FIRST(S) = { WHILE } FOLLOW(S) = { $ } FIRST(C) = { id, num } FOLLOW(C) = { ) }
5. Canonical LR(1) Items
The LR(1) item sets are constructed using closure and goto operations. Each state contains items
with lookahead symbols ensuring deterministic parsing.
6. LR(1) DFA
The DFA consists of states I0, I1, I2, ... representing canonical LR(1) item sets with transitions on
terminals and non-terminals.
7. ACTION and GOTO Table
ACTION table defines shift, reduce, and accept actions. GOTO table handles transitions for
non-terminals. Together they guide the LR(1) parsing process.
8. Parsing Example
Input: while ( id < num ) id = num ; Stack and input are processed step-by-step using ACTION and
GOTO tables until ACCEPT state is reached.
9. Conclusion
pf2

Partial preview of the text

Download while loop using LR(1) and more Summaries Computer science in PDF only on Docsity!

While Loop using LR(1) Parser

This document presents the complete design and analysis of a WHILE loop construct using an LR(1) parser as part of the Principles of Compiler Design (POCD) assignment.

1. Introduction

A parser is a core component of a compiler that checks the syntactic structure of source code. LR(1) parsers are powerful bottom-up parsers that use one lookahead symbol and deterministic parsing tables.

2. Grammar for While Loop

S → while_stmt while_stmt → WHILE ( C ) S | WHILE ( C ) { SL } SL → SL S | S C → E relop E relop → < | > | <= | >= | == | != E → E + T | T T → T * F | F F → id | num

3. Augmented Grammar

S' → S

4. FIRST and FOLLOW Sets

FIRST(S) = { WHILE } FOLLOW(S) = { $ } FIRST(C) = { id, num } FOLLOW(C) = { ) }

5. Canonical LR(1) Items

The LR(1) item sets are constructed using closure and goto operations. Each state contains items with lookahead symbols ensuring deterministic parsing.

6. LR(1) DFA

The DFA consists of states I0, I1, I2, ... representing canonical LR(1) item sets with transitions on terminals and non-terminals.

7. ACTION and GOTO Table

ACTION table defines shift, reduce, and accept actions. GOTO table handles transitions for non-terminals. Together they guide the LR(1) parsing process.

8. Parsing Example

Input: while ( id < num ) id = num ; Stack and input are processed step-by-step using ACTION and GOTO tables until ACCEPT state is reached.

9. Conclusion

The LR(1) parser successfully parses the while loop construct using a well-defined grammar and deterministic parsing tables, making it suitable for real compiler implementations.