else: from .RailroadDiagrams import NonTerminal, Terminal, Choice, HorizontalChoice, Sequence, Diagram, show_diagram if __name__ == "__main__": from IPython.display import SVG, display def syntax_diagram_symbol(symbol): if is_nonterminal(symbol): return NonTerminal(symbol[1:-1]) else: return Terminal(symbol) if __name__ == "__main__": SVG(show_diagram(syntax_diagram_symbol('<term>'))) def syntax_diagram_expr(expansion): # In later chapters, we allow expansions to be tuples, # with the expansion being the first element if isinstance(expansion, tuple): expansion = expansion[0] symbols = [sym for sym in re.split(RE_NONTERMINAL, expansion) if sym != ""] if len(symbols) == 0: symbols = [""] # special case: empty expansion return Sequence(*[syntax_diagram_symbol(sym) for sym in symbols])
def syntax_diagram(grammar): from IPython.display import SVG, display for key in grammar: print("%s" % key[1:-1]) display(SVG(show_diagram(syntax_diagram_alt(grammar[key]))))
def syntax_diagram(grammar): for key in grammar: print("%s" % key[1:-1]) display(SVG(show_diagram(syntax_diagram_alt(grammar[key]))))