Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Similar documents
Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

CSE302: Compiler Design

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Top-Down Parsing and Intro to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

Syntactic Analysis. Top-Down Parsing

Introduction to Bottom-Up Parsing

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

Introduction to Bottom-Up Parsing

Shift-Reduce parser E + (E + (E) E [a-z] In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules.

Context free languages

Syntax Analysis Part I

Bottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id

Bottom-Up Syntax Analysis

n Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)

THEORY OF COMPILATION

Syntax Analysis Part I

CA Compiler Construction

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

CS 314 Principles of Programming Languages

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 7a Syntax Analysis Elias Athanasopoulos

Creating a Recursive Descent Parse Table

Compiling Techniques

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Computer Science 160 Translation of Programming Languages

Parsing -3. A View During TD Parsing

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

CS415 Compilers Syntax Analysis Bottom-up Parsing

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16)

CS 4120 Lecture 3 Automating lexical analysis 29 August 2011 Lecturer: Andrew Myers. 1 DFAs

Compiling Techniques

1. For the following sub-problems, consider the following context-free grammar: S AA$ (1) A xa (2) A B (3) B yb (4)

Syntax Analysis (Part 2)

CS415 Compilers Syntax Analysis Top-down Parsing

Compiling Techniques

CS153: Compilers Lecture 5: LL Parsing

Compiler Design Spring 2017

Syntax Analysis Part III

Syntax Analysis - Part 1. Syntax Analysis

Computer Science 160 Translation of Programming Languages

Compiler Principles, PS4

CS Rewriting System - grammars, fa, and PDA

CS 406: Bottom-Up Parsing

CSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar

Recursive descent for grammars with contexts

The transformation to right derivation is called the canonical reduction sequence. Bottom-up analysis

Announcements. H6 posted 2 days ago (due on Tue) Midterm went well: Very nice curve. Average 65% High score 74/75

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Chapter 4: Bottom-up Analysis 106 / 338

Compiler Construction Lectures 13 16

LR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Bottom-Up Syntax Analysis

Parsing VI LR(1) Parsers

Intro to Theory of Computation

Theory of Computation

Pushdown Automata (Pre Lecture)

5/10/16. Grammar. Automata and Languages. Today s Topics. Grammars Definition A grammar G is defined as G = (V, T, P, S) where:

Plan for Today and Beginning Next week (Lexical Analysis)

CPS 220 Theory of Computation Pushdown Automata (PDA)

Lecture 11 Context-Free Languages

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

Theory of Computation (IV) Yijia Chen Fudan University

CSE 105 THEORY OF COMPUTATION

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Pushdown Automata: Introduction (2)

Theory of Computation (VI) Yijia Chen Fudan University

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Exercises. Exercise: Grammar Rewriting

CFGs and PDAs are Equivalent. We provide algorithms to convert a CFG to a PDA and vice versa.

EXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100

Announcement: Midterm Prep

Lecture 11 Sections 4.5, 4.7. Wed, Feb 18, 2009

Compiling Techniques

Compiling Techniques

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 3. Y.N. Srikant

CFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.

Homework Assignment 6 Answers

LL(1) Grammar and parser

5 Context-Free Languages

CONTEXT FREE GRAMMAR AND

This lecture covers Chapter 5 of HMU: Context-free Grammars

Compiler Principles, PS7

MA/CSSE 474 Theory of Computation

Handout 8: Computation & Hierarchical parsing II. Compute initial state set S 0 Compute initial state set S 0

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

Review. Earley Algorithm Chapter Left Recursion. Left-Recursion. Rule Ordering. Rule Ordering

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

Introduction and Motivation. Introduction and Motivation. Introduction to Computability. Introduction and Motivation. Theory. Lecture5: Context Free

Abstract Parsing for Two-staged Language. Soonho Kong May 1, 2009

UNIT-VIII COMPUTABILITY THEORY

Syntax Directed Transla1on

MA/CSSE 474 Theory of Computation

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }

1. For the following sub-problems, consider the following context-free grammar: S AB$ (1) A xax (2) A B (3) B yby (5) B A (6)

Transcription:

Language Processing Systems Prof. Mohamed Hamada Software Engineering La. The University of izu Japan

Syntax nalysis (Parsing)

1. Uses Regular Expressions to define tokens 2. Uses Finite utomata to recognize tokens next char get next char Source Program lexical analyzer next token get next token symol tale Syntax analyzer (Contains a record for each identifier) Uses Top-down parsing or Bottom-up parsing To construct a Parse tree

