71
LL(1) parsing LL(1) parsing

LL1 parsing

Embed Size (px)

DESCRIPTION

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

Citation preview

  • LL(1) parsing

    LL(1) parsing

  • Top-down parsing

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

    I Top-down parsers come in two forms:backtracking parsers and predictive parsers.

    I A predictive parser attempts to predict the next constructionusing one or more lookahead tokens.

    I Two well-known top-down parsing methods arerecursive-descent parsing and LL(1) parsing.

    I Recursive descent parsing is the most suitable method for ahandwritten parser.

    LL(1) parsing

  • Top-down parsing

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

    I Top-down parsers come in two forms:backtracking parsers and predictive parsers.

    I A predictive parser attempts to predict the next constructionusing one or more lookahead tokens.

    I Two well-known top-down parsing methods arerecursive-descent parsing and LL(1) parsing.

    I Recursive descent parsing is the most suitable method for ahandwritten parser.

    LL(1) parsing

  • Top-down parsing

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

    I Top-down parsers come in two forms:backtracking parsers and predictive parsers.

    I A predictive parser attempts to predict the next constructionusing one or more lookahead tokens.

    I Two well-known top-down parsing methods arerecursive-descent parsing and LL(1) parsing.

    I Recursive descent parsing is the most suitable method for ahandwritten parser.

    LL(1) parsing

  • Top-down parsing

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

    I Top-down parsers come in two forms:backtracking parsers and predictive parsers.

    I A predictive parser attempts to predict the next constructionusing one or more lookahead tokens.

    I Two well-known top-down parsing methods arerecursive-descent parsing and LL(1) parsing.

    I Recursive descent parsing is the most suitable method for ahandwritten parser.

    LL(1) parsing

  • Top-down parsing

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

    I Top-down parsers come in two forms:backtracking parsers and predictive parsers.

    I A predictive parser attempts to predict the next constructionusing one or more lookahead tokens.

    I Two well-known top-down parsing methods arerecursive-descent parsing and LL(1) parsing.

    I Recursive descent parsing is the most suitable method for ahandwritten parser.

    LL(1) parsing

  • LL(1) parsing

    I The first L in LL(1) refers to the fact that the input isprocessed from left to right.

    I The second L refers to the fact that LL(1) parsingdetermines a leftmost derivation for the input string.

    I The 1 in parentheses implies that LL(1) parsing uses onlyone symbol of input to predict the next grammar rule thatshould be used.

    LL(1) parsing

  • LL(1) parsing

    I The first L in LL(1) refers to the fact that the input isprocessed from left to right.

    I The second L refers to the fact that LL(1) parsingdetermines a leftmost derivation for the input string.

    I The 1 in parentheses implies that LL(1) parsing uses onlyone symbol of input to predict the next grammar rule thatshould be used.

    LL(1) parsing

  • LL(1) parsing

    I The first L in LL(1) refers to the fact that the input isprocessed from left to right.

    I The second L refers to the fact that LL(1) parsingdetermines a leftmost derivation for the input string.

    I The 1 in parentheses implies that LL(1) parsing uses onlyone symbol of input to predict the next grammar rule thatshould be used.

    LL(1) parsing

  • LL(1) parsing - Example 1

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

    I We will assume that $ marks the bottom of a stack and theend of input.

    I Parsing action of an LL(1) parser:

    Parsing Stack Input Action1 $S ()$ S (S)S2 $S)S( ()$ match3 $S)S )$ S 4 $S) )$ match5 $S $ S 6 $ $ accept

    I The LL(1) parsing table M[N,T ]: M[N,T ] ( ) $S S (S)S S S

    LL(1) parsing

  • LL(1) parsing - Example 1

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

    I We will assume that $ marks the bottom of a stack and theend of input.

    I Parsing action of an LL(1) parser:

    Parsing Stack Input Action1 $S ()$ S (S)S2 $S)S( ()$ match3 $S)S )$ S 4 $S) )$ match5 $S $ S 6 $ $ accept

    I The LL(1) parsing table M[N,T ]: M[N,T ] ( ) $S S (S)S S S

    LL(1) parsing

  • LL(1) parsing - Example 1

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

    I We will assume that $ marks the bottom of a stack and theend of input.

    I Parsing action of an LL(1) parser:

    Parsing Stack Input Action1 $S ()$ S (S)S2 $S)S( ()$ match3 $S)S )$ S 4 $S) )$ match5 $S $ S 6 $ $ accept

    I The LL(1) parsing table M[N,T ]: M[N,T ] ( ) $S S (S)S S S

    LL(1) parsing

  • LL(1) parsing - Example 1

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

    I We will assume that $ marks the bottom of a stack and theend of input.

    I Parsing action of an LL(1) parser:

    Parsing Stack Input Action1 $S ()$ S (S)S2 $S)S( ()$ match3 $S)S )$ S 4 $S) )$ match5 $S $ S 6 $ $ accept

    I The LL(1) parsing table M[N,T ]: M[N,T ] ( ) $S S (S)S S S

    LL(1) parsing

  • LL(1) parsing tables

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

    I We add production choices to the LL(1) parsing table asfollows:

    1. If A is a production choice, and there is a derivation a, where a is a token, then we add A to the tableentry 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 isa token (or $), then we add A to the table entry M[A, a].

    LL(1) parsing

  • LL(1) parsing tables

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

    I We add production choices to the LL(1) parsing table asfollows:

    1. If A is a production choice, and there is a derivation a, where a is a token, then we add A to the tableentry 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 isa token (or $), then we add A to the table entry M[A, a].

    LL(1) parsing

  • LL(1) parsing tables

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

    I We add production choices to the LL(1) parsing table asfollows:

    1. If A is a production choice, and there is a derivation a, where a is a token, then we add A to the tableentry 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 isa token (or $), then we add A to the table entry M[A, a].

    LL(1) parsing

  • LL(1) parsing tables

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

    I We add production choices to the LL(1) parsing table asfollows:

    1. If A is a production choice, and there is a derivation a, where a is a token, then we add A to the tableentry 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 isa token (or $), then we add A to the table entry M[A, a].

    LL(1) parsing

  • LL(1) parsing tables

    I The idea behind these rules are as follows:

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

    2. In rule 2, if A derives the empty string (via A ), and if a isa token that can legally come after A in a derivation, then wewant to select A to make A disappear.

    I These rules are difficult to implement directly, so we willdevelop algorithms involving first and follow sets.

    I 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

  • LL(1) parsing tables

    I The idea behind these rules are as follows: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 isa token that can legally come after A in a derivation, then wewant to select A to make A disappear.

    I These rules are difficult to implement directly, so we willdevelop algorithms involving first and follow sets.

    I 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

  • LL(1) parsing tables

    I The idea behind these rules are as follows: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 wewant to select A to make A disappear.

    I These rules are difficult to implement directly, so we willdevelop algorithms involving first and follow sets.

    I 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

  • LL(1) parsing tables

    I The idea behind these rules are as follows: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 wewant to select A to make A disappear.

    I These rules are difficult to implement directly, so we willdevelop algorithms involving first and follow sets.

    I 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

  • LL(1) parsing tables

    I The idea behind these rules are as follows: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 wewant to select A to make A disappear.

    I These rules are difficult to implement directly, so we willdevelop algorithms involving first and follow sets.

    I 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

  • First and Follow sets

    I Before we give the precise definitions of First and Follow Sets,we show how to use it in the construction of LL(1) parsingtables.

    I Repeat the following two steps for each nonterminal A andeach 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

  • First and Follow sets

    I Before we give the precise definitions of First and Follow Sets,we show how to use it in the construction of LL(1) parsingtables.

    I Repeat the following two steps for each nonterminal A andeach 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

  • First and Follow sets

    I Before we give the precise definitions of First and Follow Sets,we show how to use it in the construction of LL(1) parsingtables.

    I Repeat the following two steps for each nonterminal A andeach 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 isa token or a is $), add A to M[A, a].

    LL(1) parsing

  • First and Follow sets

    I Before we give the precise definitions of First and Follow Sets,we show how to use it in the construction of LL(1) parsingtables.

    I Repeat the following two steps for each nonterminal A andeach 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 table construction with First and Follow sets

    I Now consider the grammar S ( S ) S |

    I In this case we have thatFirst( (S)S ) = { ( }First() = {}Follow(S) = { ), $ }

    I Thus we get the following LL(1) parsing table:M[N,T ] ( ) $

    S S (S)S S S

    LL(1) parsing

  • LL(1) parsing table construction with First and Follow sets

    I Now consider the grammar S ( S ) S | I In this case we have that

    First( (S)S ) = { ( }First() = {}Follow(S) = { ), $ }

    I Thus we get the following LL(1) parsing table:M[N,T ] ( ) $

    S S (S)S S S

    LL(1) parsing

  • LL(1) parsing table construction with First and Follow sets

    I Now consider the grammar S ( S ) S | I In this case we have that

    First( (S)S ) = { ( }First() = {}Follow(S) = { ), $ }

    I Thus we get the following LL(1) parsing table:M[N,T ] ( ) $

    S S (S)S S S

    LL(1) parsing

  • First sets - definition

    I Let X be a grammar symbol (a terminal or nonterminal) or .Then the set First(X ) consisting of terminals, and possibly ,is defined 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 X1X2...Xn, First(X ) contains First(X1){}. If also forsome i < n, all the sets First(X1),...,First(Xi ) contains , thenFirst(X ) contains First(Xi+1){}. If all the setsFirst(X1),...,First(Xn) contains , then First(X ) also contains .

    LL(1) parsing

  • First sets - definition

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

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

    2. If X is a n nonterminal, then for each production choiceX X1X2...Xn, First(X ) contains First(X1){}. If also forsome i < n, all the sets First(X1),...,First(Xi ) contains , thenFirst(X ) contains First(Xi+1){}. If all the setsFirst(X1),...,First(Xn) contains , then First(X ) also contains .

    LL(1) parsing

  • First sets - definition

    I Let X be a grammar symbol (a terminal or nonterminal) or .Then the set First(X ) consisting of terminals, and possibly ,is defined 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 X1X2...Xn, First(X ) contains First(X1){}. If also forsome i < n, all the sets First(X1),...,First(Xi ) contains , thenFirst(X ) contains First(Xi+1){}. If all the setsFirst(X1),...,First(Xn) contains , then First(X ) also contains .

    LL(1) parsing

  • First sets - definition continue

    I We now define First() for any string = X1X2...Xn (astring 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

  • First sets - definition continue

    I We now define First() for any string = X1X2...Xn (astring of terminals and nonterminals) as follows:

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

    2. For each i = 2, ..., n, if First(Xk) contains for allk = 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

  • First sets - definition continue

    I We now define First() for any string = X1X2...Xn (astring 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

  • First sets - definition continue

    I We now define First() for any string = X1X2...Xn (astring 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

  • Algorithm for computing First(A) for nonterminals A

    I for all nonterminals A do First(A):={};while there are changes to any First(A) do

    for each production choice A X1...Xn dok := 1; Continue:=true;while Continue = true and k

  • Algorithm for computing First(A) for nonterminals A

    I for all nonterminals A do First(A):={};while there are changes to any First(A) do

    for each production choice A X1...Xn dok := 1; Continue:=true;while Continue = true and k

  • Algorithm for computing First(A) for nonterminals A

    I for all nonterminals A do First(A):={};while there are changes to any First(A) do

    for each production choice A X1...Xn dok := 1; Continue:=true;while Continue = true and k

  • First sets example 1

    I Consider the simple integer expression grammar:exp exp addop term | termaddop + | term term mulop factor | factormulop factor ( exp ) | number

    I

    Grammar rule Pass 1 Pass 2 Pass 3exp expaddop termexp term First(exp) =

    {(, number}addop + First(addop)

    = {+}addop First(addop)

    = {+,}term termmulop factorterm factor First(term)=

    {(, number}mulop First(mulop)

    = {}factor First(factor)( exp ) = { ( }factor First(factor)number = {(, number}

    LL(1) parsing

  • First sets example 1

    I Consider the simple integer expression grammar:exp exp addop term | termaddop + | term term mulop factor | factormulop factor ( exp ) | number

    I

    Grammar rule Pass 1 Pass 2 Pass 3exp expaddop termexp term First(exp) =

    {(, number}addop + First(addop)

    = {+}addop First(addop)

    = {+,}term termmulop factorterm factor First(term)=

    {(, number}mulop First(mulop)

    = {}factor First(factor)( exp ) = { ( }factor First(factor)number = {(, number}

    LL(1) parsing

  • First sets example 1 continue

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

    LL(1) parsing

  • First sets example 2

    I Consider the grammar:statement if -stmt | otherif -stmt if ( exp ) statement else-partelse-part else statement | exp 0 | 1

    I

    Grammar rule Pass 1 Pass 2statement if -stmt First(statement)=

    {if, other}statement other First(statement) =

    {other}if -stmt if( exp ) First(if -stmt)=

    statement else-part = {if}else-part else First(else-part)=

    statement = {else}else-part First(else-part)=

    = {else, }exp 0 First(exp)={0}exp 1 First(exp)={0, 1}

    LL(1) parsing

  • First sets example 2

    I Consider the grammar:statement if -stmt | otherif -stmt if ( exp ) statement else-partelse-part else statement | exp 0 | 1

    I

    Grammar rule Pass 1 Pass 2statement if -stmt First(statement)=

    {if, other}statement other First(statement) =

    {other}if -stmt if( exp ) First(if -stmt)=

    statement else-part = {if}else-part else First(else-part)=

    statement = {else}else-part First(else-part)=

    = {else, }exp 0 First(exp)={0}exp 1 First(exp)={0, 1}

    LL(1) parsing

  • First sets example 2 continue

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

    LL(1) parsing

  • Follow sets - definition

    I Given a nonterminal A, the follow set Follow(A), consistingof terminals, and possibly $, is defined 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 sets - definition

    I Given a nonterminal A, the follow set Follow(A), consistingof terminals, and possibly $, is defined 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 inFollow(A).

    3. If there is a production B A such that is in First(),then Follow(A) contains Follow(B).

    LL(1) parsing

  • Follow sets - definition

    I Given a nonterminal A, the follow set Follow(A), consistingof terminals, and possibly $, is defined 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 sets - definition

    I Given a nonterminal A, the follow set Follow(A), consistingof terminals, and possibly $, is defined 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

  • Algorithm for the computation of Follow Sets

    I Follow(start-symbol):= {$};for all nonterminals A 6= start-symbol do Follow(A):={};while there are changes to any Follow sets do

    for each production A X1...Xn dofor 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

  • Follow sets example 1

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

    I Recall that:First(exp) = {(,number}First(term) = {(,number}First(factor) = {(,number}First(addop) = {+,}First(mulop) = {}

    LL(1) parsing

  • Follow sets example 1

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

    I Recall that:First(exp) = {(,number}First(term) = {(,number}First(factor) = {(,number}First(addop) = {+,}First(mulop) = {}

    LL(1) parsing

  • Follow sets example 1 continue

    I In the computation of the Follow sets for the grammar weomit the four grammar rule choices that have no possibility ofaffecting the computation.

    I

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

    Follow(addop)={(, number}

    Follow(term)= {$,+,}exp termterm term Follow(term)= Follow(factor)=mulop factor {$,+,, } {$,+,, , )}

    Follow(mulop)={(, number}

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

    term factorfactor Follow(exp)

    ( exp ) = { $,+,, )}

    LL(1) parsing

  • Follow sets example 1 continue

    I In the computation of the Follow sets for the grammar weomit the four grammar rule choices that have no possibility ofaffecting the computation.

    I

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

    Follow(addop)={(, number}

    Follow(term)= {$,+,}exp termterm term Follow(term)= Follow(factor)=mulop factor {$,+,, } {$,+,, , )}

    Follow(mulop)={(, number}

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

    term factorfactor Follow(exp)

    ( exp ) = { $,+,, )}

    LL(1) parsing

  • Follow sets example 1 continue

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

    LL(1) parsing

  • LL(1) parsing example

    I statement if -stmt | otherif -stmt if ( exp ) statement else-partelse-part else statement | exp 0 | 1

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

    I One can verify that:Follow(statement) = {$, else}Follow(if -stmt) = {$, else}Follow(else-part) = {$, else}Follow(exp) = { ) }

    LL(1) parsing

  • LL(1) parsing example

    I statement if -stmt | otherif -stmt if ( exp ) statement else-partelse-part else statement | exp 0 | 1

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

    I One can verify that:Follow(statement) = {$, else}Follow(if -stmt) = {$, else}Follow(else-part) = {$, else}Follow(exp) = { ) }

    LL(1) parsing

  • LL(1) parsing example

    I statement if -stmt | otherif -stmt if ( exp ) statement else-partelse-part else statement | exp 0 | 1

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

    I One can verify that:Follow(statement) = {$, else}Follow(if -stmt) = {$, else}Follow(else-part) = {$, else}Follow(exp) = { ) }

    LL(1) parsing

  • LL(1) parsing example continue

    I Recall that the LL(1) parsing table M[N,T ] is constructed byrepeating the following two steps for each nonterminal A andeach 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 example continue

    I Recall that the LL(1) parsing table M[N,T ] is constructed byrepeating the following two steps for each nonterminal A andeach 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 isa token or a is $), add A to M[A, a].

    LL(1) parsing

  • LL(1) parsing example continue

    I Recall that the LL(1) parsing table M[N,T ] is constructed byrepeating the following two steps for each nonterminal A andeach 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 example continue

    I Using the procedure on the previous slide, we obtain thefollowing table:

    I

    M[N,T ] if other else 0 1 $statement statement statement

    if -stmt otherif -stmt if -stmt

    if ( exp )statementelse-part

    else-part else-part else-part else

    statement

    else-part

    exp exp exp 0 1

    LL(1) parsing

  • LL(1) parsing example continue

    I Using the procedure on the previous slide, we obtain thefollowing table:

    I

    M[N,T ] if other else 0 1 $statement statement statement

    if -stmt otherif -stmt if -stmt

    if ( exp )statementelse-part

    else-part else-part else-part else

    statement

    else-part

    exp exp exp 0 1

    LL(1) parsing

  • LL(1) parsing example continue

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

    I We could apply the disambiguating rule that would alwaysprefer the rule that generates the current lookahead tokenover any other (this corresponds to the most closely nesteddisambiguating rule), and thus the productionelse-part else statement

    I We now show the LL(1) parsing actions for the stringif (0) if(1) other else other

    I We use the following abbreviations:statement = Sif -stmt = Ielse-part = Lexp = Eif = ielse = eother = o

    LL(1) parsing

  • LL(1) parsing example continue

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

    I We could apply the disambiguating rule that would alwaysprefer the rule that generates the current lookahead tokenover any other (this corresponds to the most closely nesteddisambiguating rule), and thus the productionelse-part else statement

    I We now show the LL(1) parsing actions for the stringif (0) if(1) other else other

    I We use the following abbreviations:statement = Sif -stmt = Ielse-part = Lexp = Eif = ielse = eother = o

    LL(1) parsing

  • LL(1) parsing example continue

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

    I We could apply the disambiguating rule that would alwaysprefer the rule that generates the current lookahead tokenover any other (this corresponds to the most closely nesteddisambiguating rule), and thus the productionelse-part else statement

    I We now show the LL(1) parsing actions for the stringif (0) if(1) other else other

    I We use the following abbreviations:statement = Sif -stmt = Ielse-part = Lexp = Eif = ielse = eother = o

    LL(1) parsing

  • LL(1) parsing example continue

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

    I We could apply the disambiguating rule that would alwaysprefer the rule that generates the current lookahead tokenover any other (this corresponds to the most closely nesteddisambiguating rule), and thus the productionelse-part else statement

    I We now show the LL(1) parsing actions for the stringif (0) if(1) other else other

    I We use the following abbreviations:statement = Sif -stmt = Ielse-part = Lexp = Eif = ielse = eother = o

    LL(1) parsing

  • LL(1) parsing example continue

    I

    Parsing stack Input Action$S i (0) i (1) o e o$ S I$I i (0) i (1) o e o$ I i ( E ) S L$LS)E(i i (0) i (1) o e o$ match$LS)E( (0) i (1) o e o$ match$LS)E 0) i (1) o e o$ E 0$LS)0 0) i (1) o e o$ match$LS) ) i (1) o e o$ match$LS i (1) o e o$ S I$LI i (1) o e o$ I i ( E ) S L$LLS)E(i i (1) o e o$ match$LLS)E( (1) o e o$ match$LLS)E 1) o e o$ E 1$LLS)1 1) o e o$ match$LLS) ) o e o$ match$LLS o e o$ S o$LLo o e o$ match$LL e o$ L e S$LSe e o$ match$LS o$ S o$Lo o$ match$L $ L $ $ accept

    LL(1) parsing

  • LL(1) parsing in JFLAP

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

    I We use JFLAP to calculate First and Follow sets and theLL(1) parse table for the grammar:S a A B bA a A cA B b BB c

    I Now we use JFLAP to parse aacbbcb

    LL(1) parsing

  • LL(1) parsing in JFLAP

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

    I We use JFLAP to calculate First and Follow sets and theLL(1) parse table for the grammar:S a A B bA a A cA B b BB c

    I Now we use JFLAP to parse aacbbcb

    LL(1) parsing

  • LL(1) parsing in JFLAP

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

    I We use JFLAP to calculate First and Follow sets and theLL(1) parse table for the grammar:S a A B bA a A cA B b BB c

    I Now we use JFLAP to parse aacbbcb

    LL(1) parsing