SlideShare a Scribd company logo
001
PARSING
TOP-DOWN PARSING
ERROR RECOVERY
PREDICTIVE PARSING
PARSER GENERATOR
TOP-DOWN PARSING PRINCIPLES OF CFG
REGULAR EXPRESSION VS CONTEXT FREE GRAMMAR (CFG)
TOP-DOWN PARSING IMPLEMENTATION - RECURSIVE DECENT
PARSING
NON-RECURSIVE PREDICTIVE PARSING
LL(1) GRAMMAR
BOTTOM-UP PARSING
HANDLES
STACK IMPLEMENTATION OF SHIFT REDUCE PARSING
CHAPTER-3 LISTS
SYNTAX ANALYSIS
003
SYNTAX ANALYSIS(PARSERING)
Syntax analysis is the second phase of the compiler
It construct the Parse Tree for checking.
A syntax analyzer or parser takes the input from a lexical analyzer in the
form of token streams
It checks whether the given input is in the correct syntax of the
programming language or not
The Parse Tree is developed with the help of context free grammar.
It helps to detect all types of Syntax errors.
003
SYNTAX ANALYSIS(PARSING)
The parser must reject invalid texts by reporting syntax errors. the error
could happen in terms of expressions, statements, declarations, nested.
look at the below example
x = (2+3) ∗ 9); Mismatched Parentheses
if x>y x = 2; Missing Parentheses
while (x==3) do f1(); Invalid Keyword Do
Context-Free Grammar is a powerful tool for describing the syntax of
programming languages.
004
SYNTAX ANALYSIS(PARSERING)...
A context-free grammar has four components:
Set of Non-terminals (V).
Set of Terminal symbols (Σ).
Set of Productions (P).
Start symbol (S)
Non-terminals (V): Non-terminals are set of syntactic variables that
denote sets of strings. represented with capital letters V = { A,B,S.. Z }
Grammar G=(N,T,P,S)
Terminal symbols (Σ): are set of tokens represented with small letters like
a, b, c etc. it is a basic symbols from which strings are formed
Σ = { 0, 1 } or Σ = { a, b.. z }
Productions (P): The productions of a grammar specify in which the terminals and
non-terminals can be combined to form strings.
Ex : S → AA, A → aA | b
Start symbol (S) :One of the non-terminals is designated as the start symbol
005
PARSER
parser is a program that generate a parser tree for the given strings, if
the string generates from the underlying grammar
x = a+b*c;
if token string x ∈L(G), then parse tree
else error message
006
PARSER TREE
A parse tree is a graphical representation of a derivation.
It is convenient to see how strings are derived from the start symbol.
The start symbol of the derivation becomes the root of the parse tree.
007
DERIVATION
A derivation is basically a sequence of production rules, to get the
input string.
To decide which non-terminal to be replaced with production rule,
we can have two options.
1.Left-most Derivation: when the sentential form of an input is scanned and replaced
from left to right
2.Right-most Derivation: sentential form derived by the left-most derivation is called
the left-sentential form
During parse tree construction , we take two decisions for some
sentential form of input:
1.Deciding the non-terminal which is to be replaced.
2.Deciding the production rule, by which, the non-
terminal will be replaced.
S -> ID
E-> T|T
T-> T * F|F
F->ID
008
GENERATION OF PARSE TREE
eg: (S -> aABe, A-> Abc | a, B -> d ) aabcde
The generation of parser tree can be done in to two ways
eg: ((S,A), (a,b), (S->aAb, A->aAb | E)) a*n b*n
009
SOME EXAMPLES OF CFG
Construct the grammer G=({S,A,B},{a,b},P,S} where the production
set p is given by, S->abSb | aAb | a A->aAAb | bS
Prove that G is a ambiguous grammar for input string abab?
Consider the grammar G=({s},{x,y,+,*},P,S) where P consists of S->S+S|S*S|x|y is
it ambiguity grammar for context-free grammar(x+x*y)
Consider the grammar G=({E},{a,b},P,S) where P consists of E->E+E|a
show that the grammar is ambiguous w=a+a+a
Derive the string "aabbabba" using leftmost derivation, rightmost
derivation and parse tree the following CFG,
S-> aB | bA
A-> a| aS | bAA
B-> b | bS |aBB
no
Regular Expressions Context-free grammar
1
Lexical rules are quite simple in case of Regular
Expressions.
Lexical rules are difficult in case of Context free grammar.
2
Regular Expressions are most useful for describing
the structure of lexical construct such as
identifiers, constant etc.
Context free grammars are most useful in describing the nested
chain structure or syntactic structure such as balanced
parenthesis, if else etc.
and these can’t be define by Regular Expression.
3
A set of string is defined in case of Regular
Expressions.
In Context free grammar the language is defined by the
collection of productions.
It is easy to construct efficient recognizer from
Regular Expressions.
By using the context free grammar, it is very difficult to
construct the recognizer.
There is proper procedure for lexical and
syntactical analysis in case of Regular Expressions.
There is no specific guideline for lexical and syntactic analysis
in case of Context free grammar.
010
REGULAR EXPRESSION VS CONTEXT FREE GRAMMAR
011
SYNTAX DIRECTED DEFINITIONS (SDD)
EVALUATION ORDER FOR SDD
CONSTRUCTION OF SYNTAX TREES
CHAPTER-4 LISTS
SYNTAX DIRECTED TRANSLATION
012
4.1 SYNTAX DIRECTED DEFINITIONS (SDD)
Syntax Directed Translation is a technique used in compiler design and
programming language theory to associate semantic actions with the production
rules of a context-free grammar.
It combines the specification of a grammar with the execution of translation or
computation tasks during the parsing process.
Grammar + Semantic rule = SDT
This can be a separate phase of a compiler or we can augment our conventional
grammar with information to control the semantic analysis and translation.
Such grammars are called attribute grammars
In other words, the parsing process and parse trees are used to direct semantic
analysis and the translation of the source program
it checks whether the program's statements and
expressions make sense in the context of the
programming language. It goes beyond the syntax
(grammar) of the code and focuses on the intended
meaning and behavior of the program.
013
4.2 EVALUATION ORDER FOR SDD
The general approach to Syntax-Directed Translation is to construct a parse
tree or syntax tree and compute the values of attributes at the nodes of the
tree by visiting them in some order
E -> E+T | T
T -> T*F | F
F -> INTLIT
E -> E+T { E.val = E.val + T.val }
E -> T { E.val = T.val }
T -> T*F { T.val = T.val * F.val }
T -> F { T.val = F.val }
F -> INTLIT { F.val = INTLIT.lexval }
This is a grammar to syntactically
validate an expression having
additions and multiplications in it
Generalizing, SDT are augmented
rules to a CFG that associate
1) set of attributes to every node of
the grammar and
2) a set of translation rules to every
production rule using attributes,
constants, and lexical values.
https://www.geeksforgeeks.org/syntax-directed-translation-in-compiler-design/
Along with the grammar, we are giving semantic actions also such along with
the parsing task, some other tasks can also be done in parallel like code
generation, expression evaluation(EE) and etc...
014
4.2 EVALUATION ORDER FOR SDD...
Let’s take a string to see how semantic analysis happens, S = 2+3*4. Parse tree
corresponding to S would be
Rules we should follow:
1. generate parse tree from the grammar
2. parse the tree: left to right and top to bottom
3. whenever there is a reduction, go to the production and carry out the action
015
TYPES OF ATTRIBUTES
Synthesized attributes: Synthesized attributes are computed and assigned values
at a parent node based on the attributes of its child nodes. The value of a
synthesized attribute is derived from the values of attributes in the child nodes
and any additional computations performed at the parent node
They are useful for propagating information from child nodes to their parents.
Inherited attributes: Inherited attributes are passed from parent nodes to their
child nodes. The value of an inherited attribute at a child node depends on the
attribute's value at the parent node and any modifications made at the child
node
they are useful for passing information from parents to their children.
016
TYPES OF SDD
L-Attributed SDD: L-Attributed SDDs are a subset of Attribute Grammars where
the attributes can be evaluated in a single left-to-right traversal of the parse tree.
In other words, the attributes in an L-Attributed SDD can be evaluated using only
information from the current node and its left siblings..
S-Attributed SDD: S-Attributed SDDs are a type of Attribute Grammar where
attributes can be evaluated in a bottom-up fashion during a depth-first traversal
of the parse tree.
S-Attributed SDDs allow for more flexible attribute dependencies, as attributes
can depend on siblings and ancestors.

