ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Theory of Automata
    COMP3148
    Progress0 / 25 topics
    Topics
    1. Finite State Models2. Language definitions preliminaries3. Regular expressions/Regular languages4. Finite automata (FAs)5. Transition graphs (TGs)6. NFAs7. Kleene's theorem8. Transducers (automata with output)9. Pumping lemma and non-regular language10. Grammars and PDA11. CFGs12. Derivations, derivation trees and ambiguity13. Simplifying CFLs14. Normal form grammars and parsing15. Decidability16. Context sensitive languages17. Grammars and linear bounded automata (LBA)18. Chomsky's hierarchy of grammars19. Turing Machines Theory20. Turing machines21. Post machine22. Variations on TM23. TM encoding24. Universal Turing Machine25. Defining Computers by TMs
    COMP3148›Derivations, derivation trees and ambiguity
    Theory of AutomataTopic 12 of 25

    Derivations, derivation trees and ambiguity

    7 minread
    1,265words
    Intermediatelevel

    Derivations, Derivation Trees, and Ambiguity

    In formal language theory, particularly in context-free grammars (CFGs), derivations, derivation trees, and ambiguity are essential concepts that help describe how strings are generated and analyzed within a given grammar.

    1. Derivations

    A derivation is the process of applying the production rules of a grammar to a start symbol in order to generate a string of terminal symbols. In other words, a derivation shows how the start symbol of a grammar can be transformed into a string of terminal symbols (the strings that belong to the language generated by the grammar).

    A derivation can proceed in two ways:

    • Leftmost derivation: In each step, the leftmost non-terminal is replaced by one of its production rules.
    • Rightmost derivation: In each step, the rightmost non-terminal is replaced by one of its production rules.

    Example of a Leftmost Derivation:

    Consider the following CFG:

    S→aSb∣abS \to aSb \mid abS→aSb∣ab
    • Start with the start symbol: SSS.
    • Apply the first production S→aSbS \to aSbS→aSb (replace SSS with aSbaSbaSb): S⇒aSbS \Rightarrow aSbS⇒aSb
    • Now, apply the second production S→abS \to abS→ab to replace the remaining SSS: aSb⇒ab b⇒abaSb \Rightarrow ab \, b \Rightarrow abaSb⇒abb⇒ab

    So, the string "ab" is derived.

    Example of a Rightmost Derivation:

    Consider the same grammar:

    S→aSb∣abS \to aSb \mid abS→aSb∣ab
    • Start with the start symbol SSS.
    • Apply the second production S→abS \to abS→ab to replace SSS with ababab: S⇒abS \Rightarrow abS⇒ab

    This is a rightmost derivation because we applied the production where the rightmost non-terminal (in this case, the whole string SSS) was replaced.

    2. Derivation Trees (Parse Trees)

    A derivation tree (also known as a parse tree) is a graphical representation of a derivation, showing how the start symbol of a grammar is expanded into a string of terminal symbols by applying production rules.

    In a derivation tree:

    • The root represents the start symbol.
    • Each internal node represents a non-terminal.
    • Each leaf node represents a terminal symbol.
    • The edges between nodes represent the application of production rules.

    A derivation tree makes it easy to visualize how a string is derived from the grammar and how different parts of the string correspond to the non-terminals in the grammar.

    Example of a Derivation Tree:

    Consider the grammar:

    S→aSb∣abS \to aSb \mid abS→aSb∣ab

    To generate the string "ab", we can represent the derivation as a tree.

    1. Start with the start symbol SSS.
    2. Use the production S→abS \to abS→ab.

    The resulting derivation tree is:

        S
       / \
      a   b
    

    This tree shows that the start symbol SSS is directly replaced by the terminal string "ab".

    Example of a More Complex Derivation Tree:

    To generate the string "aabb", use the following derivation:

    1. Start with SSS.
    2. Apply the production S→aSbS \to aSbS→aSb.
    3. Apply the production S→abS \to abS→ab to the inner SSS.

    The derivation tree for "aabb" would look like this:

            S
           / \
          a   Sb
             / \
            a   b
    

    This tree shows how the string "aabb" is generated by recursively applying the grammar's production rules.

    3. Ambiguity in Context-Free Grammars (CFGs)

    A grammar is ambiguous if there exists a string in the language that can be derived in more than one way. This means that there are multiple distinct derivation trees (or derivations) for the same string.

    Why Ambiguity is Important:

    • Ambiguity can create problems in contexts like programming languages, where a single expression may have multiple interpretations. This can lead to confusion and errors in parsing and execution.
    • Ambiguous grammars do not have a unique structure for certain strings, which complicates the design of compilers or interpreters.

    Example of an Ambiguous Grammar:

    Consider the following CFG for arithmetic expressions:

    E→E+E∣E⋅E∣(E)∣idE \to E + E \mid E \cdot E \mid (E) \mid idE→E+E∣E⋅E∣(E)∣id

    The string "id + id \cdot id" can be derived in two ways:

    1. First derivation:

      • E→E+EE \to E + EE→E+E
      • The left EEE becomes ididid, and the right EEE becomes id⋅idid \cdot idid⋅id.
      • Derivation: E+(E⋅E)E + (E \cdot E)E+(E⋅E).
    2. Second derivation:

      • E→E⋅EE \to E \cdot EE→E⋅E
      • The left EEE becomes ididid, and the right EEE becomes id+idid + idid+id.
      • Derivation: (E+E)⋅id(E + E) \cdot id(E+E)⋅id.

    Thus, the same string "id + id \cdot id" has two distinct derivations, leading to different interpretations of operator precedence. This shows that the grammar is ambiguous.

    Ambiguity in Derivation Trees:

    For the same string "id + id \cdot id", we have two distinct derivation trees:

    1. First tree:
            E
           / | \
          E  +  E
         /      / \
        id    E   E
              |   |
             id  id
    
    1. Second tree:
            E
           / | \
          E  *  E
         /     / \
        id    E   E
              |   |
             id  id
    

    These two trees show that the string "id + id \cdot id" can be interpreted in two different ways, resulting in the ambiguity of the grammar.

    How to Handle Ambiguity

    • Remove Ambiguity:

      • Ambiguity can often be resolved by re-designing the grammar. For example, by introducing additional rules to enforce operator precedence or associativity, we can avoid ambiguity in mathematical expressions.
    • Disambiguation Rules:

      • In some cases, you can use disambiguation rules or conventions to clarify how a string should be interpreted. For example, in arithmetic expressions, multiplication may be given higher precedence than addition, which can be specified in the grammar or determined by context during parsing.

    Conclusion

    • Derivations are the process of applying production rules to generate strings from a start symbol.
    • Derivation trees (or parse trees) visually represent the structure of a derivation and show how the string is constructed from the start symbol.
    • Ambiguity occurs when a string can be derived in more than one way, leading to multiple derivation trees. Ambiguous grammars can lead to problems, especially in programming language design, where a clear and unambiguous syntax is necessary for correct interpretation.

    Understanding derivations, derivation trees, and ambiguity is critical for analyzing and designing grammars, especially in contexts like programming languages and compilers.

    Previous topic 11
    CFGs
    Next topic 13
    Simplifying CFLs

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,265
      Code examples0
      DifficultyIntermediate