Skip to content

parse

Parse a string

parse x     parse[x]

Where x is a string representing

  • a well-formed q expression, returns a parse tree (the string can contain newlines)
  • a function, returns the function
q)parse "1 2 3 + 5"            / the list 1 2 3 is parsed as a single item
+
1 2 3
5

q)parse "{x*x}"
{x*x}

Input over 2 GB

x must be shorter than 2 GB (0Wi characters). Longer input signals a domain error.

A parse tree can clarify the order of execution:

q)parse "1 2 3 +/: 5 7"        / Each Right has postfix syntax
(/:;+)
1 2 3
5 7
q)parse "1 2 3 +neg 5 7"       / neg is applied before +
+
1 2 3
(-:;5 7)

You can evaluate a parse tree with eval.

q)eval parse "1 2 3 +/: 5 7"
6 7 8
8 9 10

parse shows explicit definitions from the .q namespace in full:

q)foo:{x+2}
q)parse "foo each til 5"
k){x'y}
`foo
(k){$[0>@x;!x;'`type]};5)

Applying eval to the result of parse is essentially what the q interpreter does.

QSQL

parse returns the functional form of a QSQL query.

q)sp:([]s:`s1`s2`s1`s3;p:`p1`p1`p2`p3;qty:300 300 100 400)
q)x:parse "select part:p,qty by sup:s from sp where qty>200,p=`p1"
q)x
?
`sp
,((>;`qty;200);(=;`p;,`p1))
(,`sup)!,`s
`part`qty!`p`qty
q)eval x
sup| part qty
---| --------
s1 | p1   300
s2 | p1   300

Views

parse (-5!x) does not preserve a view definition. Evaluating the parse tree of a view definition makes an ordinary global assignment, not a view.

q)eval parse"a::5"
5
q)a
5
q)views[]                      / no view was defined
`symbol$()

eval and reval

Parse trees