Loading Grammar¶
Having defined a grammar, loading it is a breeze. You have three options:
Loading from YAML¶
import sexpr
grammar = sexpr.load('./predicate.yml')
Loading from a string¶
import sexpr
yml = '''
rules:
expr:
- first
- second
first:
- [ True ]
second:
- [ False ]
'''
grammar = sexpr.load(yml)
Loading from a dictionary¶
import sexpr
dictionary = {
'root': 'expr',
'rules': {
'expr': ['first', 'second'],
'first': [[True]],
'second': [[False]]
}
}
grammar = sexpr.load(dictionary)
Each grammar must have a root rule. Normally, it is the first rule in your YAML.
When loading from a dictionary you must specify the name yourself. There are a
couple of options. You can either use OrderedDict, add "root"
key to
your dictionary or provide it in the options like so:
sexpr.load(dictionary, options = {'root': 'expr'})
And there you go. You can quickly preview your grammar by printing it:
(grammar
path:
-
root:
expr
rules:
first = (non-terminal (seq [(terminal True)]))
expr = (alt [(ref first ...), (ref second ...)])
second = (non-terminal (seq [(terminal False)]))
)