title: yacc - Precedence of a rule with no operator?
body: <p>Thinking about parsing regular expressions using yacc (I'm actually using PLY), some of the rules would be like the following:</p>
  
  <pre><code>expr : expr expr
  expr : expr '|' expr
  expr : expr '*'
  </code></pre>
  
  <p>The problem is, the first rule(concatenation) must take precedence over the second rule, but not the third one.</p>
  
  <p>However, the concatenation rule has no operator in it.</p>
  
  <p>How can I specify the precedence correctly in this case?</p>
  
  <p>Thank you!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I modified the rules to avoid the issue, but I'm still curious what was the problem.</p>
  
  <p>Here is the source code:</p>
  
  <pre><code>tokens = ['PLEFT', 'PRIGHT', 'BAR', 'ASTERISK', 'NORMAL']
  
  t_PLEFT = r'\('
  t_PRIGHT = r'\)'
  t_BAR = r'\|'
  t_ASTERISK = '\*'
  t_NORMAL = r'[a-zA-Z0-9]'
  
  lex.lex()
  
  precedence = (
    ('left', 'BAR'),
    ('left', 'CONCAT'),
    ('left', 'ASTERISK'),
  )
  
  def p_normal(p):
      '''expr : NORMAL'''
      p[0] = p[1]
  
  def p_par(p):
      '''expr : PLEFT expr PRIGHT'''
      p[0] = p[2]
  
  def p_or(p):
      '''expr : expr BAR expr'''
      p[0] = ('|', p[1], p[3])
  
  def p_concat(p):
      '''expr : expr expr %prec CONCAT'''
      p[0] = ('CONCAT', p[1], p[2])
  
  def p_repeat(p):
      '''expr : expr ASTERISK'''
      p[0] = ('*', p[1])
  
  yacc.yacc()
  </code></pre>
  
  <p>Its parsing result of <code>'ab|cd*'</code> is <code>('CONCAT', ('|', ('CONCAT', 'a', 'b'), 'c'), ('*', 'd'))</code>.</p>