I designed a toy language :
# comments
n = 2 #an integer
k = 3.14 # a float
f(x): # a function
return (x-k)^n
g(z): #another function
return max(z-2.9, 0.1) # max should be understood
h(x,y: # another function ; note that function can call functions
return x*g(y)
pointable x # a formal variable
pointable y # another formal variable
gives h (x @ 3.2, y @ 1.9) @ 6.1 # an instruction, the meaning will be clear later
gives f (x @ 1.2) @ 3.1 #another instruction
give # the final instruction
The purpose of this language is the following : the previous code, when parsed, is supposed to create the following list :
[[1, (f), (1.2), 3.1], [2, (f, h), (3.2, 1.9), 6.1]]
There are as many elements as there are give
instructions.
I have no idea about how to design a grammar for this language. I know that it has to be in a EBNF form because I want to used plyplus
as I already toyed with it with simple algebraic expressions parsing, but there is a great move from simple algebraic expression directly evaluated and what I intend to do. (Even if I know that formally it is the same.)
(How are treated functions when you have the abstract syntax tree of the above code btw ?)
For information, at the end, from [[1, (f), (1.2), 3.1], [2, (f, h), (3.2, 1.9), 6.1]]
I want to produce the following python function :
def F(X1,X2):
# code defining f and g "as" defined above
# ...
# code using f and g
S1 = exp(-3.1)*f(X1/1.2)
S2 = exp(-6.1)*g(X1/3.2,X2/1.9)
res = S1 + S2
return res
and I would like to product it is as
ast = language_grammar.parse(code_above) #tree
F = tree.generatefunction()
I got the python grammar in the plyplus
form and tried to reduce it (I would still like to having conditions and loops in my language) and adapt, without success, as this is really not my area.
I also feel that I would need in the grammar an algebraic subgrammar using all classical math functions and combining them, but I cannot really give a formal definition to this idea.