Parsing Chapter 15. The Job of a Parser Examine a string and decide whether or not it is a syntactically well-formed member of L(G), and If it is, assign.

Slides:



Advertisements
Similar presentations
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Advertisements

Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Lecture # 11 Grammar Problems.
CS466(Prasad)L7Parse1 Parsing Recognition of strings in a language.
PARSING WITH CONTEXT-FREE GRAMMARS
YANGYANG 1 Chap 5 LL(1) Parsing LL(1) left-to-right scanning leftmost derivation 1-token lookahead parser generator: Parsing becomes the easiest! Modifying.
Mooly Sagiv and Roman Manevich School of Computer Science
LESSON 18.
Top-Down Parsing.
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
ISBN Chapter 4 Lexical and Syntax Analysis.
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
ISBN Chapter 4 Lexical and Syntax Analysis.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
Professor Yihjia Tsai Tamkang University
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
ISBN Lecture 04 Lexical and Syntax Analysis.
Chapter 3 Chang Chi-Chung Parse tree intermediate representation The Role of the Parser Lexical Analyzer Parser Source Program Token Symbol.
Lexical and syntax analysis
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax and Semantics Structure of programming languages.
Parsing. Goals of Parsing Check the input for syntactic accuracy Return appropriate error messages Recover if possible Produce, or at least traverse,
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Parsing G Programming Languages May 24, 2012 New York University Chanseok Oh
LINGUISTICA GENERALE E COMPUTAZIONALE ANALISI SINTATTICA (PARSING)
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Context-Free Grammars Chapter 11. Languages and Machines.
Lexical and Syntax Analysis
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Syntax and Semantics Structure of programming languages.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes)
Bernd Fischer RW713: Compiler and Software Language Engineering.
Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009.
Basic Parsing Algorithms: Earley Parser and Left Corner Parsing
Compilation With an emphasis on getting the job done quickly Copyright © – Curt Hill.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 4.
csa3050: Parsing Algorithms 11 CSA350: NLP Algorithms Parsing Algorithms 1 Top Down Bottom-Up Left Corner.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Bc. Jozef Lang (xlangj01) Bc. Zoltán Zemko (xzemko01) Increasing power of LL(k) parsers.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Top-Down Parsing.
PZ03BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03BX –Recursive descent parsing Programming Language.
LECTURE 7 Lex and Intro to Parsing. LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens)
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Structure and Ambiguity Removing Ambiguity Chomsky Normal Form Pushdown Automata Intro (who is he foolin', thinking that there will be time to get to this?)
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
CS 154 Formal Languages and Computability March 22 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Comp 411 Principles of Programming Languages Lecture 3 Parsing
Programming Languages Translator
Lexical and Syntax Analysis
Lecture #12 Parsing Types.
4 (c) parsing.
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
R.Rajkumar Asst.Professor CSE
CFGs: Formal Definition
Presentation transcript:

Parsing Chapter 15

The Job of a Parser Examine a string and decide whether or not it is a syntactically well-formed member of L(G), and If it is, assign to it a parse tree that describes its structure and thus can be used as the basis for further interpretation. Given a context-free grammar G:

Problems with Solutions So Far We want to use a natural grammar that will produce a natural parse tree. But: decideCFLusingGrammar, requires a grammar that is in Chomsky normal form. decideCFLusingPDA, requires a grammar that is in Greibach normal form. We want an efficient parser. But both procedures require search and take time that grows exponentially in the length of the input string. All either procedure does is to determine membership in L(G). It does not produce parse trees.

Easy Issues Actually building parse trees: Augment the parser with a function that builds a chunk of tree every time a rule is applied. Using lookahead to reduce nondeterminism: It is often possible to reduce (or even eliminate) nondeterminism by allowing the parser to look ahead at the next one or more input symbols before it makes a decision about what to do.

Dividing the Process Lexical analysis: done in linear time with a DFSM Parsing: done in, at worst O (n 3 ) time.

Lexical Analysis level = observation ; Lexical analysis produces a stream of tokens: id = id - id

Specifying id with a Grammar id  identifier | integer | float identifier  letter alphanum alphanum  letter alphnum | digit alphnum |  integer  - unsignedint | unsignedint unsignedint  digit | digit unsignedint digit  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ….

Using Reg Ex’s to Specify an FSM There exist simple tools for building lexical analyzers. The first important such tool: Lex

Top-Down, Depth-First Parsing S  NP VP $ NP  the N | N | ProperNoun N  cat | dogs | bear | girl | chocolate | rifle ProperNoun  Chris | Fluffy VP  V | V NP V  like | likes | thinks | shot | smells Input: the cat likes chocolate $

Top-Down, Depth-First Parsing S  NP VP $ NP  the N | N | ProperNoun N  cat | dogs | bear | girl | chocolate | rifle ProperNoun  Chris | Fluffy VP  V | V NP V  like | likes | thinks | shot | smells Input: the cat likes chocolate $

Top-Down, Depth-First Parsing S  NP VP $ NP  the N | N | ProperNoun N  cat | dogs | bear | girl | chocolate | rifle ProperNoun  Chris | Fluffy VP  V | V NP V  like | likes | thinks | shot | smells Input: the cat likes chocolate $

Top-Down, Depth-First Parsing S  NP VP $ NP  the N | N | ProperNoun N  cat | dogs | bear | girl | chocolate | rifle ProperNoun  Chris | Fluffy VP  V | V NP V  like | likes | thinks | shot | smells Input: the cat likes chocolate $

Top-Down, Depth-First Parsing S  NP VP $ NP  the N | N | ProperNoun N  cat | dogs | bear | girl | chocolate | rifle ProperNoun  Chris | Fluffy VP  V | V NP V  like | likes | thinks | shot | smells Input: the cat likes chocolate $

Top-Down, Depth-First Parsing S  NP VP $ NP  the N | N | ProperNoun N  cat | dogs | bear | girl | chocolate | rifle ProperNoun  Chris | Fluffy VP  V | V NP V  like | likes | thinks | shot | smells Input: the cat likes chocolate $ Fail

Top-Down, Depth-First Parsing S  NP VP $ NP  the N | N | ProperNoun N  cat | dogs | bear | girl | chocolate | rifle ProperNoun  Chris | Fluffy VP  V | V NP V  like | likes | thinks | shot | smells Input: the cat likes chocolate $ Backup to:

Top-Down, Depth-First Parsing S  NP VP $ NP  the N | N | ProperNoun N  cat | dogs | bear | girl | chocolate | rifle ProperNoun  Chris | Fluffy VP  V | V NP V  like | likes | thinks | shot | smells Input: the cat likes chocolate $

Top-Down, Depth-First Parsing S  NP VP $ NP  the N | N | ProperNoun N  cat | dogs | bear | girl | chocolate | rifle ProperNoun  Chris | Fluffy VP  V | V NP V  like | likes | thinks | shot | smells Input: the cat likes chocolate $ Built, unbuilt, built again

Left-Recursive Rules E  E + T E  T T  T  F T  F F  (E) F  id On input: id + id + id : Then: And so forth.

Indirect Left Recursion S  Y a Y  S a Y   This form too can be eliminated.

Using Lookahead and Left Factoring Change the parsing algorithm so that it exploits the ability to look one symbol ahead in the input before it makes a decision about what to do next, and Change the grammar to help the parser procrastinate decisions. Goal: Procrastinate branching as long as possible. To do that, we will:

LL(k) Grammars An LL(k) grammar allows a predictive parser: that scans its input Left to right to build a Left-most derivation if it is allowed k lookahead symbols. Every LL(k) grammar is unambiguous (because every string it generates has a unique left-most derivation). But not every unambiguous grammar is LL(k).

Recursive Descent Parsing A  BA | a B  b B | b A(n: parse tree node labeled A) = case (lookahead = b : /* Use A  BA. Invoke B on a new daughter node labeled B. Invoke A on a new daughter node labeled A. lookahead = a : /* Use A  a. Create a new daughter node labeled a.

LR(k) Grammars G is LR(k), for any positive integer k, iff it is possible to build a deterministic parser for G that: scans its input Left to right and, for any input string in L(G), builds a Rightmost derivation, looking ahead at most k symbols. A language is LR(k) iff there is an LR(k) grammar for it.

LR(k) Grammars The class of LR(k) languages is exactly the class of deterministic context-free languages. If a language is LR(k), for some k, then it is also LR(1).