5
$\begingroup$

Is it possible to validate grammar related to Box formed expression (RowBox, FractionBox, ...) ?

You can read following post first :

Purple color text related with RowBox

If you read the above post, you would know that

RowBox[{"-", "(", "3", "+", "5", ")"}] (*Expression 1*)

RowBox[{"-", RowBox[{"(", RowBox[{"3", "+", "5"}], ")"}]}] (*Expression 2*)

Expression 1 is rather an ill-formed expression, where as Expression 2 is a good, impeccable expression.

Is there a built-in function or can we construct a function that can check the validity of Box formed expression ? And furthermore, can we construction a function that change ill-formed Box expression to good-formed Box expression.

$\endgroup$

1 Answer 1

10
$\begingroup$

You may try using MathLink`CallFrontEnd[FrontEnd`ReparseBoxStructurePacket[...]].

MathLink`CallFrontEnd[
 FrontEnd`ReparseBoxStructurePacket[
  RowBox[{"-", "(", "3", "+", "5", ")"}]]]

(* RowBox[{"-", RowBox[{"(", RowBox[{"3", "+", "5"}], ")"}]}] *)

As a "validation", you may try comparing the output to your input.

validateBoxes[boxes_] := 
 boxes === MathLink`CallFrontEnd[FrontEnd`ReparseBoxStructurePacket[boxes]];

validateBoxes[RowBox[{"-", "(", "3", "+", "5", ")"}]]
(* False *)

validateBoxes[RowBox[{"-", RowBox[{"(", RowBox[{"3", "+", "5"}], ")"}]}]]
(* True *)

Note that I haven't tested this on any other inputs, so I can't tell how reliable it is.

$\endgroup$
2
  • $\begingroup$ Rarely what I imagined really come true. Thank you! $\endgroup$
    – imida k
    Apr 7, 2023 at 12:23
  • $\begingroup$ And after 10 days, I am using it quite often. It is very practical. $\endgroup$
    – imida k
    Apr 16, 2023 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.