SlideShare a Scribd company logo
1 of 20
PARSING




          9/3/2012   1
PARSING

 In the design of a compiler the second stage after
  lexical analysis is parsing. It is also called as syntax
  analysis.
 Parser will take the stream of tokens generated by
  the lexical analyzer , check if it is grammatically
  correct and generate a parse tree.
 The fundamental theory behind parsing is grammar
  theory.




                             9/3/2012                        2
CONTEXT FREE GRAMMAR

   A CFG, G=(N, T, P, S) where:
     N is a set of non-terminals.
     T is a set of terminals.
     P is a set of productions (or rules) which are given by
         A->α
         where A denotes a single non-terminal.
                  α denotes a set of terminals and non-
      terminals.
     S is the start state. If not specified, then it is the non-
      terminal that appears on the left-hand side of the first
      production.


                                 9/3/2012                       3
Parse trees

Parse trees are labeled trees characterized by
the following:
– The root is labeled by the start symbol.
– Each leaf is labeled by a token or !.
– Each interior node is labeled by a non-
  terminal.
– If A is the non-terminal labeling some interior
node and X1, X2, …, Xn are the labels of the
children of that node from left to right, then
A ::= X1, X2, …, Xn
is a production in the grammar.
                       9/3/2012                     4
AMBIGUITY AND UNAMBIGUITY :
    A word is said to be ambiguously derivable if there
     are more than one derivations existing for the
     word, that is if there are more than one distinct
     parse tree generated for that word.

     There are two kinds of derivations that are important.
     •A derivation is a leftmost derivation if it is always the
     leftmost non-terminal that is chosen to be replaced.
     •It is a rightmost derivation if it is always the rightmost
     one.

     Ambiguity is considered only when words are derived
     using the same kind of derivation.


                                  9/3/2012                         5
AMBIGUITY AND UNAMBIGUITY
    A grammar is said to be ambiguous if there exists
     at least one word which is ambiguously derivable.

    A grammar is said to be unambiguous if all the
     words derived from it are unambiguous.




                            9/3/2012                     6
 A language L is said to be unambiguous if there
   exists at least one grammar which is unambiguous.
  A language L is said to be ambiguous if all the
   grammar of the language are ambiguous.




Programming language grammars must be
unambiguous.




                         9/3/2012                      7
BOOLEAN EXPRESSIONS
The language of Boolean expressions can be defined in
English as follows:
    true is a Boolean expression.
    false is a Boolean expression.
 If exp1 and exp2 are Boolean expressions, then so are
  the following:
   • expression1 OR expression2
   • expression1 AND expression2
   • NOT expression1
                                Low         ||
   • ( expression1 )            Higher  &&
                                 Highest !
                           9/3/2012                   8
Consider this simple CFG:
 bexp  TRUE
 bexp  FALSE
 bexp  bexp || bexp
 bexp  bexp && bexp
 bexp  ! bexp
 bexp  ( bexp )




                        9/3/2012   9
CONTEXT FREE GRAMMAR FOR
BOOLEAN EXPRESSIONS
 Consider the following short hand form of the CFG
 for Boolean expressions:
     E  E && E
     E  E || E
    E!E
     E  (E)
    Et
    Ef
  E is a non-terminal and the start symbol.
  &&, ||, !, (, ), t and f are terminals.


                          9/3/2012                   10
Here are two different (leftmost derivations).
• The first one, corresponding to the first tree:
     E => E && E
        => E && E && E
        => t && E && E
        => t && t && E
        => t && t && t
• The second one, corresponding to the second
  tree:
     E => E && E
        => t && E
        => t && E && E
        => t && t && E
        => t && t && t


                             9/3/2012               11
A CFG is ambiguous if at least one word in the described language
                    has more than one parse tree.




                 E                                     E




     E        &&         E                     E      &&         E




                                                           E    &&      E
E   &&       E           t                     t




                                                           t            t
t            t

                                        9/3/2012                            12
   We construct an unambiguous version of the
    context-free grammar for Boolean expressions by
    making it reflect the following operator precedence
    conventions:
      ! (NOT) has the highest precedence
      && (AND) has the next highest precedence
      || (OR) has the lowest precedence
   For example, t v ~f ^ t should be interpreted as
    t v ((~f)^t). As long as the grammar is
    unambiguous, you can choose whether or not to
    accept expressions that would need conventions
    about operator associatively to disambiguate
    them, like t ^ t ^ t.
                             9/3/2012                     13
   Here is a version that assumes that the binary operators
    are non- associative.
    ◦ E  E1 || E1
    ◦ E  E1
    ◦ E1  E2 && E2
    ◦ E1  E2
    ◦ E2  ! E2
    ◦ E2 (E )
    ◦ E2  t
    ◦ E2 f
   Draw the derivation trees according to your
    unambiguous grammar for the following two
    expressions:
    ◦ (i) ! t || f
    ◦ (ii) (f || t) || ! f && t  9/3/2012                      14
