Parse a two-dimensional syntax Background

Alice and Bob are creating a golfing language to win every single PPCG challenge.
Alice wants to make a two-dimensional language, like >&lt;>, 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 &lt;func&gt;(&lt;arg&gt;) or &lt;func&gt;(&lt;arg1&gt;,&lt;arg2&gt;).
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 &lt;newline&gt; 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)))
CJam, 67 62 60 58 57 54 bytes

Thanks to Dennis for saving 4 bytes.

{:Gsc_32&amp;{"()"[GGz2{1m&lt;_{S#}#&gt;W&lt;\}*z]{},{J}%',**+}|}:J


This defines a named block (function) J and leaves it on the stack. The function itself expects an array of strings on the stack, which represents the input grid, and leaves the desired string in its stead.

Test it here.

I've been meaning to tackle this one since it was posted, but apparently I needed a bounty and an overly long Pyth solution to motivate myself sufficiently.

Explanation

The solution is of course recursive and gradually builds up the string.

{
  :G       e#  Store the current grid in G.
  sc       e#  Convert the grid to a string, flattening it, then to a character, which
           e#  discards everything but the first character. This is a golfed 0=0=.
           e#  This character will either be an upper-case letter (function) or a lower-
           e#  case letter (variable).
  _32&amp;     e#  Make a copy of the character and take bitwise AND with 32. This gives a
           e#  NULL character for functions and a space for variables.
  {        e#  If the result is falsy... (i.e. NULL, i.e. we have a function)
    "()"   e#   Push parentheses for later use.
    [      e#   Remember the current stack depth.
    GGz    e#   Push an array which contains the grid and its transpose.
    2{     e#   Run this block twice... (it will be applied once to each grid)
      1m&lt;  e#    Rotate the rows. This has two effects: a) It removes the first row
           e#    from the front such that we can go looking for the next non-space
           e#    character down the first column from the start. b) It places a row
           e#    at the end which we know doesn't start with a space, which acts as
           e#    a guard in case there are no further letters below the current one.
      _    e#    Duplicate this grid.
      {    e#    Find the first index where this block yields something truthy...
        S# e#     Find the index of the first space in the current row. If the row
           e#     starts with a space, this is 0, i.e. falsy and the search continues.
           e#     If there is a space in any later position, that will be positive and
           e#     truthy, so this index gets returned. If there is no space at all,
           e#     the result is -1 which is also truthy and ends the search.
      }#        
      &gt;    e#     Discard all lines up to (but not including) the found index.
      W&lt;   e#     Discard the guard row at the end.
      \    e#     Swap the grids so that the next pass processes the other grid.
    }*       
    z      e#    Transpose the second grid back to its original orientation.
    ]      e#    Wrap both processed grids in an array.
    {},    e#    Remove a grid if it's empty.
    {J}/   e#    For each remaining grid, call J recursively.
    ',*    e#    Join the grids with commas if there are still two of them.
    *      e#    Wrap this string in the parentheses below on the stack.
    +      e#    Prepend the function name.
  }|
}:J