Parsing Top Down Parsing Bottom Up Parsing Predictive Parsing LL(k) Parsing Shift-reduce Parsing LR(k) Parsing Left Recursion Left Factoring

Parsing Top Down Parsing Bottom Up Parsing Predictive Parsing LL(k) Parsing Left Recursion Left Factoring Shift-reduce Parsing LR(k) Parsing Top-down parsers: starts constructing the parse tree at the top (root) of the tree and move down towards the leaves. Easy to implement y hand, ut work with restricted grammars. Example: predictive parsers

Predictive Parser How it works? 1. Construct the parsing tale from the given grammar 2. pply the predictive parsing algorithm to construct the parse tree

Predictive Parser 1. Construct the parsing tale from the given grammar The following algorithm shows how we can construct the parsing tale: Input: a grammar G Output: the corresponding parsing tale M Method: For each production à α of the grammar do the following steps: 1. For each terminal a in FIRST(α), add à α to M[,a]. 2. If λ in FIRST(α), add à α to M[,] for each terminal in FOLLOW(). 3. If λ FIRST(α) and $ in FOLLOW(), add à α to M[,$]

Predictive Parser 2. pply the predictive parsing algorithm to construct the parse tree The following algorithm shows how we can construct the move parsing tale for an input string w$ with respect to a given grammar G. set ip to point to the first symol of the input string w$ repeat if Top(stack) is a terminal or $ then if Top(stack) = Current-Input(ip) then Pop(stack) and advance ip else null else if M[X,a]= Xà Y1 Y 2 Y k then egin Pop(stack); Push Y 1 ; Y 2 ; ; Y k onto the stack, with Y 1 on top; Output the production Xà Y 1 Y 2 Y k end else null until Top(stack) = $ (i.e. the stack ecome empty)

Predictive Parser 2. pply the predictive parsing algorithm to construct the parse tree Example Grammar: E TE E +TE λ T FT T *FT λ F ( E ) id Parsing Tale: NON- TERMINL INPUT SYMBOL id + * ( ) $ E E TE E TE E E +TE E λ E λ T T FT T FT T T λ T *FT T λ T λ F F id F (E)

Set ip to point to the first symol of the input string w$ repeat if Top(stack) is a terminal or $ then if Top(stack) = Current-Input(ip) then Pop(stack) and advance ip else else null if if M[X,a]= Xà Y1 Y 1 Y 22 Y k k then egin Pop(stack); Push Y 1 ; Y 2 ; ; Y k onto the stack, with Y 1 on top; Output the production Xà Y 1 Y 2 Y end 1 ; Y 2 ; ; Y k ; else null until Top(stack)=$ = $ (i.e. the stack ecome empty) INPUT: id + id * ip id $ OUTPUT: E STCK: E T E $ $ Predictive Parsing Program T E PRSING TBLE: NON- TERMINL INPUT SYMBOL id + * ( ) $ E E TE E TE E E +TE E λ E λ T T FT T FT T T λ T *FT T λ T λ F F id F (E)

Predictive Parser INPUT: id + id * id $ OUTPUT: E STCK: T F E T E $ $ PRSING TBLE: NON- TERMINL Predictive Parsing Program T E F T INPUT SYMBOL id + * ( ) $ E E TE E TE E E +TE E λ E λ T T FT T FT T T λ T *FT T λ T λ F F id F (E)

Predictive Parser INPUT: id + id * id $ OUTPUT: E STCK: id T F E T E $ $ PRSING TBLE: NON- TERMINL Predictive Parsing Program T E F T INPUT SYMBOL id + * ( ) $ E E TE E TE E E +TE E λ E λ T T FT T FT T T λ T *FT T λ T λ F F id F (E) id

Predictive Parser ction when Top(Stack) = input $ : Pop stack, advance input. INPUT: id + id * id $ OUTPUT: E STCK: id F T E $ PRSING TBLE: NON- TERMINL Predictive Parsing Program T E F T INPUT SYMBOL id + * ( ) $ E E TE E TE E E +TE E λ E λ T T FT T FT T T λ T *FT T λ T λ F F id F (E) id

Predictive Parser INPUT: id + id * id $ OUTPUT: E STCK: T E E $ $ Predictive Parsing Program T E F T id λ PRSING TBLE: NON- TERMINL INPUT SYMBOL id + * ( ) $ E E TE E TE E E +TE E λ E λ T T FT T FT T T λ T *FT T λ T λ F F id F (E)

