0

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.

alan
  • 1
  • Take a look at this link: https://stackoverflow.com/questions/5920120/how-to-define-a-function-from-a-string-using-python it might help. – Fletchy Aug 16 '19 at 17:29
  • ```functionList=['E', 'ED', 'T', 'TD', 'F']``` is my list, i tried creating function using exec like this ```exec("""def """+functionList[0]+"""():\n""" """ exec(x[0][0]+())\n""" """ exec(x[0][1]+())""")``` but when i try to call function using ```exec(functionList[0]+"""()""")``` it throws error ```TypeError: can only concatenate str (not "tuple") to str``` @Fletchy1995 – alan Aug 17 '19 at 11:49
  • x=[['T', 'ED'], ['T', 'ED'], ['F', 'TD'], ['F', 'TD'], '(E)|id'] – alan Aug 17 '19 at 11:58
  • 1
    Well, what you expected `exec(x[0][0]+())` to do? Anyway, I think you should reconsider trying to create functions from CFG dynamically, this looks like it will result in code that's very hard to read of reason about. – Nickolay Aug 18 '19 at 16:08
  • Yes ! the ```exec(x[0][0]+'()')``` should create a code which is a function, which is to be executed later, but it looks like python isn't going to let me execute it. @Nickolay – alan Aug 20 '19 at 07:50
  • You wrote `+()` instead of `+'()'` in a previous comment, though, which results in the "can't concatenate tuple" error. – Nickolay Aug 20 '19 at 11:19
  • I did a mistake before, but even if with ```'()' or "()" or """() """``` it is showing the same error @Nickolay – alan Aug 20 '19 at 14:23

0 Answers0