title: Ambiguous ANTLR grammar
body: <p>I'm trying to make a parser for a language where <code>.</code> could be concatenation or accessing dictionary value by key e.g.<br />
  <code>&quot;a&quot;.&quot;b&quot;</code> - concatenation of two strings<br />
  <code>foo.&quot;some text&quot;</code> - concatenation of &quot;foo&quot; variable's value and &quot;some text&quot;<br />
  <code>myDict.myKey</code> - getting value from dictionary &quot;myDict&quot; by key &quot;myKey&quot;<br />
  <code>myDict.(myKey)</code> - concatenation of two variables with names &quot;myDict&quot; and &quot;myKey&quot;</p>
  <p>Below is a sample grammar for concatenation and getting dictionary values:</p>
  <pre><code>grammar Test;
  
  expr:
                  expr DOT NAME           #DictFieldExpr
              |   expr DOT expr           #Concatenation
              |   L_PAREN expr R_PAREN    #WrappedExpr
              |   string                  #StringExpr
              |   NAME                    #VariableExpr
  ;
  
  string:         QUOTE .*? QUOTE;
  
  NAME:           [a-zA-Z]+;
  L_PAREN:        '(';
  R_PAREN:        ')';
  DOT:            '.';
  QUOTE:          '&quot;';
  </code></pre>
  <p>As I know <code>#DictFieldExpr</code> should have higher priority than <code>#Concatenation</code> because it was defined earlier. But for input like <code>outerDict.innerDict.key</code> parser returns</p>
  <pre><code>expr:Concatenation----expr:VariableExpr----NAME:&quot;outerDict&quot;
                    \
                     \
                expr:DictFieldExpr----expr:VariableExpr----NAME:&quot;innerDict&quot;
                              \
                               \NAME:&quot;key&quot;
                                   
  </code></pre>
  <p>(both nodes should be <code>DictFieldExpr</code>, but the first node is taken for <code>Concatenation</code> for some reason)<br />
  How to handle such ambiguous grammars in ANTLR4?</p>