LR(0) and SLR(1) Parsers

Total Page:16

File Type:pdf, Size:1020Kb

LR(0) and SLR(1) Parsers LR(0) and SLR(1) Parsers C.Naga Raju B.Tech(CSE),M.Tech(CSE),PhD(CSE),MIEEE,MCSI,MISTE Professor Department of CSE YSR Engineering College of YVU Proddatur Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 1 Contents • Introduction to bottom up parsers • LR(0) Parser • Example problem • Gate Questions and solutions • SLR(1) Parser • Example problem • Gate Questions and solutions Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 Bottom up Parser • Construction of parse tree for any given input string beginning at the bottom and working towards the root is called bottom up parser For example the given input string : id*id E -> E + T | T id*id F * id T * id T * F T E T -> T * F | F F -> (E) | id id F F id T * F T id id F id T * F id F id id Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 Shift-Reduce Parsing • The general idea is to shift some symbols of input to the stack until a reduction can be applied • At each reduction step, if a specific substring is matched then the body of a production is replaced by the Non Terminal at the head of the production A—>ac/b • The key decisions during bottom-up parsing are about when to reduce and what production should apply • A reduction is a reverse of a step in a derivation • The goal of a bottom-up parser is to construct a derivation in reverse: – E=>T=>T*F=>T*id=>F*id=>id*id Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 LR Parsers LR(k)LR k Left-to-Right Rightmost Derivation In Number Of Input scan Reverse Symbols Of Look Ahead Prof.C.NagaRaju YSRCE of YVU 5 CSE 9949218570 ❖ Types of LR Parsers 1.LR(0) Parser 2.Simple LR-Parser (SLR) 3.Canonical LR Parser (CLR) 4.LALR Parser. Prof.C.NagaRaju YSRCE of YVU 6 CSE 9949218570 Comparison of LL & LR Methods LL(1) LR(1) LALR SLR LR(0) LL(0) Prof.C.NagaRaju YSRCE of YVU 7 CSE 9949218570 ▪ Advantages of LR Parsers ▪ LR parsers are constructed to recognize all Programming Languages ▪ The LR-parsing is Non-Backtracking Shift- Reduce Parser ▪ An LR parser can detect a syntactic errors ▪ It scans input string from left-to-right and use left most derivation in reverse Prof.C.NagaRaju YSRCE of YVU 8 CSE 9949218570 •The LR Parsing Algorithm Input a1 a2 … ai … an $ Scanner sm LR Parsing Engine Xm s m-1 Compiler Construction Xm-1 … s Parser 0 Action Goto Grammar Generator Stack LR Parsing Tables Prof.C.NagaRaju YSRCE of YVU 9 CSE 9949218570 ❖ The LR parser consists of 1) Input 2)Output 3)Stack 4) Driver Program 5) Parsing Table ❖ The Driver Program is same for all LR Parsers. ❖ Only the Parsing Table changes from one parser to the other. ❖ In CLR method the stack holds the states from the LR(0)automation and canonical LR and LALR methods are same Prof.C.NagaRaju YSRCE of YVU 10 CSE 9949218570 ❖ The Driver Program uses the Stack to store a string s0X1s1X2…Xmsm ✓ Where sm is the Top of the Stack. ✓ The Sk‘s are State Symbols ✓ The Xi‘s are Grammar Symbols. ✓ Together State and Grammar Symbols determine a Shift-reduce Parsing Decision. Prof.C.NagaRaju YSRCE of YVU 11 CSE 9949218570 ❖ The Parsing Program reads characters from an Input Buffer one at a time ❖ The Current Input Symbols are used to index the parsing table and determine the shift-reduce parsing decision ❖ In an implementation, the grammar symbols need not appear on the stack Prof.C.NagaRaju YSRCE of YVU 12 CSE 9949218570 Parse Table ❖ The LR Shift-Reduce Parsers can be efficiently implemented by computing a table to guide the processing ❖ The Parsing Table consists of two parts: 1. A Parsing Action Function and 2. A GOTO function. Prof.C.NagaRaju YSRCE of YVU 13 CSE 9949218570 ❖ The Action Table ❖ The Action Table specifies the actions of the parser (e.g., shift or reduce), for the given parse state and the next token ✓ Rows are State Names; ✓ Columns are Terminals Prof.C.NagaRaju YSRCE of YVU 14 CSE 9949218570 LR Driver Program ❖ The LR driver Program determines Sm, the state on top of the stack and ai, the Current Input symbol. ❖ It then consults Action[ Sm, ai ] which can take one of four values: ✓ Shift ✓ Reduce ✓ Accept ✓ Error Prof.C.NagaRaju YSRCE of YVU 15 CSE 9949218570 ❖ If Action[ Sm, ai ] = Shift S ✓ Where S is a State, then the Parser pushes ai and S on to the Stack. ❖ If Action[ Sm, ai ] = Reduce A → β, ✓ Then ai and Sm are replaced by A ✓ if S was the state appearing below ai in the Stack, then GOTO[S, A] is consulted and the state pushed onto the stack. Prof.C.NagaRaju YSRCE of YVU 16 CSE 9949218570 ❖ If Action[ Sm, ai ] = Accept, ✓ Parsing is completed ❖ If Action[ Sm, ai ] = Error, ✓ The Parser discovered an Error. Prof.C.NagaRaju YSRCE of YVU 17 CSE 9949218570 ❖ GOTO Table ❖ The GOTO table specifies which state to put on top of the stack after a reduce ✓Rows are State Names; ✓Columns are Non-Terminals 18 Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 ❖ The GOTO Table is important to find out the next state after every reduction. ❖ The GOTO Table is indexed by a state of the parser and a Non Terminal (Grammar Symbol) ex : GOTO[S, A] ❖ The GOTO Table simply indicates what the next state of the parser if it has recognized a certain Non Terminal. Prof.C.NagaRaju YSRCE of YVU 19 CSE 9949218570 ❖ Right Sentential Form is a Sentential Form in a Rightmost Derivation Example: (S)S , ( (S)S) ❖ Viable Prefix is a sequence of symbols on the parsing stack Example: ✓ (S)S, (S), (S, (, ✓ ((S)S, ((S), ((S , ((, ( Prof.C.NagaRaju YSRCE of YVU 20 CSE 9949218570 LR(0) Parser ❖ The LR Parser is a Shift-reduce Parser that makes use of a Deterministic Finite Automata, recognizing the Set Of All Viable Prefixes by reading the stack from Bottom To Top. ❖ if a Finite-State Machine that recognizes viable prefixes of the right sentential forms is constructed, it can be used to guide the handle selection in the Shift-reduce Parser. Prof.C.NagaRaju YSRCE of YVU 21 CSE 9949218570 ❖ Handle: Handle is a substring that matches the body of a production ❖ Handle is a Right Sentential Form + position where reduction can be performed + production used for reduction Example ( S ) S. With S → Є ( S ) .S With S → S ( ( S ) S . ) With S → ( S ) S Prof.C.NagaRaju YSRCE of YVU 22 CSE 9949218570 • Handle pruning : Handle pruning specifies that the reduction represents one step along the reverse of a rightmost derivation Right sentential form Handle Reducing production id*id id F->id F*id F T->F T*id id F->id T*F T*F E->T*F Prof.C.NagaRaju YSRCE of YVU CSE 9949218570 Augmented Grammar ❖ If G is a Grammar with Start Symbol S, the Augmented Grammar G’ is G with a New Start Symbol S`, and New Production S` →S$. ❖ The Purpose of the Augmented Grammar is to indicate to the parser when it should stop parsing and announce acceptance of the input Prof.C.NagaRaju YSRCE of YVU 24 CSE 9949218570 LR(0) Items An LR(0) Item of a Grammar G is a Production of G with a Dot ( ) at some position of the right side. .Production A → XYZ yields the Four items: 1. A→•XYZ We hope to see a string derivable from XYZ next on the input. 2. A→X•YZ We have just seen on the input a string derivable from X and that we hope next to see a string derivable from YZ next on the Prof.C.NagaRaju YSRCE of YVU 25 input. CSE 9949218570 3. A→XY•Z 4. A→XYZ• ❖ The production A→ generates only one item, A→•. ❖ Each of this item is a Viable prefixes ❖ Closure Item : An Item created by the closure operation on a state. ❖ Complete Item : An Item where the Item Dot is at the end of the RHS. Prof.C.NagaRaju YSRCE of YVU 26 CSE 9949218570 LR(0) Example Context Free Grammar: S → E rule1: (S→S’) E → T Augumented Grammar E → E + T T → i S → •E T → ( E ) E → •T E → •E+T T → •i T → •(E) Prof.C.NagaRaju YSRCE of YVU 27 CSE 9949218570 Construction of LR(0) Closure Items S6 E → T• T T S7 S0 S → •E$ T → (•E) E → •T E → •T ( E → •E+T E → •E+T T → •i T → •i T → •(E) ( T → •(E) i S5 i E T → i• E ( S8 S1 S → E•$ T → (E•) E → E•+T i E → E•+T + + ) $ E → E+•T S3 T → •i S9 S2 T → •(E) T → (E)• S → E$• T S4 Prof.C.NagaRaju YSRCE of YVU 28 E → E+T• CSE 9949218570 Construction of LR(0) Items I : I1:Goto(I0,E) 0 I2 :Goto(I1,$ ) I :Goto(I ,+ ) I :Goto(I ,T ) S → E • $ 3 1 4 3 S → •E$ S → E $ • E → E + • T E →E+T • E → E • + T E → • T T → • i E → • E + T T → • (E) I7 :Goto(I0,( ) T → • i I5 :Goto(I0,i) I5 :Goto(I3,i) T → i • T → i • T →( •E) T → • (E) I6 :Goto(I0,T) E → • T E → T • E → • E + T I :Goto(I ,E) I :Goto(I , ) ) 8 7 9 7 T → • i T →( E•) T →( E )• T → • (E) T → E•+ T Prof.C.NagaRaju YSRCE of YVU 29 CSE 9949218570 Construction of DFA for LR(0) Items $ I I1 2 + E T i I3 I I 4 i 5 I T 0 i ( I6 T ( I9 I I7 8 S E ) Prof.C.NagaRaju YSRCE of YVU 30 CSE 9949218570 Construction of LR(0) Parsing Table Input States Action Part (Terminals) Goto Part (Non- Terminals) i + ( ) $ E T 0 5 7 1 6 Shift 1 3 2 Shift 2 S→E$ Reduce 3 5 7 4 Shift 4 E→E+T Reduce 5 T→i Reduce 6 E→T Reduce 7 5 7 8 6 Shift 8 3 9 Shift 9 T→(E) Prof.C.NagaRaju YSRCE of YVU Reduce 31 CSE 9949218570 String Acceptance Input States Action Part (Terminals) Goto Part (Non- Terminals) i + ( ) $ E T 0 5 7 1 6 Shift 1 3 2 Shift 2 S→E$ Reduce 3 5 7 4 Shift 4 E→E+T Reduce 5 T→i Reduce 6 E→T Reduce 7 5 7 8 6 Shift 8 3 9 Shift 9 T→(E) Reduce Stack Input Action S0 i + (i + i) $ Shift S0 i S5 + (i + i) $ Reduce by T→i S0 T S6 + (i + i) $ Reduce by E→T S0 E S1 + (i + i) $ Shift Prof.C.NagaRaju YSRCE of YVU 32 S0 E S1 + S3 CSE (i9949218570 + i) $ Shift String Acceptance Input States Action Part (Terminals) Goto Part (Non- Terminals) i + ( ) $ E T 0 5 7 1 6 Shift 1 3 2 Shift 2 S→E$ Reduce 3 5 7 4 Shift 4 E→E+T Reduce 5 T→i Reduce 6 E→T Reduce 7 5 7 8 6 Shift 8 3 9 Shift 9 T→(E) Reduce Stack Input Action S0 E S1 + S3 ( i + i) $ shift S0 E S1 + S3 ( S7 i + i) $ shift
Recommended publications
  • Parser Tables for Non-LR(1) Grammars with Conflict Resolution Joel E
    View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by Elsevier - Publisher Connector Science of Computer Programming 75 (2010) 943–979 Contents lists available at ScienceDirect Science of Computer Programming journal homepage: www.elsevier.com/locate/scico The IELR(1) algorithm for generating minimal LR(1) parser tables for non-LR(1) grammars with conflict resolution Joel E. Denny ∗, Brian A. Malloy School of Computing, Clemson University, Clemson, SC 29634, USA article info a b s t r a c t Article history: There has been a recent effort in the literature to reconsider grammar-dependent software Received 17 July 2008 development from an engineering point of view. As part of that effort, we examine a Received in revised form 31 March 2009 deficiency in the state of the art of practical LR parser table generation. Specifically, LALR Accepted 12 August 2009 sometimes generates parser tables that do not accept the full language that the grammar Available online 10 September 2009 developer expects, but canonical LR is too inefficient to be practical particularly during grammar development. In response, many researchers have attempted to develop minimal Keywords: LR parser table generation algorithms. In this paper, we demonstrate that a well known Grammarware Canonical LR algorithm described by David Pager and implemented in Menhir, the most robust minimal LALR LR(1) implementation we have discovered, does not always achieve the full power of Minimal LR canonical LR(1) when the given grammar is non-LR(1) coupled with a specification for Yacc resolving conflicts. We also detail an original minimal LR(1) algorithm, IELR(1) (Inadequacy Bison Elimination LR(1)), which we have implemented as an extension of GNU Bison and which does not exhibit this deficiency.
    [Show full text]
  • Buffered Shift-Reduce Parsing*
    BUFFERED SHIFT-REDUCE PARSING* Bing Swen (Sun Bin) Dept. of Computer Science, Peking University, Beijing 100871, China [email protected] Abstract A parsing method called buffered shift-reduce parsing is presented, which adds an intermediate buffer (queue) to the usual LR parser. The buffer’s usage is analogous to that of the wait-and-see parsing, but it has unlimited buffer length, and may serve as a separate reduction (pruning) stack. The general structure of its parser and some features of its grammars and parsing tables are discussed. 1. Introduction It is well known that the LR parsing is hitherto the most general shift-reduce method of bottom-up parsing [1], and is among the most widely used. For example, almost all compilers of mainstream programming languages employ the LR-like parsing (via an LALR(1) compiler generator such as YACC or GNU Bison [2]), and many NLP systems use a generalized version of LR parsing (GLR, also called the Tomita algorithm [3]). In some earlier research work on LR(k) extensions for NLP [4] and generalization of compiler generator [5], an idea naturally occurred that one can make a modification to the LR parsing so that after each reduction, the resulting symbol may not be necessarily pushed onto the analysis stack, but instead put back into the input, waiting for decisions in the following steps. This strategy postpones the time of some reduction actions in traditional LR parser, partly deviating from leftmost pruning (Leftmost Reduction). This may cause performance loss for some grammars, with the gained opportunities of earlier error detecting and avoidance of false reductions, as well as later handling of ambiguities, which is significant for the processing of highly ambiguous input strings like natural language sentences.
    [Show full text]
  • Chapter 4 Syntax Analysis
    Chapter 4 Syntax Analysis By Varun Arora Outline Role of parser Context free grammars Top down parsing Bottom up parsing Parser generators By Varun Arora The role of parser token Source Lexical Parse tree Rest of Intermediate Parser program Analyzer Front End representation getNext Token Symbol table By Varun Arora Uses of grammars E -> E + T | T T -> T * F | F F -> (E) | id E -> TE’ E’ -> +TE’ | Ɛ T -> FT’ T’ -> *FT’ | Ɛ F -> (E) | id By Varun Arora Error handling Common programming errors Lexical errors Syntactic errors Semantic errors Lexical errors Error handler goals Report the presence of errors clearly and accurately Recover from each error quickly enough to detect subsequent errors Add minimal overhead to the processing of correct progrms By Varun Arora Error-recover strategies Panic mode recovery Discard input symbol one at a time until one of designated set of synchronization tokens is found Phrase level recovery Replacing a prefix of remaining input by some string that allows the parser to continue Error productions Augment the grammar with productions that generate the erroneous constructs Global correction Choosing minimal sequence of changes to obtain a globally least-cost correction By Varun Arora Context free grammars Terminals Nonterminals expression -> expression + term Start symbol expression -> expression – term productions expression -> term term -> term * factor term -> term / factor term -> factor factor -> (expression) factor -> id By Varun Arora Derivations Productions are treated as
    [Show full text]
  • Scheme of Teaching and Examination for BE
    Scheme of Teaching and Examination for B.E (CS&E) SEMESTER: III Sl. Subject Course Title Teaching Credits Contact Marks Exam No. Code Department Hours Duration in hrs L T P TOTAL CIE SEE Total 1 MA310 Mathematics III Mathematics 4 0 0 4 4 50 50 100 03 2 CS310 Digital System CSE 4 0 1 5 6 50 50 100 03 Design 3 CS320 Discrete CSE 4 0 0 4 4 50 50 100 03 Mathematical Structures and Combinatorics 4 CS330 Computer CSE 4 0 0 4 4 50 50 100 03 Organization 5 CS340 Data Structures CSE 4 0 1 5 6 50 50 100 03 6 CS350 Object Oriented CSE 4 0 1 5 6 50 50 100 03 Programming with C++ Total Total 27 Total Marks 600 Credits Scheme of Teaching and Examination for B.E (CS&E) SEMESTER: IV Sl. Subject Course Title Teaching Credits Contact Marks Exam No. Code Department Hours Duration in hrs L T P TOTAL CIE SEE Total 1 MA410 Probability, Mathematics 4 0 0 4 4 50 50 100 03 Statistics and Queuing 2 CS410 Operating CSE 4 0 1 5 6 50 50 100 03 Systems 3 CS420 Design and CSE 4 0 1 5 6 50 50 100 03 Analysis of Algorithms 4 CS430 Theory of CSE 4 0 0 4 4 50 50 100 03 Computation 5 CS440 Microprocessors CSE 4 0 1 5 6 50 50 100 03 6 CS450 Data CSE 4 0 0 4 4 50 50 100 03 Communication Total Total 27 Total Marks 600 Credits Scheme of Teaching and Examination for B.E (CS&E) SEMESTER: V Sl.
    [Show full text]
  • Validating LR(1) Parsers
    Validating LR(1) Parsers Jacques-Henri Jourdan1;2, Fran¸coisPottier2, and Xavier Leroy2 1 Ecole´ Normale Sup´erieure 2 INRIA Paris-Rocquencourt Abstract. An LR(1) parser is a finite-state automaton, equipped with a stack, which uses a combination of its current state and one lookahead symbol in order to determine which action to perform next. We present a validator which, when applied to a context-free grammar G and an automaton A, checks that A and G agree. Validating the parser pro- vides the correctness guarantees required by verified compilers and other high-assurance software that involves parsing. The validation process is independent of which technique was used to construct A. The validator is implemented and proved correct using the Coq proof assistant. As an application, we build a formally-verified parser for the C99 language. 1 Introduction Parsing remains an essential component of compilers and other programs that input textual representations of structured data. Its theoretical foundations are well understood today, and mature technology, ranging from parser combinator libraries to sophisticated parser generators, is readily available to help imple- menting parsers. The issue we focus on in this paper is that of parser correctness: how to obtain formal evidence that a parser is correct with respect to its specification? Here, following established practice, we choose to specify parsers via context-free grammars enriched with semantic actions. One application area where the parser correctness issue naturally arises is formally-verified compilers such as the CompCert verified C compiler [14]. In- deed, in the current state of CompCert, the passes that have been formally ver- ified start at abstract syntax trees (AST) for the CompCert C subset of C and extend to ASTs for three assembly languages.
    [Show full text]
  • Efficient Computation of LALR(1) Look-Ahead Sets
    Efficient Computation of LALR(1) Look-Ahead Sets FRANK DeREMER and THOMAS PENNELLO University of California, Santa Cruz, and MetaWare TM Incorporated Two relations that capture the essential structure of the problem of computing LALR(1) look-ahead sets are defined, and an efficient algorithm is presented to compute the sets in time linear in the size of the relations. In particular, for a PASCAL grammar, the algorithm performs fewer than 15 percent of the set unions performed by the popular compiler-compiler YACC. When a grammar is not LALR(1), the relations, represented explicitly, provide for printing user- oriented error messages that specifically indicate how the look-ahead problem arose. In addition, certain loops in the digraphs induced by these relations indicate that the grammar is not LR(k) for any k. Finally, an oft-discovered and used but incorrect look-ahead set algorithm is similarly based on two other relations defined for the fwst time here. The formal presentation of this algorithm should help prevent its rediscovery. Categories and Subject Descriptors: D.3.1 [Programming Languages]: Formal Definitions and Theory--syntax; D.3.4 [Programming Languages]: Processors--translator writing systems and compiler generators; F.4.2 [Mathematical Logic and Formal Languages]: Grammars and Other Rewriting Systems--parsing General Terms: Algorithms, Languages, Theory Additional Key Words and Phrases: Context-free grammar, Backus-Naur form, strongly connected component, LALR(1), LR(k), grammar debugging, 1. INTRODUCTION Since the invention of LALR(1) grammars by DeRemer [9], LALR grammar analysis and parsing techniques have been popular as a component of translator writing systems and compiler-compilers.
    [Show full text]
  • CWI Scanprofile/PDF/300
    Centrum voor Wiskunde en lnformatica Centre for Mathematics and Computer Science J. Heering, P. Klint, J.G. Rekers Incremental generation of parsers , Computer Science/Department of Software Technology Report CS-R8822 May Biblk>tlleek Centrum ypor Wisl~unde en lnformatk:a Am~tel>dam The Centre for Mathematics and Computer Science is a research institute of the Stichting Mathematisch Centrum, which was founded on February 11, 1946, as a nonprofit institution aim­ ing at the promotion of mathematics, computer science, and their applications. It is sponsored by the Dutch Government through the Netherlands Organization for the Advancement of Pure Research (Z.W.0.). q\ ' Copyright (t:: Stichting Mathematisch Centrum, Amsterdam 1 Incremental Generation of Parsers J. Heering Department of Software Technology, Centre for Mathematics and Computer Science P.O. Box 4079, 1009 AS Amsterdam, The Netherlands P. Klint Department of Software Technology, Centre for Mathematics and Computer Science P.O. Box 4079, 1009 AS Amsterdam, The Netherlands and Programming Research Group, University of Amsterdam P.O. BOX 41882, 1009 DB Amsterdam, The Netherlands J. Rekers Department of Software Technology, Centre for Mathematics and Computer Science P.O. Box 4079, 1009 AB Amsterdam, The Netherlands A parser checks whether a text is a sentence in a language. Therefore, the parser is provided with the grammar of the language, and it usually generates a structure (parse tree) that represents the text according to that grammar. Most present-day parsers are not directly driven by the grammar but by a 'parse table', which is generated by a parse table generator. A table based parser wolks more efficiently than a grammar based parser does, and provided that the parser is used often enough, the cost of gen­ erating the parse table is outweighed by the gain in parsing efficiency.
    [Show full text]
  • B.E Computer Science and Engineering
    CURRICULUM B.E. – Computer Science and Engineering Regulations 2019 VISION MISSION “To become a center of excellence • To produce technocrats in the in Computer Science and industry and academia by Engineering and Research to create educating computer concepts and global leaders with holistic growth techniques. and ethical values for the industry • To facilitate the students to and academics.” trigger more creativity by applying modern tools and technologies in the field of computer science and engineering. • To inculcate the spirit of ethical values contributing to the welfare of the society. Department of Computer Science and Engineering Department of CSE, Francis Xavier Engineering College | Regulation 2019 2 Department of CSE, Francis Xavier Engineering College | Regulation 2019 5 B.E.-COMPUTER SCIENCE AND ENGINEERING (REGULATIONS 2019) CHOICE BASED CREDIT SYSTEM SUMMARY OF CREDIT DISTRIBUTION Range Of CREDITS PER SEMESTER Total S. TOTAL CREDITS CATEGORY Credits No CREDIT IN % I II III IV V VI VII VIII Min Max 1 HSS 3 2 3 8 4.5% 9 11 2 BS 12 4 4 4 24 14.5% 21 21 3 ES 8 11 3 22 13.9% 23 26 4 PC 13 17 10 11 8 59 35.75% 59 59 5 PE 6 6 6 6 24 14.5% 24 27 6 OE 3 3 3 3 12 7.3% 12 15 7 EEC 2 2 1 10 15 9.1% 12 15 TOTAL 23 18 20 23 22 22 21 16 165 100% - - BS - Basic Sciences ES - Engineering Sciences HSS - Humanities and Social Sciences PC - Professional Core PE - Professional Elective OE - Open Elective EEC - Employability Enhancement Course Department of CSE, Francis Xavier Engineering College | Regulation 2019 6 B.E.- COMPUTER SCIENCE AND ENGINEERING (REGULATIONS 2019) CHOICE BASED CREDIT SYSTEM I – VIII SEMESTERS CURRICULUM AND SYLLABI FIRST SEMESTER Code No.
    [Show full text]
  • LLLR Parsing: a Combination of LL and LR Parsing
    LLLR Parsing: a Combination of LL and LR Parsing Boštjan Slivnik University of Ljubljana, Faculty of Computer and Information Science, Ljubljana, Slovenia [email protected] Abstract A new parsing method called LLLR parsing is defined and a method for producing LLLR parsers is described. An LLLR parser uses an LL parser as its backbone and parses as much of its input string using LL parsing as possible. To resolve LL conflicts it triggers small embedded LR parsers. An embedded LR parser starts parsing the remaining input and once the LL conflict is resolved, the LR parser produces the left parse of the substring it has just parsed and passes the control back to the backbone LL parser. The LLLR(k) parser can be constructed for any LR(k) grammar. It produces the left parse of the input string without any backtracking and, if used for a syntax-directed translation, it evaluates semantic actions using the top-down strategy just like the canonical LL(k) parser. An LLLR(k) parser is appropriate for grammars where the LL(k) conflicting nonterminals either appear relatively close to the bottom of the derivation trees or produce short substrings. In such cases an LLLR parser can perform a significantly better error recovery than an LR parser since the most part of the input string is parsed with the backbone LL parser. LLLR parsing is similar to LL(∗) parsing except that it (a) uses LR(k) parsers instead of finite automata to resolve the LL(k) conflicts and (b) does not perform any backtracking.
    [Show full text]
  • Parser Generation Bottom-Up Parsing LR Parsing Constructing LR Parser
    CS421 COMPILERS AND INTERPRETERS CS421 COMPILERS AND INTERPRETERS Parser Generation Bottom-Up Parsing • Main Problem: given a grammar G, how to build a top-down parser or • Construct parse tree “bottom-up” --- from leaves to the root a bottom-up parser for it ? • Bottom-up parsing always constructs right-most derivation • parser : a program that, given a sentence, reconstructs a derivation for • Important parsing algorithms: shift-reduce, LR parsing that sentence ---- if done sucessfully, it “recognize” the sentence • LR parser components: input, stack (strings of grammar symbols and • all parsers read their input left-to-right, but construct parse tree states), driver routine, parsing tables. differently. input: a1 a2 a3 a4 ......an $ • bottom-up parsers --- construct the tree from leaves to root stack s shift-reduce, LR, SLR, LALR, operator precedence m LR Parsing output Xm • top-down parsers --- construct the tree from root to leaves .... s1 recursive descent, predictive parsing, LL(1) X1 parsing s0 Copyright 1994 - 2000 Carsten Schürmann, Zhong Shao, Yale University Parser Generation: Page 1 of 28 Copyright 1994 - 2000 Carsten Schürmann, Zhong Shao, Yale University Parser Generation: Page 2 of 28 CS421 COMPILERS AND INTERPRETERS CS421 COMPILERS AND INTERPRETERS LR Parsing Constructing LR Parser How to construct the parsing table and ? • A sequence of new state symbols s0,s1,s2,..., sm ----- each state ACTION GOTO sumarize the information contained in the stack below it. • basic idea: first construct DFA to recognize handles, then use DFA to construct the parsing tables ! different parsing table yield different LR • Parsing configurations: (stack, remaining input) written as parsers SLR(1), LR(1), or LALR(1) (s0X1s1X2s2...Xmsm , aiai+1ai+2...an$) • augmented grammar for context-free grammar G = G(T,N,P,S) is next “move” is determined by sm and ai defined as G’ = G’(T,N ??{ S’}, P ??{ S’ -> S}, S’) ------ adding non- • Parsing tables: ACTION[s,a] and GOTO[s,X] terminal S’ and the production S’ -> S, and S’ is the new start symbol.
    [Show full text]
  • Compiler Design
    CCOOMMPPIILLEERR DDEESSIIGGNN -- PPAARRSSEERR http://www.tutorialspoint.com/compiler_design/compiler_design_parser.htm Copyright © tutorialspoint.com In the previous chapter, we understood the basic concepts involved in parsing. In this chapter, we will learn the various types of parser construction methods available. Parsing can be defined as top-down or bottom-up based on how the parse-tree is constructed. Top-Down Parsing We have learnt in the last chapter that the top-down parsing technique parses the input, and starts constructing a parse tree from the root node gradually moving down to the leaf nodes. The types of top-down parsing are depicted below: Recursive Descent Parsing Recursive descent is a top-down parsing technique that constructs the parse tree from the top and the input is read from left to right. It uses procedures for every terminal and non-terminal entity. This parsing technique recursively parses the input to make a parse tree, which may or may not require back-tracking. But the grammar associated with it ifnotleftfactored cannot avoid back- tracking. A form of recursive-descent parsing that does not require any back-tracking is known as predictive parsing. This parsing technique is regarded recursive as it uses context-free grammar which is recursive in nature. Back-tracking Top- down parsers start from the root node startsymbol and match the input string against the production rules to replace them ifmatched. To understand this, take the following example of CFG: S → rXd | rZd X → oa | ea Z → ai For an input string: read, a top-down parser, will behave like this: It will start with S from the production rules and will match its yield to the left-most letter of the input, i.e.
    [Show full text]
  • CLR(1) and LALR(1) Parsers
    CLR(1) and LALR(1) Parsers C.Naga Raju B.Tech(CSE),M.Tech(CSE),PhD(CSE),MIEEE,MCSI,MISTE Professor Department of CSE YSR Engineering College of YVU Proddatur 6/21/2020 Prof.C.NagaRaju YSREC of YVU 9949218570 Contents • Limitations of SLR(1) Parser • Introduction to CLR(1) Parser • Animated example • Limitation of CLR(1) • LALR(1) Parser Animated example • GATE Problems and Solutions Prof.C.NagaRaju YSREC of YVU 9949218570 Drawbacks of SLR(1) ❖ The SLR Parser discussed in the earlier class has certain flaws. ❖ 1.On single input, State may be included a Final Item and a Non- Final Item. This may result in a Shift-Reduce Conflict . ❖ 2.A State may be included Two Different Final Items. This might result in a Reduce-Reduce Conflict Prof.C.NagaRaju YSREC of YVU 6/21/2020 9949218570 ❖ 3.SLR(1) Parser reduces only when the next token is in Follow of the left-hand side of the production. ❖ 4.SLR(1) can reduce shift-reduce conflicts but not reduce-reduce conflicts ❖ These two conflicts are reduced by CLR(1) Parser by keeping track of lookahead information in the states of the parser. ❖ This is also called as LR(1) grammar Prof.C.NagaRaju YSREC of YVU 6/21/2020 9949218570 CLR(1) Parser ❖ LR(1) Parser greatly increases the strength of the parser, but also the size of its parse tables. ❖ The LR(1) techniques does not rely on FOLLOW sets, but it keeps the Specific Look-ahead with each item. Prof.C.NagaRaju YSREC of YVU 6/21/2020 9949218570 CLR(1) Parser ❖ LR(1) Parsing configurations have the general form: A –> X1...Xi • Xi+1...Xj , a ❖ The Look Ahead
    [Show full text]