Context-Free Grammar

Simon has studied Context-Free Grammar, following a set of tutorials from Daniel Shiffman’s ITP course “Programming from A to Z“. Context-Free Grammar is a set of recursive “replacement” rules to define the syntax of a language and to generate text: “Programming languages, for example, are context-free grammars — a compiler reads your code to make sure it conforms to specific rules and informs you of any errors. A context-free grammar, however, isn’t robust enough to describe the complexities of natural language. After all, they have no context! Natural languages can be described using Context-sensitive grammars, a concept introduced by Noam Chomsky in the 1950s”.

A great way to get started experimenting with Context-Free Grammar text generation is to use a library. As suggested in Daniel Shiffman’s course, Simon used Tracery, a JavaScript library by Kate Compton. With Tracery, all you need to do is setup your grammar as a JSON object. Simon followed the lesson to generate stories and had great fun:

Every time he hit a key, the values of the variables in his story changed:

Tracery 13 Apr 2017 2Tracery 13 Apr 2017 3Tracery 13 Apr 2017 4Tracery 13 Apr 2017

The following day Simon programmed another grammar to generate haiku’s:

Tracery 14 Apr 2017 Haiku 1Tracery 14 Apr 2017 Haiku 2

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s