[Question]
  [
## Background
In Haskell and many other functional languages, function application `f(x)` is simply written as `f x`. Also, this form of function application is left-associative, which means `f x y z` is `((f x) y) z`, or `((f(x))(y))(z)`.
Haskell also has a binary operator called `$`. `f $ x` does function application just like `f x`, but it is right-associative, which means you can write `f $ g $ h $ x` to mean `f(g(h(x)))`. If you used Haskell enough, you might have once wondered, "would it be nice if `f x` itself were right-associative?"
Now it's about time to see this in action. For the sake of simplicity, let's assume all the identifiers are single-character and omit the spaces entirely.
## Challenge
Given a valid expression written using left-associative function application, convert it to the minimal equivalent expression where function application is right-associative. The result must not contain any unnecessary parentheses.
An expression is defined using the following grammar:
```
expr := [a-z] | "(" expr ")" | expr expr
```
To explain this in plain English, a valid expression is a lowercase English letter, another expression wrapped in a pair of parens, or multiple expressions concatenated.
I/O can be done as a string, a list of chars, or a list of codepoints. The input is guaranteed to have minimal number of parens under left-associative system.
Standard [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules apply. The shortest code in bytes wins.
## Test cases
```
input -> output
---------------
foo -> (fo)o
b(ar) -> bar
q(uu)x -> (quu)x
abcde -> (((ab)c)d)e
f(g(hx)) -> fghx
g(fx)(hy) -> (gfx)hy
```
  
[Answer]
# JavaScript, 71 bytes
*Saved 2 bytes by [Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld).*
```
f=(e,l='',r=e.shift())=>(r=r>{}?r:r<f&&f(e))?f(e,l[1]?`(${l})`+r:l+r):l
```
[Try it online!](https://tio.run/##dczRCoJAEIXh@x5DQmfQFrq1Vh9EBFfdUWNxa9QwxGffim7buwPfz7mpp5oaHu7zabStdo4k6MTIKEpYajH1A82AKDNgydm255zylcKQQCPm9G2Lc5lXcNzMjlXMqYkZU@MaO07WaGFsBwSFECIga4MS8XL4YzUoRq8@YFlw9bKqm1Z7laCDfkX/@2esCP3rV7g3 "JavaScript (Node.js) – Try It Online")
* `l`, `r` are two operand
[Answer]
# [Perl 5](https://www.perl.org/) `-p`, 73 bytes
```
1while$e='(\w|\((?1)*\))',s/$e$e(?=$e)/($&)/;1while s/\(($e*)\)(?!$e)/$1/
```
[Try it online!](https://tio.run/##JcpBCsIwEEbhfU6hMNj5ixKycCUlF8mm1UlTKKY2lkbw7MaK2/e@SebxXIpZwzAKSVOxW9@O2RrUDqiOSdPW2TYk0EwH6Msf75LeHEkNB7b73yajS/Exqo7bGerBy4Ks2u56E@W555AB1bPP4PDCJ07PId5TOU1f "Perl 5 – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 60 bytes
```
;Ṫ$FL$¡1ĿØ(jƊ¹ŒḊ?€
=þØ(Ä_Ż}ỊṖʋ/aSƲœp⁸;2ĿW$}¥2/Ẏ3œṖW;¥2/ẎƲÐLÇ
```
[Try it online!](https://tio.run/##y0rNyan8/9/64c5VKm4@KocWGh7Zf3iGRtaxrkM7j056uKPL/lHTGi7bw/uAgodb4o/urn24u@vhzmmnuvUTg49tOjq54FHjDmujI/vDVWoPLTXSf7irz/joZKCCcGso99imwxN8Drf/t37UMEdB107hUcNc68PtXA93bzncDjQ78v//tPx8riSNxCJNrkKN0lLNCq7EpOSUVK40jXSNjApNTa50jbQKTY2MSk0A "Jelly – Try It Online")
A full program taking a string argument and printing the result to STDOUT. Uses recursion in both links, but in both cases replacing `1Ŀ` or `2Ŀ` with `ß` fails to work.
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 94 bytes
```
+1`((\w|(\()|(?<-3>\)))+?(?(3)^)){2}(?!\))
($&)
+`\(((\w|(\()|(?<-3>\)))+)\)(?(3)^)(?=\)|$)
$1
```
[Try it online!](https://tio.run/##bcyxCsIwFIXh/b6FEOUcgkPtqmb0JS6SVJvWxdJiMWJ89tjB0fWH75/ax@0eyhonX2zlAX1mKJjh9tv6qCStg0PNM/nefeBWSxOYDcV6xV9A5Y/AHZTZUExVShwGaRAmyoh5ZpLQXK6tRHTo0zLtEBPRv/gF "Retina 0.8.2 – Try It Online") Link includes test cases. Explanation:
```
+1`
```
Make the first possible substitution each time, looping until no more substitutions are possible. This means that in the case of `abcde`, only `ab` is surrounded the first time, then `(ab)c` the second time, et cetera.
```
((\w|(\()|(?<-3>\)))+?(?(3)^)){2}(?!\))
($&)
```
Put parentheses around any pair of expressions that does not already have one.
```
+`\(((\w|(\()|(?<-3>\)))+)\)(?(3)^)(?=\)|$)
$1
```
Remove any parentheses not needed under right association.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 53 bytes
```
Ｆ⁺(Ｓ¿⁼ι(⊞υ⟦⟧«Ｆ⁼ι)≔⪫⊟υωιＦ‹¹Ｌ§υ±¹⊞υ⟦⪫()⪫⊟υω⟧⊞§υ±¹ι»⪫⊟υω
```
[Try it online!](https://tio.run/##bY8xC4MwEIVn/RWH0x2kg7OTQweLFKFj6SA2xkCI1iStpfS3p1GHFunBLcd733vXdPXY9LXyvu1HwEo5gwkmDAo9OHuyo9QCiQhkC7i/uVoZlAyCJNwqZzp0DM4XymKuDIdXHC2cHyXNytwYKTQeeqmx6gd0xOARVgbj6ii5MZgyKLkWtsPcFvrKp5l@5KK2HFOa55u5sBKk0HWDpblOtAj/YtbYd1yF5@y2E2XeC2wnwu5JfndXHw "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｆ⁺(Ｓ
```
Loop over the input with an extra `(` prefixed as this is golfier then setting up the stack manually.
```
¿⁼ι(⊞υ⟦⟧«
```
For each `(`, push a new empty list to the stack.
```
Ｆ⁼ι)
```
If the next character is a `)`, then...
```
≔⪫⊟υωι
```
the next term is actually the joined list at the top of the stack.
```
Ｆ‹¹Ｌ§υ±¹
```
If the list at the top of the stack already has two terms, then...
```
⊞υ⟦⪫()⪫⊟υω⟧
```
... replace it with a list of a `()` wrapped term.
```
⊞§υ±¹ι
```
Push the current term to the list at the top of the stack.
```
»⪫⊟υω
```
Join and output any remaining terms.
]