- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# What are Precedence Functions in compiler design?

Precedence relations between any two operators or symbols in the precedence table can be converted to two precedence functions f & g that map terminals symbols to integers.

- If a <. b, then f (a) <. g (b)
- If a = b, then f (a) =. g (b)
- If a .> b, then f (a) .> g (b)

Here a, b represents terminal symbols. f (a) and g (b) represents the precedence functions that have an integer value.

**Computations of Precedence Functions**

- For each terminal a, create the symbol f
_{a}&g_{a}. - Make a node for each symbol.

If a =. b, then fa & gb are in same group or node.

If a =. b & c =. b, then fa & fc must be in same group or node.

- (a) If a <. b, Mark an edge from g
_{b}to f_{a}.

(b) If a .>b, Mark an edge from f_{a} to g_{b}.

- If the graph constructed has a cycle, then no precedence functions exist.
- If there are no cycles.

(a) f_{a} = Length of longest path beginning at the group of f_{a}.

(b) g_{a} = Length of the longest path from the group of g_{a}.

**Example1** − Construct precedence graph & precedence function for the following table.

**Solution**

**Step1** − Create Symbols

**Step2** − No symbol has equal precedence, as can be seen in the given table; therefore, each symbol will remain in a different node.

**Step3** − If a <. b, create an edge from f_{a} → g_{a}

If a .>b, create an edge from g_{b} → f_{a}

Since, $ <. +,*, id. therefore, make an edge from g_{+}, g_{*}, g_{id} to f_{s}

Similarity + <. ,∗, id. ∴ make an edge from g_{*}, g_{id} to f_{+}

Similarity * <. id. Therefore, Mark an edge from g_{id} to f_{*}.

Since, +,*, id . > $ therefore, Mark an edge from f_{+}, f_{*}, f_{id} to g_{s}.

Similarity +,*, id . > +. Mark an edge from f_{+}, f_{*}, f_{id} to g_{+}.

Similarity *, id . > *. Mark an edge from f_{*}, f_{id} to g.

Combining all the edges we get

**Step4** − Computing the maximum length of the path from each node, we get the following precedence functions

Id | + | * | $ | |

F | 4 | 2 | 4 | 0 |

G | 5 | 1 | 3 | 0 |

**Example2** − Construct precedence graph & precedence function for the following table.

**Solution**

As we have (=.). Therefore f & g will be in the same group.

Computation of precedence graph

**Computation of Precedence Function Table**

Since f_{$} and g_{$} have no outgoing edges, f($ ) = g($ ) = 0.

Since f and g have no outgoing edges, f(( ) = g( )) = 0.

For all others, compute the path of the longest length starting from it.

**For Example −**

Take f_{+},

It has three outgoing edges and traces out its paths.

f_{+} → g_{$}

f_{+} → f_{(}

f_{+} → g_{+} → f_{(} and f_{+} → g_{+} → f_{$}

Select the path of maximum length, and the length is 2.

Hence, f_{+} = 2. computing all paths of f and g, we get Precedence table

+ | * | ( | ) | id | $ | |

F | 2 | 4 | 0 | 4 | 4 | 0 |

G | 1 | 3 | 5 | 0 | 5 | 0 |

- Related Questions & Answers
- What is Operator Precedence Parsing Algorithm in compiler design?
- What are Parsing Techniques in Compiler Design?
- What is Compiler Design?
- What is Design of Lexical Analysis in Compiler Design?
- What are the attributes of programming languages in compiler design?
- What are the types of the translator in compiler design?
- What are the operations on sequential files in compiler design?
- What are the Rules of Regular Expressions in Compiler Design?
- What are Lists and Self-Organizing lists in compiler design?
- What is Chomsky Hierarchy in compiler design?
- What is error handling in compiler design?
- What is Input Buffering in Compiler Design?
- What is Finite Automata in Compiler Design?
- What is Language Processing Systems in Compiler Design?
- What is minimizing of DFA in compiler design?