0 Voturi pozitive1 Voturi negative

77 (de) vizualizări71 paginithis document will give a brief about ll1 parsing in compiler construction

Apr 03, 2014

© © All Rights Reserved

PDF, TXT sau citiți online pe Scribd

this document will give a brief about ll1 parsing in compiler construction

© All Rights Reserved

77 (de) vizualizări

this document will give a brief about ll1 parsing in compiler construction

© All Rights Reserved

- Steve Jobs
- Wheel of Time
- NIV, Holy Bible, eBook
- NIV, Holy Bible, eBook, Red Letter Edition
- Cryptonomicon
- The Woman Who Smashed Codes: A True Story of Love, Spies, and the Unlikely Heroine who Outwitted America's Enemies
- Contagious: Why Things Catch On
- Crossing the Chasm: Marketing and Selling Technology Project
- Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are
- Zero to One: Notes on Start-ups, or How to Build the Future
- Console Wars: Sega, Nintendo, and the Battle that Defined a Generation
- Dust: Scarpetta (Book 21)
- Hit Refresh: The Quest to Rediscover Microsoft's Soul and Imagine a Better Future for Everyone
- The Innovators: How a Group of Hackers, Geniuses, and Geeks Created the Digital Revolution
- Crushing It!: How Great Entrepreneurs Build Their Business and Influence—and How You Can, Too
- Make Time: How to Focus on What Matters Every Day
- Algorithms to Live By: The Computer Science of Human Decisions
- Wild Cards

Sunteți pe pagina 1din 71

LL(1) parsing

Top-down parsing

A top-down parsing algorithm parses an input string in such a way that the implied traversal of the parse tree occurs from the root to the leaves.

LL(1) parsing

Top-down parsing

A top-down parsing algorithm parses an input string in such a way that the implied traversal of the parse tree occurs from the root to the leaves. Top-down parsers come in two forms: backtracking parsers and predictive parsers.

LL(1) parsing

Top-down parsing

A top-down parsing algorithm parses an input string in such a way that the implied traversal of the parse tree occurs from the root to the leaves. Top-down parsers come in two forms: backtracking parsers and predictive parsers. A predictive parser attempts to predict the next construction using one or more lookahead tokens.

LL(1) parsing

Top-down parsing

A top-down parsing algorithm parses an input string in such a way that the implied traversal of the parse tree occurs from the root to the leaves. Top-down parsers come in two forms: backtracking parsers and predictive parsers. A predictive parser attempts to predict the next construction using one or more lookahead tokens. Two well-known top-down parsing methods are recursive-descent parsing and LL(1) parsing.

LL(1) parsing

Top-down parsing

A top-down parsing algorithm parses an input string in such a way that the implied traversal of the parse tree occurs from the root to the leaves. Top-down parsers come in two forms: backtracking parsers and predictive parsers. A predictive parser attempts to predict the next construction using one or more lookahead tokens. Two well-known top-down parsing methods are recursive-descent parsing and LL(1) parsing. Recursive descent parsing is the most suitable method for a handwritten parser.

LL(1) parsing

LL(1) parsing

The rst L in LL(1) refers to the fact that the input is processed from left to right.

LL(1) parsing

LL(1) parsing

The rst L in LL(1) refers to the fact that the input is processed from left to right. The second L refers to the fact that LL(1) parsing determines a leftmost derivation for the input string.

LL(1) parsing

LL(1) parsing

The rst L in LL(1) refers to the fact that the input is processed from left to right. The second L refers to the fact that LL(1) parsing determines a leftmost derivation for the input string. The 1 in parentheses implies that LL(1) parsing uses only one symbol of input to predict the next grammar rule that should be used.

LL(1) parsing

We start by considering the following grammar that generates strings of balanced parenthesis: S (S )S |

LL(1) parsing

We start by considering the following grammar that generates strings of balanced parenthesis: S (S )S | We will assume that $ marks the bottom of a stack and the end of input.

LL(1) parsing

We start by considering the following grammar that generates strings of balanced parenthesis: S (S )S | We will assume that $ marks the bottom of a stack and the end of input.

Parsing action of an LL(1) parser: 1 2 3 4 5 6 Parsing Stack $S $ S )S ( $ S )S $S ) $S $ Input ()$ ()$ )$ )$ $ $ Action S (S )S match S match S accept

