GrammarRules
✖
GrammarRules
represents grammar rules to be deployed to a cloud object that implements the grammar in a form suitable for use with functions like GrammarApply and Interpreter.
Details and Options


- Grammar rules are of the form pattern->expr or pattern:>expr.
- The pattern in a grammar rule must be a combination of the following elements:
-
"string" literal string StringExpression[…] arbitrary string pattern RegularExpression[…] regular expression form1form2… alternative forms OptionalElement[form,def] optional form, with default FixedOrder[form1,form2,…] forms that must occur in a fixed order AnyOrder[form1,form2,…] forms that can occur in any order form.. form repeated any number of times DelimitedSequence[form,…] form repeated with delimiters in between GrammarToken["name"] built-in or defined domain CaseSensitive[form] form that must match case sensitively x:form form named to be used - CloudDeploy[GrammarRules[…]] deploys a grammar and returns a cloud object, which can be used in GrammarApply and Interpreter.
- The rules associated with the grammar in effect specify how inputs should be transformed to expressions.
- In GrammarRules[rules,defs], the list defs can give subsidiary rules for grammar domains that can be used in the main grammar rules.
- GrammarRules supports the following options:
-
AllowLooseGrammar Automatic whether to allow extra grammatical "fluff" IgnoreDiacritics True whether to ignore diacritics when applying grammar rules IgnoreCase True default for case insensitivity AmbiguityFunction Automatic default function for handling ambiguity SaveDefinitions False whether to save all definitions associated with rules and defs Initialization None an expression to be evaluated when the grammar is applied
Examples
open allclose allBasic Examples (2)Summary of the most common use cases
Deploy a simple grammar for adding numbers:

https://wolfram.com/xid/01y9o8z44y6-vpwyom

Apply the deployed grammar to a natural language string:

https://wolfram.com/xid/01y9o8z44y6-6zvjkl

Define rules for custom GrammarToken objects:

https://wolfram.com/xid/01y9o8z44y6-ps886q


https://wolfram.com/xid/01y9o8z44y6-vp3fjk


https://wolfram.com/xid/01y9o8z44y6-6elnrq

Properties & Relations (1)Properties of the function, and connections to other functions
Repeated and DelimitedSequence are equivalent to recursive grammar rules:

https://wolfram.com/xid/01y9o8z44y6-8hue5c


https://wolfram.com/xid/01y9o8z44y6-78897f


https://wolfram.com/xid/01y9o8z44y6-zqjjqn


https://wolfram.com/xid/01y9o8z44y6-p6byl8

Possible Issues (1)Common pitfalls and unexpected behavior
Expressions on the right-hand side of a grammar rule evaluate during GrammarApply:

https://wolfram.com/xid/01y9o8z44y6-xp1xzp

To keep an expression unevaluated, use a wrapper like HoldComplete or Inactivate:

https://wolfram.com/xid/01y9o8z44y6-s45zz7

Wolfram Research (2014), GrammarRules, Wolfram Language function, https://reference.wolfram.com/language/ref/GrammarRules.html (updated 2015).
Text
Wolfram Research (2014), GrammarRules, Wolfram Language function, https://reference.wolfram.com/language/ref/GrammarRules.html (updated 2015).
Wolfram Research (2014), GrammarRules, Wolfram Language function, https://reference.wolfram.com/language/ref/GrammarRules.html (updated 2015).
CMS
Wolfram Language. 2014. "GrammarRules." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/GrammarRules.html.
Wolfram Language. 2014. "GrammarRules." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/GrammarRules.html.
APA
Wolfram Language. (2014). GrammarRules. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GrammarRules.html
Wolfram Language. (2014). GrammarRules. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GrammarRules.html
BibTeX
@misc{reference.wolfram_2025_grammarrules, author="Wolfram Research", title="{GrammarRules}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/GrammarRules.html}", note=[Accessed: 20-May-2025
]}
BibLaTeX
@online{reference.wolfram_2025_grammarrules, organization={Wolfram Research}, title={GrammarRules}, year={2015}, url={https://reference.wolfram.com/language/ref/GrammarRules.html}, note=[Accessed: 20-May-2025
]}