grammar - How to write a CFG with functions? -


in assignment, asked write cfg functions like:

def f(x, y): return x + y

def g(x, y): return x – y

def h(x, y, z): return x + y % z

def w(x, y, z): return x * y – z

and

def h1(x, y, z): return (x + y) % z

def h2(x, y, z): return x + y % z

i have tried work normal cfg but, not function definitions , function bodies. not pretty sure how start kind of cfg's.

this bad question - cannot encode rule "only parameters used in function body" in cfg. ignoring little problem, however, can try:

s := def f (l): return e  f := cn c := f | g | h | w n := (empty string) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0  l := x | xy x := x | y | z y := , l  e := x | e + e | e - e | e / e | e % e | e * e | (e) 

s provides overall structure of function. f defines how function names made. l defines how list of variables made. e defines how expression involving variables , operators made. note allow stuff def f(x): return y, can't prevent in cfg.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -