# Why aren't parentheses ( ) an expression in Mathematica?

Why aren't parentheses ( ) an expression in Mathematica?

Can I get an expression in a form where parentheses are represented by an expression?

• Parentheses are used for parsing, to change the relative priorities of parts. Expressions are a result of parsing. So, parentheses simply can't survive the parsing stage, being consumed and thrown away then. It's like asking, why there is no milk in а butter. – Leonid Shifrin May 8 '15 at 3:36
• I feel this is a valid question and @leonid's comment makes for a nice answer. – Sjoerd C. de Vries May 8 '15 at 5:54
• I admit, it was a stretch on my part to criticize the answers, but I found Oleksandr's comment overly dismissive. As M. has rules to interpret/parse parentheses, his comparison with the English language is somewhat harsh. "Not fully agree" meant "I don't agree with some points in the comments". The answers, I suppose, are fine. – LLlAMnYP May 8 '15 at 10:38
• @LLlAMnYP "In this sense parentheses by themselves are not an expression, but they do affect how input is parsed (or converted to the FullForm, if you will)." - what is being parsed is not yet an expression - it is either a string or a box form. At the point when we start speaking of expressions, parentheses don't exist any more. FullForm merely shows how expressions are represented internally, but they are parsed to internal representation in any case, with or without FullForm in code. It's just that, there is nothing more to it. – Leonid Shifrin May 8 '15 at 13:24
• @LLlAMnYP Replacement rules work, because parsing a string to an expression is a two-step process. The string input is first parsed to boxes, which are expressions representing the a lower-level structure that preserves the punctuation (parentheses, things like infix and postfix forms, etc). If you intersept it at that level, you can change how expression will be parsed at the end. This is what was done in that answer you referred to. – Leonid Shifrin May 8 '15 at 15:54

As djp explains parentheses are unnecessary in the FullForm of an expression; it is logical for superfluous information to be removed.

However if you want parentheses to persist you could use something like this:

$PreRead = # /. RowBox[{"(", body___, ")"}] :> RowBox[{"paren", "[", body, "]"}] &; MakeBoxes[paren[body___], form_] := MakeBoxes[{body}, form] /. RowBox[{"{", x___, "}"}] :> RowBox[{"(", x, ")"}]  Now: foo[(bar), (1 + 2) + 5]  foo[(bar), 5 + (3)]  The FullForm of which is: foo[paren[bar], Plus[5, paren[3]]] Also: (1, 2, 3) % // FullForm  (123) paren[1,2,3]  () % // FullForm  () paren[]  You could add rules as desired to handle the head paren. Note: before taking this rather unusual step consider using the existing functionality of AngleBracket. ## Sidebar: Box form manipulation LLlAMnYP commented: I'm even surprised that the replacement rules in the answer below work at all. It prompts me to ask "how does M even apply replacement rules before parsing the expression?" To understand what is being done with $PreRead and MakeBoxes and the rules on RowBox one must understand how Box form is used by Mathematica. As the documentation states:

All textual and graphical forms in Mathematica are ultimately represented in terms of nested collections of boxes.

Input is converted into Boxes by the Front End using functionality that may be accessed by this method that John Fultz revealed, which I package as:

parseString[s_String, prep : (True | False) : True] :=
FrontEndExecute[FrontEndUndocumentedTestFEParserPacket[s, prep]]


For example:

parseString @ "(1,2,3)"

{BoxData[RowBox[{"(", RowBox[{"1", ",", "2", ",", "3"}], ")"}]], StandardForm}


This Box data is then sent to the Kernel where $PreRead, if defined, is applied before further processing. (Note: CellEvaluationFunction is a lower level hook that is applied before $PreRead.)

Recalling that "all textual and graphical forms ... are ... represented in ... boxes" the output expression must be converted back to Box form before it is sent to the Front End for display, and MakeBoxes lets us attach rules to this process. It is more flexible than Format and more robust when we want to use output as input as Michael Pilat explained.

You can make them an expression if you want. Let par[x.....] represent (x....). For example:

(x+y)*z


The FullForm would be:

Times[par[Plus[x,y]], z]


But in every such expression, the par[..] would only ever have on argument (in the example, Plus[x,y]). It would never modify the meaning of the argument. So in FullForm, there would be no point having an expression representing parentheses.

There is a use to having them in InputForm` etc., because it helps communicate with the user, but they are never needed in the internal representation of an expression.