SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Downloaden Sie, um offline zu lesen
COMPILER DESIGN- Syntax Analysis
Dr R Jegadeesan Prof-CSE
Jyothishmathi Institute of Technology and Science,
Karimnagar
SYLLABUS
UNIT - II
Syntax Analysis: Introduction, Context-Free Grammars, Writing a Grammar, Top-Down Parsing, Bottom-Up
Parsing, Introduction to LR Parsing: Simple LR, More Powerful LR Parsers, Using Ambiguous Grammars,
Parser Generators.
UNIT-II : SYNTAX ANALYSIS
Topic Name : Context free Grammars
Aim & Objective : To identify syntax errors.
Principle & Operation/ Detailed Explanation :
Context Free Grammars (CFG) can be classified on the basis of following two properties:
1) Based on number of strings it generates.
If CFG is generating finite number of strings, then CFG is Non-Recursive (or the grammar is said to
be Non-recursive grammar)
If CFG can generate infinite number of strings then the grammar is said to be Recursive grammar
During Compilation, the parser uses the grammar of the language to make a parse tree(or derivation
tree) out of the source code. The grammar used must be unambiguous. An ambiguous grammar must
not be used for parsing.
2) Based on number of derivation trees.
ď‚— If there is only 1 derivation tree then the CFG is unambiguous.
ď‚— If there are more than 1 derivation tree, then the CFG is ambiguous.
Examples of Recursive and Non-Recursive Grammars
Recursive Grammars
1) S->SaS S->b The language(set of strings) generated by the above grammar is :{b, bab, babab,…}, which
is infinite.
2) S-> Aa A->Ab|c The language generated by the above grammar is :{ca, cba, cbba …}, which is infinite.
Universities & Important Questions:
1. Explain ambiguous grammar with an example.
Top-Down parsing
Topic Name : Top down parsing
Principle & Operation/ Detailed Explanation :
Parsing is classified into two categories, i.e. Top Down Parsing and Bottom-Up Parsing. Top-Down
Parsing is based on Left Most Derivation.
The process of constructing the parse tree which starts from the root and goes down to the leaf is Top-
Down Parsing.
ď‚— Top-Down Parsers constructs from the Grammar which is free from ambiguity and left recursion.
ď‚— Top Down Parsers uses leftmost derivation to construct a parse tree.
ď‚— It allows a grammar which is free from Left Factoring.
Top –down parsing can be:
1.With Backtracking: Brute Force Technique
2.Without Backtracking:
1. Recursive Descent Parsing
2. Predictive Parsing or Non-Recursive Parsing or LL(1) Parsing or Table Driver Parsing
 Brute Force Technique or Recursive Descent Parsing –
Whenever a Non-terminal spend first time then go with the first alternative and compare with the
given I/P String
If matching doesn’t occur then go with the second alternative and compare with the given I/P String.
If matching again not found then go with the alternative and so on.
Moreover, If matching occurs for at least one alternative, then the I/P string is parsed successfully.
LL(1) or Table Driver or Predictive Parser –
ď‚— In LL1, first L stands for Left to Right and second L stands for Left-most Derivation. 1 stands for
number of Look Aheads token used by parser while parsing a sentence.
ď‚— LL(1) parsing is constructed from the grammar which is free from left recursion, common prefix, and
ambiguity.
ď‚— LL(1) parser depends on 1 look ahead symbol to predict the production to expand the parse tree.
ď‚— This parser is Non-Recursive.
BOTTOM –UP PARSING
Bottom Up Parsers / Shift Reduce Parsers
Build the parse tree from leaves to root. Bottom-up parsing can be defined as an attempt to reduce the
input string w to the start symbol of grammar by tracing out the rightmost derivations of w in reverse.
Eg.
ď‚— classification of bottom up parsers
A general shift reduce parsing is LR parsing. The L stands for scanning the input from left
to right and R stands for constructing a rightmost derivation in reverse.
Benefits of LR parsing:
ď‚— Many programming languages using some variations of an LR parser. It should be noted
that C++ and Perl are exceptions to it.
ď‚— LR Parser can be implemented very efficiently.
ď‚— Of all the Parsers that scan their symbols from left to right, LR Parsers detect syntactic
errors, as soon as possible.
LR Parser
We need two functions –
Closure()
Goto()
ď‚— Closure Operation:
If I is a set of items for a grammar G, then closure(I) is the set of items constructed from I by the two
rules:
Initially every item in I is added to closure(I).
If A -> α.Bβ is in closure(I) and B -> γ is a production then add the item B -> .γ to I, If it is not already
there. We apply this rule until no more items can be added to closure(I).
Eg:
Goto Operation :
Goto(I, X) = 1. Add I by moving dot after X.
2. Apply closure to first step.
ď‚— Eg:
Consider the grammar S ->AA
A -> aA | b
Augmented grammar S’ -> S
S -> AA
A -> aA | b
 The LR(0) parsing table for above GOTO graph will be –
