title: Operator precedence when converting from CFG to LL(1)
body: <p>I'm working in translating a context free grammar into a LL(1) grammar. I'm trying to implement operator precedence. I'm dealing with this little code:</p>
  
  <pre><code> 'Expression ::= 'Expression ~ 'Op ~ 'Expression
        | 'Expression ~ LBRACKET() ~ 'Expression ~ RBRACKET()
        | 'Expression ~ DOT() ~ LENGTH()
        | 'Expression ~ DOT() ~ 'Identifier ~ LPAREN() ~ 'Args ~ RPAREN()
        | INTLITSENT | STRINGLITSENT
        | TRUE() | FALSE() | 'Identifier | THIS()
        | NEW() ~ INT() ~ LBRACKET() ~ 'Expression ~ RBRACKET()
        | NEW() ~ 'Identifier ~ LPAREN() ~ RPAREN()
        | BANG() ~ 'Expression
        | LPAREN() ~ 'Expression ~ RPAREN(),
  
   'Op ::= AND() | OR() | EQUALS() | LESSTHAN() | PLUS() | MINUS() | TIMES() | DIV(),
  </code></pre>
  
  <p>The important here is that I want to translate line  </p>
  
  <pre><code>'Expression ::= 'Expression ~ 'Op ~ 'Expression
  </code></pre>
  
  <p>So that it actually implements precedence from high to low priority: !,*,/,+ -, &lt; , ==, &amp;&amp;, ||. I found a good method <a href="https://stackoverflow.com/questions/26471876/how-to-tell-the-precedence-of-operators-in-a-context-free-grammar">here</a>. But I'm worried that if I just write:</p>
  
  <pre><code> 'Expression ::= 'OrExpression
   'OrExpression ::= 'AndExpression ~ OR() ~ 'OrExpression | 'AndExpression,
   'AndExpression ::= 'EqualsExpression ~ AND() ~ 'AndExpression | 'EqualsExpression,
   'EqualsExpression ::= 'LessthanExpression ~ EQUALS() ~ 'EqualsExpression | 'LessthanExpression,
   'LessthanExpression ::= 'AddExpression ~ LESSTHAN() ~ 'LessthanExpression | 'AddExpression,
   'AddExpression ::= 'DivExpression ~ PLUS() ~ 'AddExpression | 'DivExpression ~ MINUS() ~ 'AddExpression | 'DivExpression,
   'DivExpression ::= 'TimesExpression ~ DIV() ~ 'DivExpression | 'TimesExpression,
   'TimesExpression ::= 'TimesExpression ~ TIMES() ~ 'TimesExpression | 'BangExpression,
   'BangExpression ::= BANG() ~ 'BangExpression | 'BracketExpression,
   'BracketExpression ::= 'Expression ~ LBRACKET() ~ 'Expression ~ RBRACKET() | 'DotExpression,
   'DotExpression ::= 'Expression ~ DOT() ~ LENGTH() | 'Expression ~ DOT() ~ 'Identifier ~ LPAREN() ~ 'Args ~ RPAREN() | 'NewExpression,
   'NewExpression ::= NEW() ~ INT() ~ LBRACKET() ~ 'Expression ~ RBRACKET() | NEW() ~ 'Identifier ~ LPAREN() ~ RPAREN() | 'BasicExpression
   'BasicExpression ::= INTLITSENT | STRINGLITSENT | TRUE() | FALSE() | 'Identifier | THIS() | LPAREN() ~ 'Expression ~ RPAREN()
  </code></pre>
  
  <p>then removing left recursion gets quite nasty. Can you think of others ways of doing it?</p>