

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
while loop using LR(1) in which steps to create it are demonstrated.
Typology: Summaries
1 / 2
This page cannot be seen from the preview
Don't miss anything!


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.
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.
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
FIRST(S) = { WHILE } FOLLOW(S) = { $ } FIRST(C) = { id, num } FOLLOW(C) = { ) }
The LR(1) item sets are constructed using closure and goto operations. Each state contains items with lookahead symbols ensuring deterministic parsing.
The DFA consists of states I0, I1, I2, ... representing canonical LR(1) item sets with transitions on terminals and non-terminals.
ACTION table defines shift, reduce, and accept actions. GOTO table handles transitions for non-terminals. Together they guide the LR(1) parsing process.
Input: while ( id < num ) id = num ; Stack and input are processed step-by-step using ACTION and GOTO tables until ACCEPT state is reached.
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.