CS 3304 Comparative Languages

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Copyright © 2009 Elsevier Chapter 2 :: Programming Language Syntax Programming Language Pragmatics Michael L. Scott.
Syntax. Syntax defines what is grammatically valid in a programming language – Set of grammatical rules – E.g. in English, a sentence cannot begin with.
Parsing G Programming Languages May 24, 2012 New York University Chanseok Oh
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Lecture 5: Syntax Analysis (Section 2.2) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC.
Copyright © 2009 Elsevier Chapter 2 :: Programming Language Syntax Programming Language Pragmatics Michael L. Scott.
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.
Top-Down Parsing.
Syntax Analyzer (Parser)
Syntax and Semantics Structure of programming languages.
CS 3304 Comparative Languages
Lexical and Syntax Analysis
lec02-parserCFG May 8, 2018 Syntax Analyzer
Parsing — Part II (Top-down parsing, left-recursion removal)
CS 326 Programming Languages, Concepts and Implementation
Programming Languages Translator
CS510 Compiler Lecture 4.
Compiler design Bottom-up parsing Concepts
50/50 rule You need to get 50% from tests, AND
Lexical and Syntax Analysis
Chapter 2 :: Programming Language Syntax
Unit-3 Bottom-Up-Parsing.
Lecture #12 Parsing Types.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
Chapter 2 :: Programming Language Syntax
Chapter 2 :: Programming Language Syntax
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
CS 404 Introduction to Compiler Design
CS 363 – Chapter 2 The first 2 phases of compilation Scanning Parsing
Bottom-Up Syntax Analysis
Chapter 2 :: Programming Language Syntax
4 (c) parsing.
Syntax.
Parsing Techniques.
Top-Down Parsing.
Syntax Analysis Sections :.
Compiler Design 4. Language Grammars
Lexical and Syntax Analysis
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Top-Down Parsing CS 671 January 29, 2008.
Parsing #2 Leonidas Fegaras.
COP4020 Programming Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Chapter 2 :: Programming Language Syntax
Lecture 7: Introduction to Parsing (Syntax Analysis)
Ambiguity, Precedence, Associativity & Top-Down Parsing
Bottom Up Parsing.
R.Rajkumar Asst.Professor CSE
Programming Language Syntax 5
Parsing IV Bottom-up Parsing
LR Parsing. Parser Generators.
Parsing #2 Leonidas Fegaras.
Parsing — Part II (Top-down parsing, left-recursion removal)
Chapter 2 :: Programming Language Syntax
Kanat Bolazar February 16, 2010
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
Chapter 2 :: Programming Language Syntax
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
lec02-parserCFG May 27, 2019 Syntax Analyzer
Chapter 2 :: Programming Language Syntax
Presentation transcript:

CS 3304 Comparative Languages Lecture 4: Parsing 26 January 2012

Terminology context-free grammar (CFG) symbols production terminals (tokens) non-terminals production derivations (left-most and right-most - canonical) parse trees sentential form

Parsing By analogy to regular expressions and DFAs, a context-free grammar (CFG) is a generator for a context-free language (CFL): A parser is a language recognizer. There is an infinite number of grammars for every context- free language: Not all grammars are created equal, however. It turns out that for any CFG we can create a parser that runs in O(n3) time. There are two well-known parsing algorithms that permit this: Early's algorithm. Cooke-Younger-Kasami (CYK) algorithm. O(n3) time is clearly unacceptable for a parser in a compiler - too slow.

Faster Parsing Fortunately, there are large classes of grammars for which we can build parsers that run in linear time: The two most important classes are called LL and LR. LL stands for 'Left-to-right, Leftmost derivation'. LR stands for 'Left-to-right, Rightmost derivation’. LL parsers are also called 'top-down', or 'predictive' parsers & LR parsers are also called 'bottom-up', or 'shift-reduce' parsers. There are several important sub-classes of LR parsers. SLR. LALR. We won't be going into detail on the differences between them.

Parsing A number in parentheses after LL or LR indicates how many tokens of look-ahead are required in order to parse: Almost all real compilers use one token of look-ahead. Every LL(1) grammar is also LR(1), though right recursion in production tends to require very deep stacks and complicates semantic analysis Every CFL that can be parsed deterministically has an SLR(1) grammar (which is LR(1)) Every deterministic CFL with the prefix property (no valid string is a prefix of another valid string) has an LR(0) grammar. The expression grammar (with precedence and associativity) you saw before is LR(1), but not LL(1).

