Пример #1
0
def t_check():
    assert analysis.LL1(productions)
    assert not analysis.LL1(parse(tokens, grammar + '\n Factor : LPAREN NUMBER RPAREN;'))
Пример #2
0
grammar = '''
    Expr : Term Expr';
    Expr' : PLUS Term Expr';
    Expr' : DASH Term Expr';
    Expr' : e;
    Term : Factor Term';
    Term' : STAR Factor Term';
    Term' : SLASH Factor Term';
    Term' : e;
    Factor : NUMBER;
    Factor : LPAREN Expr RPAREN;
'''


productions = parse(tokens, grammar)

def FIRST(sym):
    if hasattr(sym, 'sym'):
        return analysis.first(productions, sym)
    return analysis.first(productions, NonTerminal(sym))
def FOLLOW(sym):
    if hasattr(sym, 'sym'):
        return analysis.follow(productions, sym)
    return analysis.follow(productions, NonTerminal(sym))

def t_print():
    for k,v in productions.iteritems():
        print k
        for p in v:
            print ' '*4, p