I'm trying to create Recursive Descent Parser in Python. It takes input string from user at runtime
Example string :
str1="(id+id*id+id)$"
and based on predefined CFG
Example Grammar :
E->TE'
E'->+TE'|e
T->FT'
T'->*FT'|e
F->(E)|id
the input string is passed through recursive functions and if it matches, the result is printed as Success.
Example Functions :
#A pointer to scan input string character by chatacter
lookahead=str1[0]
##def E():
## #print("E()")
## T()
## ED()
##
## if count==l-1:
## if lookahead=='$':
## print("Success")
## else:
## print("Error : Rejected !")
## return
## else:
## pass
#Similarly, all the other functions are defined
def T():
F()
*remaining code for function*
def F():
*remaining code for function*
id = identifier and e = epsilon
Now, I was able to parse it for this Static CFG, but when I stated modifying it for Dynamic CFG which a user can input along with the string, I encountered error with Name Allocation for the functions and their attributes.
#Names of Functions to be created which is Extracted from User Input CFG
functionList=['E','ED','T','TD','F']
#Note: using ED to display E' for function name
#Code to create function with names from functionList
def function_builder(val):
def function(val):
print("fun_",val)
return function
dynamic_functions = {}
k=0
for i in functionList:
dynamic_functions[i] = function_builder(functionList[k])
k+=1
dynamic_functions[functionList[0]](functionList[0])
So, if input is A->TA', the program should automatically create
def A(), def T(), def AD() and so on. Also, I want to add properties to all function once they are allocated their names, like...
def A():
T()
AD()
*remaining code for function*
My question is how can I define function name from list and give property then after?
P.S.: I have searched stackoverflow for Dynamic Function Name Generation from List/variable but didn't find any answers that can help me.