Hot answers tagged

30

Symbols are created in the current context during parsing. This should not be a problem in normal circumstances as the symbols are merely "initialized" without values or properties. See these posts for more information: Is it possible to use Begin and End inside a Manipulate? Why doesn't this use of Begin[] work? When does Mathematica create a new ...


16

The main points of this answer are that,first, it seems rather difficult to have a fully universal mechanism for option-validation, and second, such a mechanism is not currently available in Mathematica on the language level (meaning automation of complete option-checking, including both the option's name and value). In the particular case in question, ...


16

First of all, I agree, as OP mentioned in his comment, ANTLR is one of the proper ways to go. Now for this specific task, it might be easier to just compose a parser in the "dirty" way, except we don't have to go so far to regex. In my opinion Mathematica's StringExpression is much more powerful and very suitable for the job. All we have to do is (as OP ...


11

No, AFAIK there is no way to see the FullForm and I think your conclusion is correct. The ^^ is not an operator, it is a form how you can input a number. Effectively, this behavior applies to all form of numerical input. For instance this is unholdable too HoldComplete[16*^2] (* HoldComplete[1600] *) Advanced expanation To give a more thorough ...


11

I worked on Interpreter. As far as the implentation is now, the DelimitedSequence parser does not support quoting, so what you want can't be done. We'll try to add it in a future version.


10

tokenize[str_] := Module[{exp, nb = CreateDocument[{ExpressionCell@ InputForm@MakeExpression[str, StandardForm]}, Visible -> False]}, SelectionMove[nb, Next, Cell]; exp = Flatten[ NotebookRead[nb][[1, 1]] /. {RowBox -> List, i_String /; StringMatchQ[i, Whitespace ..] :> Sequence[]}]; NotebookClose[nb]; exp[[3 ...


10

This seems to work: StringCases["blabla ...Hello Hello ... blabla ... Goobye Goobye ..", Longest[___ ~~ a : "Hello"] ~~ b : Shortest[___ ~~ "Goobye"] :> a ~~ b] Update If there are multiple substrings to extract you can use recursion: extractbetween[str_, x_, y_] := Module[{f}, f[s_] := StringCases[s, Longest[a___ ~~ x] ~~ b : Shortest[___ ~~ ...


10

Here is how I would approach this problem. First, when possible we should make use of the Attributes of the functions in use with regard to their effect on pattern matching. These attributes follow the natural application of the functions to which they apply so often they make mathematical matching easier. (Sometimes you don't want that.) The OneIdentity ...


9

This answer is based on the Mathematica functional parsers package FunctionalParsers.m used and described in these blog posts. This command loads that package: Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/FunctionalParsers.m"] Using direct parser definitions (first answer) Consider the EBNF: <space> = { ...


8

I'm posting this variant in the hopes that it will be a little more educational. Otherwise doesn't add anything over Kuba's version. Generally, parsing can be done using StringCases. You'll need to build up a string expression that describes the pattern of the file name, much the same way you'd write "%d_%s_Polarizer%dDeg-Temp%dK.dpt" when working with ...


8

This is an ideal use case for SemanticImport, but unfortunately it has issues getting the commas right in version 10.0. Luckily, version 10.0.1 has already fixed this bug:


8

The simple answer is, if you want a string converted to StandardForm, you could wrap BoxData around it. E.g., CellPrint[Cell[BoxData["myFunction::usage=\"myFunction does ...\";"], "Input"]] But, in general, I wouldn't structure this as a question of CellPrint vs. FrontEnd`CellPrint. FrontEnd`CellPrint is undocumented, and therefore there is no contract ...


8

Just define a new function that could take different inputs, either through overloading or using If, Which, or Switch. Clear[f] f[x_. Power[z, e_.]] := {x, e} f[Times[x_, Power[z, e_.]]] := {x, e} f[x_] := {x, 0} Cases[lis, y : Alternatives[x_., x_. Power[z, e_.], Times[x_, Power[z, e_.]]] :> f[y]] (* {{6, 0}, {1, -4}, {4, -2}, {4, 2}, {1, 4}} *) ...


8

I extended the original question to support piping like the following. wget -qO- "http://google.com" // cat and it outputs the following. cat[][wget[-qO-,http://google.com][]] To get the following. CellPrint@ Cell[BoxData[""], "Input", Evaluatable -> True, CellEvaluationFunction -> Function[ Module[{t}, t = List@ReplaceRepeated[ ...


7

shortestStringCases[str_String, from_String, to_String] := StringCases[ str, (from ~~ mid___ ~~ to) /; StringFreeQ[mid, {from, to}]] shortestStringCases["blah X blah X first Y blah X blah X second Y", "X", "Y"] (* {"X first Y", "X second Y"} *)


7

I think this particular scenario has to do with how you can create your own Import/Export filters: Developing an Import Converter Regarding 'verification' as in the Plot[Sin[x], {x, -Pi, Pi}, Frame -> True, FrameTicksStyle -> Red] example given by Nasser, keep in mind that you might have options parameterized like so: Manipulate[ Plot[Sin[x], {x, ...


7

Here is a parser that seems to satisfy your specs (with an improvement from Mr.Wizard): ClearAll[parse, sameQ, reduce]; parse[HoldPattern[Plus[terms__?parse]]] /; sameQ[terms] := True; parse[(a_: 1) * (_Sin | _Cos)] := True; parse[x_] := False; sameQ[terms__] /; MemberQ[{terms}, _Times] := sameQ @@ reduce[{terms}]; sameQ[__Sin | __Cos] := True; sameQ[__] ...


7

If you are familiar with AppleScript, you could try an approach like this: (* from http://github.com/fmeinberg/AppleScript *) AppleScript["RunFile", file_] := Run["osascript " <> file] AppleScript["RunScript", script_] := Block[{file = ToFileName[$TemporaryDirectory, "script.txt"]}, Export[file, script, "String"]; AppleScript["RunFile", file]] ...


6

Using the internal expression parser: string = "y = 0.97*x1 + 0.521*x2 - 30.21 - 0.07431*x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6"; Cases[ ToHeldExpression[string], s_Symbol * Except[_Symbol, n_?NumericQ] :> {HoldForm[s], n}, {-2} ] {{x1,0.97}, {x2,0.521}, {x3,-0.07431}, {x4,-0.126}, {x5,-0.1939}, {x6,-0.361}} Note use of HoldForm to keep ...


6

For the first case, one way will be to read the entire file as a String and manipulate it e.g: txt = ReadList["dose.txt", String]; Then do something like: Flatten @ StringCases[txt, "Mean Dose [%]: " ~~ (x : NumberString) -> ToExpression @ x] {103.3} I guess you can turn it into a function: sValues[s_String] := Flatten @ StringCases[txt, s ~~ ...


5

list = {"20140605_SampleName-C-vert_Polarizer0Deg.dpt", "20140605_SampleName-C-vert_Polarizer90Deg-Temp100K.dpt", "20140606_SampleName-C-vert_Polarizer0Deg-Temp10K.dpt"} There are many ways. This one is not bulletproof but I think it should work with your data: parse[string_] := ToExpression[{DateList@#, ##2}] & @@ Flatten[ StringCases[string, ...


5

One possibility is to use Replace with Sow/Reap: Scan[ Replace[#, { x_. Power[z, e_.] :> Sow[{x, e}], Times[x_, Power[z, e_.]] :> Sow[{x, e}], x_ :> Sow[{x, 0}]}] &, lis] // Reap // #[[-1, -1]] & {{6, 0}, {1, -4}, {4, -2}, {4, 2}, {1, 4}} However, the wildcard x_ could be tricky if you needed to operate deeper ...


5

Another possibility is to use ReplaceAll on the rhs of RuleDelayed in the second argument of Cases: rules = Alternatives[x_. Power[z, e_.] :> {x, e}, Times[x_, Power[z, e_.]] :> {x, e}, x_. :> {x, 0}]; (* your prefered second argument for Cases *) Cases[lis, pat : rules[[All, 1]] :> (pat /. List @@ rules)] (* {{6, 0}, {1, ...


5

The General Case Alas, Cases does not permit multiple replacement rules. But Replace does, although we must take care to 1) create an output list, 2) omit non-matching elements and 3) operate only upon the first level: lis = 6 + 1/z^4 + 4/z^2 + 4 z^2 + z^4; Replace[List @@ lis, {x_. z^e_. :> {x, e}, x_ :> {x, 0}, _ :> Sequence[]}, {1}] (* ...


5

I think when you press = over ≠, the underlying RowBox-es got re-grouped (which is a correct move), but somehow didn't re-group again when finishing input =!=. I don't know if the FrontEnd is supposed to re-parse code every 1/n second, but it does look like a bug to me. One possible workaround: Select the cell, from the Edit menu, Copy As Plain Text, paste ...


5

Remember that the internal form of an expression is often not the same as what you type as input or is displayed as output. For example the input 0.. is interpreted as Repeated[0] and Repeated[0] is displayed in output as (0).. Hold and HoldForm prevent evaluation but do not prevent parsing of input and formatting of output, so it is no surprise that ...


4

A possible solution is just to replace your boundary words with single characters. I think what you are venturing into is something akin to look-behind, which I don't think is supported. Anyways here's how I would do it: boundary = {"Hello", "Goobye"}; limits = {"\[FormalCapitalX]", "\[FormalCapitalY]"}; shift[str_, from_, to_] := StringReplace[str, Rule ...


4

The benefit of using a symbolic tree representation with inert heads as in Leonid's parser is that you can then decide how to represent the data. And that is indeed what you should do, instead of extracting the elements using Cases. Here's an example using your parsed output above: Block[{ulContainer, liContainer}, ulContainer[_, l__] := {l}; ...


4

Figured out what I'm going to do: Read a line from the file. Try converting it to an expression (ToExpression[]). If successful, I've found an expression (or a comment or a blank line). If not, grab additional lines and concatenate them with the line that failed to parse until I find a series of lines that succeeds. The code: ClearAll[breakExpressions]; ...


4

This answer is just for exercise purposes, I would use Coefficient way. string = "y = 0.97*x1 + 0.521*x2 - 30.21 - 0.07431*x3 - 0.126*x4 - 0.1939*x5 - 0.361*x6"; StringCases[ StringReplace[string, "- " -> "-"], c : NumberString ~~ _ ~~ x : ("x" ~~ DigitCharacter) :> {c, x}] {{"0.97", "x1"}, {"0.521", "x2"}, {"-0.07431", "x3"}, {"-0.126", ...



Only top voted, non community-wiki answers of a minimum length are eligible