Predictive Parser The predictive parser proceeds in this fashion emiting the following productions: E +TE T FT F id T * FT F id T λ E λ F T id E T E When Top(Stack) = input = $ the parser halts and accepts the input string. λ + T E F T id * F T id λ λ

LL(k) Parser This parser parses from left to right, and does a leftmost-derivation. It looks up 1 symol ahead to choose its next action. Therefore, it is known as a LL(1) parser. n LL(k) parser looks k symols ahead to decide its action. LL(1) grammar whose parsing tale has no multiply-defined entries LL(1) grammars enjoys several nice properties: for example they are not amiguous and not left recursive.

LL(1) grammar LL(k) whose Parser parsing tale has no multiply-defined entries Example 1 Whose PRSINGTBLE: The grammar E TE E +TE λ T FT T *FT λ F ( E ) id NON- TERMINL INPUT SYMBOL id + * ( ) $ E E TE E TE E E +TE E λ E λ T T FT T FT T T λ T *FT T λ T λ F F id F (E) Is LL(1) grammar

LL(k) Parser LL(1) grammar whose parsing tale has no multiply-defined entries Example 2 Whose PRSINGTBLE: The grammar S ietss` a S es λ E F NON- INPUT SYMBOL TERMINL a e i t $ S S a S ietss S S λ S es S λ E E Is NOT LL(1) grammar

Parsing Top Down Parsing Bottom Up Parsing Predictive Parsing LL(k) Parsing Shift-reduce Parsing LR(k) Parsing Left Recursion Left Factoring

Bottom-Up Parsers Bottom-up parsers: uild the nodes on the ottom of the parse tree first. Suitale for automatic parser generation, handle a larger class of grammars. Examples: shift-reduce parser (or LR(k) parsers)

Bottom-up Parsing No prolem with left-recursion Widely used in practice LR(1), SLR(1), LLR(1)

Grammar Hierarchy Non-amiguous CFG CLR(1) LLR(1) LL(1) SLR(1)

Bottom-up Parsing Works from tokens to start-symol Repeat: identify handle - reducile sequence: non-terminal is not constructed ut all its children have een constructed reduce - construct non-terminal and update stack Until reducing to start-symol

Bottom-up Parsing 1 + (2) + (3) E E + (E) E + (2) + (3) E i i = 0,1, 2,, 9 E + (E) + (3) E + (3) E E + (E) E E E E E 1 + ( 2 ) + ( 3 )

Bottom-up Parsing Is the following grammar LL(1)? NO E E + (E) E i 1 + (2) 1 + (2) + (3) But this is a useful grammar

Bottom-Up Parser ottom-up parser, or a shift-reduce parser, egins at the leaves and works up to the top of the tree. The reduction steps trace a rightmost derivation on reverse. Consider the Grammar: S abe c B d We want to parse the input string acde.

Bottom-Up Parser Example INPUT: a c d e $ OUTPUT: Production S abe c B d Bottom-Up Parsing Program

Bottom-Up Parser Example INPUT: a c d e $ OUTPUT: Production S abe c B d Bottom-Up Parsing Program

Bottom-Up Parser Example INPUT: a c d e $ OUTPUT: Production S abe c B d Bottom-Up Parsing Program

Bottom-Up Parser Example INPUT: a c d e $ OUTPUT: Production S abe c B d Bottom-Up Parsing Program We are not reducing here in this example. parser would reduce, get stuck and then acktrack!

Bottom-Up Parser Example INPUT: a c d e $ OUTPUT: Production S abe c B d Bottom-Up Parsing Program c

Bottom-Up Parser Example INPUT: a d e $ OUTPUT: Production S abe c B d Bottom-Up Parsing Program c

Bottom-Up Parser Example INPUT: a d e $ OUTPUT: Production S abe c B d Bottom-Up Parsing Program c B d

Bottom-Up Parser Example INPUT: a B e $ OUTPUT: Production S abe c B d Bottom-Up Parsing Program c B d

Bottom-Up Parser Example INPUT: a B e $ OUTPUT: S Production S abe c B d Bottom-Up Parsing Program a c B d e

Bottom-Up Parser Example INPUT: S $ OUTPUT: S Production S abe c B d Bottom-Up Parsing Program a c B d e This parser is known as an LR Parser ecause it scans the input from Left to right, and it constructs a Rightmost derivation in reverse order.

Bottom-Up Parser Example The scanning of productions for matching with handles in the input string, and acktracking makes the method used in the previous example very inefficient. Can we do etter? See next lecture