LL(1) parsing

We start by considering the following grammar that generates strings of balanced parenthesis: S (S )S | We will assume that $ marks the bottom of a stack and the end of input.

Parsing action of an LL(1) parser: 1 2 3 4 5 6 Parsing Stack $S $ S )S ( $ S )S $S ) $S $ Input ()$ ()$ )$ )$ $ $ Action S (S )S match S match S accept $ S

M [N , T ] S

( S (S )S

) S

LL(1) parsing

We use the parsing table to decide which decision should be made if a given nonterminal N is at the top of the parsing stack, based on the current input symbol T .

LL(1) parsing

We use the parsing table to decide which decision should be made if a given nonterminal N is at the top of the parsing stack, based on the current input symbol T . We add production choices to the LL(1) parsing table as follows:

LL(1) parsing

We use the parsing table to decide which decision should be made if a given nonterminal N is at the top of the parsing stack, based on the current input symbol T . We add production choices to the LL(1) parsing table as follows:

1. If A is a production choice, and there is a derivation a , where a is a token, then we add A to the table entry M [A, a].

LL(1) parsing

We use the parsing table to decide which decision should be made if a given nonterminal N is at the top of the parsing stack, based on the current input symbol T . We add production choices to the LL(1) parsing table as follows:

1. If A is a production choice, and there is a derivation a , where a is a token, then we add A to the table entry M [A, a]. 2. If A is a production choice, and there are derivations and S $ Aa , where S is the start symbol and a is a token (or $), then we add A to the table entry M [A, a].

LL(1) parsing

LL(1) parsing

1. In rule 1, given a token a in the input, we wish to select a rule A if can produce an a for matching.

LL(1) parsing

1. In rule 1, given a token a in the input, we wish to select a rule A if can produce an a for matching. 2. In rule 2, if A derives the empty string (via A ), and if a is a token that can legally come after A in a derivation, then we want to select A to make A disappear.

LL(1) parsing

1. In rule 1, given a token a in the input, we wish to select a rule A if can produce an a for matching. 2. In rule 2, if A derives the empty string (via A ), and if a is a token that can legally come after A in a derivation, then we want to select A to make A disappear.

These rules are dicult to implement directly, so we will develop algorithms involving rst and follow sets.

LL(1) parsing

1. In rule 1, given a token a in the input, we wish to select a rule A if can produce an a for matching. 2. In rule 2, if A derives the empty string (via A ), and if a is a token that can legally come after A in a derivation, then we want to select A to make A disappear.

These rules are dicult to implement directly, so we will develop algorithms involving rst and follow sets. A grammar is an LL(1) grammar if the associated LL(1) parsing table has at most one production in each table entry.

LL(1) parsing

Before we give the precise denitions of First and Follow Sets, we show how to use it in the construction of LL(1) parsing tables.

LL(1) parsing

Before we give the precise denitions of First and Follow Sets, we show how to use it in the construction of LL(1) parsing tables. Repeat the following two steps for each nonterminal A and each production A :

LL(1) parsing

Before we give the precise denitions of First and Follow Sets, we show how to use it in the construction of LL(1) parsing tables. Repeat the following two steps for each nonterminal A and each production A :

1. For each token a in First(), add A to the entry M [A, a].

LL(1) parsing

Before we give the precise denitions of First and Follow Sets, we show how to use it in the construction of LL(1) parsing tables. Repeat the following two steps for each nonterminal A and each production A :

1. For each token a in First(), add A to the entry M [A, a]. 2. If is in First(), for each element a of Follow(A) (where a is a token or a is $), add A to M [A, a].

LL(1) parsing

LL(1) parsing

Now consider the grammar S ( S ) S | In this case we have that First( (S )S ) = { ( } First() = {} Follow(S ) = { ), $ }

LL(1) parsing

Now consider the grammar S ( S ) S | In this case we have that First( (S )S ) = { ( } First() = {} Follow(S ) = { ), $ } Thus we get the following LL(1) parsing table: M [N , T ] ( ) $ S S (S )S S S

LL(1) parsing

Let X be a grammar symbol (a terminal or nonterminal) or . Then the set First(X ) consisting of terminals, and possibly , is dened as follows:

