Grammar

From The K Language Wiki
Revision as of 21:52, 4 July 2021 by Ngn (talk | contribs)

K syntax can be described with the following one-line grammar[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"

K can be parsed statically - knowledge about the runtime values of variables is not required to build a parse tree.