Formal Grammar Example: Barsalou Perceptual Symbol Grammar

To demonstrate the formal grammar and language framework I developed, I created a formal grammar that implements a weird version of Barsalou's Percetpual Symbol Systems. Each sentence is a "simulation", with each simulation having exactly one "simulator", which in turn could have many categories and instances that make it up. Not that that's exactly what he was saying, but it's a fun attempt to formalize his system from his 1999 BBS paper.


In [66]:
from IPython.display import display, Latex

from grammar import Grammar, ProductionRule

# build a hierarchy of production rules
production_rules = [
    ProductionRule('simulation', ['simulator', 'category', 'instance'], n_outputs='all'),
        ProductionRule('simulator', ['driving', 'flying', 'seeing red', 'seeing blue', 'seeing green'], n_outputs='one'),
        ProductionRule('category', ['car', 'airplane', 'bird', 'grass', 'bed'], n_outputs='many'),
        ProductionRule('instance', ['2004 pontiac grand am', '2016 subaru crosstrack', '2010 chevy silverado', 
                                    'Boeing 747', 'queen bed with yellow sheets', 'cardinal', 'sky',
                                    'cheatgrass', 'kentucky bluegrass'], n_outputs='many')
]

# instantiate the grammar based on the rules
perceptual_symbol_grammar = Grammar(production_rules)
# use the print_latex() method of the Grammar class to print the formal grammar
display(Latex(perceptual_symbol_grammar.print_latex()))


\begin{equation} \begin{array}{ll} ( & \\ & S = \{\textrm{ simulation }\}, \\ & N = \{ \textrm{category}, \textrm{simulation}, \textrm{instance}, \textrm{simulator} \}, \\ & \Sigma = \{ \textrm{queen bed with yellow sheets}, \textrm{2016 subaru crosstrack}, \textrm{car}, \textrm{seeing blue}, \textrm{cheatgrass}, \textrm{2004 pontiac grand am}, \textrm{flying}, \textrm{seeing green}, \textrm{bed}, \textrm{Boeing 747}, \textrm{driving}, \textrm{seeing red}, \textrm{grass}, \textrm{2010 chevy silverado}, \textrm{sky}, \textrm{cardinal}, \textrm{bird}, \textrm{airplane}, \textrm{kentucky bluegrass} \}, \\ &P = \{ \\ & \begin{array}{ll} & simulation\rightarrow\textrm{simulator}~|~\textrm{category}~|~\textrm{instance} \\ & simulator\rightarrow\textrm{driving}~|~\textrm{flying}~|~\textrm{seeing red}~|~\textrm{seeing blue}~|~\textrm{seeing green} \\ & category\rightarrow\textrm{car}~|~\textrm{airplane}~|~\textrm{bird}~|~\textrm{grass}~|~\textrm{bed} \\ & instance\rightarrow\textrm{2004 pontiac grand am}~|~\textrm{2016 subaru crosstrack}~|~\textrm{2010 chevy silverado}~|~\textrm{Boeing 747}~|~\textrm{queen bed with yellow sheets}~|~\textrm{cardinal}~|~\textrm{sky}~|~\textrm{cheatgrass}~|~\textrm{kentucky bluegrass} \\ \end{array} \\ & \} \\ ) & \end{array} \end{equation}

In [67]:
# now let's create a language that will produce sentences based on the grammar
from grammar import Language

l = Language(perceptual_symbol_grammar)

prods = l.productions()

In [68]:
# You can run this as many times as you like and get a new production
for i in range(10):
    p = next(prods)
    print(next(prods) + '\n')


seeing green bird car airplane grass 2010 chevy silverado cheatgrass sky Boeing 747

flying airplane 2004 pontiac grand am Boeing 747 2010 chevy silverado queen bed with yellow sheets kentucky bluegrass cheatgrass sky 2016 subaru crosstrack

seeing green grass airplane bird car bed sky cardinal 2016 subaru crosstrack kentucky bluegrass cheatgrass 2004 pontiac grand am Boeing 747 2010 chevy silverado queen bed with yellow sheets

driving car bird bed airplane grass 2010 chevy silverado Boeing 747 2016 subaru crosstrack

seeing red bird car kentucky bluegrass 2010 chevy silverado Boeing 747 cardinal cheatgrass sky 2016 subaru crosstrack

seeing blue airplane car bed bird queen bed with yellow sheets 2016 subaru crosstrack Boeing 747

seeing red bird airplane 2010 chevy silverado

flying car 2010 chevy silverado queen bed with yellow sheets kentucky bluegrass cheatgrass cardinal 2004 pontiac grand am sky 2016 subaru crosstrack Boeing 747

driving bird car queen bed with yellow sheets Boeing 747 sky 2004 pontiac grand am 2016 subaru crosstrack cardinal 2010 chevy silverado

seeing blue airplane grass Boeing 747 2010 chevy silverado queen bed with yellow sheets 2016 subaru crosstrack