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 PortalCOMP3149Ambiguity Associativit…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3149

    Compiler Construction

    Progress0 / 32 done
    Topics32
    1Introduction to interpreter and compiler2Structure of a Compiler and its Phases3Lexical Analyzer and Input Buffering4Specifications and Recognitions of Tokens5Regular Expressions and Finite Automata6Transition Table and Transition Graph7Definitions of Grammars, Derivations, and Parse Trees8Ambiguity, Associativity, and Precedence of Operators9Syntax Analysis and Role of the Parser10Eliminating Ambiguity, Left Recursion, and Left Factoring11Top-Down Parsing and Recursive-Descent Parsing12First and Follow Sets13LL(1) Grammars and Non-recursive Predictive Parsing
    14Reductions and Shift-Reduce Parsing15LR Parsing and LR(0) Parsers16LR(0) Automaton and Parsing Table17Shift-Reduce Conflicts
    18Automaton and Parsing Table
    19Automaton and Parsing Table
    20Automaton and Parsing Table21Semantic Analysis and Intermediate Code Generation22Three Address Code23Tasks of Semantic Analyzer and Types of Errors24Type Checking and Environments
    25Implicit vs Explicit26Back Patching and Switch Statements27Storage Organization and Stack Allocation of Space28Heap Management and Optimization
    29Design of a Code Generator30Target Language and Addresses in Target Code31Basic Blocks and Flow Graphs32Optimization of Basic Blocks
    3 min read
    Topic 8 of 32
    Topic Study Note Illustration
    08

    Ambiguity, Associativity, and Precedence of Operators

    A grammar is ambiguous if a single string can have more than one parse tree (or derivation) .

    πŸ“˜ 1. Ambiguity in Grammar

    βœ… Definition

    A grammar is ambiguous if a single string can have more than one parse tree (or derivation).


    πŸ“Œ Example Grammar

    E β†’ E + E | E * E | id
    

    🎯 String:

    id + id * id
    

    ❗ Problem

    This string can be interpreted in two different ways:


    πŸ”Ή Interpretation 1:

    (id + id) * id
    

    πŸ”Ή Interpretation 2:

    id + (id * id)
    

    🌳 Two Parse Trees (Conceptual)

    Tree 1:

           E
         / | \
        E  *  E
       /|\     |
      E + E   id
      |   |
     id  id
    

    Tree 2:

           E
         / | \
        E  +  E
        |     /|\
       id    E * E
             |   |
            id  id
    

    ⚠️ Conclusion

    πŸ‘‰ Same input β†’ Different meanings β†’ ❌ Ambiguity


    ⭐ Key Points (Exam)

    • Ambiguous grammar β†’ more than one parse tree
    • Not suitable for compiler design
    • Must be removed

    🧠 2. Associativity of Operators

    βœ… Definition

    Associativity defines the order in which operators of the same precedence are evaluated.


    πŸ”„ Types of Associativity


    πŸ”Ή 1. Left Associativity

    πŸ‘‰ Evaluation from left to right


    πŸ“Œ Example:

    a - b - c
    

    πŸ‘‰ Interpreted as:

    (a - b) - c
    

    ⭐ Operators

    • +, -, *, /

    πŸ”Ή 2. Right Associativity

    πŸ‘‰ Evaluation from right to left


    πŸ“Œ Example:

    a = b = c
    

    πŸ‘‰ Interpreted as:

    a = (b = c)
    

    ⭐ Operators

    • Assignment (=)
    • Exponentiation (^)

    🎯 Key Points (Exam)

    • Same operator β†’ associativity decides order
    • Left vs Right is very important

    🧠 3. Precedence of Operators

    βœ… Definition

    Precedence defines which operator is evaluated first when different operators appear together.


    πŸ“Œ Example

    a + b * c
    

    πŸ‘‰ Multiplication has higher precedence:

    a + (b * c)
    

    πŸ“Š Common Precedence Order

    Level Operator Meaning
    High ^ Power
    Medium *, / Multiply/Divide
    Low +, - Add/Subtract
    Lowest = Assignment

    ⭐ Key Points (Exam)

    • Higher precedence β†’ evaluated first
    • Reduces ambiguity
    • Used in grammar design

    πŸ”„ 4. Removing Ambiguity Using Precedence & Associativity

    πŸ“Œ Ambiguous Grammar

    E β†’ E + E | E * E | id
    

    βœ… Unambiguous Grammar

    E β†’ E + T | T
    T β†’ T * F | F
    F β†’ id
    

    🧠 Explanation

    • * has higher precedence than +
    • Grammar enforces correct evaluation

    πŸ”— 5. Relationship Between Concepts

    Ambiguity β†’ Resolved by β†’ Precedence + Associativity
    

    πŸ§ͺ 6. Complete Example

    Expression:

    a + b * c - d
    

    Step-by-Step Evaluation

    1. * first β†’ b * c
    2. + and - left to right

    Final Form:

    ((a + (b * c)) - d)
    

    ⚠️ 7. Common Mistakes

    • Ignoring precedence
    • Confusing associativity
    • Writing ambiguous grammar

    🎯 8. Important Exam Concepts

    πŸ‘‰ Frequently asked:

    • Define ambiguity with example
    • Draw multiple parse trees
    • Define associativity (left/right)
    • Define precedence with examples
    • Remove ambiguity from grammar
    • Difference between precedence and associativity

    πŸ“ 9. Short Notes (Quick Revision)

    Ambiguity

    • Multiple parse trees
    • Must be removed

    Associativity

    • Same operator order
    • Left or right

    Precedence

    • Priority of operators
    • Higher evaluated first

    πŸ“Š 10. Final Summary Table

    Aspect Ambiguity Associativity Precedence
    Definition Multiple parse trees Order for same operators Priority of operators
    Purpose Identify problem Resolve order Resolve priority
    Example id + id * id a - b - c a + b * c
    Type β€” Left/Right Levels
    Role Problem Solution Solution
    Importance Very High Very High Very High

    βœ… Final Conclusion

    • Ambiguity creates confusion in parsing ❌
    • Associativity and precedence resolve that confusion βœ…
    • Proper grammar design ensures correct evaluation of expressions

    ← Previous topic 7
    Definitions of Grammars, Derivations, and Parse Trees
    Next topic 9 β†’
    Syntax Analysis and Role of the Parser

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    586words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction