컴파일러구성 제 2 강 Recursive-descent Parser / Predictive Parser

Similar documents
LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

Lexical and Syntax Analysis. Top-Down Parsing

Table-Driven Parsing

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Compilers. Predictive Parsing. Alex Aiken

Top down vs. bottom up parsing

CS502: Compilers & Programming Systems

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

1 Introduction. 2 Recursive descent parsing. Predicative parsing. Computer Language Implementation Lecture Note 3 February 4, 2004

CA Compiler Construction

Lexical and Syntax Analysis

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

Talen en Compilers. Johan Jeuring , period 2. January 17, Department of Information and Computing Sciences Utrecht University

컴파일러입문 제 6 장 구문분석

Lexical and Syntax Analysis (2)

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38


LR Parsing. The first L means the input string is processed from left to right.

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Monday, September 13, Parsers

It parses an input string of tokens by tracing out the steps in a leftmost derivation.

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

Abstract Syntax Trees & Top-Down Parsing

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Syntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Syntax Analysis. The Big Picture. The Big Picture. COMP 524: Programming Languages Srinivas Krishnan January 25, 2011

LL Parsing: A piece of cake after LR

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

Chapter 4. Lexical and Syntax Analysis

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

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

CMSC 330: Organization of Programming Languages

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

CS 321 Programming Languages and Compilers. VI. Parsing

CSCI312 Principles of Programming Languages

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Wednesday, August 31, Parsers

Types of parsing. CMSC 430 Lecture 4, Page 1

3. Parsing. Oscar Nierstrasz

4. Lexical and Syntax Analysis

CMSC 330 Practice Problem 4 Solutions

Compiler Design 1. Top-Down Parsing. Goutam Biswas. Lect 5

CPS 506 Comparative Programming Languages. Syntax Specification

Question Points Score

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

4. Lexical and Syntax Analysis

Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 1. Top-Down Parsing. Lect 5. Goutam Biswas

Parsing Part II (Top-down parsing, left-recursion removal)

Fall Compiler Principles Lecture 3: Parsing part 2. Roman Manevich Ben-Gurion University

Defining syntax using CFGs

CS308 Compiler Principles Syntax Analyzer Li Jiang

Compiler construction in4303 lecture 3

Syntactic Analysis. Top-Down Parsing

Chapter 3. Parsing #1

Parsing. Lecture 11: Parsing. Recursive Descent Parser. Arithmetic grammar. - drops irrelevant details from parse tree

CMSC 330, Fall 2009, Practice Problem 3 Solutions

LL(1) Grammars. Example. Recursive Descent Parsers. S A a {b,d,a} A B D {b, d, a} B b { b } B λ {d, a} D d { d } D λ { a }

Fall Compiler Principles Lecture 2: LL parsing. Roman Manevich Ben-Gurion University of the Negev

Compiler Construction 2016/2017 Syntax Analysis

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 3, vrijdag 22 september 2017

Compilation Lecture 3: Syntax Analysis: Top-Down parsing. Noam Rinetzky

UNIT-III BOTTOM-UP PARSING

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

Introduction to parsers

Introduction to Syntax Analysis. The Second Phase of Front-End

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Compilers. Yannis Smaragdakis, U. Athens (original slides by Sam

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Introduction to Syntax Analysis

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

CS375: Logic and Theory of Computing

The procedure attempts to "match" the right hand side of some production for a nonterminal.

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

LL parsing Nullable, FIRST, and FOLLOW

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

4 (c) parsing. Parsing. Top down vs. bo5om up parsing

Lexical and Syntax Analysis. Bottom-Up Parsing

Fall Compiler Principles Lecture 2: LL parsing. Roman Manevich Ben-Gurion University of the Negev

Introduction to Bottom-Up Parsing

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Chapter 3. Describing Syntax and Semantics ISBN

Building Compilers with Phoenix

Prelude COMP 181 Tufts University Computer Science Last time Grammar issues Key structure meaning Tufts University Computer Science

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

CSE431 Translation of Computer Languages

Programming Language Syntax and Analysis

Transcription:

컴파일러구성 제 2 강 Recursive-descent Parser / Predictive Parser

Recursive-descent parsing ::= A top-down method that uses a set of recursive procedures to recognize its input with no backtracking. Create a procedure for each nonterminal. ex) G : S aa bb A aa c B bb d procedure ps; begin if nextsymbol = ta then begin getnextsymbol; pa end else if nextsymbol = tb then begin getnextsymbol; pb end else error end;

procedure pa; begin if nextsymbol = ta then begin getnextsymbol; pa end else if nextsymbol = tc then getnextsymbol else error end; procedure pb;... /* main */ begin getnextsymbol; ps; if nextsymbol = '$' then accept else error end. ω = aac$ procedure call sequence ::= leftmost derivation

The main problem in constructing a recursive-descent syntax analyzer is the choice of productions when a procedure is first entered. To resolve this problem, we can compute the lookahead of each production. LOOKAHEAD of a production Definition : LOOKAHEAD(A α) = FIRST({ω S * µaβ µαβ * µω V * T }). Meaning : the set of terminals which can be generated by * α and if α ε, then FOLLOW(A) is added to the set. Computing formula: LOOKAHEAD(A X 1 X 2...X n ) = FIRST(X 1 X 2...X n ) FOLLOW(A)

S asa ε A c Nullable Set = {S} FIRST(S) = {a, ε} FOLLOW(S) = {$,c} FIRST(A) = {c} FOLLOW(A) = {$,c} LOOKAHEAD(S asa) = FIRST(aSA) FOLLOW(S) = {a} LOOKAHEAD(S ε) = FIRST(ε) FOLLOW(S) = {$,c} LOOKAHEAD(A c) = FIRST(c) FOLLOW(A) = {c} LOOKAHEAD 를구하는순서 : Nullable => FIRST => FOLLOW => LOOKAHEAD

