Wolfram Language & System 10.3 (2015)|Legacy Documentation

This is documentation for an earlier version of the Wolfram Language.View current documentation (Version 11.2)

GrammarRules

GrammarRules[rules]
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.

GrammarRules[rules,defs]
uses grammar definitions defs.

Details and OptionsDetails and Options

  • Grammar rules are of the form or .
  • The pattern in a grammar rule must be a combination of the following elements:
  • "string"literal string
    StringExpression[]arbitrary string pattern
    RegularExpression[]regular expression
    form1|form2|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:formform 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:
  • AllowLooseGrammarAutomaticwhether to allow extra grammatical "fluff"
    IgnoreDiacriticsTruewhether to ignore diacritics when applying grammar rules
    IgnoreCaseTruedefault for case insensitivity
    AmbiguityFunctionAutomaticdefault function for handling ambiguity
    SaveDefinitionsFalsewhether to save all definitions associated with rules and defs
    InitializationNonean expression to be evaluated when the grammar is applied

ExamplesExamplesopen allclose all

Basic Examples  (2)Basic Examples  (2)

Deploy a simple grammar for adding numbers:

In[1]:=
Click for copyable input
Out[1]=

Apply the deployed grammar to a natural language string:

In[2]:=
Click for copyable input
Out[2]=

Define rules for custom GrammarToken objects:

In[1]:=
Click for copyable input
Out[1]=

Apply the grammar:

In[2]:=
Click for copyable input
Out[2]=
In[3]:=
Click for copyable input
Out[3]=
Introduced in 2014
(10.0)