Parse tree for !t v||f:                 E




                              E1
                                        ||       E1




                              E2
                                                  E2




                          !        E2              f




                                    t
                                             9/3/2012   15
E
Parse tree for
(f || t) || !f&&t:       E                             E
                         1            ||               1


                         E                     E            E
                         2                             &&
                                               2            2


                     (   E    )                    E
                                           !                t
                                                   2


                     E        E
                         ||                        f
                     1        1


                     E        E
                     2        2


                     f            t
                              9/3/2012                          16
ASSOCIATIVITY
The binary operators && and || are be
considered to be left-associative in most
programming languages.
 i.e. an expression like t || t || t would be interpreted
  as (t || t) || t



                Short Circuit




                           9/3/2012                          17
Making the production rules for the binary
 operators left associatively:
 E  E || E1
 E  E1
 E1 E1 && E2
 E1 E2
 E2 !E3
 E2 E3
 E3 ( E )
 E3 T
 E3 F
                     9/3/2012                18
E


Parse tree       E
                                 E
                          ||     1
for:
f||f||t
                      E          E
             E   ||   1          2


             E        E          E
             1        2          3


             E        E          t
             2        3

             E
             3        f


             f
                      9/3/2012       19
THANK
YOU..



      9/3/2012   20

More Related Content

What's hot

Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...Mustafa Kamel Mohammadi
 
Transformational generative grammar
Transformational  generative grammarTransformational  generative grammar
Transformational generative grammarBaishakhi Amin
 
CS571: Phrase Structure Grammar
CS571: Phrase Structure GrammarCS571: Phrase Structure Grammar
CS571: Phrase Structure GrammarJinho Choi
 
Nondeterministic Finite Automata
Nondeterministic Finite AutomataNondeterministic Finite Automata
Nondeterministic Finite AutomataAdel Al-Ofairi
 
Pragmatics 1
Pragmatics 1Pragmatics 1
Pragmatics 1Radia Ali
 
Characteristics Schedule based on Recover-ability & Serial-ability
Characteristics Schedule based on Recover-ability & Serial-abilityCharacteristics Schedule based on Recover-ability & Serial-ability
Characteristics Schedule based on Recover-ability & Serial-abilityMeghaj Mallick
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languagesparmeet834
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Iffat Anjum
 
Age As An Individual Difference In Sla
Age As An Individual Difference In SlaAge As An Individual Difference In Sla
Age As An Individual Difference In SlaDr. Cupid Lucid
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free GrammarsMarina Santini
 
Linguistics and language
Linguistics and languageLinguistics and language
Linguistics and languagetahajoon
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysisBilalzafar22
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsShiraz316
 
Pragmatics
PragmaticsPragmatics
PragmaticsVivaAs
 
The Psychological Basis of Contrastive Analysis
The Psychological Basis  of  Contrastive Analysis The Psychological Basis  of  Contrastive Analysis
The Psychological Basis of Contrastive Analysis zahraa Aamir
 

What's hot (20)

Fundamentals of database system - Relational data model and relational datab...
Fundamentals of database system  - Relational data model and relational datab...Fundamentals of database system  - Relational data model and relational datab...
Fundamentals of database system - Relational data model and relational datab...
 
Transformational generative grammar
Transformational  generative grammarTransformational  generative grammar
Transformational generative grammar
 
Semantic analysis
Semantic analysisSemantic analysis
Semantic analysis
 
CS571: Phrase Structure Grammar
CS571: Phrase Structure GrammarCS571: Phrase Structure Grammar
CS571: Phrase Structure Grammar
 
Nondeterministic Finite Automata
Nondeterministic Finite AutomataNondeterministic Finite Automata
Nondeterministic Finite Automata
 
Pragmatics 1
Pragmatics 1Pragmatics 1
Pragmatics 1
 
