CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

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.
Bottom-up Parsing A general style of bottom-up syntax analysis, known as shift-reduce parsing. Two types of bottom-up parsing: Operator-Precedence parsing.
LR-Grammars LR(0), LR(1), and LR(K).
Mooly Sagiv and Roman Manevich School of Computer Science
Functional Design and Programming Lecture 9: Lexical analysis and parsing.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
ISBN Chapter 4 Lexical and Syntax Analysis.
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
ISBN Chapter 4 Lexical and Syntax Analysis.
Parsing V Introduction to LR(1) Parsers. from Cooper & Torczon2 LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
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.
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
Compiler construction in4020 – lecture 3 Koen Langendoen Delft University of Technology The Netherlands.
Bottom-up parsing Goal of parser : build a derivation
ISBN Lecture 04 Lexical and Syntax Analysis.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
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,
Parsing Chapter 4 Parsing2 Outline Top-down v.s. Bottom-up Top-down parsing Recursive-descent parsing LL(1) parsing LL(1) parsing algorithm First.
Joey Paquet, 2000, 2002, 2012, Lecture 6 Bottom-Up Parsing.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
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.
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
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
LANGUAGE TRANSLATORS: WEEK 17 scom.hud.ac.uk/scomtlm/cis2380/ See Appel’s book chapter 3 for support reading Last Week: Top-down, Table driven parsers.
Lexical and Syntax Analysis
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
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.
ISBN Chapter 4 Lexical and Syntax Analysis.
ISBN Chapter 4 Lexical and Syntax Analysis.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 4.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
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.
Earley’s Algorithm J. Earley, "An efficient context-free parsing algorithm", Communications of the Association for Computing Machinery, 13:2:94-102, 1970."An.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Bernd Fischer RW713: Compiler and Software Language Engineering.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
Parsing V LR(1) Parsers. LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited right context (1 token) for handle recognition.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
1 Chapter 6 Bottom-Up Parsing. 2 Bottom-up Parsing A bottom-up parsing corresponds to the construction of a parse tree for an input tokens beginning at.
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.
Syntax and Semantics Structure of programming languages.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Unit-3 Bottom-Up-Parsing.
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Bottom-Up Syntax Analysis
Regular Grammar - Finite Automaton
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
Compiler Construction
Presentation transcript:

CS3230R

What is a parser?

What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear time

Deterministic context-free what? Class of context-free languages that can be accepted by a deterministic pushdown automaton

Deterministic context-free what?  Unambiguous  Of great practical use

Context-sensitive grammars int x; typedef int x;

Context-sensitive grammars MyObject object; MyObject object(parameters); MyObject object(); // ?

What is an LR parser?  Left-to-right, Rightmost derivation  Deterministic – single correct parse without guesswork or backtracking  Lookahead to avoid guessing or backtracking

What is an LR parser?  Bottom-up construction of syntax tree

What is an LR parser?  Unsuited for human languages, which require more flexible but slower methods  Shift-reduce parser

Shift-reduce  Shift Advances the input stream by one symbol That symbol becomes a new single-node parse tree  Reduce Applies a grammar rule to some of the recent parse trees Joins them into one with a new root symbol

Shift-reduce  Shifts and reduces until all input has been consumed, or until a syntax error is encountered  Differs from other parsers in when to reduce and breaking ties between rules

Decisions, decisions…  When to shift, when to reduce?

Decisions, decisions…  Naïve approach: Compare current parse trees against rules Reduce if any matches Shift if no matches  Check all rules every time  Parser gets slower and slower for large input programs

Decisions, decisions…  Observation: CFGs are unambiguous Finite number of states

Decisions, decisions…  Preprocessing Encode each state the parser may be in with a number Goal → Sums EOF Sums → Sums + Products Sums → Products Products → Products * Value Products → Value Value → int Value → id

Decisions, decisions…  Preprocessing Encode each state the parser may be in with a number Sums → ● Sums + Products Sums → Sums ● + Products Sums → Sums + ● Products Sums → Sums + Products ●

Decisions, decisions…  Preprocessing Decide in advance whether to shift or reduce Note what state the parser will be in afterwards Build a static parse table with this information

Decisions, decisions…  Parse table

Decisions, decisions…  LR parsers are table-driven FSMs

Decisions, decisions…  LR parsers are table-driven FSMs  Tables are large and difficult to compute by hand  LR parsers are generated from grammars written in DSLs

Example ::= | | fun (,...) -> | | ( ) |

Example ::= | if then else | let = in | (,...)

Example

Thanks!