Definition : A α β P, LOOKAHEAD(A α) LOOKAHEAD(A β) = φ. Meaning : for each distinct pair of productions with the same left-hand side, it can select the unique alternate that derives a string beginning with the input symbol. The grammar G is said to be strong LL(1) if it satisfies the strong LL condition. ex) G : S asa ε A c LOOKAHEAD(S asa) = {a} LOOKAHEAD(S ε) = FOLLOW(S) = {$, c} LOOKAHEAD(S asa) LOOKAHEAD(S ε) = φ G 는 strong LL(1) 이다.

If a grammar is strong LL(1), we can construct a parser for sentences of the grammar using the following scheme. Terminal procedure: a V T, procedure pa; /* getnextsymbol => scanner */ begin end; if nextsymbol = ta then getnextsymbol else error getnextsymbol : 스캐너에해당하는루틴으로입력스트림으로부터토큰한개를만들어변수 nextsymbol 에배정한다.

Text p.284 A V N, procedure pa; var i: integer; begin case nextsymbol of LOOKAHEAD(A X 1 X 2...X m ): for i := 1 to m do px i ; LOOKAHEAD(A Y 1 Y 2...Y n ): for i := 1 to n do py i ; : LOOKAHEAD(A Z 1 Z 2...Z r ): for i := 1 to r do pz i ; LOOKAHEAD(A ε): ; otherwise: error end /* case */ end;

Improving the efficiency and structure of recursive-descent parser 1) Eliminating terminal procedures ::= In practice it is better not to write a procedure for each terminal. Instead the action of advancing the input marker can always be initiated by the nonterminal procedures. In this way many redundant tests can be eliminated. ex) text p.285 [ 예제 7.9] 2) BNF EBNF : reduce the number of productions and nonterminals. 1 repetitive part : { } 2 optional part : [ ] 3 alternation : ( )

< if_st > ::= if < cond > then < st > [ else < st > ] procedure pif; begin if nextsymbol = tif then begin getnextsymbol; pcond; if nextsymbol = tthen then begin getnextsymbol; pst end else error(10) end else error(11); if nextsymbol = telse then begin getnextsymbol; pst end end;

<id_list> ::= id {, id } procedure pid_list; begin if nextsymbol = tid then begin getnextsymbol; while (nextsymbol = tcomma) do begin getnextsymbol; if nextsymbol = tid then getnextsymbol else error(100) end end end;

< 문제 > 다음 grammar 를 extended BNF 로바꾸고그에따른 recursive-descent parser 를위한 procedure 를 작성하시오. <D> ::= label <L> integer <L> <L> ::= <id> <R> <R> ::= ;, <L> * <L> <id> {, <id> } ; <D> ::= ( label integer ) <id> {, <id>} ;

procedure pd; begin if nextsymbol in [qlabel,qinteger] then begin getnextsymbol; if nextsymbol = tid then begin getnextsymbol; while (nextsymbol = tcomma) do begin getnextsymbol; if nextsymbol = tid then getnextsymbol else error(3) end; end else error(2); if nextsymbol = tsemi then getnextsymbol else error(4) end else error(1) end;

Implement a recursive-descent syntax analyzer for the grammar given in exercise 5.31(text p. 229). Problem Specification Input : SPL program to find a Minimum and a Maximum. Output : left parse Methods : (1) write the getnextsymbol routine. (2) compute LOOKAHEADs for each production. (3) create a procedure for each nonterminal. (4) integrate the procedures with main program.

RDP 의단점 문법이변경되면프로그램을수정해야함. Predictive 파서 프로그램과테이블로분리 Driver routine + Table RDP 의단점을극복 문법변경시테이블만재구성 Predictive parsing ::= a deterministic top-down parsing method using a stack. The stack contains a sequence of grammar symbols.

The input buffer contains the string to be parsed, followed by $.

Current input symbol 과 stack top symbol 사이의관계에따라 parsing. Initial configuration : STACK INPUT $S ω$ Parsing table(ll) : parsing action을결정지어줌. M[X,a] = r : stack top symbol 이 X 이고 current symbol 이 a 일때, r 번생성규칙으로 expand.

Parsing Actions X : stack top symbol, a : current input symbol 1. if X = a = $, then accept. 2. if X = a, then pop X and advance input. 3. if X V N, then if M[X,a] = r (X ABC), then replace X by ABC else error.

Text p.291 Algorithm Predictive_Parser_Action; begin // set ip to point to the first symbol of ω$; repeat // let X be the top stack symbol and a the symbol pointed to by ip; if X is a terminal or $ then if X = a then pop X from the stack and advance ip else error(1) else /* X is nonterminal */ if M[X,a] = X Y 1 Y 2...Y k then begin pop X from the stack; push Y k Y k-1,...,y 1 onto the stack, with Y 1 on top; output the production X Y 1 Y 2...Y k end else error(2) until X = a = $ /* stack is empty */ end.

G : 1. S asb 2. S ba 3. A Aa 4. A b string : aabbbb Parsing Table: terminals nonterminal a b $ S 1 2. A 3 4.

STACK INPUT ACTIONS OUTPUT $S aabbbb$ expand 1 1 $bsa aabbbb$ pop a and advance expand $bs abbbb$ 1 1 $bbsa abbbb$ pop a and advance $bbs bbbb$ expand 2 2 $bbab bbbb$ pop b and advance $bba bbb$ expand 4 4 $bbb bbb$ pop b and advance $bb bb$ pop b and advance $b b$ pop b and advance $ $ Accept How to construct a predictive parsing table for the grammar.