I have the following grammar (this a simplified one):
S -> EXPR
EXPR -> ITEM addop EXPR
EXPR -> ITEM
ITEM -> num
ITEM -> ident
having:
num: a floating point number
ident: a string representing an identifier
addop: +
I am using PLY library for python, and have the following code:
def p_L_S(self,p):
''' S : EXPR'''
p[0] = p[1]
def p_L_EXPR_1(self,p):
''' EXPR : ITEM addop EXPR'''
p[0] = p[1] + p[2]
def p_L_EXPR_2(self,p):
''' EXPR : ITEM'''
p[0] = p[1]
def p_L_ITEM_1(self,p):
''' ITEM : num '''
p[0] = float(p[1])
def p_L_ITEM_2(self,p):
''' ITEM : ident '''
p[0] = value_of_expr_associated_to_ident(p[1])
[...]
in the last function (p_L_ITEM_2) I would like to interprete the string associated to p[1] (which is an expression recognized by the grammar) without launching another parsing.
Today, the function value_of_expr_associated_to_ident
launches a new parsing (calling parse
method) of the expression associated to ident
.
But this gives really poor performances, even if it works.
Is there a way to send to the parser the lexems associated to the expresion associated to the ident
to avoid having to start a new parsing ?
I don't know if this is clear, and if not I will try to clarify.
Thanks a lot. Sam