JKKGBE JKKGBE - 6 days ago 5
Python Question

PLY differentiate between grammars

so I'm doing this project where you order a forklift to do certain things with natural language and I'm using Python Lex Yacc. I wanted to know if there is a way to differentiate between permuted grammar that is the same length? The documentation only states that you can do it with len(p) but that only helps if they different in length. Sample code:

def p_moveitemfromto(p):
'''moveitemfromto : MOVEITEM fulltype item rack rack side
| MOVEITEM rack rack side fulltype item
| rack MOVEITEM fulltype item rack side
| rack MOVEITEM rack side fulltype item'''


I need the output to always be in an unified order (so I stick to 1st being default), what I'm doing now is just adding different letters on the lower levels so then I can order it the right way and then deleting the letters, but it seems like it's not the way to go. I could also split it to different defs but that again doesn't seem like the way to go. Or maybe I've got the whole concept wrong since it's my first time.

Answer

If different productions (even for the same non-terminal) have different actions, then implement them in different functions. That is a normal and totally reasonable way to use Ply. Combining different production actions so that you have to figure out which production was reduced is false economy.

Comments