LL(1) parsing

Let X be a grammar symbol (a terminal or nonterminal) or . Then the set First(X ) consisting of terminals, and possibly , is dened as follows:

1. If X is a terminal or , First(X ) = {X }.

LL(1) parsing

Let X be a grammar symbol (a terminal or nonterminal) or . Then the set First(X ) consisting of terminals, and possibly , is dened as follows:

1. If X is a terminal or , First(X ) = {X }. 2. If X is a n nonterminal, then for each production choice X X1 X2 ...Xn , First(X ) contains First(X1 ){}. If also for some i < n, all the sets First(X1 ),...,First(Xi ) contains , then First(X ) contains First(Xi +1 ){}. If all the sets First(X1 ),...,First(Xn ) contains , then First(X ) also contains .

LL(1) parsing

We now dene First() for any string = X1 X2 ...Xn (a string of terminals and nonterminals) as follows:

LL(1) parsing

We now dene First() for any string = X1 X2 ...Xn (a string of terminals and nonterminals) as follows:

1. First() contains First(X1 ){}.

LL(1) parsing

We now dene First() for any string = X1 X2 ...Xn (a string of terminals and nonterminals) as follows:

1. First() contains First(X1 ){}. 2. For each i = 2, ..., n, if First(Xk ) contains for all k = 1, ..., i 1, then First() contains First(Xi ){}.

LL(1) parsing

We now dene First() for any string = X1 X2 ...Xn (a string of terminals and nonterminals) as follows:

1. First() contains First(X1 ){}. 2. For each i = 2, ..., n, if First(Xk ) contains for all k = 1, ..., i 1, then First() contains First(Xi ){}. 3. Finally, if for all i = 1, ..., n, First(Xi ) contains , then First() contains .

LL(1) parsing

for all nonterminals A do First(A):={}; while there are changes to any First(A) do for each production choice A X1 ...Xn do k := 1; Continue:=true; while Continue = true and k <= n do add First(Xk ){} to First(A); if not in First(Xk ) then Continue:=false; k := k + 1; if Continue = true then add to First(A);

LL(1) parsing

for all nonterminals A do First(A):={}; while there are changes to any First(A) do for each production choice A X1 ...Xn do k := 1; Continue:=true; while Continue = true and k <= n do add First(Xk ){} to First(A); if not in First(Xk ) then Continue:=false; k := k + 1; if Continue = true then add to First(A); Simplied algorithm for First Sets in the absence of -productions:

LL(1) parsing

for all nonterminals A do First(A):={}; while there are changes to any First(A) do for each production choice A X1 ...Xn do k := 1; Continue:=true; while Continue = true and k <= n do add First(Xk ){} to First(A); if not in First(Xk ) then Continue:=false; k := k + 1; if Continue = true then add to First(A); Simplied algorithm for First Sets in the absence of -productions: for all nonterminals A do First(A):={}; while there are changes to any First(A) do for each production choice A X1 ...Xn do add First(X1 ) to First(A);

LL(1) parsing

Consider the simple integer expression grammar:

exp exp addop term | term addop + | term term mulop factor | factor mulop factor ( exp ) | number

LL(1) parsing

Consider the simple integer expression grammar:

exp exp addop term | term addop + | term term mulop factor | factor mulop factor ( exp ) | number Grammar rule exp exp addop term exp term addop + addop term term mulop factor term factor mulop factor ( exp ) factor number First(mulop ) = {} First(factor ) ={(} First(factor ) = {(, number} Pass 1 Pass 2 Pass 3

LL(1) parsing

Thus: First(exp ) = {(, number} First(term) = {(, number} First(factor ) = {(, number} First(addop ) = {+, } First(mulop ) = {}

LL(1) parsing

statement if -stmt | other if -stmt if ( exp ) statement else -part else -part else statement | exp 0 | 1

LL(1) parsing

statement if -stmt | other if -stmt if ( exp ) statement else -part else -part else statement | exp 0 | 1 Grammar rule statement if -stmt statement other if -stmt if ( exp ) statement else -part else -part else statement else -part exp 0 exp 1 Pass 1 Pass 2 First(statement )= {if , other}

First(statement ) = {other} First(if -stmt )= = {if } First(else -part )= = {else} First(else -part )= = {else, } First(exp )={0} First(exp )={0, 1}

LL(1) parsing

Thus: First(statement ) = {if , other} First(if -stmt ) = {if } First(else -part ) = {else, } First(exp ) = {0, 1}

LL(1) parsing

Given a nonterminal A, the follow set Follow(A), consisting of terminals, and possibly $, is dened as follows:

LL(1) parsing

Given a nonterminal A, the follow set Follow(A), consisting of terminals, and possibly $, is dened as follows:

1. If A is the start symbol, then $ is in Follow(A).

LL(1) parsing

Given a nonterminal A, the follow set Follow(A), consisting of terminals, and possibly $, is dened as follows:

1. If A is the start symbol, then $ is in Follow(A). 2. If there is a production B A , then First( ){} is in Follow(A).

LL(1) parsing

Given a nonterminal A, the follow set Follow(A), consisting of terminals, and possibly $, is dened as follows:

1. If A is the start symbol, then $ is in Follow(A). 2. If there is a production B A , then First( ){} is in Follow(A). 3. If there is a production B A such that is in First( ), then Follow(A) contains Follow(B ).

LL(1) parsing

Follow(start-symbol):= {$}; for all nonterminals A = start-symbol do Follow(A):={}; while there are changes to any Follow sets do for each production A X1 ...Xn do for each Xi that is a nonterminal do add First(Xi +1 ...Xn ){} to Follow(Xi ) (* Note: if i = n, then Xi +1 ...Xn = *) if is in First(Xi +1 ...Xn ) then add Follow(A) to Follow(Xi )

LL(1) parsing

We consider again the grammar: exp exp addop term | term addop + | term term mulop factor | factor mulop factor ( exp ) | number

LL(1) parsing

We consider again the grammar: exp exp addop term | term addop + | term term mulop factor | factor mulop factor ( exp ) | number Recall that: First(exp ) = {(, number} First(term) = {(, number} First(factor ) = {(, number} First(addop ) = {+, } First(mulop ) = {}

LL(1) parsing

In the computation of the Follow sets for the grammar we omit the four grammar rule choices that have no possibility of aecting the computation.

LL(1) parsing

In the computation of the Follow sets for the grammar we omit the four grammar rule choices that have no possibility of aecting the computation.

Grammar rule exp exp addop term Pass 1 Follow(exp )= {$, +, } Follow(addop )= {(, number} Follow(term)= {$, +, } Follow(term)= {$, +, , } Follow(mulop )= {(, number} Follow(factor ) = {$, +, , } Follow(exp ) = { $, +, , )} Pass 2 Follow(term)= {$, +, , , )}

Follow(factor )= {$, +, , , )}

LL(1) parsing

Thus: Follow(exp ) = {$, +, , )} Follow(term) = {$, +, , , )} Follow(factor ) = {$, +, , , )} Follow(addop ) = {(, number} Follow(mulop ) = {(, number}

LL(1) parsing

statement if -stmt | other if -stmt if ( exp ) statement else -part else -part else statement | exp 0 | 1

LL(1) parsing

statement if -stmt | other if -stmt if ( exp ) statement else -part else -part else statement | exp 0 | 1 Recall that: First(statement ) = {if , other} First(if -stmt ) = {if } First(else -part ) = {else, } First(exp ) = {0, 1}

LL(1) parsing

statement if -stmt | other if -stmt if ( exp ) statement else -part else -part else statement | exp 0 | 1 Recall that: First(statement ) = {if , other} First(if -stmt ) = {if } First(else -part ) = {else, } First(exp ) = {0, 1} One can verify that: Follow(statement ) = {$, else} Follow(if -stmt ) = {$, else} Follow(else -part ) = {$, else} Follow(exp ) = { ) }

LL(1) parsing

Recall that the LL(1) parsing table M [N , T ] is constructed by repeating the following two steps for each nonterminal A and each production A :

LL(1) parsing

Recall that the LL(1) parsing table M [N , T ] is constructed by repeating the following two steps for each nonterminal A and each production A :

1. For each token a in First(), add A to the entry M [A, a].

LL(1) parsing

Recall that the LL(1) parsing table M [N , T ] is constructed by repeating the following two steps for each nonterminal A and each production A :

1. For each token a in First(), add A to the entry M [A, a]. 2. If is in First(), for each element a of Follow(A) (where a is a token or a is $), add A to M [A, a].

LL(1) parsing

Using the procedure on the previous slide, we obtain the following table:

LL(1) parsing

Using the procedure on the previous slide, we obtain the following table:

M [N , T ] statement if -stmt if statement if -stmt if -stmt if ( exp ) statement else -part other statement other else 0 1 $

else -part

else -part

exp

exp 0

exp 1

LL(1) parsing

We notice, that as expected, this grammar is not LL(1), since the entry M[else -part ,else] contains two entries, corresponding to the dangling else ambiguity.

LL(1) parsing

We notice, that as expected, this grammar is not LL(1), since the entry M[else -part ,else] contains two entries, corresponding to the dangling else ambiguity. We could apply the disambiguating rule that would always prefer the rule that generates the current lookahead token over any other (this corresponds to the most closely nested disambiguating rule), and thus the production else -part else statement

LL(1) parsing

We notice, that as expected, this grammar is not LL(1), since the entry M[else -part ,else] contains two entries, corresponding to the dangling else ambiguity. We could apply the disambiguating rule that would always prefer the rule that generates the current lookahead token over any other (this corresponds to the most closely nested disambiguating rule), and thus the production else -part else statement We now show the LL(1) parsing actions for the string if (0) if (1) other else other

LL(1) parsing

We notice, that as expected, this grammar is not LL(1), since the entry M[else -part ,else] contains two entries, corresponding to the dangling else ambiguity. We could apply the disambiguating rule that would always prefer the rule that generates the current lookahead token over any other (this corresponds to the most closely nested disambiguating rule), and thus the production else -part else statement We now show the LL(1) parsing actions for the string if (0) if (1) other else other We use the following abbreviations:

statement = S if -stmt = I else -part = L exp = E if = i else = e other = o

LL(1) parsing

Parsing stack $S $I $LS )E (i $LS )E ( $LS )E $LS )0 $LS ) $LS $LI $LLS )E (i $LLS )E ( $LLS )E $LLS )1 $LLS ) $LLS $LLo $LL $LS e $LS $Lo $L $

