yacc statement recognition

S

Slafs

Hi ALL!

I have to write in yacc an acceptor of files with lines matching this
regexp:
'[0-9],[0-9]'
and I don't know what I am doing wrong beacuse this:

--------
tokens = (
'NUMBER',
)
literals = [',']

t_NUMBER = r'\d'

....

def p_statement_exp(p):
'''statement : NUMBER ',' NUMBER
'''
print "OK!"
sys.exit()
-------

also accepts lines like 2,abcdef3 which of
could someone please tell me what's wrong in my code?

full source on http://paste-it.net/public/vba22d5/
 
F

Francesco Bochicchio

Slafs ha scritto:
Hi ALL!

I have to write in yacc an acceptor of files with lines matching this
regexp:
'[0-9],[0-9]'
and I don't know what I am doing wrong beacuse this:

--------
tokens = (
'NUMBER',
)
literals = [',']

t_NUMBER = r'\d'

...

def p_statement_exp(p):
'''statement : NUMBER ',' NUMBER
'''
print "OK!"
sys.exit()
-------

also accepts lines like 2,abcdef3 which of
could someone please tell me what's wrong in my code?

full source on http://paste-it.net/public/vba22d5/

Your code works for me (Python 2.5 and ply 2.3 on ubuntu 8.10), except
that, from the rest of your code, I see that you parse separately each
line of file. Since you put a sys.exit() in the parsing rule, it means
that at the first line successfully parsed your program will quit.

Try putting a simple print in it and instead putting the sys.exit in
your p_error function (if you want to exit at the first 'wrong' line line).

BTW, are you aware that exists a specific google group for ply users?
Specific ply questions could be answered here :
http://groups.google.com/group/ply-hack?pli=1

Ciao
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top