ply yacc lineno not working?

Laszlo Nagy gandalf at shopzeus.com
Fri May 16 07:42:03 EDT 2008


I'm sorry for the dumb question. I had to add these to the lexer:

def t_comment(t):
    r"\#[^\n]*\n"
    t.lexer.lineno += 1
    # We do not return anything - comments are ignored.

# Define a rule so we can track line numbers
def t_newline(t):
    r'\n+'
    t.lexer.lineno += len(t.value)


Well, it is not very straightforward. From the docs, it was not clear 
that I HAVE TO increment t.lexer.lineno in my lexer if you want yacc to 
parse line numbers. But it is logical. I believe this could be done 
automatically, since "line number" ALWAYS means "\n", there is nothing 
to be configured here. (Am I wrong? There can be parsers to parse files 
where new lines are chr(255) or something?)

Thanks,

   Laszlo




More information about the Python-list mailing list