Grammar

From The K Language Wiki
Revision as of 19:37, 9 July 2022 by Promovicz (talk | contribs) ("Fix typo")

The following one-line grammar describes the essence of k syntax[1]:

E:E;e|e e:nve|te| t:n|v v:tA|V n:t[E]|(E)|{E}|N

or in more verbose form:

<Exprs>  ::=  <Exprs> ";" <expr>  |  <expr>
<expr>   ::=  <noun> <verb> <expr>  |  <term> <expr>  |  empty
<term>   ::=  <noun>  |  <verb>
<verb>   ::=  <term> <Adverb>  |  <Verb>
<noun>   ::=  <term> "[" <Exprs> "]"  |  "(" <Exprs> ")"  |  "{" <Exprs> "}"  |  <Noun>

Further, <Adverb> <Verb> <Noun> can be defined, with minor variations between dialects, as:

<Adverb> ::=  "'" | "/" | "\" | "':" | "/:" | "\:"
<Verb>   ::=  <Verb1> | <Verb1> ":"
<Verb1>  ::=  ":" | "+" | "-" | "*" | "%" | "!" | "&" | "|" | "<" | ">" | "=" | "~" | "," |
              "^" | "#" | "_" | "$" | "?" | "@" | "." | <Digit> ":"
<Noun>   ::=  <Names>  |  <Ints>  |  <Floats>  |  <String>  |  <Symbols>
<Names>  ::=  <Names> "." <Name>  |  <Name>
<Name>   ::=  <Letter>  |  <Name> <Letter>  |  <Name> <Digit>
<Ints>   ::=  <Int>  |  <Ints> " " <Int>
<Int>    ::=  "-" <Digits>  |  <Digits>
<Floats> ::=  <Float>  |  <Floats> " " <Float>
<Float>  ::=  <Int>  |  <Int> "." <Digits>  |  <Int> "." <Digits> "e" <Int>
<String> ::=  '"' <Chars> '"'  |  "0x" <Bytes>
<Chars>  ::=  <Chars> <Char>  |  empty
<Char>   ::=  "\0" | "\t" | "\n" | "\r" | '\"' | "\\" | any
<Bytes>  ::=  <Bytes> <Hex> <Hex>  |  empty
<Symbols>::=  <Symbols> <Symbol>  |  <Symbol>
<Symbol> ::=  "`"  |  "`" <Name>  |  "`" <String>
<Digits> ::=  <Digit>  |  <Digits> <Digit>
<Digit>  ::=  "0" | .. | "9"
<Hex>    ::=  <Digit> | "a" | .. | "f"
<Letter> ::=  "A" | .. | "Z" | "a" | .. | "z"

An important property of k syntax is it's static parsability - a parse tree can be built just by looking at the source code, without any knowledge of the values variables have at runtime.

Note that a k expression could have the syntactic role of a <noun> in the source code and evaluate to a function at runtime, for instance {1+x} or (+). A bare + would be a verb, but enclosing anything in parentheses marks it as a noun - this is called nominalization ("turning into a noun").

Examples of nouns:

0 1 2   "a"   3.14   ``a`bc    {1+2*x}   (+)   -[;1]

Examples of verbs:

+   0:   -:   */:   ","\'   2*