25
1
Background
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!
Syntax specification
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-Z
.
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 <func>(<arg>)
or <func>(<arg1>,<arg2>)
.
For example, the above program results in this expression:
F(H(R(x)),G(x,y))
The details of the parsing process are as follows:
- The spaces are just filler, so they are not parsed.
- Every variable
a-z
is always parsed as itself. - Every function
A-Z
is 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 x
and y
are parsed as themselves.
The function R
has nothing below it and x
to its right, so it's parsed as the one-argument invocation R(x)
.
Similarly, H
is parsed as H(R(x))
, since it has R
below it.
The function G
has x
below it and y
to its right, so it's parsed as G(x,y)
, and similarly for F
.
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.
Test cases
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)))
Would an output like
(A (B (D x)) (C (D x)))
be suitable or is the format fixed? – coredump – 2015-12-03T19:22:05.3831@coredump In this challenge, the output format is strict; Alice and Bob want to be able to use the parsed expression directly. – Zgarb – 2015-12-03T19:28:41.347
Where is the "infix" part of the language? I only see prefix. – Paŭlo Ebermann – 2015-12-03T19:38:33.333
@PaŭloEbermann A function with two arguments can be thought of being between the arguments, on the Γ-shaped path from the bottom argument to the right one. – Zgarb – 2015-12-03T19:40:53.777
6Can you please actually create a language with this syntax? :) – Martin Ender – 2015-12-10T10:26:31.313
4Follow-up challenge: write a meta-golfer for this syntax (given an expression tree, find the smallest grid corresponding to it). For extra difficulty, distinguish between commutative and non-commutative functions. – Martin Ender – 2015-12-11T14:30:34.907