Grammar: Difference between revisions
Content added Content deleted
mNo edit summary |
(k.txt from webarchive) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
The following one-line '''grammar''' describes the essence of k syntax<ref> |
The following one-line '''grammar''' describes the essence of k syntax<ref>https://web.archive.org/web/20220826224612/https://kparc.com/k.txt</ref>: |
||
E:E;e|e e:nve|te| t:n|v v:tA|V n:t[E]|(E)|{E}|N |
|||
or in more verbose form: |
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, <tt><Adverb></tt> <tt><Verb></tt> <tt><Noun></tt> can be defined, with minor variations between dialects, as: |
Further, <tt><Adverb></tt> <tt><Verb></tt> <tt><Noun></tt> 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. |
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 <tt><noun></tt> in the source code and evaluate to a function at runtime, for instance <tt>{1+x}</tt> or <tt>(+)</tt>. A bare <tt>+</tt> would be a verb, but enclosing anything in parentheses marks it as a noun - this is called ''nominalization'' ("turning into a noun"). |
Note that a k expression could have the syntactic role of a <tt><noun></tt> in the source code and evaluate to a function at runtime, for instance <tt>{1+x}</tt> or <tt>(+)</tt>. A bare <tt>+</tt> 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* |
|||
[[Category:Language]] |
Latest revision as of 13:35, 15 June 2023
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*