I have faced a problem when using PLY.
I want to create a call graph generator by PLY.
In some situation, I need to discard some tokens in the grammar file.
That is because I need to do something when the parser recognize that token before I discard it, so
I can’t just discard in the lexer file.
For example, the ‘IF’ token is the one which I want to discard.
So I try to do something to discard it in the grammar file.
Just like:
def p_if(p):
'if : IF'
print "if"
parser.symstack.pop()
But things didn’t go the way I think.
I print the symstack(it’s a atribute of parser, and parser is a LRParser instance of yacc.py),
and the symstack list just contain the previous tokens but not ‘if’.
So I am wondering how to discard a token in this situation.
Could anyone help me? Thanks a lot!
Thanks a lot!!
Actually, I have found a way to solve my problem.
You are right, the parser.symstack should never be manipulated.
Because my English is not good(my mother language is not English), I didn’t explain
my problem clearly.But I still appreciate for your advice.
The following is my solution, and I hope this will help someone else in the future.
I. define p_error(p) function
II. raise syntax error in the function you need