SlideShare ist ein Scribd-Unternehmen logo
1 von 29
COMPILER CONSTRUCTION
PRINCIPLES, TECHNIQUES AND TOOLS
2ND EDITION
ALFRED. V AHO
MUHAMMAD YASIR KHAN
AIR UNIVERSITY MULTAN
TODAY'S AGENDA
• 4.7 More Powerful LR Parser
• 4.7.1 Canonical LR(1) Items
• 4.7.2 Constructing LR(1) Sets of Items
• 4.7.3 Canonical LR(1) Parsing Table
• 4.7.4 Constructing LALR Parsing Table
• 4.7.5 Efficient Construction of LALR Parsing Tables
• 4.7.6 Compaction of LR Parsing Tables
INTRODUCTION
There are two different Methods
• Canonical LR or “LR” method
It makes full use of the lookahead Symbol(s).
this method uses a large set of items, called LR(1) items.
• LookaHead or “LALR” Method:
based on LR(0) sets of items parser,
has many fewer states than typical parsers based on LR(1)
items.
(LALR is method of choice in most situations.)
4.7.1 CANONICAL LR(1) ITEMS
• in the SLR method, state i calls for reduction by A  a if the set of
items Ii contains item [A  a·] and a is in FOLLOW (A) .
• In some situations,
when state i appears on top of the stack, the viable prefix βa on
the stack is such that βA cannot be followed by a in any right-sentential
form.
Thus, the reduction by A  a should be invalid on input a.
EXAMPLE DEMONSTRATION
S  L=R I R
L  *R I id
R  L
I2:
S  L· = R
R L·
Example 4.48, where in state 2 we had item R  L·, which could correspond to A  a
above, and a could be the = sign, which is in FOLLOW (R) . Thus, the SLR parser calls for
reduction by R  L in state 2 with = as the next input. However, there is no right-sentential
form of the grammar in Example 4.48 that begins R = ...
Thus state 2, which is the state corresponding to viable prefix L only, should not really call
for reduction of that L to R.
CANONICAL LR(1) PARSING
• In the beginning, all we know is that we have not read any input
(S' S), we hope to parse an S and after that we should expect to
see a $ as lookahead. We write this as: S' S, $
• Now, consider a general item A , x. It means that we have
parsed an , we hope to parse and after those we should expect
an x. Recall that if there is a production , we should add to
the state. What kind of lookahead should we expect to see after we
have parsed ?
• We should expect to see whatever starts a . If is empty or can vanish, then
we should expect to see an x after we have parsed (and reduced it to B)
CONTINUE…
• Formally, we say LR( l) item [A  a.β, a] is valid for a viable prefix γ if
• there is a derivation S => δ Aw => γa β w , where
• 1. γ = δa , and
• 2. Either a is the first symbol of w, or w is ϵ and a is $.
rm rm
4.7.2 CONSTRUCTING LR(1) SETS OF ITEMS
• The method for building the collection of sets of valid LR( l ) items is
essentially the same as the one for building the canonical collection of
sets of LR ( O) items.
• We need only to modify the two procedures CLOSURE and GOTO .
• The closure function for LR(1) items is then defined as follows:
For each item A , x in state I, each production in the
grammar,
and each terminal b in FIRST( x), add , b to I
If a state contains core item with multiple possible lookaheads
b1, b2,..., we write , b1/b2 as shorthand for , b1 and , b2
For better understanding
Please Open chapter 4
section 4.7.2
Page # 263
Example 4.54
Compilers 2ns Edition
BY Alferd V. Aho
4.7.3 CANONICAL LR(1) PARSING TABLES
• The table is created in the same way as SLR, except we now use the
possible lookahead tokens saved in each state, instead of the
FOLLOW sets.
• Note that the conflict that had appeared in the SLR parser is now
gone.
• However, the LR(1) parser has many more states. This is not very
practical.
CANONICAL LR(1)
• The table formed from the parsing action and goto functions produced
by
• Algorithm is called the canonical LR( l) parsing table.
• An LR parser using this table is called a canonical- LR( l) parser.
• If the parsing action function has no multiply defined entries, then the
given grammar is called an LR (1) grammar.
4.7.4 CONSTRUCTING LALR PARSING
TABLE
• LALR (lookahead -LR) technique is often used in practice, because
• Tables ob-tained by it are considerably smaller than the canonical LR
tables
• most common syntactic constructs of programming languages can be
expressed con-veniently by an LALR grammar.
• For a comparison of parser size
• SLR and LALR tables for a grammar always have the same number of
states
• The canonical LR table would typically have several thousand states
for the same-size language.
EXAMPLE REFERENCE: PAGE 263
EXAMPLE 4.54
• S'  S
S  CC ( 4.55)
C  cC I d
• Regular Language : c * d c * d
I4: C  d·, c/d
I7: C  d· , $
Take Union of then I47
Set of items represented
as
[C  d. , c/d/$]
MERGE WILL CAUSE REDUCE/REDUCE
CONFLICT!
• Example 4.58 : Consider the grammar
S'  S
S  a A d I b B d I a B e I b A e
A  c
B  c
{[A  c. , d], [B  c. e]} valid for Viable Prefix ac
{[A  c. , e], [B  c. , d]} valid for Viable Prefix bc
String Generated by
Grammar:
acd , ace , bcd, bce
Conflict arise when taking
union.
A  c. , d/e
B  c. , d/e
SOLUTION:
• The general idea is to construct the sets of LR( 1 ) items, and if no
conflicts arise, merge sets with common cores.
EXAMPLE 4.60S’  S
S  CC,
C  cC,
C  d
136: C c·C, c/d/$
C  ·cC, c/d/$
C  · d, c/d/$
I47: C  d., c/d/$
189: C  cC· , c/d/ $
4.7.5 EFFICIENT CONSTRUCTION OF LALR PARSING
TABLES
Rules to construct efficient LALR parsing tables:
• We can represent any set of LR(O) or LR(l) items 1 by its kernel, [S'
 ·S, $]
• We can construct the LALR( l ) item kernels from the LR(O) -item
kernels by a process of propagation and spontaneous generation of
lookaheads.
• If we have the LALR( l ) kernels , we can generate the LALR( l )
parsing table by closing each kernel, using the function CLOSURE
and then computing table entries by Algorithm
EXAMPLE 4.61 Need
Lookahead to
make LALR(1)
• There are two ways a lookahead b can get attached to an LR ( O ) item B  γ.δ in
some set of LALR( l ) items J:
• There is a set of items I, with a kernel item A  a·β, a, and J = GOTO (I,
X), and the construction of GOTO (CLOSURE({[A  a.β, a] }) ,X ) Such a
lookahead b is said to be generated spontaneously for B  γ.δ
• As a special case, lookahead $ is generated spontaneously for the item S'  ·S in
the initial set of items.
• All is as in (1), but a = b, and GOTO (CLOSURE({[A --+ a.β, b] } ),X ), contains
[B  γ.δ, b) only because A  a.β has b as one of its associated lookaheads . In
such a case, we say that lookaheads propagate from A  a.β in the kernel of I to B γ.δ
in the kernel of J.
• Note that propagation does not depend on the particular lookahead symbol; either
all lookaheads propagate from one item to another, or none do .
4.7.6 COMPACTION OF LR PARSING
TABLES
• One useful technique for compacting the action field is to
recognize that usually many rows of the action table are identical.
• We can save considerable space, at little cost in time , if we
create a pointer for each state into a one-dimensional array.
• To access information from this array, we assign each terminal a
number from zero to one less than the number of terminals, and
we use this integer as an offset from the pointer value for each
state.
2ND WAY…
• Further space efficiency can be achieved at the expense of a
somewhat slower parser by creating a list for the actions of each
state.
• The list consists of (terminal-symbol, action) pairs. The most
frequent action for a state can be placed at the end of the list.
• in place of a terminal we may use the notation "any," meaning that if
the current input symbol has not been found so far on the list, we
should do that action no matter what the input is.
• Moreover, error entries can safely be replaced by reduce actions
• We can also encode the GOTO table by a list , but here it
appears more efficient to make a list of pairs for each nonterminal
A.
• Each pair on the list for A is of the form (currentState, nextState),
indicating
GOTO [curr entState, A] = next State
THANK YOU…
• For more better understanding please review book articles.
Questions are welcome.

Weitere ähnliche Inhalte

Was ist angesagt?

Recursive descent parsing
Recursive descent parsingRecursive descent parsing
Recursive descent parsing
Boy Baukema
 

Was ist angesagt? (20)

Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Lecture 15 16
Lecture 15 16Lecture 15 16
Lecture 15 16
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Parsing
ParsingParsing
Parsing
 
Ch4b
Ch4bCh4b
Ch4b
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
 
Parsing
ParsingParsing
Parsing
 
LISP: Data types in lisp
LISP: Data types in lispLISP: Data types in lisp
LISP: Data types in lisp
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
 
Parsing
ParsingParsing
Parsing
 
AI Programming language (LISP)
AI Programming language (LISP)AI Programming language (LISP)
AI Programming language (LISP)
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
 
Lexyacc
LexyaccLexyacc
Lexyacc
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 
Recursive Descent Parsing
Recursive Descent Parsing  Recursive Descent Parsing
Recursive Descent Parsing
 
Recursive descent parsing
Recursive descent parsingRecursive descent parsing
Recursive descent parsing
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
LISP: Introduction to lisp
LISP: Introduction to lispLISP: Introduction to lisp
LISP: Introduction to lisp
 

Ähnlich wie Compilers section 4.7

cd-1-221103004637-f7b43300.pdf
cd-1-221103004637-f7b43300.pdfcd-1-221103004637-f7b43300.pdf
cd-1-221103004637-f7b43300.pdf
ClaraBenedotti
 

Ähnlich wie Compilers section 4.7 (20)

Lecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptxLecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptx
 
Programming_Language_Syntax.ppt
Programming_Language_Syntax.pptProgramming_Language_Syntax.ppt
Programming_Language_Syntax.ppt
 
あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法あなたのScalaを爆速にする7つの方法
あなたのScalaを爆速にする7つの方法
 
CD-1.pptx
CD-1.pptxCD-1.pptx
CD-1.pptx
 
cd-1-221103004637-f7b43300.pdf
cd-1-221103004637-f7b43300.pdfcd-1-221103004637-f7b43300.pdf
cd-1-221103004637-f7b43300.pdf
 
Redis Lua Scripts
Redis Lua ScriptsRedis Lua Scripts
Redis Lua Scripts
 
Lisp
LispLisp
Lisp
 
CD
CDCD
CD
 
parsing.pptx
parsing.pptxparsing.pptx
parsing.pptx
 
Parsing example
Parsing exampleParsing example
Parsing example
 
LR PARSE.pptx
LR PARSE.pptxLR PARSE.pptx
LR PARSE.pptx
 
How to create a programming language
How to create a programming languageHow to create a programming language
How to create a programming language
 
intro.ppt
intro.pptintro.ppt
intro.ppt
 
Lisp, An Introduction.ppt
Lisp, An Introduction.pptLisp, An Introduction.ppt
Lisp, An Introduction.ppt
 
intro.ppt
intro.pptintro.ppt
intro.ppt
 
Lisp and scheme i
Lisp and scheme iLisp and scheme i
Lisp and scheme i
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
 
A practical parser with combined parsing
A practical parser with combined parsingA practical parser with combined parsing
A practical parser with combined parsing
 
COMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.pptCOMPILER_DESIGN_CLASS 2.ppt
COMPILER_DESIGN_CLASS 2.ppt
 
COMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptxCOMPILER_DESIGN_CLASS 1.pptx
COMPILER_DESIGN_CLASS 1.pptx
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

Compilers section 4.7

  • 1. COMPILER CONSTRUCTION PRINCIPLES, TECHNIQUES AND TOOLS 2ND EDITION ALFRED. V AHO MUHAMMAD YASIR KHAN AIR UNIVERSITY MULTAN
  • 2. TODAY'S AGENDA • 4.7 More Powerful LR Parser • 4.7.1 Canonical LR(1) Items • 4.7.2 Constructing LR(1) Sets of Items • 4.7.3 Canonical LR(1) Parsing Table • 4.7.4 Constructing LALR Parsing Table • 4.7.5 Efficient Construction of LALR Parsing Tables • 4.7.6 Compaction of LR Parsing Tables
  • 3. INTRODUCTION There are two different Methods • Canonical LR or “LR” method It makes full use of the lookahead Symbol(s). this method uses a large set of items, called LR(1) items. • LookaHead or “LALR” Method: based on LR(0) sets of items parser, has many fewer states than typical parsers based on LR(1) items. (LALR is method of choice in most situations.)
  • 4. 4.7.1 CANONICAL LR(1) ITEMS • in the SLR method, state i calls for reduction by A  a if the set of items Ii contains item [A  a·] and a is in FOLLOW (A) . • In some situations, when state i appears on top of the stack, the viable prefix βa on the stack is such that βA cannot be followed by a in any right-sentential form. Thus, the reduction by A  a should be invalid on input a.
  • 5. EXAMPLE DEMONSTRATION S  L=R I R L  *R I id R  L I2: S  L· = R R L· Example 4.48, where in state 2 we had item R  L·, which could correspond to A  a above, and a could be the = sign, which is in FOLLOW (R) . Thus, the SLR parser calls for reduction by R  L in state 2 with = as the next input. However, there is no right-sentential form of the grammar in Example 4.48 that begins R = ... Thus state 2, which is the state corresponding to viable prefix L only, should not really call for reduction of that L to R.
  • 6. CANONICAL LR(1) PARSING • In the beginning, all we know is that we have not read any input (S' S), we hope to parse an S and after that we should expect to see a $ as lookahead. We write this as: S' S, $ • Now, consider a general item A , x. It means that we have parsed an , we hope to parse and after those we should expect an x. Recall that if there is a production , we should add to the state. What kind of lookahead should we expect to see after we have parsed ? • We should expect to see whatever starts a . If is empty or can vanish, then we should expect to see an x after we have parsed (and reduced it to B)
  • 7. CONTINUE… • Formally, we say LR( l) item [A  a.β, a] is valid for a viable prefix γ if • there is a derivation S => δ Aw => γa β w , where • 1. γ = δa , and • 2. Either a is the first symbol of w, or w is ϵ and a is $. rm rm
  • 8. 4.7.2 CONSTRUCTING LR(1) SETS OF ITEMS • The method for building the collection of sets of valid LR( l ) items is essentially the same as the one for building the canonical collection of sets of LR ( O) items. • We need only to modify the two procedures CLOSURE and GOTO .
  • 9. • The closure function for LR(1) items is then defined as follows: For each item A , x in state I, each production in the grammar, and each terminal b in FIRST( x), add , b to I If a state contains core item with multiple possible lookaheads b1, b2,..., we write , b1/b2 as shorthand for , b1 and , b2
  • 10. For better understanding Please Open chapter 4 section 4.7.2 Page # 263 Example 4.54 Compilers 2ns Edition BY Alferd V. Aho
  • 11. 4.7.3 CANONICAL LR(1) PARSING TABLES • The table is created in the same way as SLR, except we now use the possible lookahead tokens saved in each state, instead of the FOLLOW sets. • Note that the conflict that had appeared in the SLR parser is now gone. • However, the LR(1) parser has many more states. This is not very practical.
  • 12.
  • 13. CANONICAL LR(1) • The table formed from the parsing action and goto functions produced by • Algorithm is called the canonical LR( l) parsing table. • An LR parser using this table is called a canonical- LR( l) parser. • If the parsing action function has no multiply defined entries, then the given grammar is called an LR (1) grammar.
  • 14.
  • 15. 4.7.4 CONSTRUCTING LALR PARSING TABLE • LALR (lookahead -LR) technique is often used in practice, because • Tables ob-tained by it are considerably smaller than the canonical LR tables • most common syntactic constructs of programming languages can be expressed con-veniently by an LALR grammar.
  • 16. • For a comparison of parser size • SLR and LALR tables for a grammar always have the same number of states • The canonical LR table would typically have several thousand states for the same-size language.
  • 17. EXAMPLE REFERENCE: PAGE 263 EXAMPLE 4.54 • S'  S S  CC ( 4.55) C  cC I d • Regular Language : c * d c * d I4: C  d·, c/d I7: C  d· , $ Take Union of then I47 Set of items represented as [C  d. , c/d/$]
  • 18. MERGE WILL CAUSE REDUCE/REDUCE CONFLICT! • Example 4.58 : Consider the grammar S'  S S  a A d I b B d I a B e I b A e A  c B  c {[A  c. , d], [B  c. e]} valid for Viable Prefix ac {[A  c. , e], [B  c. , d]} valid for Viable Prefix bc String Generated by Grammar: acd , ace , bcd, bce Conflict arise when taking union. A  c. , d/e B  c. , d/e
  • 19. SOLUTION: • The general idea is to construct the sets of LR( 1 ) items, and if no conflicts arise, merge sets with common cores.
  • 20. EXAMPLE 4.60S’  S S  CC, C  cC, C  d 136: C c·C, c/d/$ C  ·cC, c/d/$ C  · d, c/d/$ I47: C  d., c/d/$ 189: C  cC· , c/d/ $
  • 21. 4.7.5 EFFICIENT CONSTRUCTION OF LALR PARSING TABLES Rules to construct efficient LALR parsing tables: • We can represent any set of LR(O) or LR(l) items 1 by its kernel, [S'  ·S, $] • We can construct the LALR( l ) item kernels from the LR(O) -item kernels by a process of propagation and spontaneous generation of lookaheads. • If we have the LALR( l ) kernels , we can generate the LALR( l ) parsing table by closing each kernel, using the function CLOSURE and then computing table entries by Algorithm
  • 22. EXAMPLE 4.61 Need Lookahead to make LALR(1)
  • 23. • There are two ways a lookahead b can get attached to an LR ( O ) item B  γ.δ in some set of LALR( l ) items J: • There is a set of items I, with a kernel item A  a·β, a, and J = GOTO (I, X), and the construction of GOTO (CLOSURE({[A  a.β, a] }) ,X ) Such a lookahead b is said to be generated spontaneously for B  γ.δ • As a special case, lookahead $ is generated spontaneously for the item S'  ·S in the initial set of items. • All is as in (1), but a = b, and GOTO (CLOSURE({[A --+ a.β, b] } ),X ), contains [B  γ.δ, b) only because A  a.β has b as one of its associated lookaheads . In such a case, we say that lookaheads propagate from A  a.β in the kernel of I to B γ.δ in the kernel of J. • Note that propagation does not depend on the particular lookahead symbol; either all lookaheads propagate from one item to another, or none do .
  • 24. 4.7.6 COMPACTION OF LR PARSING TABLES • One useful technique for compacting the action field is to recognize that usually many rows of the action table are identical. • We can save considerable space, at little cost in time , if we create a pointer for each state into a one-dimensional array. • To access information from this array, we assign each terminal a number from zero to one less than the number of terminals, and we use this integer as an offset from the pointer value for each state.
  • 25. 2ND WAY… • Further space efficiency can be achieved at the expense of a somewhat slower parser by creating a list for the actions of each state. • The list consists of (terminal-symbol, action) pairs. The most frequent action for a state can be placed at the end of the list. • in place of a terminal we may use the notation "any," meaning that if the current input symbol has not been found so far on the list, we should do that action no matter what the input is. • Moreover, error entries can safely be replaced by reduce actions
  • 26.
  • 27. • We can also encode the GOTO table by a list , but here it appears more efficient to make a list of pairs for each nonterminal A. • Each pair on the list for A is of the form (currentState, nextState), indicating GOTO [curr entState, A] = next State
  • 28.
  • 29. THANK YOU… • For more better understanding please review book articles. Questions are welcome.