More Related Content

Similar to compiler design course focus of chapter 3 and 4 part

Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLPkartikaVashisht
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.pptvenkatapranaykumarGa
 
A simple approach of lexical analyzers
A simple approach of lexical analyzersA simple approach of lexical analyzers
A simple approach of lexical analyzersArchana Gopinath
 
Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...ganeshjaggineni1927
 
Chapter -4.pptx
Chapter -4.pptxChapter -4.pptx
Chapter -4.pptxwoldu2
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bisonvip_du
 
Ch6_Semantic_Analysis_ppt_fgjdkidskjfdd.ppt
Ch6_Semantic_Analysis_ppt_fgjdkidskjfdd.pptCh6_Semantic_Analysis_ppt_fgjdkidskjfdd.ppt
Ch6_Semantic_Analysis_ppt_fgjdkidskjfdd.pptFutureTechnologies3
 
Unit iv-syntax-directed-translation
Unit iv-syntax-directed-translationUnit iv-syntax-directed-translation
Unit iv-syntax-directed-translationAjith kumar M P
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01riddhi viradiya
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Iffat Anjum
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Bhavin Darji
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Keyappasami
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)bolovv
 

Similar to compiler design course focus of chapter 3 and 4 part (20)

Parsing
ParsingParsing
Parsing
 