Simple LR
ď‚— SLR Parser
The SLR parser is similar to LR(0) parser except that the reduced entry. The reduced productions are
written only in the FOLLOW of the variable whose production is reduced.
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
Canonical LR
In the SLR method we were working with LR(0)) items. In CLR parsing we will be using LR(1) items.
LR(k) item is defined to be an item using lookaheads of length k. So , the LR(1) item is comprised of
two parts : the LR(0) item and the lookahead associated with the item.
LR(1) parsers are more powerful parser.
For LR(1) items we modify the Closure and GOTO function.
Closure
Closure(I)
repeat
for (each item [ A -> ?.B?, a ] in I )
for (each production B -> ? in G’)
for (each terminal b in FIRST(?a))
add [ B -> .? , b ] to set I;
until no more items are added to I;
return I;
Goto Operation
Goto(I, X)
Initialise J to be the empty set;
for ( each item A -> ?.X?, a ] in I )
Add item A -> ?X.?, a ] to se J; /* move the dot one step */
return Closure(J); /* apply closure to the set */
Eg:
Lookahead LR
LALR PARSER
LALR parser are same as CLR parser with one difference. In CLR parser if two states differ only in
lookahead then we combine those states in LALR parser. After minimisation if the parsing table has no
conflict that the grammar is LALR also.
Eg:
consider the grammar
S ->AA
A -> aA | b
Augmented grammar - S’ -> S
S ->AA
A -> aA | b
Thank you

Weitere ähnliche Inhalte

Was ist angesagt?

Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
Shashwat Shriparv
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
Anshul Sharma
 

Was ist angesagt? (20)

Ch5a
Ch5aCh5a
Ch5a
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Back patching
Back patchingBack patching
Back patching
 
Module 11
Module 11Module 11
Module 11
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Ch5b
Ch5bCh5b
Ch5b
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
Lecture5 syntax analysis_1
Lecture5 syntax analysis_1Lecture5 syntax analysis_1
Lecture5 syntax analysis_1
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Lecture8 syntax analysis_4
Lecture8 syntax analysis_4Lecture8 syntax analysis_4
Lecture8 syntax analysis_4
 
Lecture10 syntax analysis_6
Lecture10 syntax analysis_6Lecture10 syntax analysis_6
Lecture10 syntax analysis_6
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 

Ă„hnlich wie COMPILER DESIGN- Syntax Analysis

07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
Harish Khodke
 
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
FutureTechnologies3
 
Lecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptxLecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptx
Yusra11491
 
Compilers section 4.7
Compilers section 4.7Compilers section 4.7
Compilers section 4.7
myasir16
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
rohithprabhas1
 

Ă„hnlich wie COMPILER DESIGN- Syntax Analysis (20)

LR PARSE.pptx
LR PARSE.pptxLR PARSE.pptx
LR PARSE.pptx
 
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.pdf
compiler design.pdfcompiler design.pdf
compiler design.pdf
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questions
 
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
 
Compiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLRCompiler Design LR parsing SLR ,LALR CLR
Compiler Design LR parsing SLR ,LALR CLR
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.ppt
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Lecture 15 16
Lecture 15 16Lecture 15 16
Lecture 15 16
 
Introduction to R for beginners
Introduction to R for beginnersIntroduction to R for beginners
Introduction to R for beginners
 
LR(0) Parsing.pptx
LR(0) Parsing.pptxLR(0) Parsing.pptx
LR(0) Parsing.pptx
 
Lecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptxLecture 12 Bottom-UP Parsing.pptx
Lecture 12 Bottom-UP Parsing.pptx
 
Presentation mam saima kanwal
Presentation mam saima kanwalPresentation mam saima kanwal
Presentation mam saima kanwal
 
Compilers section 4.7
Compilers section 4.7Compilers section 4.7
Compilers section 4.7
 
Python For Data Science.pptx
Python For Data Science.pptxPython For Data Science.pptx
Python For Data Science.pptx
 
Parsing
ParsingParsing
Parsing
 

Mehr von Jyothishmathi Institute of Technology and Science Karimnagar

Mehr von Jyothishmathi Institute of Technology and Science Karimnagar (20)

JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
 
JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework JAVA PROGRAMMING - The Collections Framework
JAVA PROGRAMMING - The Collections Framework
 
JAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - MultithreadingJAVA PROGRAMMING- Exception handling - Multithreading
JAVA PROGRAMMING- Exception handling - Multithreading
 
JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O JAVA PROGRAMMING – Packages - Stream based I/O
JAVA PROGRAMMING – Packages - Stream based I/O
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
WEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScriptWEB TECHNOLOGIES JavaScript
WEB TECHNOLOGIES JavaScript
 
WEB TECHNOLOGIES JSP
WEB TECHNOLOGIES  JSPWEB TECHNOLOGIES  JSP
WEB TECHNOLOGIES JSP
 
WEB TECHNOLOGIES Servlet
WEB TECHNOLOGIES ServletWEB TECHNOLOGIES Servlet
WEB TECHNOLOGIES Servlet
 
WEB TECHNOLOGIES XML
WEB TECHNOLOGIES XMLWEB TECHNOLOGIES XML
WEB TECHNOLOGIES XML
 
WEB TECHNOLOGIES- PHP Programming
WEB TECHNOLOGIES-  PHP ProgrammingWEB TECHNOLOGIES-  PHP Programming
WEB TECHNOLOGIES- PHP Programming
 
Compiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent OptimizationsCompiler Design- Machine Independent Optimizations
Compiler Design- Machine Independent Optimizations
 
COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 
COMPILER DESIGN- Introduction & Lexical Analysis:
COMPILER DESIGN- Introduction & Lexical Analysis: COMPILER DESIGN- Introduction & Lexical Analysis:
COMPILER DESIGN- Introduction & Lexical Analysis:
 
CRYPTOGRAPHY AND NETWORK SECURITY- E-Mail Security
CRYPTOGRAPHY AND NETWORK SECURITY- E-Mail SecurityCRYPTOGRAPHY AND NETWORK SECURITY- E-Mail Security
CRYPTOGRAPHY AND NETWORK SECURITY- E-Mail Security
 
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level SecurityCRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
CRYPTOGRAPHY AND NETWORK SECURITY- Transport-level Security
 
CRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash Functions
CRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash FunctionsCRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash Functions
CRYPTOGRAPHY & NETWORK SECURITY- Cryptographic Hash Functions
 
CRYPTOGRAPHY & NETWOK SECURITY- Symmetric key Ciphers
CRYPTOGRAPHY & NETWOK SECURITY- Symmetric key CiphersCRYPTOGRAPHY & NETWOK SECURITY- Symmetric key Ciphers
CRYPTOGRAPHY & NETWOK SECURITY- Symmetric key Ciphers
 
CRYPTOGRAPHY & NETWORK SECURITY
CRYPTOGRAPHY & NETWORK SECURITYCRYPTOGRAPHY & NETWORK SECURITY
CRYPTOGRAPHY & NETWORK SECURITY
 
Computer Forensics Working with Windows and DOS Systems
Computer Forensics Working with Windows and DOS SystemsComputer Forensics Working with Windows and DOS Systems
Computer Forensics Working with Windows and DOS Systems
 

KĂĽrzlich hochgeladen

KĂĽrzlich hochgeladen (20)

Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
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
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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
 
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, ...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