Characteristics Schedule based on Recover-ability & Serial-ability
Characteristics Schedule based on Recover-ability & Serial-abilityCharacteristics Schedule based on Recover-ability & Serial-ability
Characteristics Schedule based on Recover-ability & Serial-ability
 
Metadiscourse
MetadiscourseMetadiscourse
Metadiscourse
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
 
Age As An Individual Difference In Sla
Age As An Individual Difference In SlaAge As An Individual Difference In Sla
Age As An Individual Difference In Sla
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Linguistics and language
Linguistics and languageLinguistics and language
Linguistics and language
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysis
 
Extension and Prototype
Extension and PrototypeExtension and Prototype
Extension and Prototype
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Parsing
ParsingParsing
Parsing
 
Pragmatics
Pragmatics Pragmatics
Pragmatics
 
Pragmatics
PragmaticsPragmatics
Pragmatics
 
The Psychological Basis of Contrastive Analysis
The Psychological Basis  of  Contrastive Analysis The Psychological Basis  of  Contrastive Analysis
The Psychological Basis of Contrastive Analysis
 

Viewers also liked (20)

Parsing
ParsingParsing
Parsing
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Parsing example
Parsing exampleParsing example
Parsing example
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Ch5a
Ch5aCh5a
Ch5a
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
Module 11
Module 11Module 11
Module 11
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
Lecture7 syntax analysis_3
Lecture7 syntax analysis_3Lecture7 syntax analysis_3
Lecture7 syntax analysis_3
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
 
Symbol table format
Symbol table formatSymbol table format
Symbol table format
 
What is symbol table?
What is symbol table?What is symbol table?
What is symbol table?
 

More from Tech_MX

Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimationTech_MX
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2Tech_MX
 
Stack data structure
Stack data structureStack data structure
Stack data structureTech_MX
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application Tech_MX
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applicationsTech_MX
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2Tech_MX
 
Set data structure
Set data structure Set data structure
Set data structure Tech_MX
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating SystemTech_MX
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Tech_MX
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pcTech_MX
 
More on Lex
More on LexMore on Lex
More on LexTech_MX
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbmsTech_MX
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)Tech_MX
 
Memory dbms
Memory dbmsMemory dbms
Memory dbmsTech_MX
 

More from Tech_MX (20)

Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Uid
UidUid
Uid
 
Theory of estimation
Theory of estimationTheory of estimation
Theory of estimation
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
String & its application
String & its applicationString & its application
String & its application
 
Statistical quality__control_2
Statistical  quality__control_2Statistical  quality__control_2
Statistical quality__control_2
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stack Data Structure & It's Application
Stack Data Structure & It's Application Stack Data Structure & It's Application
Stack Data Structure & It's Application
 
Spss
SpssSpss
Spss
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
 
Set data structure 2
Set data structure 2Set data structure 2
Set data structure 2
 
Set data structure
Set data structure Set data structure
Set data structure
 
Real time Operating System
Real time Operating SystemReal time Operating System
Real time Operating System
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)
 
Motherboard of a pc
Motherboard of a pcMotherboard of a pc
Motherboard of a pc
 
More on Lex
More on LexMore on Lex
More on Lex
 
MultiMedia dbms
MultiMedia dbmsMultiMedia dbms
MultiMedia dbms
 
Merging files (Data Structure)
Merging files (Data Structure)Merging files (Data Structure)
Merging files (Data Structure)
 
Memory dbms
Memory dbmsMemory dbms
Memory dbms
 
Linkers
LinkersLinkers
Linkers
 

Recently uploaded

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 

