I'm developing a parser in python with ply. But I have a question about the parse rules, because I have something like this:
def p_main(p):
main : PROGRAMA ID declaraciones declaracion_funcion bloque
butI have seen that rules use simple quote, why?, what is it used it for?
For example:
def p_expression_binop(p):
'''expression : expression PLUS expression
| expression MINUS expression
| expression TIMES expression
| expression DIVIDE expression'''
if p[2] == '+' : p[0] = p[1] + p[3]
elif p[2] == '-': p[0] = p[1] - p[3]
elif p[2] == '*': p[0] = p[1] * p[3]
elif p[2] == '/': p[0] = p[1] / p[3]
why the 3 simple quotes?
Have you tried running your program yet? I expect that if you do, you will get SyntaxErrors, because your definition of your parse rules, while valid BNF, is not valid Python. PLY introspects the docstrings of the "p_xxx" methods to read the BNF that corresponds to that expression's parse rules, since there is no constraint on the content of your quoted strings. The triple-quote is how we define a multiline string in Python, so it is perfect for capturing the parse rule to correspond to a parse method.