You are on page 1of 57
are ILCs oe EST Sa oO) CC) ee Cod) eRe WaT me Ren oe Reus eur SWS Cao oun nr Deut ame inderstand. The parser takes input from a Pee eaten ‘them down, and organizes them into a data Sue nai Pe OM eee a renee Reon Pere el et eae Cee es ec Cire rr The parser's role in compiler design is to: Analyze the syntax of a program, Verify that the Re Uae oc Produce a parse tree, and Generate intermediate oem De eee Ele Deateet Mc} (Cnty cer FY eae Re el a sn a a eS Ringe Sete eee esas cel] Peo Ae ce eae rere) Ce eae eon a eT eeu eget te Cue) pe ue ete Ee Un cu ei kaon Cee en eee es Parsing Set 1 (Introduction, Ambiguity ond Pareers) Seen aetna te meet Be Rae ‘THE ROLE OF PARSER ‘The parser or syntactle analyzer obtains a string of tokens from the lexical analyzer and verifies that the string can be generated by dhe grammar for the source language. 1 reports any syntax errors inthe program. It also recovers from commonly accurring errars so that i ‘can continue processing its input. =(2 Ee" ein HE erst node mt Fig. 2.1 Position of parser In comp Funetions of the parser: 2ult veri the str onthe grammar 2.tt constructs the parse tree. Slt reports the errors 4steperforms error recovery re generated by the tokens based Parser cannot detest exrors such 3 1.Wariablere-dectration 2.\ariable initialization before use 5, Datatype mismatch for an operation. The ahove Issues are handled by Semantic Analysis hase ‘Syntax error handling: Programs can contain errors at many diferent levels, For example: 4. Lexeal, such as misspe 2, Syntactic, such as an arithmetle expression ith unbalanced parentheses 3, Semantic, sueh as an operator applied (0 an Incompatible operand, 4. Logical, such as an infinitely recursive cal ‘an Wdeniter, Keyword of Functions of error handler 1.1 should report the presence of errors clearly and accurately 2.1e should recover from each error quickly enough to be able ro detect subsequent errors. 2. ould not significantly sloe down the processing of correct programs. Error recovery strategies The diffrent strategie that a parse uses to recover from a syntactic ervor are s.Pante mode 2.Phrage level 3.Error produetions 4.Global correction Panie mode recovery: fon discovering an error, the parser discards input symbols one at a time until a synchronizing token ts found. The synchronizing tokens are usually delimiters, such as Bee ees semicolon or end. thas the advantage of simplichy and does not go nto an infinite loop, When multiple errors in tn vsefl sme statement are rare, this method is quite Phrase level recovery: (on discovering an error, the parser performs local correction on the remaining input that allows it to ‘ontinte, Example: insert a missing semicolon or delete Error productions: The parser Is constructed using augmented ‘grammar with ervor productions. Iran error prostucton Is used by the parser, appropriate error diagnostics can bbe generated to indleate the error recognized by the inp. Global correction: Given an incorrect input string x and grammar G, ‘certain algorithms can he used to nia parse tree for & sing ¥, sich that the number of insertions, deletions and changes of tokens Is as stall a possible, However, ‘these metho are in general too costly in terms of time and space ‘nots amo Onion: Awe Tene ot Lngneing A nce nto ans eson: Sota Ao nd aT BER REAS a EY Mmee oyseengtinenta naetnenats ueatgspen OBS Canpveuens gageaneipy cP ‘he toviak analyzer and the parser producce the paror tree forthe tokene while ehecling the sveam of tokens agalnst the syntax ees Role of Parser In the syetax analysis phese, a compiler veries whether oF not the tokens generates by the lexical analyzer ore grouped ccording to the syntactic ules ofthe Language. This is dane by parser. The pats obtains @ string of tokens from the lescal analyzer and verifies that the sting can be the ‘grammar fr the source language, It detects and reports any syntax etors and produces porse ‘tee from which niermekate code ean te geneted eee ‘Types of Parsing ‘The parsing is ded into two types. whieh ae a allows 1. Top-down Parsing 2. Bottom-up Parsing “Top-Down Parsing ‘Top-down passing attempts to bull the porse tree fom the roar nae to the Let nec. The top: ‘down parser wil tart from the start aymol and proceed tothe ang. It follows te leftmost Cervaion, i temas derivation, the leftmost ron-termina in eee sententit sways chosen, 1. Recursive parsing or predictive parsing ate other names for top-down parsing, 2, Aparse ees bul for an input string using bottom-up parsing transformed inte the star sb. 3, When parsing is dane top-down, the input eymibel ‘The top-down parsing is further categarized 2 flows 4. With Backtracking: + Brute Force Technique 2, Without Beetracking + Recursive Descent Parsing + Presietve Parsing o Non Recursive Parsing or LL) Parsing o Table Driver Potsng Bottom-up Parsing puting ll reduce the input stdg tothe start symbol. It uaces the rightmost derivation of the a (O} 4 BEERS a GE tote eyneieginenates Hatten pentgsn OS Cannes Egllegcon tion CP Botiom-up_ensing builds the parse tre from the leaf node to the rot node, The bottom patsing ll reduce the input stg t the start symbol. It traces the rightmost derivation of the Siving la reverse. Bottom-up parsers ae alse known as shit-educe parsars 1. Sit-reduce parsing is another name for botam-up parsing 2.Aparse ees bit for an inpt sting using betom-up parsing 3. Whee parsing fam the bottom up, the process becins with the Input symbol and builds the parse vee up othe start symbol by reversing the rightmost sting der Generally bottom vp parting estegorized nt the following Spee: 2. UR parsing/Shit Reuce Parsing Shit reduce Parsing fs process of parsing @ string to obtain the start symbol of tho grammar. +180 + S.au) Las +o 2, Operator Precedence Parsing: The grammar defined using operator grammar is known as perator precedence passing. n operator precedence patsng there should be no null preduction and two non terminals shuld nt be aejacent to eachother. Fags QE What Parsing? Parsing, syn analysis or syntactic anelyss ie the process of examining osrng of symbele that aohers tothe ries of 3 formal grammar. whether tay ben natura language, computer longuages, er dat srctues. .Q2: What i the need for Top-down parsing? Answer: Top-down parsing can be thought of as an offort to locate the input steams leftmost eviuations by leaking fer parse toes using 2 top-down expansion of the provided frm jrammar rules. By encompassing all possible right-hand ses of grammar sues, inclusive hoie fe sed ta dal with abit. Frontend/Backend/Full Steck > Build Projects > Keep Applying to Jobs ‘ne why go anywhere else when our DSA to Development Coding Guide helps you do tis ina ingle program! Apply now to our DSA te Develooment Prageam ond aur counselors wil connect with you for further guicance 8 suport an Susnestimorovement < Previous Nxt > Classication of Contest Free Grammars ‘lasiticatlon of Top Down Parsers Share yur tots in te comments Similar Reads oftancabstnenTop doonparingand cto up of area WPA ea Ce cee ficou seme ere uMe cae e ce aE Riese eine A MNT Run Place teal ue ee gene ad Pee cnr tees Cae Cre Grammar Is made up of two elements: ee tek Into a syntax tree. Only syntactically valid eed a aL Seen ee ae eae eee eget sa nie atts Formal grammar is a set of rules that Identifies correct or incorrect strings of tokens in a ee eke caer es must eg eed ea tee cM pis Co a t ee ee Ener manent si ee ey || ‘The productions of a grammar specify the Se es terminals can be combined to form strings. Each production consists of @ non- Dee ee neta! enna BT WBA ES a Ef tots Eyneieinenaes Sate pentgsn OMS Cannes gllegcan tion CP LOQUCUON 10 Grammar in ineory oF Lompurauen Prerequisite ~ Theory of Campion ‘Grammar Iti afte set of formal roles for genera 19 syntactically correct sentences or meaningful comect ‘Grammars besiealycomoosed of two base elements Terminal Symbols — Terminal symbol are those whlch are the components ofthe sentences generated using a Non-Terminal Symbols ‘Non-Terminal Symbols are those symbols which takepart inthe generation of the sentence but are not the component ofthe sentence. Non Terminal Symbols ae alro called ni Symbols and Variables. These symoos are represented using a capita eter ike A. B.C, ete Formal Defiaition of Grammar ‘Any Granear can be represented by 4 tuples —<1.7,P, > 1 N= Finite Non-Empty Set of Non-Terminat Symbols + P= Finte Non-Empty Set of Production Rules + S=Start Syinbot (Sb rom where we start prodicing aur sentences or strings) Production Rulor | produton or production eule in compute selene isa rewrite cule speiying 9 symbol substitution that can be recursively performed to generate neve symbol sequences, ti ofthe forma Pushers ain Non-Terminal Symbol suhich can ae replaced by Bwhich ea ettng of Teaminal Symbol or Non Terminal Smbots Example-1: Consider Grammar G1 = 2N.T, PS» T= (2,8) 50t of terminal symbols P= (hota, ARb,Acoa, Arba } ser oF alt production rutes =) AStart Syabol ‘Ac the start symbol S then wo cn produce Aa, Ab aunhich en further produce tings whore ‘scan be replaced bythe Strings mentioned in he procucton rues anc hence this grammar can be used to proce stings of the form ab). Derivation OF Stings Asa fusing production rule 2 or ‘Aevaa using production rule 1 Aasrba using production rule 4 of Avan using produetion rule 1 Aacokta fusing production rule 1 Bela eae a Fof=tees} (0) fe =1-1 «cK 0) (0) 1 a a Error Handling in Compiler Design 1 the Error Handling process ate to detect exch error, report te the moke some recovery stateay and implemeat them tonal the error. During this whole process Functions of Ear Handler: or Detection + Error Report + Error Recovery Error handler-trror Detection'Errer ReportsErrar Recovery {an Errors the blank entries in the symiol able the pareer ean handle ane continue top he input Although he ps ‘esponible for checking fo evors,eors may accu at various stages ef the compilation process mostly lle. run-time and compile-time err: 1. Loge errrs occur when programs operate incorrectly but do rot terminate abaormll or ram and usually lack ace ding the execution af @ memory to run an appletion ar a memory conf with anather program and logical ear example ofthis, Loge eros occur when executed cade does nat preduce te expect Logi rrore are hoes handled by metioleus program debugging '3. Compile-time errors rise at complete, before the execatlon ofthe program. Syntax error rviszng flo roferenco that prevents the program fom sucsaesflly commiting isan example of Classteaion of Compite-time eror— doe mizepolngs of eens, keywords or operator 2. Lovieat-Thie time, before the execution of the program. Syntax error or missing file reference that prevents the program from successfully compiling is an example of this. Classification of Compile-time error — 1. Lexical : This includes misspellings of identifiers, keywords or operators 2. Syntactical : a missing semicolon or BRC ea SMe (2) iiolkets2 Mle) aa Classification of Compite-tine ever ~ 1. Lexie This neues mistpetings of eentsirs, keywords oF operators 2 Symtacica:2 mising semicolon or unbalanced parenthesis [3 Semantiea:Incompatae value assignment or type miematehes between operator and operand 4 Lagisl code not reachable, nfnte loop Finding eror or reporting on error Viabie-pafie isthe property ofa parser that allows caty etectlon of syntax errs + Goal detection ofan eror a soon as posible without further consuming uinnecsEESry input + How: detec an ero as soon asthe prix ofthe input dees rot match a prefix of any sting In the language. Example: for ths wil report an eror a for having two semicolons inside Braces. Error Recovery — compilation, There are some common recovery methods that areas follows, We areay discuss the eros. Now. e's try to understand the recovery of erors in every phase of the compitr. 4. Panic mode recovery ‘This te easiest way of error-racovory an alsa provets the parcorfrom developing init loops while recovering error. The parser clsards the put symbol ane a atime unt one ofthe lsignates tke end smicten st of synchronizing tokens (oe typically the stnernentot expression terminators is found, This is edequote when the presence of multiple ers in the same statement irae. Example: Consider the eroncous expression 1 ++ 2) + 3. Panie-mode recovery ‘Skip shead to the next inter and then continue. zon: use the special tenminal errr te describe how much input te shp. » Ny rea E-sint|£46|(E) Jerror int|(errar) 2. Phase level recovery ‘hen an err is scovered, the parser performs local correction onthe remaining input Ifa parser ‘encounters an erer it males the necbesary corrections on te reining input eo that tho parece ‘can continue to parse terest ete statement. You con correct te ero by deleting extra ing semicclons, To prevent -somietons tepasing commer with semieslons,o eintrducng mi ‘ng in an infinite nap ding the cretion, ipa care shouldbe taken. Whenever any pete fund athe realang input, its replaced with SFM sting. a this way, the parser can connue to Peas Si. GE eee ye 2. Phase eve recovery ion an oxarieecovere. the parser perform local corection onthe remsining input Ifa parser egianenal Hateanenals Spanien COME Comptes callagcandSeipn Che encounters an exex it makes the necessary coreections on te remaining Input so tha the paeser can continue to parse the ret ofthe statement. You can correct the err by deleting extra semicolons, replacing commas with samicelons,o enteducng missing semicolons, To prevent {going in an finite Loop during the caret 1 care shouldbe taker. Whenever any prefs found in te remaining inp te replaced with some string. In this ny, the parser can continue 0 2. Eror productions: These ofthe eror production method canbe incorporated he useris aware of commen lates that ate encountered in grammar in conjunction with ors that produce eroncous Wen tis suse rer messages canbe generated curing the parsing process. andthe paring ean continue. Example: witeSxinstead of 4. Global corroction inorder 1 recover rom eroneous input the parser analyzes the whole program and test ext the closest mate frit, which serar-fee. The elosest match sone that does nat do many insertions, deletions and changes of tokens, This methas is na racial due ot igh time and space complexity ‘Advantages of Error Hanating in Compiler Desig: {LRoburtnes: Mistake deaing with improves the stent ofthe comple by permiting it desk sith art recuperate from ciferent sort of blunders smoathiy. Ths guarantees that even within the ight of blunders, the compiler can keep honing the information program and give significant mate messages, 2étror Location: By consolidating under taking care ef components 2 compiler can dstingush and recognize mistakes in the coutce code. This incorporates syntactic stakes, semantic Hunde, type blunders, sd otner potential issues that might make the program act starting er produce crroneous rel ‘3.rror revealing: Compiler mistake taking care of works with vinbe blunder ansviring to the lien or software engineer. creates engaging blunder messages that asst developers itn understanding the nate ad ares of the mistake. empowering them o effectively fix theissues Clear and exact mltske mescages save designers ignfiant tne in the taubleshooting system ‘AE ror recuperation: stake dcaling with permits the compiler to recuperate from blunders and proceed with the aggregation cele whenever he situation allow. Thi is secomplihe through lferent methods ike blunder adjustment, mistake synchvonizallon and resynchronlzatin. The compiler endeavors to reroes the blunders and continuse with ascomblage, keeping the whole interaction rom being ended unexpected, Sncremental gathering: Wisake taking are of emposters gradual aggregation, where a complet contain blunders. This elomentis especinlly helpful for enormous scape projects, a permits engineers to test and investigate expt modules without recompiling the wrote codebase ‘Productivity improvement Wt estimate mistake aking cae of, the compiler diminishes the ‘ime and exertion spent an ueubleshooting and blunder tng. By giving exact mstake messages and supporting bluncer ceeupetation, it assists programmes with rapely recognizing and resolve ‘sues, prompting futher developed efiiency and quicker advancement eyes 7Lenguage tum of event: Mistake taking eve of fundamental part of language plan and advancement. By consolidating mistake dealing with systems inthe comple. language feshioners can characterize the normal blunder eonchct en authorize expt standards and imperatives. Thi ‘ads tothe general dependability and consistency ofthe language, querantesing that developers ‘tick tothe expected lization designs Distevantages of error handing in compiler design: Increased complexity Error handling in compiler design can significa increase the complet of ‘he comple. The can make the compiler mors challenging o develop tet, 3d maintain, The mare complex te eros handling mechanisn the more felt becomes to ensure that fs working comely arto find apd fix ears, Reduced performance: vor hancling In comple design can also pot he performance ofthe comple. This especially tue rrechaniem is time-consuming and CptagSpien OMS Canptretnane ighllgcantoim Ce Error detection and Recovery in Compiler In thie phace of compilation, al possiie errors made by the Usot ate detocte snd repertod to the User in form af ewor messages. This precess of ocating erors and reporting them to users Is called ‘he Error Handing proces. + Detection + Reporting + Rocovory Classification of Errors Errors Compile time Runtime Lexical phase || Syntactic ‘Semantic errors || phase errors errors CCompite-time errors ‘Compile-time ears ar of hve types: Wide Range Of Courses Wide Range Of Courses @ MancAmritear open > Lonel phase rors ‘These errors ar detacted during the esca nalysie phase. Typical lexical errs are + Exceeding length of lentifer or numeric constants 1 The pearance of illegal characters + Unmatched string vanple 1: printf("Geeksforgecks*):$ This is 2 levical error since an illegal character § appenrs at the end of vanple 2 : This 4s 2 coment */ This is an lexical error since end of coment is present but beginning 4 not Evvr recovery fo lexiealerors ani Mode Recovery +n this method, successive character om the input are removed ane at atime una designated Set of synchronizing tokens i found. Sneteonzing tokens oe delimiters such 9: or} + Tae advantages that is easy to implement and guarantees nt to go into an nile Loop sektonal crore ‘Thoeo errors ar dotted dring the eyntax analyse hace. Typeal ayntax errors ae + Errrsin structure (} TERA a 2 GE tose cynetntnentes Beets Ope eSpiem OOS Canperetnoneiglegcan tn ‘Those evors are detected during the syntax anayss phase. Typeal syntax errors ae: + Errorsin sucture + Missing operator 1 Misspettes keywores + Unbalanced parenthesis Example + sxich(ch) ‘ ‘The keyword switch is acorecty writen asa swich, Hence, an “Unidentified Keyword/éentiir” Error recovery for syntacte phase error: 2. Ponic Mode Recovery + in this methoe, successive characters rm the input are removed one at a tine unt adesienated et of =ynchronzing tokens i found. Synchronizing tokens are deli-mstars euch as; or} + Tue advantages tha fs easy to implement and querantees nt to go into an ite to +The ciaadvantago fe that considerable amount of input iz skipped without checking i for ardlonat errs 2, Statement Mode recovery ‘in this metnod, when » parser encounters an eo. it performs the necessory cretion on the remaining input otha tho ret of te input statement allows the pareer to parse shes, ‘The conection can be deletion of exta semicolons, replacing the comnms with semicolons, incorting 9 missing semicolon, + nie performing correction, utmost cre shouldbe taken for not going fan iit oop, + A endvantage is that i ins & cifult to handle stutions where the scual error eecrred before pointing of detection. 23. ror production + Hf 2 user nas inewedge of common errs that can be encountered then, these errors can be incorporsed by sugmenting the grammar with eror procuetone that generate erroneous + i this is ured then, during parsing spproprine errr messages can be generated and parsing can + The sisodvantage thats ficult ko maintain, 4. Global Corection +The parser examines the whole program and ties to Find ut the closest match for which error fee +The closest match progrem has tess numberof insertions, deletions. and changes af tokens to + Due to high time and space complex. his methods not implemented practi, ‘Semantic erors These errors are detected during the semante analysis phate. Typical semante ero are + Incompatbietype of operands + Not matching of actual arguments with a formal ene Example © ant (101, 6 te generates a semantic error because of an incompatible type ofa and, Error recovory for Semantic errore BT WMO EF tee geen Satenena What ts Context-Free Grammar? eSpien Oc Context Free Grammar is formal grammar, the syntax or structure ef 2 format language can be eseribed using context-free grammer (CF), 0 type of formal grammer. The gransmar has four tuples (VIPS) = If 4s the collection of variables or nontersina! Is ie 9 sot of torninale symbols = 1E As the production rules that consist of both terminals and nonterainals. 21g is the Starting symbol ‘A grammars sad ta be the Contex-fee grammar if every prucion Inthe form of| 6 > (WUT, where GeV + And the lefsnand sce of the G.herein the example-can only bea Variable. cana bea + onthe rig nana side here scan be a Variable or Terminal or beth combi jon of Variable and Teri, ‘Above oqustionetatce that every production which contain any combination of tho V" variable oF “T terminalis sid to bea conten fee ramet. For example the grammar A= {5,98 PS) having production ‘Wide Range Of Courses Wide Range Of Courses Maacamrtsar Open > MAACAmtsar (pen > + Hore Si the starting yo. + {a0 are the terminols generally represented by small characters + Picvarible along with 5 so 35 s-> bse so5ba is rot + CFG az on the Left-hand cide there is a variable which does not follow the CFG rule In the computer since fea, conten ree grammars are frequently used, especialy inthe areas of formal Language theory, compiler development, and natura language processing, tis also sed fr explaining the syntax of programming languages and ater formal Languages. LUmitations of Context-Free Grammar ‘Apart rom all the uses an inpertance of Context-Free Gamma in the Comper design and the CCompster scence fle, there are rome limitations thet are adkressed, thot i CFOs are less expressive, and nether English nor programming language ean be expressed using Context-Free Grammar. Context-Free Grammar can be ambiguous means we can generate mutta | 2 tees of ‘the samo input. For somo grammer, Contoxt-Fee Grammar ean be less oficent becauce of the (CGS era reporting system is nt that price tat can give more detailed ror meseagoe snd information exponential time comply And the less precise eror reporting. Here's a compete roscmep fr you to become a developer: Learn DSA -> Master Frontend/Backend/Fll Stack > Bull Projects > Keep Applying to Jobs sve why go anywhere else when our DSA to Developatent Covina Guide helos vou do tis in 12:30 AM | 21.9KB/s £ WRITING A GRAMMAR A grammar consists of a mumber of productions ach production has an abstract symbol called a onterminal as is lef-hand side, anda sequence of one ‘or mote nontermainal and erminal symbols as is right ‘hand side. For each grammar, the terminal symbols are ‘drawn from a specified alphabet starting from a sentence consisting of a single ‘istingushed nonterrinal, called the goat symbol, a given contextfree grammar specifies a language, namely, the set of possible sequences of terminal ‘symbols that ean result from repeatedly replacing any nonterminal inthe sequence with a righthand side of ‘8 production for whieh the nonterminal the left-hand. side, REGULAR EXPRESSION Is used (o deseribe the tokens of programming languages, Tes used to check whether the given input 1s vale or not wsingtransition d ‘The transitlon diagram has set of states and edges. has no start symbol. 1 Is useful for describing the structure of texieat constructs such asidentifiers, constants, keywards, and so forth ‘CONTEXT-FREE GRAMMAR W consists ofa quadruple where $ = start symbol, P+ production, T terminal, V ~ variable or non- terminal Irs used to check whether the given input is val or not using derivation, "he context-free grammar has tof productions has sare symbol. parentheses, matching begin-ena’s and soon, ‘There are four categories in writing a grammar 1. Regular Expression Vs Context Free Grammar 2. Eliminating ambiguowe grammar. 3, Fliminating leterecursion 4. Leftfactoring Each parsing method ean handle grammare only of a certain fort hence, de inidal grammar may have to be rewritten to make it parsable, Reasons for using the regular expression to define Bed Reasons for using the regular expresston to define ‘the lexical syntax ofa language (othe lexical rules of a language are simple andl RE used to describe them.tiRegular expressions provide @ more concise and easier to understand notation {oxOtokens than grammars © ficient texical analyzers can be constructed ‘automatically from RE than from grammars, © Separating the syntactic structure of a language into lesieal and nonlesial parts provides a convenient way ‘of modularizing the Mont end into two manageaDle- sized components. Eliminating ambiguity: Ambiguity ofthe grammar that produces more than one parse tee for lefunost or rightmost derivation can be ‘liminated by re-writing the grammar, consider this example, s sunt Mexprthenstmt| iexprthensirelsance|other “This grammar is ombiguous since the string FEA then AC £2 then S1 else 82 has the following two parse trees {or letimost derivation (Fig, 23) ‘To eliminate ambiguity. the fllowing grammar may be vse stint matched | anmatchedstmt st ‘maiched -Atexprstmithenmatchedelsematchedont stmt other ‘unmatched ~ifexprsthenmistne |ifexprehenmatchedelseurmatchedimt stmt Eliminating Left Reeurston: A grammar Is said to be left recusivefithasanon- terminal Asch that there is a derivation A=>Aa for some string a. Top-down parsing methods cannot handle lem-recursive grammars. Hence, lft recursion canbe eliminated as follows MC there Is aaA produe|Pitioncan Abe replaced with asa Ba" Awan |e ‘without changing the st of strings derivable from A. Algorithm to eliminate left recursion: arrange the non terminals in some order Ay A+ 1.fré= 1 tondo begin forf= 1 to-1 dothegin replace each production of the form ay a y By the productions Ay + 61 VI &y |= | Bey where Aj ~ 51 | 62 | ++ | Seare all the eurrent ay productions; end bliminate the immediate left recursion among the Afproductions end Wea) all the current A productions end eliminate the immediate let recursion among te Ayproductions end 2.3 TWo parse trees for a ambiguous sentence Left factoring: Left factoring i @ grammar transformation that Is Useful for producing a grammar suitable for predictive parsing. When it isnot clear which of fsro alternative productions to use to expand a non-terminal A, we can rewslte the A-productions to defer the decision until we have seen enough of the input to make the right choice ME there is any.-ait|productionzaf,tean_ bea wo ftl28 ‘Consider the grammar, 2S — S818 | 12865 | a Eb Left faetored this grammar becomes SESS" | a SseS [ebb Reais Tenement openings SENS. Classification of Top Down Parsers Parsing sessed nt two categories ie, Top-Down Parsing, and Betiem-Up Parsing, Top-Down Bazing is bated on Left Most Derivation whereas Bottom-Up Parsing is dopendont en Reverso Fight MestDerhaten ‘The process of Top-Down Parsing wtructing the patae tree which stats from the root and goee down to the lee it 4. Top-Down Parsers constructs fam the Grama which fee fom ambiguity. af recrson. 2. Top-Down Parsers use letnest derivation to construct a pase vee. 13. eda nat allow Grammar With Common Prefixes Classification of Top-Down Parsing 4. With Bocktroeking® Brute Force Technique 2, Without Backtracing eae ‘Wide Range Of Courses Wide Range Of Courses Mancamitear Open > MAACAMsten Open > 2. Recursive Descent Parsing 2. Predictive Parsing or Non-Recursve Parsing or LL{2) Parsing oT Recursive Descent Parsing 1 Wherever a Non-terminal spends the fist tne then go with the fat altamative and compare ith the given HP String 2 if matching doesnt oecur then go with the second alternative and cempareit with the gven IP Suing, ‘3. fmatehing snot found again then go with the alterative an 0 0, succor Recursive Descent Parsing so. {Choose any S production, § >X%ow Xk for i= 110) ‘ Tt (X4 $4 0 non-terminal) Call procedure 150) else if ( Xi eauols the current input, increnent inut) Else /* error has occurred, backtrack and try snether possibility */ > ) Lots understand it beter with an example: 50 1A recursive descent parsing program consists of ect of procedure, one for each nontermina, Execution begins with the procedure for th start symbol which halts If ts procedure body scans the entire input sing Non-Recursive Predictive Parsing: Ths typeof parsing doesnot require backtracking, Prealctve poreere con be constructs for LL) grammar, the frst’. stonds fr scanning the input rom ef to cit the second’ tend for etext derivation, and fer sing one np symboklonkahese! at ‘sch step ta make posing setiondeesons LL(1) or Table Driver or Predictive Parser {Lin LLL th First stand for Left Right, an the socend Lstands fr Lof-moet Derivation stands forthe number of Looks Ahead tokens used by the parser while parsing a sentence, LL) parsing is constructed from the grammar which ere ramet recursion, commen profi and ambit '3.LLQ) parser depends an 1 look ahead symbol to predict the production to expand the parse tee 4. This parsers Non-Recursive Construction of LL(S}predictive parsing table For atch production A> arepest the fllowing step: + Ad A> avunder ME, 6] or all in FIRST(@) ‘ IFFIRSTI) contains © ten a4. > @under MIA] for a + Siz of parsing table = (No, of terminals + 1)" avorables Example: ‘Consider the grammar Sle Lest Us eyse Soya @ Lest’ © u->/8. ———____________+@ DTA a CpentagSpien OMS Canperetnane ighllgcanteim Che 5 4 No multiple entries this is LU(1) grammar For any grammar iW has multe entries then tis mot LC) a Example Stes 5 sesie e-b \si] % multiple entries this is not LL(1) grammar Important Notes if grammar contains tet factxing then tcan nt be LL) te-s as) = both productions go in = if grammar contains tet recursion ean not be L(t) feos >ss1 5 5a goes to FIRSTS) = b 5 > goes to b, thus D has 2 entries hence roe LLC) rane grammar is ambiguous then ican notte LL). Every regular grammar need not be L(t) boeauro regular grammar may contain loft factoring tft recursion or ambiguity wo ue( a Frequently Asked Questions Is the ufference between top-down and bottom-up parsing? "The man atfrence betwaen top-down and hatom-p parsing sn he ection in which they constuct parse tres. Top-devn parsers Mt Ham the top ofthe pars ree and BPxP WIPE) OQ % geeksforgeeks.org/ree + oS Qf Recursive Descent Parser Provoquiste—Constastion of LL) Pacing Table, Clasiscation of tap down patcors srsing isthe process to determine whether the start symbol can derive the program or not the Parsing i successful then the program is 2 valid program otherwise the programs iva, “There are generally two types of Parser 1. Top-Down Parser: + In this Pasig technique we expand the start symbol othe whole program. + Recursive Descent ond LL prs are the Top-Down parsers 2, Bottom-Up Ps + In this Parsing technique ve reduce inte progrom to star symbol + Operator Precedence Parser, LR(O) Parser, SLR Pareer, LALR Parser and CLR Paceer are the Bottom-Up parsers. Recursive Descent Parser: Ite kind of Top-Down Parser. A top-down parser builds the parse tee fem the top to devin, stating with te ster non-termina. A Price Parser @ special case of Recursive Det Parser, where na Back Tedking 2y carefully wring a grammar means eliminating left recursion ane factoring from the ‘eauting grammar wil bea grrmmar that can be parsed by a ecurive descent poser. Example Before removing left recursion After removing loft recursion esestiT EosTele Fscenid Tsterte F-S(enis ‘Here ef Easton For Recursive Detcent Parser, we ate golag to write one program fr every vail example! Grammar (} 12:33 AM | 121KB/s Z. Recursive Descent Parser A top-down parser is known as a recursive descent parser that analyses input based on grammar rules using recursive methods. Starting with the grammar's topmost rule, the parser executes subroutines backward for each input symbol that is not a terminal symbol until it reaches one. A parse tree representing the input's structure according to the grammar is the parser's output. Recursive Parser Descent: The process of parsing is frequently employed in language processing and compiler design. It is founded on the idea that a difficult problem can be broken down into simpler problems and then solved recursively. Starting with a_ top-level nonterminal symbol, the parsing process proceeds by recursively expanding nonterminals until it reaches terminal symbols. Repeated descent, the fundamental idea behind parsing is to create a set of parsing functions, each of which corresponds to a_nonterminal grammar symbol. These procedures are responsible for locating and assessing the relevant linguistic components. The top-level function is called first by the parser, which then calls the necessary parsing routines recursively depending SEAS Init Grammar: Defining the grammar of the language to be parsed is the first stage in developing a recursive descent parser. A collection of rules known as grammar define the syntax of a language. Each rule is made up of a series of terminal and nonterminal symbols on the right side and a nonterminal symbol on the left side. ; Wide Range Of Courses cD Take the grammar for a straightforward arithmetic language, for instance: expression == term ( ‘+ term | “term )* term factor number factor (factor |'/' factor )* DAL CEs Expression, term, factor, and number are the grammar's. four nonterminal symbols. An arithmetic expression is defined as one or more terms divided by plus or minus signs by the expression rule, which serves as the foundation for parsing. According to the term rule, a term is defined as one or more factors divided by multiplication or division signs. A number or an expression surrounded in brackets is what the factor rule refers to as a factor. As per the number rule, a number consists of one or more digits. Parsing: The fundamental principle of recursive descent is Writing a collection of recursive functions, one for each nonterminal symbol in the grammar, is the process of parsing. A series of symbols that matches a particular rule must be parsed by each function, which is assigned to a grammar rule. The expression function, which is invoked with the input string, is where the recursive descent parser begins. Depending on whether the symbol is a number or an opening parenthesis, the function analyses the first symbol of the input and chooses which alternative of the term rule to apply. The factor function is used to parse the symbol's value if it is a number. The expression function is used BREE TELL Cee matches a particular rule must be parsed by each function, which is assigned to a grammar rule. The expression function, which is invoked with the input string, is where the recursive descent parser begins. Depending on whether the symbol is a number or an opening parenthesis, the function analyses the first symbol of the input and chooses which alternative of the term rule to apply. The factor function is used to parse the symbol's value if it is a number. The expression function is used recursively to parse the expression inside the parentheses if the symbol is an _ opening parenthesis. The term function is invoked recursively to parse any subsequent multiplication or division signs and factors after the factor or expression function has returned. The expression function determines whether there are any plus or minus signs after the term if the term function returns a value. If so, the term function is invoked once more to parse the subsequent term. Until an error happens or all of the input has been parsed, this procedure continues Say, for instance, that we want to parse the formula 2 + 3 * (4-1) / 2. This is how the recursive descent parser operates: 12:33 AM | 821KB/s Zi. again Calls the term function wi je INDU: Recursive descent parsing has the following benefits: 1. Ease of use: Because recursive descent parsing closely mimics the grammar rules of the language being parsed, it is simple to comprehend and use. 2. Readability: The parsing code is usually set up in a structured and modular way, which makes it easier to read and maintain. 3. Recursive descent parsers can produce descriptive error messages, which make it simpler to find and detect syntax mistakes in the input. 3. Error reporting. 4, Predictability: The predictable behavior of recursive descent parsers makes the parsing process deterministic and clear. Recursive descent parsing, however, also has certain drawbacks: 1. Recursive descent parsers encounter difficulties with left-recursive grammar rules since they can result in unbounded recursion. To effectively handle left recursion, care must be made to avoid it or employ methods like memoization. 2. Recursive descent parsers rely on backtracking when internal alternatives to a grammar rule are unsuccessful. This could result in inefficiencies, especially if the grammar contains a lot of ambiguity or options. 3. Recursive descent parsers frequently adhere to the LL(1) constraint, which requires that they only use one token of lookahead. The grammar's expressiveness is constrained by this restriction because it is unable to handle some ambiguous or __ context-sensitive languages. An outline of the Recursive Descent Parsing BEE EWR EES © Grammar: The first step in parsing a language is to define its grammar. A set of production rules that outline the language's syntactic structure makes up the grammar. Each rule is made up of a series of terminal and nonterminal symbols on the right side and a nonterminal symbol on the left side. © Create parsing functions: For each nonterminal symbol in the grammar, create a parsing function. The task of identifying and parsing the linguistic expressions corresponding to each nonterminal symbol will fall to each function. o Input tokens read: Read the input tokens that came from the tokenizer or lexical analyzer. The IDs, keywords, operators, and other components of the input language are represented by these tokens. Implement parsing functions: Recursively implement each parsing function. These steps should be followed by each function: 1. Verify if the current token matches the nonterminal's anticipated symbol. 2. If the nonterminal has numerous production tules, handle each alternative using an if-else or switch statement. Each possibility ought to be represented by a different function call or block of code. 3. Recursively invoke the parsing routines for each alternative's matching nonterminals in the rule. The parsing procedure will continue until all of the input has been processed thanks to this recursive call. 4. Take care of any additional nonterminal- specific logic, such as parse tree construction or semantic actions. Start parsing: Launch the parsing operation by invoking the parsing function that corresponds to the grammar's start symbol. The recursive descent parsing procedure will get started with this functior BPX OIE) Felcrl 43 (0) Ke[=12). Mel ge yf O13 [Ef fottese pseergsnenaie; cetelisnenas Gpintngitem OBIS Conn Nehnns pllageandtedpn CPs Predictive Parser in Compiler Design he overview of Pedic 1 Parser ar ni focus on the role of reactive Parser. Ad wl igocth fr the implementation ofthe Predictive parse algaritnm and finaly wil discuss an example by llementing te algorithm fr precedence patsing Let's Predictive Parser: A predictive parser isa escent prea with no baeleactng or backup ie = top-down pater that doce not requ backtracking, AK ech step, he choice ofthe rule to be expanded is ‘conser Ifthe nan-erminn\ is tobe furter expanded 1°, the rule selected nasecon the currant input Predictive Parser Algorithm 1. Moke a transition aiagram(OFANFA) fr every rule of grammy. 2. Optimize the OFA by reducing the number 14. fe tranaion diagram roaches an accopt state afer the put ic eoneumed, 5, Simulate te string onthe tansiton dagen to pa Eoeet i is BPs WPL eae OQ (26 google.com/search?q) + coed Yor eens lsed in compiler design to analyze Pac Neckar toetsrstats Pei atiseuaite) ose Kone Ic ue jE ee ea eed tule to be expanded based on the next terminal Peace ac es predictive descent parsers and non-recursive See ent ene aes 5 ferent eon iall A predictive parser is also known as an LL(1) Redon arid ‘grammar known as LL(k) grammar. A predictive eee ore ae CS Sac haces Dea ea ane ee Ree es A predictive parser sustains a stack, which is a eel ete en Ros Peat ean aR ce Aen ma enka ais eal pe Cee Rn) Rr ened Se mae ar Tere eeepc aks Cee ea aa ett] Penh cae Coeur t ae ee) ed Cee! area e Meissner Pieri seinen} Piva lc ttar cats esl Pent momen ek Stecns : () 12:35 AM | 14.3KB/s Zi fore eet eer ed Yes Predictive parsing is & technique ised in compiler design to analyze PINK ctor tt icine ute) etek ica rae gp ge Tule to be expanded based on the next terminal nacre nace predictive descent parsers and non-recursive Cece cent OSes es Geechee acne A predictive parser is also known as an LL(1) eens ta eis Pua ata eee ceo eee ee eae Perea evil MT Chany eis Pel eae R Ma eet ae een Re an ec A predictive parser sustains a stack, which is a Coles ear cea neg RO Pa ae en table, it becomes effortless for the top-down parser to choose the production to be eee Ua RO Cr) rT ened Sema arc SE ene eek any Pee cae eter) PR cu earned Pen Ae See rere ee et ey earner ee amen eer cs ian ed a ertesiared Ss) Pcclsnerisitee icone! Petal taia as Ie parse the input, recursive descent Pearce) coriacaf-ranurchve alaarithnacs : () aPC SE CEs COMER ei (olne(-1 Kole ci [El ottese npseergsnenaie; oeteisnenas Gpentngitem OBIS Conn Wehnns pllageanttedpn CPs Shift Reduce Parser in Compiler Preroquiste Pain| Sct? \Dttam Up Shift Reduce Pre] Shit Reduce parser tems for the construction of parse na silar manner as done in bottom up porcng ie. the arse tees constuced from envesfoto] tothe roctup. Amore general formate annie parser ithe LR pater ‘This peserrequles some dee sutures Le + A stack for soning and sccesing the prodcton eos Basic Operations ~ + Reduce: if thohondle appears ontop of brocucion rules done te, RHS of production ste prodution ree pushed ano the stack, + Accept f ony the start symbols present inthe stack and the input bari erty then, the parsing actions called accept When seeeted action is obtain, tis means suceseol parsing isdore, + Error This is th loc then, its reduction by using appropriate epped Ou ofa Stack and LHS of tution in which the parser can neither perform shift action nor reduce ation Example 1 Consider the grammar Soses soars soa Perform Shift Reduce parsing fr input sting “id +H + oa a qi a ‘wut : iad oo : case Example 2~ Consider tho grammar €-> 262 ©5369 Perform Shift Reduee parsing for input sting '92423" ‘Stack | Input Butler | Parsing Action = Ey ‘hit i TE ees Shift reduce parsing © Shift reduce parsing is a process of reducing a string to the start symbol of a grammar. © Shift reduce parsing uses a stack to hold the grammar and an input tape to hold the string. sting > te staring symbol reauesto © Sift reduce parsing performs the two actions: shift and reduce. That's why it is known as shift reduces parsing. o At the shift action, the current symbol in the input string is pushed to a stack. © At each reduction, the symbols will replaced by the non-terminals. The symbol is the right side of the production and non-terminal is the left side of the production. Example: Grammar: 6) Input string: anfazr) Parsing table: Parsing table: ‘Stack contents Tapa ing ‘ations * st-(2ra018 shit sn a24a3)$ reauce by S-» 8 s -o20030¢ ie ss oanne shite ss a2sasis shu a2 $5.02 +08 reduce by 5» @ 5 04 ie s(t as shies ss.(s108 ds reduce by Sea s5-(508 bs i) A reduce by 5 5 reduce by S* (5) 5 reduce by 5+ 5-5 s A pect There are two main categories of shift reduce parsing as follows: 1, Operator-Precedence Parsing 2. LR-Parser aPC Cae a aa Fe Welel nero) yAe) elicit a Operator precedence parsing Operator precedence grammar is kinds of shift reduce parsing method. It is applied to a small class of operator grammars. A grammar is said to be operator precedence grammar if it has two properties: © NoR.HS. of any production has ae. © No two non-terminals are adjacent. Operator precedence can only _ established between the terminals of the grammar. It ignores the non-terminal. There are the three operator precedence relations: a > b means that terminal "a" has the higher precedence than terminal "b". PEW RAEAS an) a < b means that terminal "a" has the lower precedence than terminal "b". a= b means that the terminal "a" and "b" both have same precedence. Precedence table: 0 Parsing Action © Both end of the given input string, add the $ symbol. o Now scan the input string from left right until the > is encountered. © Scan towards left over all the equal precedence until the first left most < is encountered. Everything between left most < and right most > is a handle. o $on$ means parsing is successful. Example Grammar: e-em ToT Given string: weidniad Let us consider a parse tree for it as follows: JN LAN Li BAW A Res E [=n] LR Parser LR parsing is one type of bottom up parsing. It is used to parse the large class of grammars. In the LR parsing, scanning of the input. stands for left-to-right "R" stands for constructing a right most derivation in reverse. "K" is the number of input symbols of the look ahead used to make number of parsing decision. LR parsing is divided into four parts: LR (0) parsing, SLR parsing, CLR parsing and LALR parsing. Figs TypesoFUR parser LR algorithm: The LR algorithm requires stack, input, output and parsing table. In all type of LR parsing, input, output and stack are same but parsing table is Bere Reg LR algorithm: The LR algorithm requires stack, input, output and parsing table. In all type of LR parsing, input, output and stack are same but parsing table is different. Lee Hea LR parsing able Fig: Block diagram of LR parser Input buffer is used to indicate end of input and it contains the string to be parsed followed by a $ Symbol. A stack is used to contain a sequence of grammar symbols with a $ at the bottom of the stack. Parsing table is a two dimensional array. It contains two parts: Action part and Go To part. LR (1) Parsing Various steps involved in the LR (1) Parsing: 12:37 AM | 130KB/s Zi. © For the given input string write a context free grammar. © Check the ambiguity of the grammar. © Add Augment production in the given grammar. © Create Canonical collection of LR (0) items. © Draw a data flow diagram (DFA). © Construct a LR (1) parsing table. Augment Grammar Augmented grammar G* will be generated if we add one more production in the given grammar G. It helps the parser to identify when to stop the parsing and announce the acceptance of the input. Example Given grammar An atib The Augment grammar G° is represented by As atib arte ILCs OQ 2% geeksforgeeks.org/sir + [Ef ottase pneergnenaie; oeteisnenas Gpentngsitem OBIS. Conn Wehnns pllageanttedpn CPs SLR Parser (with Examples) Prerequisite: Lease: UR parser: Is an ecient bottom-up syntax analysis technique that can be use to pase large castes of context fee grammar i called LR() parsing, L tangs forthe et ooh sxanning Rstands fr rightinast derivation in eves 0 stan for no ofinpit symbols of lookahead Adventages of LR parsing + Wtrosogizes willy al programming language constructs for sihich CF can be writen + itis on ecient non-backirocking shit redoing parsing method, ‘Typee of LR parsing methods 2.cLR SUR Parser sin te consttuct ands simkar to LR parsing, The ony ciference between SLR, ‘that LR(O} parsing table, there's» chance of si reve” confit bs reduc’ corespanding to ll termina states, We ean solve ths problem by entering educe” corresponding to FOLLOW of LHS of produetionin he terminating stte. This is called SUR) ‘smple LA. a the smallest clase of grammar having few umber of states, SLR very enny ser anc LO} parser we ae entering ‘Steps for constructing the SLR parsing table 1. vig augmented grammar 2 LR) calecton of tems ta be found 4, Defining 2 functionsgotoli of terials] and actionfint ofr terminala inthe parsing tele EXAMPLE - Conrtract LR patting table forte given context-free grammar SoA a ‘Solution ‘STEPL Find augmented erammer S55 (Oth production) S->.AA [ist production ASA [2nd production) Ace [Bd pe tctlon| STEP2~ Find LR(O) collection of items o () REE AAs GE tose cretion: Geeta Op eSpien OS c 'STEP2 Find LR(0)eolecton of tems low ie the figure shewing the LR(} clletion efi ms, We wil understand everything one by ‘The terminals ofthis gammas are (9b) ‘The noa-terminais ef tis grammar ae (S.A) ruLe- if any non-terminal has" receding it, we have to write alts production and aed." preceding ch offs production from exch state tothe net tate, the". shifts to one place to the ght + inthe gure, 10 consists of augmented grammer. + fo goesto ll when *. "of th prediction i shifted towards the right oF SSS) thir tate isthe accepted state Sls seen by the compiler. + lo goestol2 when *."of lst production i shied toward right (5->A.A). is seen by the 110 g2es 019 when * “of he 2nd proceton i shifted towards right [A =9.A). ae soon by the +10 goes tol when '.* ofthe ed production I shifted towards ght (A->b) bis seen by the +12 goes tol when '.“of 1st production fs shied towards right (->AA) Als seen bythe +12 goes tol when "of 3d rocucton is shifted towards ght (A->b) bis seen ty the compen +12 goes 018 when *.“of the 2nd procucton fs shifted towards right (>a. A) as seen by the compen +19 g0es to when * “of the Sra production is sifted towerds ght [A> bie seen by the compe +13 goes to15 when *, of 2nd predvetion i shifted towerds the right [A->9A) A seen by the ompier +13 goes to when °F the 2nd proeton i sited towards right [->8.A) ts seen bythe comper Find FOLLOW of LHS of production FOLLOWS) FOLLOWIAI-ab$ To ind FOLLOW of non-terminals, please read follow satin syntax analysis STEP 4 Desining 2 functions gett of non-texminas] ar ation of termina] inthe parsing table Below i the SLR persing table ‘ACTION ‘GOTO. a b 3 x = of s 4 2 1 a apt 2 = 5 [3s se ‘ 5 te sl e m Re Ef Hotta tpeesingtinenaes Sete peatgsen OBS Canteens allapeon non Ce Selow is the SLR persing table ‘ACTION ‘GoTo, b + Sie by cofsults nontormina| that takes acepting sate, + 012.3.45,6 denotes 101)21814)5 6 +10 gives Ain 2, co 2 is added tothe A column and O rows. +10 gles Sinits0 1 sade ta the Sc + simlary Sle wrtten in column and 2 tow 6 ic writen aA clara and 3 tov, jr and 1 row, +10 gives an 80 53st 3s aides to column an Orow +10 gives bin & 20 Sti i dod to the bealumn and O cow. + Sinan, S9[sin 3) fs odded ona column and 2.3 row SAlshif 4 Is edded en b column and 2.3 + ls reduced state as feat the en. is the Sel production of grammarlA-> BLLHS ofthis production i A FOLLOWIA)=a8$ weit lreduee 3 inthe columns of 3.86 an 4th row + 15g reduced state as", Kat the en. 15 she 1 preducton of grammartS-> AA). LHS af his production i. FOLLOW(S)=5 -wete 1 veduced 1) nthe column of $d Sth row + 1Gie» reduced state as. "is atthe ene IG ie the 2nd production of grammar A->aA). The LHS ofthis productions FOLLOW(A)=a.n$ write 2educed 2} nthe columns of ab.$ and 6th row "GeeteforGesks helped me ace the GATE exam! Whenever had any doubt garding any top GFE always helped me and made my concepts quet clear” -Anstika Modi | AIR 22 ‘choose GeeksforGeoks as your perfect GATE 2025 Preparation partner wit these newly launched programe ATE Cs aT SATEDSAAL ‘GATE offtine @atunca ‘Over 125,000+ scents already ust us to be thet GATE Exam gue. nin them & let us help you in opening the GATE to top-tech ITs & NIT oe 2 Suggest imoroeement « Previous Nxt > ‘Shin Reluce Parserin compiler (ith Examples) Share your thought inthe comments =D Similar Reads Compl ekg SUR Pre Ure ython fen buteen Rect dice Deter Pare and Non Reha Pate Desert Pe arte AR Leas Omer ree oe ae SLR (1) Parsing SLR (1) refers to simple LR Parsing. It is same as LR(0) parsing. The only difference is in the parsing table.To construct SLR (1) parsing table, we use canonical collection of LR (0) item. In the SLR (1) parsing, we place the reduce move only in the follow of left hand side. Various steps involved in the SLR (1) Parsing: For the given input string write a context free grammar © Check the ambiguity of the grammar © Add Augment production in the given grammar © Create Canonical collection of LR (0) items © Draw a data flow diagram (DFA) Construct a SLR (1) parsing table SLR (1) Table Construction The steps which use to construct SLR (1) Table is given below: BRC If a state (|i) is going to some other state (I) on a terminal then it corresponds to a shift move in the action part. & . cS soe ve oe If a state (lj) is going to some other state (Ij) on a variable then it correspond to go to move in the Go to part. Aes = If a state (|;) contains the final item like A — abs which has no transitions to the next state then the production is known as reduce production. For all terminals X in FOLLOW (A), write the reduce entry along with their production numbers. Example ote age Ftlen(s)=18) tow (a= © BCE SLR (1) Grammar SE EsE+TIT ToT*FIF F = id Add Augment Production and insert '*' symbol at the first position for every production in G S.:E E-E+T E=-T To -TtF TF F = «id 10 State: Add Augment production to the 10 State and Compute the Closure 10 = Closure (S* = +E) Add all productions starting with E in to 10 State because "." is followed by the non-terminal. So, the 10 State becomes l0=S' —-E Add all productions starting with T and F in modified 10 State because "." is followed by the non-terminal. So, the 10 State becomes. l0=S | +E E=*E+T Es:T To -+T*F To F = id I1= Go to (10, E) = closure (S\ = Es, E > E+ +T) = Go to (l0, T) = closure (E = T:T, T: = *F) 13= Go to (10, F) = Closure (T = F+)=T = Fe ( (l 14= Go to (I0, id) = closure (F = id) =F = ide I5= Go to (I1, +) = Closure (E — E +*T) Add all productions starting with T and F in I5 State because "." is followed by the non-terminal. So, the 15 State becomes ISsE>E+T T{°T*F Be TE ToTtE Wide Range Of Courses ir Go to (15, F) = Closure (T = F+) = (same as 13) Go to (I5, id) = Closure (F = id+) = (same as 14) 16= Go to (12, *) = Closure (T = T* -F) Wide Range Of .. Wide Range of Courses Courses Add all productions starting with F in 16 State because "." is followed by the non-terminal. So, the 16 State becomes l6=T TF F = sid Go to (I6, id) = Closure (F = id+) = (same as 14) 17= Go to (15, T) = Closure (E > E+T+)=E — E+T> 18= Go to (16, F) = Closure (T = T*F*)=T + T*F+ Drawing DFA: aPC aL leas OMe lie Roles | oS Q LALR Parser (with Examples) LALR Parser LALR Paseris lookahead LR parser. its the most powerful parser which can handle arge classes of LR silo states of CLR parsing table Into one singe state ‘The ganeal syntax becomes (A>=8. a] of rammsr. The ing table ie aut large 3 compared to other parsing table. LALR where A->+.Bis production and alsa terminal or ight end marker $ (a emsLRIO} toms look send How to edd lookahead with the production? casea- ‘Suppose thi i the Oth production Now, since.” precedes 8.20 we have to wile Bs prodictions at wll 8->.0 [1st procuction) ‘Suppose this is Bs production, The lok ahead ofthis productions given at- we Lack at previous prhueton ie, 00 prodveton, Whatever i afer B, wef leakaneud of Ist production. Se, here a Oth production after B, Cis there. Assume FIR ‘then 1st producton become IRSTlf tha valve}, thats he Tie) 25.0.4 case - Now i the Oth production ws tke this fence Hereave ean see there's nothing ater 8. So the ookaheod of Oth production wil be he lookahead of 1st production ie casea— Assume a production >a) 4A-23,$ (ath production) Aob.4 (ist production) Horo, tho Ls production i 3 part of tho previous production, ro the lokshead wil Bo th sao r (O} 4 BOREL ea) COCO Veron ecole a LALR (1) Parsing: LALR refers to the lookahead LR. To construct the LALR (1) parsing table, we use the canonical collection of LR (1) items. In the LALR (1) parsing, the LR (1) items which have same productions but different look ahead are combined to form a single set of items LALR (1) parsing is same as the CLR (1) parsing, only difference in the parsing table. Example LALR (1) Grammar Add Augment Production, insert '*' symbol at the first position for every production in G and also add the look ahead. sos BO REC 10 State: Add Augment production to the 10 State and Compute the ClosureL 10 = Closure (S* = +S) Add all productions starting with S in to 10 State because "+" is followed by the non-terminal. So, the 10 State becomes lo=S' —°S,$ S = AAS Add all productions starting with A in modified 10 State because "+" is followed by the non-terminal. So, the 10 State becomes. lo=S = °S,$ S 3 AAS A = *aA, a/b A = +b, a/b I= Go to (0, S) = closure (S’ = S+,$)=S’ > S+,$ 12= Go to (I0, A) = closure (S — A*A,$) Add all productions starting with A in I2 State because "+" is followed by the non-terminal. So, the 12 State becomes 13= Go to (10, a) = Closure (A = a‘A, a/b) Add all productions starting with A in I3 State because "+" is followed by the non-terminal. So, the 13 State becomes I3= A — aA, a/b A = saA,a/b A = *b,a/b Go to (I3, a) = Closure (A = arA, a/b) = (same as 13) Go to (13, b) = Closure (A = bs, a/b) = (same as 14) 14= Go to (10, b) = closure (A — bs, a/b) =A = br, a/b 15= Go to (12, A) = Closure (S = AA+,$) =S = AA:,$ 16= Go to (12, a) = Closure (A — arA, $) eon Add all productions starting with A in 16 State because "+" is followed by the non-terminal. So, the 16 State becomes 16=A — aA,$ A= -aA,$ A-=*b,$ Go to (16, a) = Closure (A = aA, $) = (same as 16) Go to (16, b) = Closure (A — bs, $) = (same as I7) I7= Go to (I2, b) = Closure (A = bs, $) =A = bs, $ I8= Go to (I3, A) = Closure (A — aA+, a/b) = A = aA:, a/b 19= Go to (16, A) = Closure (A — aA,$) A = aAr,S If we analyze then LR (0) items of I3 and 16 are same but they differ only in their lookahead. I3={A = aA, a/b A = aA,a/b A = +b, a/b } l6={A = aA, $ A :aA,$ A-="b,$ } POWELL ae } Clearly 13 and 16 are same in their LR (0) items but differ in their lookahead, so we can combine them and called as 136. x mue Raiige Ur Courses 136 ={A = arA,a/b/$ A = +aA, a/b/S A = +b, a/b/$ } The I4 and I7 are same but they differ only in their look ahead, so we can combine them and called as 147. 147 = {A — bs, a/b/$} The 18 and 19 are same but they differ only in their look ahead, so we can combine them and called as 189. 189 = {A — aAs, a/b/$} Drawing DFA: BRE ea) SMe (2) ioliets2 NNie) (<1 a [Ef voutase pseergnenaic; Osteisnenaies Gpentngitem OBIS. CanpurNehnns pllageandtedpn CPs Error Handling in YACC in Compiler Design ‘compliers 8 computer program that translates cade waten In ne programming language (he language) into another programming language tthe target language) The process of compling involves several stage, including leneal analyse, syntax analysis, semantic anaysis code generation, and code optimization. Toe fet tage ofthe compilation process i Lexical analysis alsa known as scanning. The lee analyzer reads the souce code, character by character, and groups the characte nto tokens, hich ate the basi elements of the source Language Tokens are typically Keywords, eperator, and dente, The next stage Is eyntax analysis, alco known as parsing, The syntax analyzer uses the tkene aenerated by the lexical analyzer to construct the parse tte, stich represents the syntactic structure of the source code. The parte tree is then used to check if the source code is arammatically correct. «The comantic analysis stage checks ifthe source code adheres to the semantic ree of the Uapquage, This ineudes checking if variable names are declared before they ate used and if the datatype of varabios ane expression match Tne code generation stage tates te parse tree and generates target code, whlch canbe ether ‘the form ofan executable program or an intrmdte code to bo wre by other compiler tages Tis stage also includes code optimization. which aims to Improve the performance of the generates cose. Compiler design isan active area of research and development, wich new techniques ané tools being developed to improve the effeney, accuracy, and flexbilty of compilers. The goal af compe design sto crete compiler tat can effectively and efficient translate source code into target code, while ase detecting nd reporting ers in the sourcecode ‘Therese diferent approaches to compiler design, such a5 the tradonalone-pate andl mali-paee compilers, and the modern Land LR parsers. Compiler design is 8 complex process that requires a deep understanding of programming languages. computer architecture, and software engineering ‘YACC is often used in oniuetion witha teical anatyze toot svc alex, whi the Inout source code into a stienm of tokens, The lelelanalyer and the YACC-geneted parser work together turn the input source ede inte a more easly processed format. i used to toxenize vac YAGE (vet Another Compiler Compile) i tool for generating a parser fr @ specified grammar twas developed by Stephen C. Johnson at ATT Bell Laborateries inthe 19705, A parser i= & pregtam that takes input in the form af a sequence of tokens and checks fit conforms te a specified Set of rules, called grammar. ifthe input is vali, the parser generates parse tree, whieh present the structure ofthe input secoring tothe grammer ‘YACE wavks by taking file containing grammar in spaced format end generating Cor C++ code for» parser that implements the grammar, The ver alo provides code for actions to be taken when carta grammar ules ae recognized, suchas creating pare tre nodes or generating code ‘Thefte ie typiealy named with they" Me exenson, YYACE uses LALP (Look-Aheed Left-to-Right) parsing, which is @ type of bettem-up parsing that r (O} 4 RE usa Pree The fee typialy named wth they e5pien OE ‘YACE uses LALR (Leok-Aheed Left-to-Right) parsing, which is @ type of bottom-up parsing tha ‘YACE erammarsconslst fa set of ules, each of wiih has 0 lft-hand side (LHS) ond a right-hand Sie (RHS).The LHS is @ronterminal symbol. which represents a category of input. ar he RHS is 1 sequence ofthe teminel ane nonteminal symbols, stich represents a possible sequence of inpst. For exsmple, + grammar for simple arthmetc expressions might have a rule for an pression, with the LHS being “exoression’ andthe RHS being “term + expresion” a “term ~ the cexpresslon” or “er” where "tens another nonterminal symbol ‘YACE also provides @ numberof built-in features, such as eto recovery and cole resolution, tha low for more rebust and exible paring. ‘Tne YACE Input file, eso known a5 the YACC specification file, contains the following mala 1. Declarations: This secon includes any glebal variables oF constants that are used in the prearam, 2 Terminal symbols: This section defines the terminal symbels, oF the tokens thatthe parser wil recogrize. Thess symbole ae typically defined sing the Stokon directive {3.Non-terminat symbols: This secon defines the non-terinal symbols, or the grammar rues, that the parser wll use to parse the input. These symbols are typically defined uslg the Sénonterm dct 4. Grammar rules: Ths section defines the grammar rules forthe parser Each rue starts with non-tsrminal symbol, fellowed by a colon anda Ust of terminal and rn-termiral symbols that sake up the ule. The grammar rues ate typically separated bya vertical ba '5, Start symbol This section defines the slat symbol forthe parser. The start symbat is te non tat the parser wil begin parsing with 30 implement actions that are taken when 3 specie grammar alee matched “Tne YACC input Te also clades aadionalclrectves en options that canbe used to customize ‘the behevor ofthe parser. These include HAstar, Sle, right, Neprec, and Weype, emong Here Is an example of 2 simple calculator program writen In € us Compiler Comper tock. 39 the YACC (Yet Another ‘This program defines a simple calculator that can evaluate expressions containing numbers and the ‘operators +» * and / The YACC code defines the grammar forthe calculator, en! the cada inthe curly broces species the actions t be taken when a particular grammar rule fe matched, The yvlex) function i ueed to readin and return tho nest token, and the errr) function is called when an errs encounteredin the input Exvor Hendting Error handling refers to the proces of identiing, diagnosing, and rerolving errors or buge in 2 meothly and without intsrupson. Eror handing cane done through the use of error messsg65 Céebugaing tools, and other techniques. These methods can help cevelopersisentity the cause of the evr and take aporoprite acon ta fix Adtionally, eter handing ean alsa help to prevent rors from occuring in the fist place by implementing error-checking cade or other safeguards, In 1 parser generated by YACE, erorheing is achieved though the use of eror recovery rules when the parser encounters token that isnot prt of the grammar, enters an errr-tecevery rode, In his mode, the parser Wes o finda way to continue parsing by Looking fer a sequence of {ohans that con be used to resume parsing. Thi le tyelly one by skipping ane a mae tkene and then trying to match the nau toa diferent ule in the grammer. YACC provides two typer of exror recovery recovery, Panic mace error covery Is used when the parser encounters a token that Is not part of ic-mede error recovery and Phraeelove error ‘the grammar, and it skipe input unt finds token that can be Use to resume pa ng, Para (vel enor tecovery i used when the poser encounters a token thot not pert a the eure ral, andi west finda way to continue posing by matching the input a ferent te ‘Aassonally, YACC also provides © mechanism for resolving confess Such as shif-reduce and ‘educe-educe conf, that ean eceur dung parsing Thi allows the parser to exeose the eareet action to take when muliple es are applcabe othe input. Error hanaing in 8 YACC program can be cone Bs the following techniques: ReneS, Pree level enor recovery 1 seo Wen the peer encounters token that = not pare oF ne curent rte, and tiesto find 3 way to continue parsing by matching the inputto a ferent re, e5piem Oc fcitionally, YACC alse provides » mechanism for resolving conflet, such as shif-raduce and cove reduce confit, tet can occu ding parsing. This allows the parser to choose the corect dion te take when multiple ses are appeadie a theinput. Error handing in 8 YACC program can be done using the following techniques: grammar. When an efor is encountered, the sor token iz used to chip over the input ane continue parsing Error rte: You can alzo define error rosin your ACC program. Thec roles ar ured te hance Speci errs in gremmar, For example. 8 specie input is expected but not found. the error rule canbe used to handle tha situation, Error recovery YACC also provides an error recovery mechanism, This mechanism allows the porer to recover from eror by shiping over int and continuing parsing. This can be useful forhancing unexpected inp 4. Error messages: YACC allows you to speify evr messages that wll be displayed wien an aor is encountered. This can help the user understand what went wrong and howto fic the problem. error) funtion: You canals use the yyeror) function to handle errors in your YACC program ‘The fometon ie calle ashen an error ie encountered, and it message or perform ether eor-handlng task. 9 be used to display an enor ‘Overall, tro handling in YACC requires a combination of l these methods to be succesful Termiscteler 1Lyvlexts yer elect analyzer genertor, use to generate lexical anelyzers for scanners) or programming langusges and other formal languages. I reads 9 specication file, which defines the paterns and rules for recognising the tens ef a language, and generates a C oe C++ rogram that con be used to scan and tekenze input text according to those cules This ‘generated! program is typically used a© 2 component of 2 larger compiler or interpreter forthe targuage. _yverord) yyoror ie = function inthe YACE (Yet Another Compiler Compile) ibeary tate called ‘en a parsing evo encountered, fs typically used to print an error message andr take ‘other appropriate action, The function edtned bythe user and ean be customized to sit heir needs 5. Parzer: A parser isa software program or algorth that recssees and analyzes tet oF dat breaking ft down Into smaller components and Identifying paterns and structures. Ils often used In natural Language processing, computer programming, and date analyic to extract meaning and information from large sets of txt or data, Parsers can be used to enty syntax grammar and semante elements in text or data, and can alco be used to generate code or axe output based on the information they extract ‘yyparea(l ypares i 3 uneton sed in the YACC (Yet Another Compier-Compiley) tol to parse given input according tothe grammer defined inthe YACC Me I reags the input ond genertes a pare toe which is used to generate the target code or perform other actions as defied in the Here's a complete roscmap for you te become a developer: Learn DSA > Master Frontend/Backend/Fall Stack > Bult Projects -> Keep Applying to Jobs ‘nd why go anywhere ese when ur DSA to Development: Cong Guide tps you do thisin 8 ‘ingle program! Apply now to our DSA ta Development Pragram snd aur counselors will connect sth you for further guicance & supoor. a Suggest imorovemen < Previous Next > How to remove banklnes from a tte in short Term Scheduler in Operating System Python SSace your thoughts inthe comments SANs YACC e YACC stands for Yet Another Compiler Compiler. e YACC provides a tool to produce a parser for a given grammar. © YACC is a program designed to compile a LALR (1) grammar. © It is used to produce the source code of the syntactic analyzer of the language produced by LALR (1) grammar. © The input of YACC is the rule or grammar and the output is a C program. These are some points about YACC: Input: A CFG- file.y Output: A parser y.tab.c (yacc) © The output file "file.output" contains the parsing tables © The file "file.tab.h” contains declarations. © The parser called the yyparse (). Parser expects to use a function called yylex () to get tokens. The basic operational sequence is as follows: ERS ces gram.y ee This file contains the desired grammar in YACC format. yacc It shows the YACC program. <> y.tab.c Rsaaeaaoe) It is the c source program created by YACC. cc or gcc C Compiler ——_~ a.out Executable file that will parse grammar given in gram.Y

You might also like