LL(1) Grammar (Figure 2.15) program → stmt list $$$ stmt_list → stmt stmt_list | ε stmt → id := expr | read id | write expr expr → term term_tail term_tail → add op term term_tail term → factor fact_tailt fact_tail → mult_op fact fact_tail factor → ( expr ) | id | number add_op → + | - mult_op → * | /

LL Parsing Like the bottom-up grammar, this one captures associativity and precedence, but most people don't find it as pretty: For one thing, the operands of a given operator aren't in a RHS together! However, the simplicity of the parsing algorithm makes up for this weakness. How do we parse a string with this grammar? By building the parse tree incrementally.

LL Parsing Example Example (average program): read A read B sum := A + B write sum write sum / 2 We start at the top and predict needed productions on the basis of the current left-most non-terminal in the tree and the current input token.

LL Parsing Tree (Figure 2.17)

Table-Driven LL Parsing Table-driven LL parsing: you have a big loop in which you repeatedly look up an action in a two-dimensional table based on current leftmost non-terminal and current input token. The actions are: (1) match a terminal. (2) predict a production. (3) announce a syntax error. To keep track of the left-most non-terminal, you push the as- yet-unseen portions of productions onto a stack (Figure 2.20). The key thing to keep in mind is that the stack contains all the stuff you expect to see between now and the end of the program: What you predict you will see.

LL Parsing Table

LL Parsing Problem: Left Recursion Example: id_list → id | id_list , id is replaced by id_list → id id_list_tail id_list_tail → , id id_list_tail | epsilon We can get rid of all left recursion mechanically in any grammar.

LL Parsing Problem: Common Prefixes Solved by "left-factoring”. Example: stmt → id := expr | id ( arg_list ) is replaced by stmt → id id_stmt_tail id_stmt_tail → := expr | ( arg_list) We can eliminate left-factor mechanically.

LL Parsing Note that eliminating left recursion and common prefixes does NOT make a grammar LL: There are infinitely many non-LL LANGUAGES, and the mechanical transformations work on them just fine. The few that arise in practice, however, can generally be handled with kludges. Problems trying to make a grammar LL(1): The "dangling else" problem prevents grammars from being LL(1) (or in fact LL(k) for any k).

Pascal Example The following natural grammar fragment is ambiguous: stmt → if cond then_clause else_clause | other_stuff then_clause → then stmt else_clause → else stmt | epsilon The less natural grammar fragment can be parsed bottom-up but not top-down: stmt → balanced_stmt | unbalanced_stmt balanced_stmt → if cond then balanced_stmt else balanced_stmt | other_stuff unbalanced_stmt → if cond then stmt | if cond then balanced_stmt else unbalanced_stmt

Addressing the Dangling “else” The usual approach, whether top-down OR bottom-up, is to use the ambiguous grammar together with a disambiguating rule that says: Else goes with the closest then or, More generally, the first of two possible productions is the one to predict (or reduce). Better yet, languages (since Pascal) generally employ explicit end-markers, which eliminate this problem, e.g. Modula-2: if A = B then if C = D then E := F end else G := H end Ada says 'end if'; other languages say 'fi'

Problem with End Markers One problem with end markers is that they tend to bunch up. In Pascal you say: if A = B then … else if A = C then … else if A = D then … else if A = E then … else ...; With end markers this becomes: if A = B then … else if A = C then … else if A = D then … else if A = E then … else ...; end; end; end; end;

LL Parsing If any token belongs to the predict set of more than one production with the same LHS, then the grammar is not LL(1). A conflict can arise because: The same token can begin more than one RHS. It can begin one RHS and can also appear after the LHS in some valid program, and one possible RHS is ε.

LR Parsing LR parsers are almost always table-driven: Like a table-driven LL parser, an LR parser uses a big loop to repeatedly inspects a two-dimensional table to find out what action to take. Unlike the LL parser,, the LR driver has non-trivial state (like a DFA), and the table is indexed by current input token and current state. The stack contains a record of what has been seen SO FAR (NOT what is expected).