Module 11
Module 11Module 11
Module 11
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
 
A simple approach of lexical analyzers
A simple approach of lexical analyzersA simple approach of lexical analyzers
A simple approach of lexical analyzers
 
Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Chapter -4.pptx
Chapter -4.pptxChapter -4.pptx
Chapter -4.pptx
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
 
Ch6_Semantic_Analysis_ppt_fgjdkidskjfdd.ppt
Ch6_Semantic_Analysis_ppt_fgjdkidskjfdd.pptCh6_Semantic_Analysis_ppt_fgjdkidskjfdd.ppt
Ch6_Semantic_Analysis_ppt_fgjdkidskjfdd.ppt
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
Unit iv-syntax-directed-translation
Unit iv-syntax-directed-translationUnit iv-syntax-directed-translation
Unit iv-syntax-directed-translation
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
Parsing
ParsingParsing
Parsing
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)
 

Recently uploaded

Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxJheel Barad
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Abhinav Gaur Kaptaan
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345beazzy04
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaasiemaillard
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxjmorse8
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPCeline George
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersPedroFerreira53928
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...Nguyen Thanh Tu Collection
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxCapitolTechU
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 

Recently uploaded (20)

B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
NCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdfNCERT Solutions Power Sharing Class 10 Notes pdf
NCERT Solutions Power Sharing Class 10 Notes pdf
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 

