SlideShare a Scribd company logo
Parsing
LL(1), SLR, LR(1)
NITIN MOHAN SHARMA
CREATED BY:
What is Parsing?
 In the syntax analysis phase, a compiler verifies
whether or not the tokens generated by the lexical
analyser are grouped according to the syntactic rules
of the language.
 This is done by a parser.
 It detects and reports any syntax errors and produces
a parse tree from which intermediate code can be
generated.
Position of Parser in Compiler Model
Parse Tree
 It is a picture of derivation in which there is a
node for each nonterminal that appears in the
derivation. The children of a node are symbols by
which that nonterminal is replaced in the
derivation.
Parsers
 The process of deriving the string from the given
grammar is known as derivation (parsing).
Depending upon how derivation is done we have
two kinds of parsers :-
 Top Down Parser
 Bottom Up Parser
Top Down Parser
 Top down parsing attempts to build the parse tree from
root to leave. Top down parser will start from start symbol
and proceeds to string. It follows leftmost derivation.
First & Follow
 FIRST(X) for a grammar symbol X is the set of terminals
that begin the strings derivable from X.
 Rules to compute FIRST set:
 If x is a terminal, then FIRST(x) = { ‘x’ }
 If x-> Є, is a production rule, then add Є to FIRST(x).
 If X->Y1 Y2 Y3….Yn is a production,
 FIRST(X) = FIRST(Y1)
 If FIRST(Y1) contains Є then FIRST(X) = { FIRST(Y1) – Є }
U { FIRST(Y2) }
 If FIRST (Yi) contains Є for all i = 1 to n, then add Є to
FIRST(X).
First & Follow continued
 Follow(X) to be the set of terminals that can appear
immediately to the right of Non-Terminal X in some
sentential form.
 Rules to compute FOLLOW set:
 Follow(S)={$} where S is the start symbol.
 If A->pBq is a production where p,B,q any grammar
symbols then everything in FIRST(q) except Є is in
FOLLOW(B).
 If A->pB is a production or a production A->pBq where
FIRST(q) contains Є then everything in FOLLOW(A) is in
FOLLOW(B).
LL(1) Parser
 Predictive parsers can be constructed for LL(1) grammar,
the first ‘L’ stands for scanning the input from left to
right, the second ‘L’ stands for leftmost derivation and
‘1’ for using one input symbol lookahead at each step to
make parsing action decisions.
 A grammar G is LL(1) if A → α | β are two distinct
productions of G:
 for no terminal, both α and β derive strings beginning
with a.
 at most one of α and β can derive empty string.
 if β → t, then α does not derive any string beginning with
a terminal in FOLLOW(A).
Bottom Up Parser
Bottom-up parsing starts from the leaf nodes of a tree
and works in upward direction till it reaches the root
node. Here, we start from a sentence and then apply
production rules in reverse manner in order to reach the
start symbol.
SLR
 SLR stands for Simple LR, this is basically a method of adding
lookahead to LR(0) parsers as simply as possible. The reduced
productions are written only in the FOLLOW of the variable
whose production is reduced.
 The technique is based on the following observation:
 If we are in a DFA state containing the item: A → α4 then a
possible action will be to reduce by this rule. Doing this
reduction would involve:
 going from a sentential form that looks like: . . . α4 . . .
 to one that looks like: . . . A4 . . .
 By looking at examples, we can see that the symbol
immediately to the right of the marker in a sentential form
should correspond to the next input symbol: we can rephrase
this as: the symbol following A should be the next symbol in the
input.
 Since we already have a method of characterising the set of
symbols which can follow a non-terminal in a sentential form,
we can formulate the SLR(1) reduction rule:
 • Reduce by the rule A → α only if the current state contains A
→ α4 and the next input symbol is in F OLLOW(A)
 This provides a quick and easy way to incorporate lookahead
into the parser; however, there are many languages which are
not SLR(1).
 Construction of SLR parsing table –
 Construct C = { I0, I1, ……. In}, the collection of sets of LR(0) items for
G’.
 State i is constructed from Ii. The parsing actions for state i are
determined as follow :
 If [ A -> ?.a? ] is in Ii and GOTO(Ii , a) = Ij , then set ACTION[i, a] to
“shift j”. Here a must be terminal.
 If [A -> ?.] is in Ii, then set ACTION[i, a] to “reduce A -> ?” for all a
in FOLLOW(A); here A may not be S’.
 Is [S -> S.] is in Ii, then set action[i, $] to “accept”. If any
conflicting actions are generated by the above rules we say that
the grammar is not SLR.
 The goto transitions for state i are constructed for all nonterminals A
using the rule:
if GOTO( Ii , A ) = Ij then GOTO [i, A] = j.
 All entries not defined by rules 2 and 3 are made error.
 Eg:
