Q Language grammar

This section lists the complete Q language grammar. See Lexical elements for the structure of the tokens.

The following table displays a summary of the typographical conventions used in the presentation of the grammar.

Style
Meaning
[ ]
Optional element
( )+
One or more occurrences
( )*
Zero or more occurrences
ID, STRING_LITERAL
Tokens
let, for_all, =>, ~<>
Reserved words or punctuation

program

:  expr

expr

:  expr′
|  expr′ |= expr′

expr′

:  implies-expr
|  let-expr
|  if-expr
|  universal-expr
|  existential-expr

let-expr

: let ID = expr in expr′

if-expr

:  if expr then expr else expr′

universal-expr

:  for_all ID in expr = expr′

existential-expr

:  there_exists ID in expr = expr′

implies-expr

:  or-expr
|  or-expr implies implies-expr

or-expr

:  and-expr
|  or-expr or and-expr

and-expr

:  relational-expr
|  and-expr and relational-expr

relational-expr

:  additive-expr
|  additive-expr = additive-expr
|  additive-expr <> additive-expr
|  additive-expr ~= additive-expr
|  additive-expr ~<> additive-expr
|  additive-expr < additive-expr
|  additive-expr <= additive-expr
|  additive-expr > additive-expr
|  additive-expr  > == additive-expr

additive-expr

:  multiplicative-expr
|  additive-expr + multiplicative-expr
|  additive-expr - multiplicative-expr

multiplicative-expr

:  unary-expr
|  multiplicative-expr * unary-expr
|  multiplicative-expr / unary-expr

unary-expr

:  primary-expr
| - unary-expr
| not unary-expr

primary-expr

:  constant
|  $ ID
|  $ ID of primary-expr
|  map { expr } over primary-expr
|  filter { expr } over primary-expr
|  traverse { expr } over primary-expr
|  sort-expr
|  ID primary-expr
|  conversion-expr
|  parenthesized-expr
|  path

constant

:  REGEXP_LITERAL
|  STRING_LITERAL
|  INTEGER_LITERAL
|  REAL_LITERAL
|  BOOLEAN_LITERAL

sort-expr

:  sort-component ( & sort-component )* over primary-expr

sort-component

   :  sort-direction { expr }

sort-direction

:  sort
|  sortd

conversion-expr

:  conversion-operator primary-expr

conversion-operator

:  string
|  integer
|  real
|  boolean

parenthesized-expr

:  ( )
|  ( expr )
|  ( expr (, expr )+ )

path

:  anchor
|  anchor -association-chain
|  association-chain

anchor

:  this
|  current
|  model
|  ID

association-chain

:  simple-association ( -simple-association )* [ association-chain-tail ]

simple-association

:  [ ID : ]  [^] ASSOC_LITERAL [ { expr } ]

association-chain-tail

:  -reference-association
|  -composite-association

reference-association

:  @ ID

composite-association

:  ( component-association ( ;  component-association )+ )

component-association

:  reference-association
|  association-chain

Feedback