Wolfram Language Paclet Repository

Community-contributed installable additions to the Wolfram Language

Primary Navigation

    • Cloud & Deployment
    • Core Language & Structure
    • Data Manipulation & Analysis
    • Engineering Data & Computation
    • External Interfaces & Connections
    • Financial Data & Computation
    • Geographic Data & Computation
    • Geometry
    • Graphs & Networks
    • Higher Mathematical Computation
    • Images
    • Knowledge Representation & Natural Language
    • Machine Learning
    • Notebook Documents & Presentation
    • Scientific and Medical Data & Computation
    • Social, Cultural & Linguistic Data
    • Strings & Text
    • Symbolic & Numeric Computation
    • System Operation & Setup
    • Time-Related Computation
    • User Interface Construction
    • Visualization & Graphics
    • Random Paclet
    • Alphabetical List
  • Using Paclets
    • Get Started
    • Download Definition Notebook
  • Learn More about Wolfram Language

FunctionalParsers

Guides

  • Functional parsers

Tech Notes

  • Интерпретатори на желания за храни
  • Food cravings grammar and interpreters
  • Парсване на имена на цели числа
  • Integer names parsing

Symbols

  • AddParserModifier
  • EBNFAlternativesInterpreter
  • EBNFAlternatives
  • EBNFContextRules
  • EBNF
  • EBNFNonTerminalInterpreter
  • EBNFNonTerminal
  • EBNFOptionInterpreter
  • EBNFOption
  • EBNFRepetitionInterpreter
  • EBNFRepetition
  • EBNFRuleInterpreter
  • EBNFRule
  • EBNFSequenceInterpreter
  • EBNFSequence
  • EBNFTerminalInterpreter
  • EBNFTerminal
  • GenerateParsersFromEBNF
  • GrammarNormalize
  • GrammarRandomSentences
  • InterpretWithContext
  • ParseAlternativeComposition
  • ParseApply
  • ParseBracketed
  • ParseChain1Left
  • ParseChainLeft
  • ParseChainRight
  • ParseChoice
  • ParseComposeWithResults
  • ParseCurlyBracketed
  • ParseEBNF
  • ParseEpsilon
  • ParseFail
  • ParseFuzzySymbol
  • ParseJust
  • ParseListOf
  • ParseMany1
  • ParseManyByBranching
  • ParseMany
  • ParseModify
  • ParseOption1
  • ParseOption
  • ParsePack
  • ParseParenthesized
  • ParsePredicate
  • ParseRecursiveDefinition
  • ParseSequentialComposition
  • ParseSequentialCompositionPickLeft
  • ParseSequentialCompositionPickRight
  • ParseShortest
  • ParseSome
  • ParseSpaces
  • ParseSucceed
  • ParseSymbol
  • ParseToEBNFTokens
  • ParseToken
  • ParseToTokens
  • ParsingTestTable
  • SetParserModifier
  • ToTokens
Food cravings grammar and interpreters
Introduction
Random sentences generation
Grammar
Interpreters
Introduction
In this notebook we show how to make a grammar for a simple Domain Specific Language (DSL) that has sentences proclaiming food cravings and affinities.
Load the paclet
In[11]:=
Needs["AntonAntonov`FunctionalParsers`"]
Grammar
Here is an EBNF description of a Domain Specific Language (DSL) for food cravings:
In[12]:=
ebnfCode=" <lovefood> = <subject> , <loveverb> , <object-spec> <@ LoveFood[Flatten[#]]& ; <loveverb> = ( 'love' | 'crave' | 'demand' ) <@ LoveType ; <object-spec> = ( <object-list> | <object> | <objects> | <objects-mult> ) <@ LoveObjects[Flatten[{#}]]& ; <subject> = 'i' | 'we' | 'you' <@ Who ; <object> = 'sushi' | [ 'a' ] , 'chocolate' | 'milk' | [ 'an' , 'ice' ] , 'cream' | 'a' , 'tangerine' ; <objects> = 'sushi' | 'chocolates' | 'milks' | 'ice' , 'creams' | 'ice-creams' | 'tangerines' ; <objects-mult> = 'Range[2,100]' , <objects> <@ Mult ; <object-list> = ( <object> | <objects> | <objects-mult> ) , { 'and' &> ( <object> | <objects> | <objects-mult> ) } ; ";
Generate parsers from the EBNF string:
In[13]:=
GenerateParsersFromEBNF

ParseToTokens
@ebnfCode;
Test the parser
pLOVEFOOD
-- for the highest level rule "<lovefood>" -- with a list of sentences:
In[14]:=
sentences={"I love milk","We demand 2 ice creams","I crave 2 ice creams and 5 chocolates","You crave chocolate and milk"};
ParsingTestTable
[pLOVEFOOD,ToLowerCase@sentences,"Layout""Vertical"]
Out[14]=
1
command:
i love milk
parsed:
LoveFood[{Who[i],LoveType[love],LoveObjects[{milk}]}]
residual:
{}
2
command:
we demand 2 ice creams
parsed:
LoveFood[{Who[we],LoveType[demand],LoveObjects[{Mult[{2,{ice,creams}}]}]}]
residual:
{}
3
command:
i crave 2 ice creams and 5 chocolates
parsed:
LoveFood[{Who[i],LoveType[crave],LoveObjects[{Mult[{2,{ice,creams}}],Mult[{5,chocolates}]}]}]
residual:
{}
4
command:
you crave chocolate and milk
parsed:
LoveFood[{Who[you],LoveType[crave],LoveObjects[{chocolate,milk}]}]
residual:
{}
Note the EBNF rule wrappers -- those are symbols specified at the ends of some of the rules.
Random sentences generation
Generate random sentences from the grammar above:
In[15]:=
SeedRandom[3];​​
GrammarRandomSentences

GrammarNormalize
[ebnfCode],12//Sort//ColumnForm
Out[16]=
i crave 59 milks
i demand 73 sushi and 43 tangerines and 77 ice creams and ice creams and 32 sushi and tangerines
i demand sushi
we crave ice-creams and 53 sushi and milk and 9 milks and sushi
we demand chocolates
we demand milks and 65 ice-creams and ice-creams
we love 14 ice-creams and tangerines and 47 milks
we love ice creams
we love milks and 31 ice creams and milks and cream
you demand milks
you love milk and 32 milks and a tangerine
you love tangerines
Interpreters
We can implement interpreters that tell calorie intake for proclaimed food affinities and cravings. We can use
WolframAlpha
to get the calories, but since using
WolframAlpha
can be slow it is overridden inside
Block
:
Interpreter for
LoveObjects
In[17]:=
Clear[LoveObjectsCalories];LoveObjectsCalories[parsed_]:=Block[{res,wares,WolframAlpha={}&},res=(StringJoin@@Flatten[Riffle[parsed," and "]/.Mult[{x_,y_}](StringJoin@@Riffle[Flatten[{ToString[x],y}]," "])]);wares=WolframAlpha[res<>" calories","DataRules",TimeConstraint60];{{"Result",1},"ComputableData"}/.wares/.{{"Result",1},"ComputableData"}Quantity[RandomInteger[{20,1200}],"LargeCalories"]​​];
Interpreter for
LoveFood
In[18]:=
Clear[LoveFoodCalories];​​LoveFoodCalories[parsed_]:=​​Block[{who,type},​​who=Cases[parsed,Who[id_]id,∞]〚1〛;​​type=Cases[parsed,LoveType[id_]id,∞]〚1〛;​​Which[​​who"you",Row[{"No, I do not. I am a machine."}],​​type"love",Row[{"you gain ",Sqrt[1*10.]parsed〚-1〛," per day"}],​​True,Row[{"you will gain ",parsed〚-1〛}]​​]​​];
Here the parsing tests are re-done by changing the definitions of the wrapping symbols
LoveFood
and
LoveObjects
:
In[20]:=
Block{LoveFood=LoveFoodCalories,LoveObjects=LoveObjectsCalories},​​
ParsingTestTable
[pLOVEFOOD,ToLowerCase@sentences,"Layout""Vertical"]​​
Out[20]=
1
command:
i love milk
parsed:
you gain
3469.02
Cal
per day
residual:
{}
2
command:
we demand 2 ice creams
parsed:
you will gain
469
Cal
residual:
{}
3
command:
i crave 2 ice creams and 5 chocolates
parsed:
you will gain
1171
Cal
residual:
{}
4
command:
you crave chocolate and milk
parsed:
No, I do not. I am a machine.
residual:
{}
​
RelatedTechNotes
▪
Integer names parsing
""

© 2025 Wolfram. All rights reserved.

  • Legal & Privacy Policy
  • Contact Us
  • WolframAlpha.com
  • WolframCloud.com