So im writing a grammar on Ply that recognizes basic C statements, such as a variable declaration or a while statement. For now, what I want to do is to be able to concatenate all the tokens and then print it or have it propagate up the tree, like this:
def p_whileStmt(p):
'''whileStmt : WHILE '(' condition ')' '{' stmt '}' '''
p[0] += p[1] + p[2] + p[3] + p[4] + p[5] + p[6] + p[7]
Is there a better way to concatenate all the tokens (I only need the character values, I only need to build a string) than the one I'm using?
EDIT: In certain cases I need to concatenate all the tokens except a few, like for example:
def p_whileStmt(p):
'''whileStmt : WHILE '(' condition ')' '{' stmt '}' '''
p[0] += p[1] + p[3] + p[5]
You can avoid
+=
by usingAnd for
p[0] += p[1]+p[3]+p[5]
you can do