GrammarToken[form]
is a grammar rules pattern object that represents any input of the specified form.
GrammarToken
Listing of GrammarToken Types »GrammarToken[form]
is a grammar rules pattern object that represents any input of the specified form.
Details
- GrammarToken is used in GrammarRules, along with FixedOrder, AnyOrder, etc.
- GrammarToken works with any form supported by Interpreter.
- New forms can be introduced by giving rules for GrammarToken["string"] in GrammarRules.
Examples
open all close allBasic Examples (2)
Deploy a grammar that makes use of a built-in GrammarToken:
grammar = CloudDeploy[GrammarRules[{GrammarToken["City"]}]]Apply the grammar to an input:
GrammarApply[grammar, "London"]Deploy a grammar that defines and uses an "Appliance" token:
grammar = CloudDeploy[GrammarRules[{GrammarToken["Appliance"]},
{
"Appliance" -> "stove" :> "Stove",
"Appliance" -> "oven" :> "Oven",
"Appliance" -> "fridge" | "refrigerator" :> "Refrigerator"
}
]]Apply the grammar to an input:
GrammarApply[grammar, "stove"]Scope (1)
A custom GrammarToken can be used before and after its definition in the GrammarRules:
grammar = CloudDeploy[GrammarRules[{GrammarToken["Route"]}, {
"Route" -> AnyOrder[end : GrammarToken["Destination"], start : GrammarToken["Origin"]] :> (start -> end),
"Destination" -> FixedOrder["to", loc : GrammarToken["Location"]] :> loc,
"Origin" -> FixedOrder["from", loc : GrammarToken["Location"]] :> loc
}]]GrammarApply[grammar, "from boston to beijing"]Applications (1)
New tokens can be used to build up complicated expressions, e.g. to deploy a simple calculator:
calc = CloudDeploy[
GrammarRules[{GrammarToken["Addition"], GrammarToken["Multiplication"], GrammarToken["SemanticNumber"]}, {
"Summand" -> s : (GrammarToken["Addition"] | GrammarToken["Multiplication"] | GrammarToken["SemanticNumber"]) :> s,
"Multiplicand" -> m : (GrammarToken["Multiplication"] | GrammarToken["SemanticNumber"]) :> m,
"Addition" -> FixedOrder[a : GrammarToken["Summand"], "+", b : GrammarToken["Summand"]] :> a + b,
"Addition" -> FixedOrder[a : GrammarToken["Summand"], "-", b : GrammarToken["Summand"]] :> a - b,
"Multiplication" -> FixedOrder[a : GrammarToken["Multiplicand"], "*", b : GrammarToken["Multiplicand"]] :> a * b,
"Multiplication" -> FixedOrder[a : GrammarToken["Multiplicand"], "/", b : GrammarToken["Multiplicand"]] :> a / b
}]
]Apply the calculator grammar and actions:
GrammarApply[calc, "2+3"]GrammarApply[calc, "two + three / four"]GrammarApply[calc, "two+one/forty-five"]Properties & Relations (1)
If a definition is given for a built-in GrammarToken, the given definition is used, not the built-in one:
myCity = CloudDeploy[GrammarRules[{GrammarToken["City"]}, {
"City" -> "beijing" :> ["beijing"],
"City" -> "london" :> ["london"]
}]]Apply the new definition to an input string:
GrammarApply[myCity, "beijing"]The replacement for GrammarToken["City"] prevents use of the usual one by this grammar:
GrammarApply[myCity, "paris"]See Also
Related Guides
History
Text
Wolfram Research (2014), GrammarToken, Wolfram Language function, https://reference.wolfram.com/language/ref/GrammarToken.html.
CMS
Wolfram Language. 2014. "GrammarToken." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/GrammarToken.html.
APA
Wolfram Language. (2014). GrammarToken. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GrammarToken.html
BibTeX
@misc{reference.wolfram_2026_grammartoken, author="Wolfram Research", title="{GrammarToken}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/GrammarToken.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_grammartoken, organization={Wolfram Research}, title={GrammarToken}, year={2014}, url={https://reference.wolfram.com/language/ref/GrammarToken.html}, note=[Accessed: 12-June-2026]}