Statistical Parsing and CKY Algorithm

Total Page:16

File Type:pdf, Size:1020Kb

Statistical Parsing and CKY Algorithm Statistical Parsing and CKY Algorithm Instructor: Wei Xu Ohio State University Many slides from Ray Mooney and Michael Collins TA Office Hours for HW#2 • Dreese 390: - 03/28 Tue 10:00AM-12:00 noon - 03/30 Thu 10:00AM-12:00 noon - 04/04 Tue 10:00AM-12:00 noon Wuwei Lan • Readings: - textbook http://ciml.info/dl/v0_99/ciml-v0_99-ch17.pdf - slide #28,29.30: https://cocoxu.github.io/courses/ 5525_slides_spring17/15_more_memm.pdf Syntax Syntactic Parsing Parsing • Given a string of terminals and a CFG, determine if the string can be generated by the CFG: - also return a parse tree for the string - also return all possible parse trees for the string • Must search space of derivations for one that derives the given string. - Top-Down Parsing - Bottom-Up Parsing Simple CFG for ATIS English Grammar Lexicon S → NP VP Det → the | a | that | this S → Aux NP VP Noun → book | flight | meal | money S → VP Verb → book | include | prefer NP → Pronoun Pronoun → I | he | she | me NP → Proper-Noun Proper-Noun → Houston | NWA NP → Det Nominal Aux → does Nominal → Noun Prep → from | to | on | near | through Nominal → Nominal Noun Nominal → Nominal PP VP → Verb VP → Verb NP VP → VP PP PP → Prep NP Parsing Example S VP Verb NP book that flight book Det Nominal that Noun flight Top Down Parsing • Start searching space of derivations for the start symbol. S NP VP Pronoun Top Down Parsing S NP VP Pronoun X book Top Down Parsing S NP VP ProperNoun Top Down Parsing S NP VP ProperNoun X book Top Down Parsing S NP VP Det Nominal Top Down Parsing S NP VP Det Nominal X book Top Down Parsing S Aux NP VP Top Down Parsing S Aux NP VP X book Top Down Parsing S VP Top Down Parsing S VP Verb Top Down Parsing S VP Verb book Top Down Parsing S VP Verb X book that Top Down Parsing S VP Verb NP Top Down Parsing S VP Verb NP book Top Down Parsing S VP Verb NP book Pronoun Top Down Parsing S VP Verb NP book Pronoun X that Top Down Parsing S VP Verb NP book ProperNoun Top Down Parsing S VP Verb NP book ProperNoun X that Top Down Parsing S VP Verb NP book Det Nominal Top Down Parsing S VP Verb NP book Det Nominal that Top Down Parsing S VP Verb NP book Det Nominal that Noun Top Down Parsing S VP Verb NP book Det Nominal that Noun flight Bottom Up Parsing • Start searching space of reverse derivations from the terminal symbols in the string. book that flight Bottom Up Parsing Noun book that flight Bottom Up Parsing Nominal Noun book that flight Bottom Up Parsing Nominal Nominal Noun Noun book that flight Bottom Up Parsing Nominal Nominal Noun X Noun book that flight Bottom Up Parsing Nominal Nominal PP Noun book that flight Bottom Up Parsing Nominal Nominal PP Noun Det book that flight Bottom Up Parsing Nominal Nominal PP NP Noun Det Nominal book that flight Bottom Up Parsing Nominal Nominal PP NP Noun Det Nominal book that Noun flight Bottom Up Parsing Nominal Nominal PP NP Noun Det Nominal book that Noun flight Bottom Up Parsing Nominal Nominal PP S NP VP Noun Det Nominal book that Noun flight Bottom Up Parsing Nominal Nominal PP S NP VP Noun Det Nominal X book that Noun flight Bottom Up Parsing Nominal Nominal PP X NP Noun Det Nominal book that Noun flight Bottom Up Parsing NP Verb Det Nominal book that Noun flight Bottom Up Parsing VP NP Verb Det Nominal book that Noun flight Bottom Up Parsing S VP NP Verb Det Nominal book that Noun flight Bottom Up Parsing S VP X NP Verb Det Nominal book that Noun flight Bottom Up Parsing VP VP PP NP Verb Det Nominal book that Noun flight Bottom Up Parsing VP VP PP X NP Verb Det Nominal book that Noun flight Bottom Up Parsing VP NP NP Verb Det Nominal book that Noun flight Bottom Up Parsing VP NP Verb Det Nominal book that Noun flight Bottom Up Parsing S VP NP Verb Det Nominal book that Noun flight Top Down vs. Bottom Up • Top down never explores options that will not lead to a full parse, but can explore many options that never connect to the actual sentence. • Bottom up never explores options that do not connect to the actual sentence but can explore options that can never lead to a full parse. • Relative amounts of wasted search depend on how much the grammar branches in each direction. Syntax CYK Algorithm Dynamic Programming Parsing • CKY (Cocke-Kasami-Younger) algorithm based on bottom-up parsing and requires first normalizing the grammar. • First grammar must be converted to Chomsky normal form (CNF) in which productions must have either exactly 2 non- terminal symbols on the RHS or 1 terminal symbol (lexicon rules). • Parse bottom-up storing phrases formed from all substrings in a triangular table (chart). Dynamic Programming • a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems • first invented by Richard Bellman in 1950s • “programming” here means “planning” or finding an optimal program, as also seen in the term “linear programming” • Main idea: • setup a recurrence of smaller subproblems • solve subproblems once and record solutions in a table (avoid any recalculation) ATIS English Grammar Conversion Original Grammar Chomsky Normal Form S → NP VP S → NP VP S → Aux NP VP S → X1 VP X1 → Aux NP S → VP S → book | include | prefer S → Verb NP S → VP PP NP → Pronoun NP → I | he | she | me NP → Proper-Noun NP → Houston | NWA NP → Det Nominal NP → Det Nominal Nominal → Noun Nominal → book | flight | meal | money Nominal → Nominal Noun Nominal → Nominal Noun Nominal → Nominal PP Nominal → Nominal PP VP → Verb VP → book | include | prefer VP → Verb NP VP → Verb NP VP → VP PP VP → VP PP PP → Prep NP PP → Prep NP Note that, although not shown here, original grammar contain all the lexical entires. Exercise CKY Parser Book the flight through Houston j= 1 2 3 4 5 i= 0 Cell[i,j] contains all 1 constituents (non-terminals) covering words 2 i +1 through j 3 4 CKY Parser Book the flight through Houston j= 1 2 3 4 5 i= 0 Cell[i,j] contains all 1 constituents (non-terminals) covering words 2 i +1 through j 3 4 CKY Parser Book the flight through Houston S, VP, Verb, Nominal, Noun None NP Det Nominal, Noun CKY Parser Book the flight through Houston S, VP, Verb, Nominal, VP Noun None NP Det Nominal, Noun CKY Parser Book the flight through Houston S, VP, Verb, S Nominal, VP Noun None NP Det Nominal, Noun CKY Parser Book the flight through Houston S, VP, Verb, S Nominal, VP Noun None NP Det Nominal, Noun CKY Parser Book the flight through Houston S, VP, Verb, S Nominal, VP Noun None None NP Det None Nominal, Noun None Prep CKY Parser Book the flight through Houston S, VP, Verb, S Nominal, VP Noun None None NP Det None Nominal, Noun None Prep PP NP ProperNoun CKY Parser Book the flight through Houston S, VP, Verb, S Nominal, VP Noun None None NP Det None Nominal, Noun None Nominal Prep PP NP ProperNoun CKY Parser Book the flight through Houston S, VP, Verb, S Nominal, VP Noun None None NP NP Det None Nominal, Noun None Nominal Prep PP NP ProperNoun CKY Parser Book the flight through Houston S, VP, Verb, S Nominal, VP Noun None None VP NP NP Det None Nominal, Noun None Nominal Prep PP NP ProperNoun CKY Parser Book the flight through Houston S, VP, Verb, S Nominal, VP S Noun None None VP NP NP Det None Nominal, Noun None Nominal Prep PP NP ProperNoun CKY Parser Book the flight through Houston S, VP, Verb, S Nominal, VP VP S Noun None None VP NP NP Det None Nominal, Noun None Nominal Prep PP NP ProperNoun CKY Parser Book the flight through Houston S, VP, Verb, S S Nominal, VP VP S Noun None None VP NP NP Det None Nominal, Noun None Nominal Prep PP NP ProperNoun CKY Parser Book the flight through Houston Parse S, VP, Verb, S S Nominal, VP VP Tree S Noun None None #1 VP NP NP Det None Nominal, Noun None Nominal Prep PP NP ProperNoun CKY Parser Book the flight through Houston Parse S, VP, Verb, S S Nominal, VP VP Tree S Noun None None #2 VP NP NP Det None Nominal, Noun None Nominal Prep PP NP ProperNoun The Problem with Parsing: Ambiguity INPUT: She announced a program to promote safety in trucks and vans + POSSIBLE OUTPUTS: S S S S S S NP VP NP VP NP VP NP VP She Sh e NP VP She NP VP She announced NP She She announced NP announced NP announced NP NP VP NP VP a program announced NP announced NP NP VP a program NP PP to promote NP a progra m to promote NP PP in NP NP VP safety PP safety in NP a program trucks and va ns to promote NP in NP to promote trucks and vans NP safe ty trucks and vans NP and NP NP and NP va ns vans NP and NP NP VP va ns NP VP sa fety PP a program a program in NP to promote NP PP trucks to promote NP safe ty in NP trucks safety PP in NP trucks And there are more..
Recommended publications
  • Syntax-Directed Translation, Parse Trees, Abstract Syntax Trees
    Syntax-Directed Translation Extending CFGs Grammar Annotation Parse Trees Abstract Syntax Trees (ASTs) Readings: Section 5.1, 5.2, 5.5, 5.6 Motivation: parser as a translator syntax-directed translation stream of ASTs, or tokens parser assembly code syntax + translation rules (typically hardcoded in the parser) 1 Mechanism of syntax-directed translation syntax-directed translation is done by extending the CFG a translation rule is defined for each production given X Æ d A B c the translation of X is defined in terms of translation of nonterminals A, B values of attributes of terminals d, c constants To translate an input string: 1. Build the parse tree. 2. Working bottom-up • Use the translation rules to compute the translation of each nonterminal in the tree Result: the translation of the string is the translation of the parse tree's root nonterminal Why bottom up? a nonterminal's value may depend on the value of the symbols on the right-hand side, so translate a non-terminal node only after children translations are available 2 Example 1: arith expr to its value Syntax-directed translation: the CFG translation rules E Æ E + T E1.trans = E2.trans + T.trans E Æ T E.trans = T.trans T Æ T * F T1.trans = T2.trans * F.trans T Æ F T.trans = F.trans F Æ int F.trans = int.value F Æ ( E ) F.trans = E.trans Example 1 (cont) E (18) Input: 2 * (4 + 5) T (18) T (2) * F (9) F (2) ( E (9) ) int (2) E (4) * T (5) Annotated Parse Tree T (4) F (5) F (4) int (5) int (4) 3 Example 2: Compute type of expr E -> E + E if ((E2.trans == INT) and (E3.trans == INT) then E1.trans = INT else E1.trans = ERROR E -> E and E if ((E2.trans == BOOL) and (E3.trans == BOOL) then E1.trans = BOOL else E1.trans = ERROR E -> E == E if ((E2.trans == E3.trans) and (E2.trans != ERROR)) then E1.trans = BOOL else E1.trans = ERROR E -> true E.trans = BOOL E -> false E.trans = BOOL E -> int E.trans = INT E -> ( E ) E1.trans = E2.trans Example 2 (cont) Input: (2 + 2) == 4 1.
    [Show full text]
  • Derivatives of Parsing Expression Grammars
    Derivatives of Parsing Expression Grammars Aaron Moss Cheriton School of Computer Science University of Waterloo Waterloo, Ontario, Canada [email protected] This paper introduces a new derivative parsing algorithm for recognition of parsing expression gram- mars. Derivative parsing is shown to have a polynomial worst-case time bound, an improvement on the exponential bound of the recursive descent algorithm. This work also introduces asymptotic analysis based on inputs with a constant bound on both grammar nesting depth and number of back- tracking choices; derivative and recursive descent parsing are shown to run in linear time and constant space on this useful class of inputs, with both the theoretical bounds and the reasonability of the in- put class validated empirically. This common-case constant memory usage of derivative parsing is an improvement on the linear space required by the packrat algorithm. 1 Introduction Parsing expression grammars (PEGs) are a parsing formalism introduced by Ford [6]. Any LR(k) lan- guage can be represented as a PEG [7], but there are some non-context-free languages that may also be represented as PEGs (e.g. anbncn [7]). Unlike context-free grammars (CFGs), PEGs are unambiguous, admitting no more than one parse tree for any grammar and input. PEGs are a formalization of recursive descent parsers allowing limited backtracking and infinite lookahead; a string in the language of a PEG can be recognized in exponential time and linear space using a recursive descent algorithm, or linear time and space using the memoized packrat algorithm [6]. PEGs are formally defined and these algo- rithms outlined in Section 3.
    [Show full text]
  • The Earley Algorithm Is to Avoid This, by Only Building Constituents That Are Compatible with the Input Read So Far
    Earley Parsing Informatics 2A: Lecture 21 Shay Cohen 3 November 2017 1 / 31 1 The CYK chart as a graph What's wrong with CYK Adding Prediction to the Chart 2 The Earley Parsing Algorithm The Predictor Operator The Scanner Operator The Completer Operator Earley parsing: example Comparing Earley and CYK 2 / 31 We would have to split a given span into all possible subspans according to the length of the RHS. What is the complexity of such algorithm? Still O(n2) charts, but now it takes O(nk−1) time to process each cell, where k is the maximal length of an RHS. Therefore: O(nk+1). For CYK, k = 2. Can we do better than that? Note about CYK The CYK algorithm parses input strings in Chomsky normal form. Can you see how to change it to an algorithm with an arbitrary RHS length (of only nonterminals)? 3 / 31 Still O(n2) charts, but now it takes O(nk−1) time to process each cell, where k is the maximal length of an RHS. Therefore: O(nk+1). For CYK, k = 2. Can we do better than that? Note about CYK The CYK algorithm parses input strings in Chomsky normal form. Can you see how to change it to an algorithm with an arbitrary RHS length (of only nonterminals)? We would have to split a given span into all possible subspans according to the length of the RHS. What is the complexity of such algorithm? 3 / 31 Note about CYK The CYK algorithm parses input strings in Chomsky normal form.
    [Show full text]
  • LATE Ain't Earley: a Faster Parallel Earley Parser
    LATE Ain’T Earley: A Faster Parallel Earley Parser Peter Ahrens John Feser Joseph Hui [email protected] [email protected] [email protected] July 18, 2018 Abstract We present the LATE algorithm, an asynchronous variant of the Earley algorithm for pars- ing context-free grammars. The Earley algorithm is naturally task-based, but is difficult to parallelize because of dependencies between the tasks. We present the LATE algorithm, which uses additional data structures to maintain information about the state of the parse so that work items may be processed in any order. This property allows the LATE algorithm to be sped up using task parallelism. We show that the LATE algorithm can achieve a 120x speedup over the Earley algorithm on a natural language task. 1 Introduction Improvements in the efficiency of parsers for context-free grammars (CFGs) have the potential to speed up applications in software development, computational linguistics, and human-computer interaction. The Earley parser has an asymptotic complexity that scales with the complexity of the CFG, a unique, desirable trait among parsers for arbitrary CFGs. However, while the more commonly used Cocke-Younger-Kasami (CYK) [2, 5, 12] parser has been successfully parallelized [1, 7], the Earley algorithm has seen relatively few attempts at parallelization. Our research objectives were to understand when there exists parallelism in the Earley algorithm, and to explore methods for exploiting this parallelism. We first tried to naively parallelize the Earley algorithm by processing the Earley items in each Earley set in parallel. We found that this approach does not produce any speedup, because the dependencies between Earley items force much of the work to be performed sequentially.
    [Show full text]
  • Lecture 10: CYK and Earley Parsers Alvin Cheung Building Parse Trees Maaz Ahmad CYK and Earley Algorithms Talia Ringer More Disambiguation
    Hack Your Language! CSE401 Winter 2016 Introduction to Compiler Construction Ras Bodik Lecture 10: CYK and Earley Parsers Alvin Cheung Building Parse Trees Maaz Ahmad CYK and Earley algorithms Talia Ringer More Disambiguation Ben Tebbs 1 Announcements • HW3 due Sunday • Project proposals due tonight – No late days • Review session this Sunday 6-7pm EEB 115 2 Outline • Last time we saw how to construct AST from parse tree • We will now discuss algorithms for generating parse trees from input strings 3 Today CYK parser builds the parse tree bottom up More Disambiguation Forcing the parser to select the desired parse tree Earley parser solves CYK’s inefficiency 4 CYK parser Parser Motivation • Given a grammar G and an input string s, we need an algorithm to: – Decide whether s is in L(G) – If so, generate a parse tree for s • We will see two algorithms for doing this today – Many others are available – Each with different tradeoffs in time and space 6 CYK Algorithm • Parsing algorithm for context-free grammars • Invented by John Cocke, Daniel Younger, and Tadao Kasami • Basic idea given string s with n tokens: 1. Find production rules that cover 1 token in s 2. Use 1. to find rules that cover 2 tokens in s 3. Use 2. to find rules that cover 3 tokens in s 4. … N. Use N-1. to find rules that cover n tokens in s. If succeeds then s is in L(G), else it is not 7 A graphical way to visualize CYK Initial graph: the input (terminals) Repeat: add non-terminal edges until no more can be added.
    [Show full text]
  • Parsing 1. Grammars and Parsing 2. Top-Down and Bottom-Up Parsing 3
    Syntax Parsing syntax: from the Greek syntaxis, meaning “setting out together or arrangement.” 1. Grammars and parsing Refers to the way words are arranged together. 2. Top-down and bottom-up parsing Why worry about syntax? 3. Chart parsers • The boy ate the frog. 4. Bottom-up chart parsing • The frog was eaten by the boy. 5. The Earley Algorithm • The frog that the boy ate died. • The boy whom the frog was eaten by died. Slide CS474–1 Slide CS474–2 Grammars and Parsing Need a grammar: a formal specification of the structures allowable in Syntactic Analysis the language. Key ideas: Need a parser: algorithm for assigning syntactic structure to an input • constituency: groups of words may behave as a single unit or phrase sentence. • grammatical relations: refer to the subject, object, indirect Sentence Parse Tree object, etc. Beavis ate the cat. S • subcategorization and dependencies: refer to certain kinds of relations between words and phrases, e.g. want can be followed by an NP VP infinitive, but find and work cannot. NAME V NP All can be modeled by various kinds of grammars that are based on ART N context-free grammars. Beavis ate the cat Slide CS474–3 Slide CS474–4 CFG example CFG’s are also called phrase-structure grammars. CFG’s Equivalent to Backus-Naur Form (BNF). A context free grammar consists of: 1. S → NP VP 5. NAME → Beavis 1. a set of non-terminal symbols N 2. VP → V NP 6. V → ate 2. a set of terminal symbols Σ (disjoint from N) 3.
    [Show full text]
  • Abstract Syntax Trees & Top-Down Parsing
    Abstract Syntax Trees & Top-Down Parsing Review of Parsing • Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree • Issues: – How do we recognize that s ∈ L(G) ? – A parse tree of s describes how s ∈ L(G) – Ambiguity: more than one parse tree (possible interpretation) for some string s – Error: no parse tree for some string s – How do we construct the parse tree? Compiler Design 1 (2011) 2 Abstract Syntax Trees • So far, a parser traces the derivation of a sequence of tokens • The rest of the compiler needs a structural representation of the program • Abstract syntax trees – Like parse trees but ignore some details – Abbreviated as AST Compiler Design 1 (2011) 3 Abstract Syntax Trees (Cont.) • Consider the grammar E → int | ( E ) | E + E • And the string 5 + (2 + 3) • After lexical analysis (a list of tokens) int5 ‘+’ ‘(‘ int2 ‘+’ int3 ‘)’ • During parsing we build a parse tree … Compiler Design 1 (2011) 4 Example of Parse Tree E • Traces the operation of the parser E + E • Captures the nesting structure • But too much info int5 ( E ) – Parentheses – Single-successor nodes + E E int 2 int3 Compiler Design 1 (2011) 5 Example of Abstract Syntax Tree PLUS PLUS 5 2 3 • Also captures the nesting structure • But abstracts from the concrete syntax a more compact and easier to use • An important data structure in a compiler Compiler Design 1 (2011) 6 Semantic Actions • This is what we’ll use to construct ASTs • Each grammar symbol may have attributes – An attribute is a property of a programming language construct
    [Show full text]
  • A Mobile App for Teaching Formal Languages and Automata
    Received: 21 December 2017 | Accepted: 19 March 2018 DOI: 10.1002/cae.21944 SPECIAL ISSUE ARTICLE A mobile app for teaching formal languages and automata Carlos H. Pereira | Ricardo Terra Department of Computer Science, Federal University of Lavras, Lavras, Brazil Abstract Formal Languages and Automata (FLA) address mathematical models able to Correspondence Ricardo Terra, Department of Computer specify and recognize languages, their properties and characteristics. Although Science, Federal University of Lavras, solid knowledge of FLA is extremely important for a B.Sc. degree in Computer Postal Code 3037, Lavras, Brazil. Science and similar fields, the algorithms and techniques covered in the course Email: [email protected] are complex and difficult to assimilate. Therefore, this article presents FLApp, Funding information a mobile application—which we consider the new way to reach students—for FAPEMIG (Fundação de Amparo à teaching FLA. The application—developed for mobile phones and tablets Pesquisa do Estado de Minas Gerais) running Android—provides students not only with answers to problems involving Regular, Context-free, Context-Sensitive, and Recursively Enumer- able Languages, but also an Educational environment that describes and illustrates each step of the algorithms to support students in the learning process. KEYWORDS automata, education, formal languages, mobile application 1 | INTRODUCTION In this article, we present FLApp (Formal Languages and Automata Application), a mobile application for teaching FLA Formal Languages and Automata (FLA) is an important area that helps students by solving problems involving Regular, of Computer Science that approaches mathematical models Context-free, Context-Sensitive, and Recursively Enumerable able to specify and recognize languages, their properties and Languages (levels 3 to 0, respectively), in addition to create an characteristics [14].
    [Show full text]
  • Compiler Construction
    UNIVERSITY OF CAMBRIDGE Compiler Construction An 18-lecture course Alan Mycroft Computer Laboratory, Cambridge University http://www.cl.cam.ac.uk/users/am/ Lent Term 2007 Compiler Construction 1 Lent Term 2007 Course Plan UNIVERSITY OF CAMBRIDGE Part A : intro/background Part B : a simple compiler for a simple language Part C : implementing harder things Compiler Construction 2 Lent Term 2007 A compiler UNIVERSITY OF CAMBRIDGE A compiler is a program which translates the source form of a program into a semantically equivalent target form. • Traditionally this was machine code or relocatable binary form, but nowadays the target form may be a virtual machine (e.g. JVM) or indeed another language such as C. • Can appear a very hard program to write. • How can one even start? • It’s just like juggling too many balls (picking instructions while determining whether this ‘+’ is part of ‘++’ or whether its right operand is just a variable or an expression ...). Compiler Construction 3 Lent Term 2007 How to even start? UNIVERSITY OF CAMBRIDGE “When finding it hard to juggle 4 balls at once, juggle them each in turn instead ...” character -token -parse -intermediate -target stream stream tree code code syn trans cg lex A multi-pass compiler does one ‘simple’ thing at once and passes its output to the next stage. These are pretty standard stages, and indeed language and (e.g. JVM) system design has co-evolved around them. Compiler Construction 4 Lent Term 2007 Compilers can be big and hard to understand UNIVERSITY OF CAMBRIDGE Compilers can be very large. In 2004 the Gnu Compiler Collection (GCC) was noted to “[consist] of about 2.1 million lines of code and has been in development for over 15 years”.
    [Show full text]
  • An Earley Parsing Algorithm for Range Concatenation Grammars
    An Earley Parsing Algorithm for Range Concatenation Grammars Laura Kallmeyer Wolfgang Maier Yannick Parmentier SFB 441 SFB 441 CNRS - LORIA Universitat¨ Tubingen¨ Universitat¨ Tubingen¨ Nancy Universite´ 72074 Tubingen,¨ Germany 72074 Tubingen,¨ Germany 54506 Vandœuvre, France [email protected] [email protected] [email protected] Abstract class of LCFRS has received more attention con- cerning parsing (Villemonte de la Clergerie, 2002; We present a CYK and an Earley-style Burden and Ljunglof,¨ 2005). This article proposes algorithm for parsing Range Concatena- new CYK and Earley parsers for RCG, formulat- tion Grammar (RCG), using the deduc- ing them in the framework of parsing as deduction tive parsing framework. The characteris- (Shieber et al., 1995). The second section intro- tic property of the Earley parser is that we duces necessary definitions. Section 3 presents a use a technique of range boundary con- CYK-style algorithm and Section 4 extends this straint propagation to compute the yields with an Earley-style prediction. of non-terminals as late as possible. Ex- periments show that, compared to previ- 2 Preliminaries ous approaches, the constraint propagation The rules (clauses) of RCGs1 rewrite predicates helps to considerably decrease the number ranging over parts of the input by other predicates. of items in the chart. E.g., a clause S(aXb) S(X) signifies that S is → 1 Introduction true for a part of the input if this part starts with an a, ends with a b, and if, furthermore, S is also true RCGs (Boullier, 2000) have recently received a for the part between a and b.
    [Show full text]
  • Advanced Parsing Techniques
    Advanced Parsing Techniques Announcements ● Written Set 1 graded. ● Hard copies available for pickup right now. ● Electronic submissions: feedback returned later today. Where We Are Where We Are Parsing so Far ● We've explored five deterministic parsing algorithms: ● LL(1) ● LR(0) ● SLR(1) ● LALR(1) ● LR(1) ● These algorithms all have their limitations. ● Can we parse arbitrary context-free grammars? Why Parse Arbitrary Grammars? ● They're easier to write. ● Can leave operator precedence and associativity out of the grammar. ● No worries about shift/reduce or FIRST/FOLLOW conflicts. ● If ambiguous, can filter out invalid trees at the end. ● Generate candidate parse trees, then eliminate them when not needed. ● Practical concern for some languages. ● We need to have C and C++ compilers! Questions for Today ● How do you go about parsing ambiguous grammars efficiently? ● How do you produce all possible parse trees? ● What else can we do with a general parser? The Earley Parser Motivation: The Limits of LR ● LR parsers use shift and reduce actions to reduce the input to the start symbol. ● LR parsers cannot deterministically handle shift/reduce or reduce/reduce conflicts. ● However, they can nondeterministically handle these conflicts by guessing which option to choose. ● What if we try all options and see if any of them work? The Earley Parser ● Maintain a collection of Earley items, which are LR(0) items annotated with a start position. ● The item A → α·ω @n means we are working on recognizing A → αω, have seen α, and the start position of the item was the nth token. ● Using techniques similar to LR parsing, try to scan across the input creating these items.
    [Show full text]
  • Lecture 3: Recursive Descent Limitations, Precedence Climbing
    Lecture 3: Recursive descent limitations, precedence climbing David Hovemeyer September 9, 2020 601.428/628 Compilers and Interpreters Today I Limitations of recursive descent I Precedence climbing I Abstract syntax trees I Supporting parenthesized expressions Before we begin... Assume a context-free struct Node *Parser::parse_A() { grammar has the struct Node *next_tok = lexer_peek(m_lexer); following productions on if (!next_tok) { the nonterminal A: error("Unexpected end of input"); } A → b C A → d E struct Node *a = node_build0(NODE_A); int tag = node_get_tag(next_tok); (A, C, E are if (tag == TOK_b) { nonterminals; b, d are node_add_kid(a, expect(TOK_b)); node_add_kid(a, parse_C()); terminals) } else if (tag == TOK_d) { What is the problem node_add_kid(a, expect(TOK_d)); node_add_kid(a, parse_E()); with the parse function } shown on the right? return a; } Limitations of recursive descent Recall: a better infix expression grammar Grammar (start symbol is A): A → i = A T → T*F A → E T → T/F E → E + T T → F E → E-T F → i E → T F → n Precedence levels: Nonterminal Precedence Meaning Operators Associativity A lowest Assignment = right E Expression + - left T Term * / left F highest Factor No Parsing infix expressions Can we write a recursive descent parser for infix expressions using this grammar? Parsing infix expressions Can we write a recursive descent parser for infix expressions using this grammar? No Left recursion Left-associative operators want to have left-recursive productions, but recursive descent parsers can’t handle left recursion
    [Show full text]