i (0) i (1) i (0) i (1) i (0) i (1) (0) i (1) 0) i (1) 0) i (1) ) i (1) i (1) i (1) i (1) (1) 1) 1) )

Input o e o$ o e o$ o e o$ o e o$ o e o$ o e o$ o e o$ o e o$ o e o$ o e o$ o e o$ o e o$ o e o$ o e o$ o e o$ o e o$ e o$ e o$ o$ o$ $ $

Action S I I i(E )S L match match E 0 match match S I I i(E )S L match match E 1 match match S o match LeS match S o match L accept

LL(1) parsing

Finally we consider the LL(1) parsing example discussed in the JFLAP tutorial at http://www.jap.org/tutorial/

LL(1) parsing

Finally we consider the LL(1) parsing example discussed in the JFLAP tutorial at http://www.jap.org/tutorial/ We use JFLAP to calculate First and Follow sets and the LL(1) parse table for the grammar: S aAB b AaAc A B bB B c

LL(1) parsing

Finally we consider the LL(1) parsing example discussed in the JFLAP tutorial at http://www.jap.org/tutorial/ We use JFLAP to calculate First and Follow sets and the LL(1) parse table for the grammar: S aAB b AaAc A B bB B c Now we use JFLAP to parse aacbbcb

LL(1) parsing