Parsing

  • 1. PARSING 9/3/2012 1
  • 2. PARSING  In the design of a compiler the second stage after lexical analysis is parsing. It is also called as syntax analysis.  Parser will take the stream of tokens generated by the lexical analyzer , check if it is grammatically correct and generate a parse tree.  The fundamental theory behind parsing is grammar theory. 9/3/2012 2
  • 3. CONTEXT FREE GRAMMAR  A CFG, G=(N, T, P, S) where:  N is a set of non-terminals.  T is a set of terminals.  P is a set of productions (or rules) which are given by A->α where A denotes a single non-terminal. α denotes a set of terminals and non- terminals.  S is the start state. If not specified, then it is the non- terminal that appears on the left-hand side of the first production. 9/3/2012 3
  • 4. Parse trees Parse trees are labeled trees characterized by the following: – The root is labeled by the start symbol. – Each leaf is labeled by a token or !. – Each interior node is labeled by a non- terminal. – If A is the non-terminal labeling some interior node and X1, X2, …, Xn are the labels of the children of that node from left to right, then A ::= X1, X2, …, Xn is a production in the grammar. 9/3/2012 4
  • 5. AMBIGUITY AND UNAMBIGUITY :  A word is said to be ambiguously derivable if there are more than one derivations existing for the word, that is if there are more than one distinct parse tree generated for that word. There are two kinds of derivations that are important. •A derivation is a leftmost derivation if it is always the leftmost non-terminal that is chosen to be replaced. •It is a rightmost derivation if it is always the rightmost one. Ambiguity is considered only when words are derived using the same kind of derivation. 9/3/2012 5
  • 6. AMBIGUITY AND UNAMBIGUITY  A grammar is said to be ambiguous if there exists at least one word which is ambiguously derivable.  A grammar is said to be unambiguous if all the words derived from it are unambiguous. 9/3/2012 6
  • 7.  A language L is said to be unambiguous if there exists at least one grammar which is unambiguous.  A language L is said to be ambiguous if all the grammar of the language are ambiguous. Programming language grammars must be unambiguous. 9/3/2012 7
  • 8. BOOLEAN EXPRESSIONS The language of Boolean expressions can be defined in English as follows:  true is a Boolean expression.  false is a Boolean expression.  If exp1 and exp2 are Boolean expressions, then so are the following: • expression1 OR expression2 • expression1 AND expression2 • NOT expression1 Low  || • ( expression1 ) Higher  && Highest ! 9/3/2012 8
  • 9. Consider this simple CFG:  bexp  TRUE  bexp  FALSE  bexp  bexp || bexp  bexp  bexp && bexp  bexp  ! bexp  bexp  ( bexp ) 9/3/2012 9
  • 10. CONTEXT FREE GRAMMAR FOR BOOLEAN EXPRESSIONS Consider the following short hand form of the CFG for Boolean expressions:  E  E && E  E  E || E E!E  E  (E) Et Ef  E is a non-terminal and the start symbol.  &&, ||, !, (, ), t and f are terminals. 9/3/2012 10
  • 11. Here are two different (leftmost derivations). • The first one, corresponding to the first tree: E => E && E => E && E && E => t && E && E => t && t && E => t && t && t • The second one, corresponding to the second tree: E => E && E => t && E => t && E && E => t && t && E => t && t && t 9/3/2012 11
  • 12. A CFG is ambiguous if at least one word in the described language has more than one parse tree. E E E && E E && E E && E E && E t t t t t t 9/3/2012 12
  • 13. We construct an unambiguous version of the context-free grammar for Boolean expressions by making it reflect the following operator precedence conventions:  ! (NOT) has the highest precedence  && (AND) has the next highest precedence  || (OR) has the lowest precedence  For example, t v ~f ^ t should be interpreted as t v ((~f)^t). As long as the grammar is unambiguous, you can choose whether or not to accept expressions that would need conventions about operator associatively to disambiguate them, like t ^ t ^ t. 9/3/2012 13
  • 14. Here is a version that assumes that the binary operators are non- associative. ◦ E  E1 || E1 ◦ E  E1 ◦ E1  E2 && E2 ◦ E1  E2 ◦ E2  ! E2 ◦ E2 (E ) ◦ E2  t ◦ E2 f  Draw the derivation trees according to your unambiguous grammar for the following two expressions: ◦ (i) ! t || f ◦ (ii) (f || t) || ! f && t 9/3/2012 14
  • 15. Parse tree for !t v||f: E E1 || E1 E2 E2 ! E2 f t 9/3/2012 15
  • 16. E Parse tree for (f || t) || !f&&t: E E 1 || 1 E E E 2 && 2 2 ( E ) E ! t 2 E E || f 1 1 E E 2 2 f t 9/3/2012 16
  • 17. ASSOCIATIVITY The binary operators && and || are be considered to be left-associative in most programming languages.  i.e. an expression like t || t || t would be interpreted as (t || t) || t Short Circuit 9/3/2012 17
  • 18. Making the production rules for the binary operators left associatively: E  E || E1 E  E1 E1 E1 && E2 E1 E2 E2 !E3 E2 E3 E3 ( E ) E3 T E3 F 9/3/2012 18
  • 19. E Parse tree E E || 1 for: f||f||t E E E || 1 2 E E E 1 2 3 E E t 2 3 E 3 f f 9/3/2012 19
  • 20. THANK YOU.. 9/3/2012 20