COMPILER DESIGN- Syntax Analysis

  • 1. COMPILER DESIGN- Syntax Analysis Dr R Jegadeesan Prof-CSE Jyothishmathi Institute of Technology and Science, Karimnagar
  • 2. SYLLABUS UNIT - II Syntax Analysis: Introduction, Context-Free Grammars, Writing a Grammar, Top-Down Parsing, Bottom-Up Parsing, Introduction to LR Parsing: Simple LR, More Powerful LR Parsers, Using Ambiguous Grammars, Parser Generators.
  • 3. UNIT-II : SYNTAX ANALYSIS Topic Name : Context free Grammars Aim & Objective : To identify syntax errors. Principle & Operation/ Detailed Explanation : Context Free Grammars (CFG) can be classified on the basis of following two properties: 1) Based on number of strings it generates. If CFG is generating finite number of strings, then CFG is Non-Recursive (or the grammar is said to be Non-recursive grammar) If CFG can generate infinite number of strings then the grammar is said to be Recursive grammar During Compilation, the parser uses the grammar of the language to make a parse tree(or derivation tree) out of the source code. The grammar used must be unambiguous. An ambiguous grammar must not be used for parsing.
  • 4. 2) Based on number of derivation trees. ď‚— If there is only 1 derivation tree then the CFG is unambiguous. ď‚— If there are more than 1 derivation tree, then the CFG is ambiguous. Examples of Recursive and Non-Recursive Grammars Recursive Grammars 1) S->SaS S->b The language(set of strings) generated by the above grammar is :{b, bab, babab,…}, which is infinite. 2) S-> Aa A->Ab|c The language generated by the above grammar is :{ca, cba, cbba …}, which is infinite. Universities & Important Questions: 1. Explain ambiguous grammar with an example.
  • 5. Top-Down parsing Topic Name : Top down parsing Principle & Operation/ Detailed Explanation : Parsing is classified into two categories, i.e. Top Down Parsing and Bottom-Up Parsing. Top-Down Parsing is based on Left Most Derivation. The process of constructing the parse tree which starts from the root and goes down to the leaf is Top- Down Parsing. ď‚— Top-Down Parsers constructs from the Grammar which is free from ambiguity and left recursion. ď‚— Top Down Parsers uses leftmost derivation to construct a parse tree. ď‚— It allows a grammar which is free from Left Factoring. Top –down parsing can be: 1.With Backtracking: Brute Force Technique 2.Without Backtracking: 1. Recursive Descent Parsing 2. Predictive Parsing or Non-Recursive Parsing or LL(1) Parsing or Table Driver Parsing
  • 6. ď‚— Brute Force Technique or Recursive Descent Parsing – Whenever a Non-terminal spend first time then go with the first alternative and compare with the given I/P String If matching doesn’t occur then go with the second alternative and compare with the given I/P String. If matching again not found then go with the alternative and so on. Moreover, If matching occurs for at least one alternative, then the I/P string is parsed successfully. LL(1) or Table Driver or Predictive Parser – ď‚— In LL1, first L stands for Left to Right and second L stands for Left-most Derivation. 1 stands for number of Look Aheads token used by parser while parsing a sentence. ď‚— LL(1) parsing is constructed from the grammar which is free from left recursion, common prefix, and ambiguity. ď‚— LL(1) parser depends on 1 look ahead symbol to predict the production to expand the parse tree. ď‚— This parser is Non-Recursive.
  • 7. BOTTOM –UP PARSING Bottom Up Parsers / Shift Reduce Parsers Build the parse tree from leaves to root. Bottom-up parsing can be defined as an attempt to reduce the input string w to the start symbol of grammar by tracing out the rightmost derivations of w in reverse. Eg.
  • 8. ď‚— classification of bottom up parsers
  • 9. A general shift reduce parsing is LR parsing. The L stands for scanning the input from left to right and R stands for constructing a rightmost derivation in reverse. Benefits of LR parsing: ď‚— Many programming languages using some variations of an LR parser. It should be noted that C++ and Perl are exceptions to it. ď‚— LR Parser can be implemented very efficiently. ď‚— Of all the Parsers that scan their symbols from left to right, LR Parsers detect syntactic errors, as soon as possible.
  • 10. LR Parser We need two functions – Closure() Goto() ď‚— Closure Operation: If I is a set of items for a grammar G, then closure(I) is the set of items constructed from I by the two rules: Initially every item in I is added to closure(I). If A -> α.Bβ is in closure(I) and B -> Îł is a production then add the item B -> .Îł to I, If it is not already there. We apply this rule until no more items can be added to closure(I). Eg:
  • 11. Goto Operation : Goto(I, X) = 1. Add I by moving dot after X. 2. Apply closure to first step.
  • 12. ď‚— Eg: Consider the grammar S ->AA A -> aA | b Augmented grammar S’ -> S S -> AA A -> aA | b ď‚— The LR(0) parsing table for above GOTO graph will be –
  • 13.
  • 14. Simple LR ď‚— SLR Parser The SLR parser is similar to LR(0) parser except that the reduced entry. The reduced productions are written only in the FOLLOW of the variable whose production is reduced. 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.
  • 15. ď‚— 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
  • 16. Canonical LR In the SLR method we were working with LR(0)) items. In CLR parsing we will be using LR(1) items. LR(k) item is defined to be an item using lookaheads of length k. So , the LR(1) item is comprised of two parts : the LR(0) item and the lookahead associated with the item. LR(1) parsers are more powerful parser. For LR(1) items we modify the Closure and GOTO function. Closure Closure(I) repeat for (each item [ A -> ?.B?, a ] in I ) for (each production B -> ? in G’) for (each terminal b in FIRST(?a)) add [ B -> .? , b ] to set I; until no more items are added to I; return I;
  • 17.
  • 18. Goto Operation Goto(I, X) Initialise J to be the empty set; for ( each item A -> ?.X?, a ] in I ) Add item A -> ?X.?, a ] to se J; /* move the dot one step */ return Closure(J); /* apply closure to the set */ Eg:
  • 19. Lookahead LR LALR PARSER LALR parser are same as CLR parser with one difference. In CLR parser if two states differ only in lookahead then we combine those states in LALR parser. After minimisation if the parsing table has no conflict that the grammar is LALR also. Eg: consider the grammar S ->AA A -> aA | b Augmented grammar - S’ -> S S ->AA A -> aA | b
  • 20.