compiler design course focus of chapter 3 and 4 part

  • 1. 001 PARSING TOP-DOWN PARSING ERROR RECOVERY PREDICTIVE PARSING PARSER GENERATOR TOP-DOWN PARSING PRINCIPLES OF CFG REGULAR EXPRESSION VS CONTEXT FREE GRAMMAR (CFG) TOP-DOWN PARSING IMPLEMENTATION - RECURSIVE DECENT PARSING NON-RECURSIVE PREDICTIVE PARSING LL(1) GRAMMAR BOTTOM-UP PARSING HANDLES STACK IMPLEMENTATION OF SHIFT REDUCE PARSING CHAPTER-3 LISTS SYNTAX ANALYSIS
  • 2. 003 SYNTAX ANALYSIS(PARSERING) Syntax analysis is the second phase of the compiler It construct the Parse Tree for checking. A syntax analyzer or parser takes the input from a lexical analyzer in the form of token streams It checks whether the given input is in the correct syntax of the programming language or not The Parse Tree is developed with the help of context free grammar. It helps to detect all types of Syntax errors.
  • 3. 003 SYNTAX ANALYSIS(PARSING) The parser must reject invalid texts by reporting syntax errors. the error could happen in terms of expressions, statements, declarations, nested. look at the below example x = (2+3) ∗ 9); Mismatched Parentheses if x>y x = 2; Missing Parentheses while (x==3) do f1(); Invalid Keyword Do Context-Free Grammar is a powerful tool for describing the syntax of programming languages.
  • 4. 004 SYNTAX ANALYSIS(PARSERING)... A context-free grammar has four components: Set of Non-terminals (V). Set of Terminal symbols (Σ). Set of Productions (P). Start symbol (S) Non-terminals (V): Non-terminals are set of syntactic variables that denote sets of strings. represented with capital letters V = { A,B,S.. Z } Grammar G=(N,T,P,S) Terminal symbols (Σ): are set of tokens represented with small letters like a, b, c etc. it is a basic symbols from which strings are formed Σ = { 0, 1 } or Σ = { a, b.. z } Productions (P): The productions of a grammar specify in which the terminals and non-terminals can be combined to form strings. Ex : S → AA, A → aA | b Start symbol (S) :One of the non-terminals is designated as the start symbol
  • 5. 005 PARSER parser is a program that generate a parser tree for the given strings, if the string generates from the underlying grammar x = a+b*c; if token string x ∈L(G), then parse tree else error message
  • 6. 006 PARSER TREE A parse tree is a graphical representation of a derivation. It is convenient to see how strings are derived from the start symbol. The start symbol of the derivation becomes the root of the parse tree.
  • 7. 007 DERIVATION A derivation is basically a sequence of production rules, to get the input string. To decide which non-terminal to be replaced with production rule, we can have two options. 1.Left-most Derivation: when the sentential form of an input is scanned and replaced from left to right 2.Right-most Derivation: sentential form derived by the left-most derivation is called the left-sentential form During parse tree construction , we take two decisions for some sentential form of input: 1.Deciding the non-terminal which is to be replaced. 2.Deciding the production rule, by which, the non- terminal will be replaced.
  • 8. S -> ID E-> T|T T-> T * F|F F->ID 008 GENERATION OF PARSE TREE eg: (S -> aABe, A-> Abc | a, B -> d ) aabcde The generation of parser tree can be done in to two ways eg: ((S,A), (a,b), (S->aAb, A->aAb | E)) a*n b*n
  • 9. 009 SOME EXAMPLES OF CFG Construct the grammer G=({S,A,B},{a,b},P,S} where the production set p is given by, S->abSb | aAb | a A->aAAb | bS Prove that G is a ambiguous grammar for input string abab? Consider the grammar G=({s},{x,y,+,*},P,S) where P consists of S->S+S|S*S|x|y is it ambiguity grammar for context-free grammar(x+x*y) Consider the grammar G=({E},{a,b},P,S) where P consists of E->E+E|a show that the grammar is ambiguous w=a+a+a Derive the string "aabbabba" using leftmost derivation, rightmost derivation and parse tree the following CFG, S-> aB | bA A-> a| aS | bAA B-> b | bS |aBB
  • 10. no Regular Expressions Context-free grammar 1 Lexical rules are quite simple in case of Regular Expressions. Lexical rules are difficult in case of Context free grammar. 2 Regular Expressions are most useful for describing the structure of lexical construct such as identifiers, constant etc. Context free grammars are most useful in describing the nested chain structure or syntactic structure such as balanced parenthesis, if else etc. and these can’t be define by Regular Expression. 3 A set of string is defined in case of Regular Expressions. In Context free grammar the language is defined by the collection of productions. It is easy to construct efficient recognizer from Regular Expressions. By using the context free grammar, it is very difficult to construct the recognizer. There is proper procedure for lexical and syntactical analysis in case of Regular Expressions. There is no specific guideline for lexical and syntactic analysis in case of Context free grammar. 010 REGULAR EXPRESSION VS CONTEXT FREE GRAMMAR
  • 11. 011 SYNTAX DIRECTED DEFINITIONS (SDD) EVALUATION ORDER FOR SDD CONSTRUCTION OF SYNTAX TREES CHAPTER-4 LISTS SYNTAX DIRECTED TRANSLATION
  • 12. 012 4.1 SYNTAX DIRECTED DEFINITIONS (SDD) Syntax Directed Translation is a technique used in compiler design and programming language theory to associate semantic actions with the production rules of a context-free grammar. It combines the specification of a grammar with the execution of translation or computation tasks during the parsing process. Grammar + Semantic rule = SDT This can be a separate phase of a compiler or we can augment our conventional grammar with information to control the semantic analysis and translation. Such grammars are called attribute grammars In other words, the parsing process and parse trees are used to direct semantic analysis and the translation of the source program it checks whether the program's statements and expressions make sense in the context of the programming language. It goes beyond the syntax (grammar) of the code and focuses on the intended meaning and behavior of the program.
  • 13. 013 4.2 EVALUATION ORDER FOR SDD The general approach to Syntax-Directed Translation is to construct a parse tree or syntax tree and compute the values of attributes at the nodes of the tree by visiting them in some order E -> E+T | T T -> T*F | F F -> INTLIT E -> E+T { E.val = E.val + T.val } E -> T { E.val = T.val } T -> T*F { T.val = T.val * F.val } T -> F { T.val = F.val } F -> INTLIT { F.val = INTLIT.lexval } This is a grammar to syntactically validate an expression having additions and multiplications in it Generalizing, SDT are augmented rules to a CFG that associate 1) set of attributes to every node of the grammar and 2) a set of translation rules to every production rule using attributes, constants, and lexical values. https://www.geeksforgeeks.org/syntax-directed-translation-in-compiler-design/ Along with the grammar, we are giving semantic actions also such along with the parsing task, some other tasks can also be done in parallel like code generation, expression evaluation(EE) and etc...
  • 14. 014 4.2 EVALUATION ORDER FOR SDD... Let’s take a string to see how semantic analysis happens, S = 2+3*4. Parse tree corresponding to S would be Rules we should follow: 1. generate parse tree from the grammar 2. parse the tree: left to right and top to bottom 3. whenever there is a reduction, go to the production and carry out the action
  • 15. 015 TYPES OF ATTRIBUTES Synthesized attributes: Synthesized attributes are computed and assigned values at a parent node based on the attributes of its child nodes. The value of a synthesized attribute is derived from the values of attributes in the child nodes and any additional computations performed at the parent node They are useful for propagating information from child nodes to their parents. Inherited attributes: Inherited attributes are passed from parent nodes to their child nodes. The value of an inherited attribute at a child node depends on the attribute's value at the parent node and any modifications made at the child node they are useful for passing information from parents to their children.
  • 16. 016 TYPES OF SDD L-Attributed SDD: L-Attributed SDDs are a subset of Attribute Grammars where the attributes can be evaluated in a single left-to-right traversal of the parse tree. In other words, the attributes in an L-Attributed SDD can be evaluated using only information from the current node and its left siblings.. S-Attributed SDD: S-Attributed SDDs are a type of Attribute Grammar where attributes can be evaluated in a bottom-up fashion during a depth-first traversal of the parse tree. S-Attributed SDDs allow for more flexible attribute dependencies, as attributes can depend on siblings and ancestors.