Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009.

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

lec02-parserCFG March 27, 2017 Syntax Analyzer
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Top-Down Parsing.
Parsing III (Eliminating left recursion, recursive descent parsing)
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
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.
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.
1 Languages and Compilers (SProg og Oversættere) Parsing.
Top-Down Parsing - recursive descent - predictive parsing
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.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
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.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lexical and Syntax Analysis
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
Top Down Parsing - Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Basic Parsing Algorithms: Earley Parser and Left Corner Parsing
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
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.
ISBN Chapter 4 Lexical and Syntax Analysis.
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.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Top-Down Parsing.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
CSE P501 – Compiler Construction Top-Down Parsing Predictive Parsing LL(k) Recursive Descent Grammar Grooming Left recursion Left factoring Next Spring.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
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)
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Comp 411 Principles of Programming Languages Lecture 3 Parsing
CSE 3302 Programming Languages
lec02-parserCFG May 8, 2018 Syntax Analyzer
Programming Languages Translator
Lexical and Syntax Analysis
Lecture #12 Parsing Types.
Parsing — Part II (Top-down parsing, left-recursion removal)
Syntax Analysis Chapter 4.
Context-free Languages
Parsing with Context Free Grammars
4 (c) parsing.
Parsing Techniques.
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Definition
Lecture 8: Top-Down Parsing
Programming Language Syntax 5
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
LL and Recursive-Descent Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
lec02-parserCFG May 27, 2019 Syntax Analyzer
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009

Top Down Parsing What is a context-free grammar (CFG)? A recursive definition of a set of strings; it is identical in format to the data definitions used in Comp 211 except for the fact that it defines sets of strings (using concatenation) rather than sets of trees (objects/structs) using tree construction. The root symbol of a grammar generates the language of he grammar. In other words, it designates the syntax of complete programs. Example. The language of expressions generated by ::= | + ::= | | ( ) ‏ Some sample strings generated by this CFG (5+10)+7 What is the fundamental difference between generating strings and generating trees? –The derivation of a generated tree is manifest in the structure of the tree. –The derivation of a generated string is not manifest in the structure of the string; it must be reconstructed by the parsing process. The reconstruction may be amibiguous.

Top Down Parsing cont. Data definition corresponding to sample grammar: Expr = Expr + Expr | Number | Variable Why is the data definition simpler? (Why did we introduce the syntactic category in the CFG?)‏ Consider the following example: Are strings a good data representation for programs? Why do we use string representations for programs?

Parsing algorithms Top-down (predictive) parsing: use k token lookahead to determine next syntactic category. Simplest description uses syntax diagrams expr : term : +expr number variable (expr)‏)‏ term

Key Idea in Top Down Parsing Use k token look-ahead to determine which direction to go at a branch point in the current syntax diagram. Example: 5+10 –Start parsing by reading first token 5 and matching the syntax diagram for expr –Must recognize a term ; invoke rule (diagram) for term –Select the number branch (path) based on current token 5 –Digest the current token to match number and read next token + ; return from term back to expr –Select the + branch in expr diagram based on current token –Digest the current token to match + and read the next token 10 –Must recognize an expr ; invoke rule (diagram) for expr –Must recognize a term ; invoke rule (diagram) for term –Select the number branch based on current token 10 –Digest the current token to match number and read next token EOF –Return from term ; return from expr

Designing Grammars for Top-Down Parsing Many different grammars generate the same language (set of strings): Requirement for any efficient parsing technique: determinism (non-ambiguity) For deterministic top-down parsing, we must design the grammar so that we can always tell what rule to use next starting from the root of the parse tree by looking ahead some small number (k) of tokens (formalized as LL(k) parsing). For top down parsing –Eliminate left recursion; use right recursion instead –Factor out common prefixes (as in syntax diagrams)‏ –Use iteration in syntax diagrams instead of right recursion where necessary –In extreme cases, hack the lexer to split token categories based on local context

Other Parsing Methods When we parse a sentence using a CFG, we effectively build a (parse) tree showing how to construct the sentence using the grammar. The root (start) symbol is the root of the tree and the tokens in the input stream are the leaves. Top-down (predictive) parsing is simple and intuitive, but is is not as powerful a deterministic parsing strategy as bottom-up parsing which is much more tedious. Bottom up deterministic parsing is formalized as LR(k) parsing. Every LL(k) grammar is also LR(1) but many LR(1) grammars are not LL(k) for any k. No sane person manually writes a bottom-up parser. In other words, there is no credible bottom-up alternative to recursive descent parsing. Bottom-up parsers are generated using parser-generator tools which until recently were almost universally based on LR(k) parsing (or some bottom-up restriction of LR(k) such as SLR(k) or LALR(k)). But some newer parser generators like javacc are based on LL(k) parsing. In DrJava, we have several different parsers including both recursive descent parsers and automatically generated parsers produced by javacc. Why is top-down parsing making inroads among parser generators? Top-down parsing is much easier to understand and more amenable to generating intelligible syntax diagnostics. Why is recursive descent still used in production compilers? Because it is much easier to generate sensible error diagnostics. If you want to learn about the mechanics of bottom-up parsing, take Comp 412.