Alice and Bob are creating a golfing language to win every single PPCG challenge. Alice wants to make a two-dimensional language, like ><>, but Bob prefers a prefix-infix syntax like in J. As a compromise, they decide to create a two-dimensional prefix-infix language. The parser is a pain to write, and they need your help!
In Alice's and Bob's language, there are variables, which are represented by lowercase ASCII letters
a-z, and functions, which are represented by uppercase ASCII letters
A function can be invoked with one or two arguments.
A program is a rectangular grid of letters
a-zA-Z and spaces, and the top left corner must not contain a space.
This is an example of a valid program:
F Gy H R x
When the program is parsed, it's transformed into an expression of a C-style language (C, Java, Python...) containing single-letter variables and function calls in the format
For example, the above program results in this expression:
The details of the parsing process are as follows:
- The spaces are just filler, so they are not parsed.
- Every variable
a-zis always parsed as itself.
- Every function
A-Zis parsed as a function call. Its arguments are the closest expressions below it and to its right in the grid, in this order. If only one of these is present, it's given as the sole argument. You can assume that all functions have at least one argument in the grid.
In the above example, the variables
y are parsed as themselves.
R has nothing below it and
x to its right, so it's parsed as the one-argument invocation
H is parsed as
H(R(x)), since it has
R below it.
x below it and
y to its right, so it's parsed as
G(x,y), and similarly for
The expression parsed at the top left corner is the result of the parsing process.
Input and output
Your input is a non-empty rectangular array of characters. It will always be a valid program in Alice's and Bob's language, but it may contain expressions that are not used in the output. Your output shall be the parsed expression resulting from the above process.
Rules and scoring
You can write a full program of a function. The lowest byte count wins, and standard loopholes are disallowed.
These are given in the format
grid <newline> expression, with hyphens
--- between the cases.
The SE format leaves some lines blank, but they should be filled with spaces.
x x --- x y z x --- Fx F(x) --- Fx y F(y,x) --- ABu A(B(u)) --- G H k G(H(k)) --- ABCA x xs DFk A(x,B(D(F(k)),C(x,A(s)))) --- A B C D x A(C(D(x)),B(D(x))) --- RT Hq I xR k R(I(x),T(H(R(k),q))) --- A A A a S A b B C Dx d X u f A(B(d,C(D(f,x))),A(X(u),A(u,a)))
(A (B (D x)) (C (D x)))be suitable or is the format fixed? \$\endgroup\$