If in the parsing table we have multiple
entries then it is said to be a conflict.
 Consider the grammar
E -> T+E | T
T ->id
Augmented grammar –
E’ -> E
E -> T+E | T
T -> id
LR(1)
 LR(1) grammar, the first ‘L’ stands for scanning
the input from left to right, the ‘R’ stands for
rightmost derivation and ‘1’ for using one input
symbol lookahead at each step to make parsing
action decisions.
 An LR(1) parser keeps track of which terminals
are actual permitted followers of a given symbol
in each given parsing state. It thus recognizes
fewer valid reduce operations, and thus some
shift-reduce and reduce-reduce conflicts are
avoided.
Change to the Augmented Rule
 In LR(0) and SLR(1), we add a rule, called the
augmented rule, for recognition of the Start
symbol of the grammar:
S’ :- S $
 In LR(1) and LALR(1), the format of this rule does
not include the $
S’ :- S
Grammar Limitations
 A LR(1) grammar is one where the
construction of an LR(1) parse table does
not require two action (shift-reduce or
reduce-reduce) in any one cell. Many
conflicts in SLR(1) parse tables are avoided
if the LR(1) parse approach is used,
because the latter approach is more
restrictive on where it allows reduce
operations. An SLR(1) parse table may allow
reduces where the next input token should
not allow such.
THANK YOU!
NITIN MOHAN SHARMA

More Related Content

What's hot

LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingR Islam
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresPriyanka Rana
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGJothi Lakshmi
 
Recursive Descent Parsing
Recursive Descent Parsing  Recursive Descent Parsing
Recursive Descent Parsing Md Tajul Islam
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysisBilalzafar22
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Aman Sharma
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Applications of stack
Applications of stackApplications of stack
Applications of stackeShikshak
 

What's hot (20)

LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Parse Tree
Parse TreeParse Tree
Parse Tree
 
Recursive Descent Parsing
Recursive Descent Parsing  Recursive Descent Parsing
Recursive Descent Parsing
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Back patching
Back patchingBack patching
Back patching
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysis
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
First and follow set
First and follow setFirst and follow set
First and follow set
 

Similar to Parsing LL(1), SLR, LR(1) (20)

Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 
LR PARSE.pptx
LR PARSE.pptxLR PARSE.pptx
LR PARSE.pptx
 
Lecture 15 16
Lecture 15 16Lecture 15 16
Lecture 15 16
 
sameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptxsameermlr0parser-200701133032.pptx
sameermlr0parser-200701133032.pptx
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
compiler design.pdf
compiler design.pdfcompiler design.pdf
compiler design.pdf
 
Module 11
Module 11Module 11
Module 11
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
 
Lecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptLecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.ppt
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Ch3.ppt
Ch3.pptCh3.ppt
Ch3.ppt
 
Chapt 06
Chapt 06Chapt 06
Chapt 06
 
Chapt 06
Chapt 06Chapt 06
Chapt 06
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 

Recently uploaded

UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2DianaGray10
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaRTTS
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...CzechDreamin
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesThousandEyes
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor TurskyiFwdays
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...CzechDreamin
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Product School
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...Product School
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...Product School
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyJohn Staveley
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekCzechDreamin
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Jeffrey Haguewood
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...CzechDreamin
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
 

Recently uploaded (20)

UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
Integrating Telephony Systems with Salesforce: Insights and Considerations, B...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 

Parsing LL(1), SLR, LR(1)

  • 1. Parsing LL(1), SLR, LR(1) NITIN MOHAN SHARMA CREATED BY:
  • 2. What is Parsing?  In the syntax analysis phase, a compiler verifies whether or not the tokens generated by the lexical analyser are grouped according to the syntactic rules of the language.  This is done by a parser.  It detects and reports any syntax errors and produces a parse tree from which intermediate code can be generated.
  • 3. Position of Parser in Compiler Model
  • 4. Parse Tree  It is a picture of derivation in which there is a node for each nonterminal that appears in the derivation. The children of a node are symbols by which that nonterminal is replaced in the derivation.
  • 5. Parsers  The process of deriving the string from the given grammar is known as derivation (parsing). Depending upon how derivation is done we have two kinds of parsers :-  Top Down Parser  Bottom Up Parser
  • 6. Top Down Parser  Top down parsing attempts to build the parse tree from root to leave. Top down parser will start from start symbol and proceeds to string. It follows leftmost derivation.
  • 7. First & Follow  FIRST(X) for a grammar symbol X is the set of terminals that begin the strings derivable from X.  Rules to compute FIRST set:  If x is a terminal, then FIRST(x) = { ‘x’ }  If x-> Є, is a production rule, then add Є to FIRST(x).  If X->Y1 Y2 Y3….Yn is a production,  FIRST(X) = FIRST(Y1)  If FIRST(Y1) contains Є then FIRST(X) = { FIRST(Y1) – Є } U { FIRST(Y2) }  If FIRST (Yi) contains Є for all i = 1 to n, then add Є to FIRST(X).
  • 8. First & Follow continued  Follow(X) to be the set of terminals that can appear immediately to the right of Non-Terminal X in some sentential form.  Rules to compute FOLLOW set:  Follow(S)={$} where S is the start symbol.  If A->pBq is a production where p,B,q any grammar symbols then everything in FIRST(q) except Є is in FOLLOW(B).  If A->pB is a production or a production A->pBq where FIRST(q) contains Є then everything in FOLLOW(A) is in FOLLOW(B).
  • 9. LL(1) Parser  Predictive parsers can be constructed for LL(1) grammar, the first ‘L’ stands for scanning the input from left to right, the second ‘L’ stands for leftmost derivation and ‘1’ for using one input symbol lookahead at each step to make parsing action decisions.  A grammar G is LL(1) if A → α | β are two distinct productions of G:  for no terminal, both α and β derive strings beginning with a.  at most one of α and β can derive empty string.  if β → t, then α does not derive any string beginning with a terminal in FOLLOW(A).
  • 10. Bottom Up Parser Bottom-up parsing starts from the leaf nodes of a tree and works in upward direction till it reaches the root node. Here, we start from a sentence and then apply production rules in reverse manner in order to reach the start symbol.
  • 11. SLR  SLR stands for Simple LR, this is basically a method of adding lookahead to LR(0) parsers as simply as possible. The reduced productions are written only in the FOLLOW of the variable whose production is reduced.  The technique is based on the following observation:  If we are in a DFA state containing the item: A → α4 then a possible action will be to reduce by this rule. Doing this reduction would involve:  going from a sentential form that looks like: . . . α4 . . .  to one that looks like: . . . A4 . . .
  • 12.  By looking at examples, we can see that the symbol immediately to the right of the marker in a sentential form should correspond to the next input symbol: we can rephrase this as: the symbol following A should be the next symbol in the input.  Since we already have a method of characterising the set of symbols which can follow a non-terminal in a sentential form, we can formulate the SLR(1) reduction rule:  • Reduce by the rule A → α only if the current state contains A → α4 and the next input symbol is in F OLLOW(A)  This provides a quick and easy way to incorporate lookahead into the parser; however, there are many languages which are not SLR(1).
  • 13.  Construction of SLR parsing table –  Construct C = { I0, I1, ……. In}, the collection of sets of LR(0) items for G’.  State i is constructed from Ii. The parsing actions for state i are determined as follow :  If [ A -> ?.a? ] is in Ii and GOTO(Ii , a) = Ij , then set ACTION[i, a] to “shift j”. Here a must be terminal.  If [A -> ?.] is in Ii, then set ACTION[i, a] to “reduce A -> ?” for all a in FOLLOW(A); here A may not be S’.  Is [S -> S.] is in Ii, then set action[i, $] to “accept”. If any conflicting actions are generated by the above rules we say that the grammar is not SLR.  The goto transitions for state i are constructed for all nonterminals A using the rule: if GOTO( Ii , A ) = Ij then GOTO [i, A] = j.  All entries not defined by rules 2 and 3 are made error.
  • 14.  Eg: If in the parsing table we have multiple entries then it is said to be a conflict.  Consider the grammar E -> T+E | T T ->id Augmented grammar – E’ -> E E -> T+E | T T -> id
  • 15.
  • 16. LR(1)  LR(1) grammar, the first ‘L’ stands for scanning the input from left to right, the ‘R’ stands for rightmost derivation and ‘1’ for using one input symbol lookahead at each step to make parsing action decisions.  An LR(1) parser keeps track of which terminals are actual permitted followers of a given symbol in each given parsing state. It thus recognizes fewer valid reduce operations, and thus some shift-reduce and reduce-reduce conflicts are avoided.
  • 17. Change to the Augmented Rule  In LR(0) and SLR(1), we add a rule, called the augmented rule, for recognition of the Start symbol of the grammar: S’ :- S $  In LR(1) and LALR(1), the format of this rule does not include the $ S’ :- S
  • 18. Grammar Limitations  A LR(1) grammar is one where the construction of an LR(1) parse table does not require two action (shift-reduce or reduce-reduce) in any one cell. Many conflicts in SLR(1) parse tables are avoided if the LR(1) parse approach is used, because the latter approach is more restrictive on where it allows reduce operations. An SLR(1) parse table may allow reduces where the next input token should not allow such.