- BASIC PARSING TECHNIQUES IN NATURAL LANGUAGE PROCESSINGÎncărcat deeditor3854
- recursive parser(4.12).pdfÎncărcat deCiprian Sulu
- Unit 6Încărcat deJay Shah
- Syllabus of MeÎncărcat dekanvarsh
- Professional JavaScript for Web Developers, 3rd Edition - WroxÎncărcat deionikandy
- Syntax AnalyzerÎncărcat deBaba Sravan
- CONTEXT AWARE TEACHING AID CEM FOR LEARNING COMPILER DESIGN CONCEPT FOR STUDENTS WITH LEARNING DISABILITYÎncărcat deIJAR Journal
- Compiler201516C MoodleÎncărcat deHarshit Garg
- GopichandÎncărcat deGopichand
- Electromagnatic TheoryÎncărcat deRajat Singla
- Comcast IP Holdings I, LLC v. Sprint Communications Company L.P., et al., C.A. No. 12-205-RGS (D. Del. Aug. 22, 2014)Încărcat deYCSTBlog
- Hack SlidesÎncărcat deMouath Alsalmy
- CS 1998 UnsolvedÎncărcat deRakesh Kumar
- Bhat ThesisÎncărcat deRiyaz Bhat
- 3-1 syllabusÎncărcat dePraveen Babu
- 26070754 Analytic Reading of Scores Allen ForteÎncărcat deCesare Angeli
- 253 [ Android Newbie ])Încărcat deNgọc Trung Đặng
- NLP for RoboticÎncărcat deEmil Sabil
- 2016 - Noraset - Definition Modeling- Learning to Define Word Embeddings in Natural LanguageÎncărcat deyounge994
- Ss and os manual.pdfÎncărcat deShiv Mangal Singh Rajawat
- 1601.07724Încărcat dejaber ghazouani
- AI _5 Units_ -2 m With AnswerÎncărcat desuganya
- Eighth SemÎncărcat deNilimoy Buragohain
- Count-Min Sketch TalkÎncărcat dehopchuathurac
- AE454 ProjeÎncărcat deyounizim
- SQL FunctionsÎncărcat deVignesh Kumar
- bash_cheat_sheet.pdfÎncărcat decihat.yildiz
- Frame Panel WordÎncărcat deJonathan Nascimento
- Math 02Încărcat deAndrew Philbert Kelley
- Worker SchedulingÎncărcat deAditya Shukla

- Ch7_Pushdown_Automata.pdfÎncărcat deBharath Choudhary
- PPL - R16Încărcat deAnonymous EPMcWRlh8K
- 2.1-Reasoning.pdfÎncărcat dexxxxxxx
- Consolidated Symbolic Logic Report 8.1 8.5 (2)Încărcat deKlydeen Agito
- Logic SymbolsÎncărcat deJohannesLiechtenauer
- boolean.pdfÎncărcat deRafayGhafoor
- Rules of Inference for Propositional CalculusÎncărcat deRajendranbehappy
- CompilersÎncărcat de99806672
- TOC MCQÎncărcat deSurya Kameswari
- Session-9.pptxÎncărcat deNalam Ashika
- Oracle Bites TheoryÎncărcat deBruno Bandeira
- Modens PonesÎncărcat deroncurtiss
- IndexÎncărcat deSri Kanta
- 01 Regex and CfgÎncărcat deLuis Antonio Bosch
- Mid SolÎncărcat deKunal Das
- Back NaurÎncărcat deLeo Vásquez
- InvestigationsÎncărcat demelo_inquadri
- Predicate CalculusÎncărcat deRomalyn Galingan
- SP AND MP_TM_CHAPTER 1Încărcat deMITMCA
- 200S Practice CS143 Midterm SolutionsÎncărcat deMuhammad Dawood
- TMF1814 Tutorial 5 Ans.docxÎncărcat deDemus Johney
- Enayat PaperÎncărcat dechkornaros
- UGC-NET Computer ScienceÎncărcat deswastik1
- Cs2303 Theory of Computation 2marksÎncărcat deShanmugasundaram Muthuswamy
- (Jaakko Hintikka Selected Papers 3) Jaakko Hintikka (Auth.)-Language, Truth and Logic in Mathematics-Springer Netherlands (1998)Încărcat deE. Goldstajn
- Regular ExpressionsÎncărcat deEaswara Elayaraja
- HW3 Solutions 2017 SpringÎncărcat deAtaush Sabuj
- foils.pdfÎncărcat deYevguenie Yev
- FatÎncărcat deMoiz Mansoor
- Tim Button, Sean Walsh - Philosophy and Model Theory (2018, Oxford University Press)Încărcat deCarlos Diaz Huerta

## Mult mai mult decât documente.

Descoperiți tot ce are Scribd de oferit, inclusiv cărți și cărți audio de la editori majori.

Anulați oricând.