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
    🧩
    Compiler Construction
    COMP3149
    Progress0 / 32 topics
    Topics
    1. Introduction to interpreter and compiler2. Structure of a Compiler and its Phases3. Lexical Analyzer and Input Buffering4. Specifications and Recognitions of Tokens5. Regular Expressions and Finite Automata6. Transition Table and Transition Graph7. Definitions of Grammars, Derivations, and Parse Trees8. Ambiguity, Associativity, and Precedence of Operators9. Syntax Analysis and Role of the Parser10. Eliminating Ambiguity, Left Recursion, and Left Factoring11. Top-Down Parsing and Recursive-Descent Parsing12. First and Follow Sets13. LL(1) Grammars and Non-recursive Predictive Parsing14. Bottom-Up Parsing: Reductions and Shift-Reduce Parsing15. LR Parsing and LR(0) Parsers16. LR(0) Automaton and Parsing Table17. Shift-Reduce Conflicts18. SLR(1) Parsers: Automaton and Parsing Table19. LR(1) Parsers: Automaton and Parsing Table20. LALR Parsing: Automaton and Parsing Table21. Semantic Analysis and Intermediate Code Generation22. Three Address Code23. Tasks of Semantic Analyzer and Types of Errors24. Type Checking and Environments25. Type Conversions: Implicit vs Explicit26. Back Patching and Switch Statements27. Storage Organization and Stack Allocation of Space28. Heap Management and Optimization29. Code Generation: Design of a Code Generator30. Target Language and Addresses in Target Code31. Basic Blocks and Flow Graphs32. Optimization of Basic Blocks
    COMP3149›Ambiguity, Associativity, and Precedence of Operators
    Compiler ConstructionTopic 8 of 32

    Ambiguity, Associativity, and Precedence of Operators

    3 minread
    586words
    Beginnerlevel

    📘 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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time3 min
      Word count586
      Code examples0
      DifficultyBeginner