Python parser ply matches wrong regex
<p>I'm trying to create a parser using Ply but I am confronted to a strange error.
Here is a MCVE where the matching error occurs :</p>

<p>Lexer</p>

<pre><code>import ply.lex as lex

tokens = (
    'IDENTIFIER',
    'NAME',
    'EQUALS'
)

def t_IDENTIFIER(t):
    r'\* *[a-zA-Z_]+'
    print("identifier")
    return t

def t_NAME(t):
    r"[a-zA-Z_]+"
    print("name")
    return t

t_EQUALS  = r"="
t_ignore = ' \t'


def t_newline(t):
    r'\n+'
    t.lexer.lineno += len(t.value)

# Error handling rule
def t_error(t):
    print("Illegal character '%s' at line' %s'" % (t.value[0] , t.lexer.lineno ))
    t.lexer.skip(1)

# Build the lexer
lexer = lex.lex()
</code></pre>

<p>Parser</p>

<pre><code>import ply.yacc as yacc
from l import tokens

def p_main(p):
    '''
    main : NAME EQUALS NAME
    '''

def p_error(p):
    if p is not None:
        print ("Line %s, illegal token %s" % (p.lineno, p.value))
    else:
        print("Syntax error in input!")

parser = yacc.yacc()

with open('simple2','r') as f:
    result = parser.parse(f.read())
</code></pre>

<p>My input file only contains this :</p>

<pre><code>A = B
</code></pre>

<p>And what happens is that the first word <code>A</code> is matched by the token <code>IDENTIFIER</code> even if it is not supposed to do since the regex requires a <code>*</code> before the letters.
After this the parser is unable to recognize the expression since the lexer does not return the right tokens.</p>

<p>What is wrong ? The regex used for the token IDENTIFIER works perfectly in Python.</p>

<p>I think I found problem and solution. </p>

<p>Problem is <code>'*'</code> in <code>' *'</code> because it treats <code>'\* '</code> as one string - so <code>'\* *'</code> means <code>'\* '</code> many times or <strong>none</strong> (like <code>'abc*'</code> means <code>'abc'</code> many times or none).</p>

<p>You need <code>'\*[ ]*'</code> or <code>'\*\s*'</code></p>

python regex parsing lexer ply