0% found this document useful (0 votes)
4 views

CD unit-2

The document discusses the role of parsers in compilers, explaining their function in processing input code and checking syntactic structure based on grammar. It outlines different types of parsers, including top-down and bottom-up approaches, and emphasizes the importance of grammar in defining language rules, guiding parsing techniques, and handling errors. Additionally, it covers error recovery strategies in predictive parsing, highlighting various methods to manage unexpected tokens and syntax errors.

Uploaded by

bhemanthk0911
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
4 views

CD unit-2

The document discusses the role of parsers in compilers, explaining their function in processing input code and checking syntactic structure based on grammar. It outlines different types of parsers, including top-down and bottom-up approaches, and emphasizes the importance of grammar in defining language rules, guiding parsing techniques, and handling errors. Additionally, it covers error recovery strategies in predictive parsing, highlighting various methods to manage unexpected tokens and syntax errors.

Uploaded by

bhemanthk0911
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 13
7 wat B wan vambfguows ypuorninan « wnt 0. Poocdore eo elmSnat th dome “ult vam ami + 2% ambiguous wararrmar 8. a ygramman thet Con Gorwat Cat cadet core stitog buch wa usoey Shed Hore iu mleGle vat Parse Dus fon th Some aa The ambsquly, wan kad to Lonfrston uh wnelnst nog eee CaO vant sour Cour asus GN Tponsurs wohou a wntouve cece. 20D cation B Essrthule Sar sa A/R AD aA/e B> aBle 's Ss ASAA DOE 2 ssBraBsae Poweedyrs 40. ELimbrat Amble Thou ore varfous tedntques uo fmfrate amblgutiyy Gn A gtamman. Ere Common aa 2s no Cerca grammon uo wnfava va Umfaure Grin Qretotton wabensags ARaIS oo ayTcod Pracne. to Lorde cantly By « (0 Debovit hy ahh eee jes Production Pte ae Prassarge the h eeacnarman he Gone a wriquse itn pretation fs wach HG. 3 Apply Precdimcr or asocnd oh thy 2 susolve abi yuth 40 Test de mod fad (grommen cdo Gum no ambgutt vumraihs: Exam@le - eUsntnate Amby gulhy Ww cam Gatimatt _ @cpresefor Avamrnan + Evedse lerele [fa fd +fd +%d CEdttd#Pa ae fd +€bd ey) 22 6+0lTToT FIFE Fo Ce) lfa Cd + Fax fol Ta + Cl * (al) ESE st TOTX P/F Fe cey/iaDefinition of a Parser A parser is a component of a compiler that processes input code and checks its syntactic structure based on a given grammar. It takes a sequence of tokens generated by the lexical analyzer and builds a parse tree by following the rules of the language grammar. Parsers are broadly classified in L. Top-Down Parsers: Construct parse trees from the root (start symbol) to leaves (e.g,, Recursive Descent, LL(1)). 2. Bottom-Up Parsers: Construct parse trees from leaves to root (e.g,, Shift-Reduce, LR parsers).Role of Grammar in Parser Construction Grammar defines the syntax of a programming language and serves as a blueprint for parser construction. It plays a crucial role in: 1. Defining Language Rules: Specifies valid sequences of tokens. 2. Guiding Parsing Techniques: Determines whether a top-down (LL) or bottom-up (LR) approach is used. 3. Disambiguating Expressions: Helps resolve operator precedence and associativity. 4, Generating Parsing Tables: Predictive parsers (LL(1)) and LR parsers use grammar to build parsing tables. 5. Handling Errors: Helps design error recovery mechanisms.Rewriting the grammar: | mathematica Boe le oete le T+FT TotrT le Fo+(€) | 4d for + Ce) [ad be pans aeStep 2: Compute FIRST and FOLLOW Sets FIRST Sets FIRST(E) = FIRST(T) = {(, id} FIRST(E') = { +, €} FIRST(T) = FIRST(F) = { (, id} *FIRST(T) = (, €} FIRST(F) = {(, id} FOLLOW Sets FOLLOW(E) = {$,)} FOLLOW(E') = {$,)} FOLLOW(T) = {+,$,)} FOLLOW(T') = {+,$,)} FOLLOW(F) = (*, +,$,)}Step 3: Construct the Predictive Parsing Table | Non-Terminal E a Fe F2(e) TFT Faia esate rrTypes of Top-Down Parsing Top-down parsing is a parsing technique that starts from the start symbol of the grammar and attempts to derive the input string by applying production rules. It constructs the parse tree from root to leaves. ‘Types of Top-Down Parsing Top-down parsers can be classified into the following types 1. Recursive Descent Parsing I 2. Backtracking Parser | 3, Predictive Parsing (LL(1) Parsing) 4. Non-Recursive Predictive Parsing (Table-Driven) | i. Backtracking Parser * Tries all possible rules until a match is found, + Itrewinds (backtracks) when a wrong production rule is chosen. + Example: plaintext OCopy v Edit SsaBblb Beeld Parsing ac, the parser fist tries a8 » a d (als), then backtracks to $+ ab > ac (succeeds). © Drawback: Inefficient for large grammars due to excessive backtracking.Predictive Parsing (LL(1) Parsing) ‘+ Eliminates backtracking by using lookahead ( ke1 token), * Requires left recursion removal and left factoring for implementation + Uses a parsing table instead of recursion for efficiency. + Example Grammar: plaintext OCopy v edit este Baste de Teer Tererde Fe(e) iid This grammar can be parsed without backtracking, * Advantage: Efficient for parsing simple grammars.4. Non-Recursive Predictive Parsing (Table-Driven) + Uses a stack and a predictive parsing table, * The parser reads the input left to right and decides which rule to apply using the parsing table. * Faster and more efficient than recursive. methods. + Example: * Predictive parsing table is constructed using FIRST and FOLLOW sets. * The stack and parsing table guide parsing decisions without recursion, Error Recovery in Predictive Parsing Errors occur when: 1. An unexpected token is encountered, 2. Amissing token is required for correct parsing. 3. The input does not match any production.Error Recovery Strategies 1. Panic-Mode Recovery ‘+ Skips tokens until a synchronization token (e.g, ; or })is found. + Simple but may discard large portions of input. 2. Phrase-Level Recovery * Replaces or inserts missing tokens based on parsing table suggestions. + Example: If ) is missing in {€,, the parser may insert ). 3. Error Productions == + Adds extra rules inthe grammar to recognize common Syntax errors. + Example: plaintext O Copy» Edit E+E+E|E-£ | error + If an invalid operator is found, the parser produces error instead of failing, 4, Global Error Correction ‘+ Modifies the input minimally to make it vali, ‘+ Computationally expensive but used in robust compilers.

You might also like