# Precedence Table Construction Algorithm for CFGs Regardless of Being OPGs
## 4 Approach
### Algorithms
Initially, precedence relations are added between the delimiting symbol \(\$\) and the Left and Right sets of the non-terminal symbol of start \(S\), according to rules (A.1) and (A.2), respectively.  
Algorithm 5 operates in an iterative manner. It traverses all the productions of the set \(P\), and for each iteration, it initializes two temporary variables, \(u\) and \(l\), to null and an empty list, respectively. It then performs a series of processes for each pair of contiguous grammatical symbols \(X\) and \(Y\) in a production.  
1. The algorithm checks if \(XY\) has the form \(ab\); if so, it adds the corresponding precedence relationship between \(a\) and \(b\), following rule (B).
2. The algorithm checks if \(XY\) has the form \(aB\); if so, it assigns \(u\) the value of \(a\) and adds the corresponding precedence relations between \(a\) and terminals in \(\texttt{Left}(B)\), following rule (C).
3. The algorithm checks if \(XY\) has the form \(Ab\); if so, it adds the corresponding precedence relations between \(b\) and terminals in \(\texttt{Right}(A)\), following rule (D.1). Then, check if \(u\
eq null\), and if so, add the corresponding precedence relationship between \(u\) and \(b\), following rule (D.2), and reset \(u\) to \(null\). Finally, checks if list \(l\) has elements, and if so, adds the corresponding precedence relations between \(b\) and terminals in \(\texttt{Right}(B)\), following rule (D.3), and resets list \(l\) to empty.  
4. The algorithm checks if \(XY\) has the form \(AB\); if so, it adds the corresponding precedence relations between terminals in \(\texttt{Right}(A)\) and terminals in \(\texttt{Leftmost}(B)\), following rule (E.1). Then, it checks if \(u\
eq null\), and if so, adds the corresponding precedence relation between \(u\) and terminals in \(\texttt{Left}(B)\), following rule (E.2). Finally, it checks if the list \(l\) has elements and if so, adds the corresponding precedence relations between terminals in \(\texttt{Right}(C)\), with \(C\in l\) and terminals in \(\texttt{Leftmost}(B)\), following rule (E.3), and resets the list \(l\) if necessary.  
```
\(G(S)=<T,N,S,P>\) procedure\(\texttt{Table}(G,\texttt{Left}(\cdot),\texttt{Right}(\cdot),\texttt{Leftmost}(\cdot))\)  compute \(\texttt{Left}(A),\texttt{Right}(A),\texttt{Leftmost}(A),\forall A\in N\)  add \(\$\lessdot a,\forall a\in\texttt{Left}(S)\) (A.1)  add \(a\succ\$,\forall a\in\texttt{Right}(S)\) (A.2) for each \(A\sim\gamma\) in \(P\)do \(u\leftarrow null\) \(l\leftarrow\lfloor\ \rfloor\ //\) empty list for each \(XY\) contiguous in \(\gamma\)do if\(XY=ab\)then  add \(a\doteq b\) (B) endif if\(XY=ab\)then \(u\gets a\)  add \(a\lessdot b,\forall b\in\texttt{Left}(B)\) (C) endif if\(XY=Ab\)then  add \(a\gtrdot b,\forall a\in\texttt{Right}(A)\) (D.1) if\(u\
eq null\)then  add \(u\doteq b\) (D.2) \(u\leftarrow null\) endif if\(l\
eq\lfloor\ \rceil\)then  for\(B\in l\)do  add \(a\gtrdot b,\forall a\in\texttt{Right}(B)\) (D.3) endfor \(l\leftarrow\lfloor\ \rceil\) endif endif if\(XY=AB\)then  add \(a\gtrdot b,\forall a\in\texttt{Right}(A)\land\forall b\in\texttt{Leftmost}(B)\) (E.1) if\(u\
eq null\)then  add \(u\lessdot b,\forall b\in\texttt{Left}(B)\) (E.2) endif if\(l\
eq\lfloor\ \rceil\)then  add \(c\gtrdot b,\forall c\in\texttt{Right}(C)\land\forall b\in\texttt{Leftmost}(B)\) (E.3) endfor endif if\(B\stackrel{{*}}{{\Longrightarrow}}\epsilon\)then  add \(A\) to \(l\) else \(l\leftarrow\lfloor\ \rceil\) endif  endif  endfor endprocedure
```  
**Algorithm 5** Algorithm to obtain the operator precedence table of a \(G\) grammar