In [1]:
from tock import *
In [2]:
g = Grammar.from_lines(["S -> a T b",
"S -> b",
"T -> T a",
"T -> &"])
g
Out[2]:
In [3]:
g.nonterminals
Out[3]:
A Grammar
can be any unrestricted (type-0) grammar, but currently the grammar-reading functions can only read CFGs.
In [4]:
g.is_contextfree()
Out[4]:
In [5]:
m = from_grammar(g)
to_graph(m)
In [6]:
run(m, "a a b", show_stack=4)
In [7]:
m = from_grammar(g, mode="bottomup")
to_graph(m)
In [8]:
run(m, "a a b")
In [9]:
g = Grammar.from_lines(["S -> a S c",
"S -> T",
"T -> b T",
"T -> &"])
m = from_grammar(g, mode='ll1')
m
In [10]:
m.is_deterministic()
Out[10]:
In [11]:
run(m, "a a b c c", show_stack=5).only_path()
Out[11]:
Construction of an LR parser involves building the LR automaton:
In [17]:
g = Grammar.from_lines([
'S -> T -|',
'T -> T l T r',
'T -> &'
])
m = from_grammar(g, mode='lr0')
m
In [14]:
m.is_deterministic()
Out[14]:
In [15]:
run(m, 'l l r l r r -|', show_stack=15).only_path()
Out[15]:
You can visualize the LR automaton using grammars.lr_automaton
, and you can also build an LR(1) parser using mode='lr1'
.
In [16]:
m = read_csv('examples/sipser-2-14.csv')
g = to_grammar(m).remove_useless()
g
Out[16]: