ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalDC-222Derivations Ambiguity
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    DC-222

    Theory of Automata

    Progress0 / 25 done
    Topics25
    1Finite State Models2Language definitions preliminaries3Regular expressions/Regular languages4Finite automata (FAs)5Transition graphs (TGs)6NFAs7Kleene's theorem8Transducers (automata with output)9Pumping lemma and non-regular language10Grammars and PDA11CFGs12Derivations, derivation trees and ambiguity13Simplifying CFLs14Normal form grammars and parsing15Decidability16Context sensitive languages17Grammars and linear bounded automata (LBA)18Chomsky's hierarchy of grammars19Turing Machines Theory20Turing machines21Post machine22Variations on TM23TM encoding24Universal Turing Machine25Defining Computers by TMs
    7 min read
    Topic 12 of 25
    Topic Study Note Illustration
    12

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.3kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Theory of Automata