Ejemplo n.º 1
0
        W,
    ]

    Complex = [
        ('(', Sentence, ')'),
        ('[', Sentence, ']'),
        (NEG, Sentence),
        (Sentence, CON, Sentence),
        (Sentence, DIS, Sentence),
        (Sentence, IMP, Sentence),
        (Sentence, IFF, Sentence),
    ]


# pprint(E)

# g = Grammar(*E)
# pprint(g)
pprint(E.lex2pats)
p = LALR(E)

# pprint([*p.lexer.tokenize('True & False', True)])
# pprint(p.parse('P & Q | R & !S'))

s = p.dump('meta_dumps.py')
p1 = LALR.load('meta_dumps.py', globals())

# print(s)

pprint(p1.parse('P & Q | R & !S'))
Ejemplo n.º 2
0
import preamble
from metaparse import LALR

calc = LALR()

with calc as (lex, rule):

    lex(IGNORED=r'\s+')

    @lex(NUM=r'[0-9]+')
    def NUM(val):
        return int(val)

    lex(EQ=r'=')
    lex(ID=r'[_a-zA-Z]\w*')

    lex(POW=r'\*\*', p=3)
    lex(MUL=r'\*', p=2)
    lex(ADD=r'\+', p=1)
    lex(SUB=r'\-', p=1)

    @rule
    def assign(ID, EQ, expr):
        table[ID] = expr
        return expr

    @rule
    def expr(ID):
        return table[ID]

    @rule
Ejemplo n.º 3
0
        | input line
;

line:     '\n'
        | exp '\n'  { printf ("\t%.10g\n", $1); }
;

exp:      NUM             { $$ = $1;         }
        | exp exp '+'     { $$ = $1 + $2;    }
        | exp exp '-'     { $$ = $1 - $2;    }
        | exp exp '*'     { $$ = $1 * $2;    }
        | exp exp '/'     { $$ = $1 / $2;    }
      /* Exponentiation */
        | exp exp '^'     { $$ = pow ($1, $2); }
      /* Unary minus    */
        | exp '-'         { $$ = -$1;        }
;    
"""

# pprint([*YACC.tokenize(eg, True)])

yacc = LALR(YACC)
tr = yacc.parse(eg)
res = yacc.interpret(eg)

# pprint(yacc.grammar.lexers)
# pprint(yacc)
# pprint(tr)
print()
print(res)
Ejemplo n.º 4
0
        W,
    ]

    Complex = [
        ('(', Sentence, ')'),
        ('[', Sentence, ']'),
        (NEG, Sentence),
        (Sentence, CON, Sentence),
        (Sentence, DIS, Sentence),
        (Sentence, IMP, Sentence),
        (Sentence, IFF, Sentence),
    ]


# pprint(E)

# g = Grammar(*E)
# pprint(g)
pprint(E.lex2pats)
p = LALR(E)

# pprint([*p.lexer.tokenize('True & False', True)])
# pprint(p.parse('P & Q | R & !S'))

s = p.dump('meta_dumps.py')
p1 = LALR.load('meta_dumps.py', globals())

# print(s)

pprint(p1.parse('P & Q | R & !S'))
Ejemplo n.º 5
0
    def E(E, mns, E_1):
        return '({} - {})'.format(E, E_1)
    def E(E, mul, E_1):
        return '({} * {})'.format(E, E_1)
    def E(E, div, E_1):
        return '({} / {})'.format(E, E_1)
    def E(E, pow, E_1):
        return '({} ** {})'.format(E, E_1)
    def E(num):
        return num
    def E(l, E, r):
        return E


import pprint as pp

# pp.pprint(E.parse_many('3 + 2 * 7'))
# pp.pprint(E.parse_many('3 + 2 * 7 + 1'))
# pp.pprint(E.interpret_many('3 + 2 * 7 + 1'))

print(E)
pp.pprint(E.precedence)
psr = LALR(E)
# print(psr.table.__len__())
# pp.pprint([*zip(psr.Ks, psr.ACTION)])

# print(psr.interpret('3 + 2 * 7'))
# print(psr.interpret('3 * 2 + 7'))
print(psr.interpret('3 + 2 * 7 / 5 - 1'))
print(psr.interpret('3 + 2 * 7 ** 2 * 5'))
Ejemplo n.º 6
0
import preamble
from metaparse import LALR

pCalc = LALR()

lex = pCalc.lexer
rule = pCalc.rule

# lex(<terminal-symbol> = <pattern>)
lex(IGNORED=r'\s+')
lex(NUM=r'[0-9]+')
lex(EQ=r'=')
lex(ID=r'[_a-zA-Z]\w*')

# lex(... , p = <precedence>)
lex(POW=r'\*\*', p=3)
lex(POW=r'\^')  # No need to give the precedence twice for POW.
lex(MUL=r'\*', p=2)
lex(ADD=r'\+', p=1)


# @rule
# def <lhs> ( <rhs> ):
#     <semantics>
@rule
def assign(ID, EQ, expr):
    context[ID] = expr
    return expr


@rule
Ejemplo n.º 7
0
        return table[ID]

    def expr(expr_1, ADD, expr_2):   # With TeX-subscripts, meaning (expr → expr₁ + expr₂)
        return expr_1 + expr_2

    def expr(expr, MUL, expr_1):     # Can ignore one of the subscripts.
        return expr * expr_1

    def expr(expr, POW, expr_1):
        return expr ** expr_1


from metaparse import cfg


pCalc = LALR(G_Calc)

# parse and tree
t = pCalc.parse("x = 1 + 4 * 3 ** 2 + 5")
print(t)

# interpretation of tree
print(t.translate())
print(table)
assert table == {'x': 42}

# direct interpretation
# pCalc.interpret("x = 1 + 4 * 3 ** 2 + 5")
pCalc.interpret("y = 5 + x * 2")
pCalc.interpret("z = 99")
print(table)
Ejemplo n.º 8
0
    @rule
    def expr(expr, MUL, expr_1):
        return expr * expr_1

    @rule
    def expr(expr, POW, expr_1):
        return expr**expr_1


from pprint import pprint

table = {}

calc.interpret('x  =  8')
calc.interpret('y  =  x -  6 ')
calc.interpret('z  =  x ** y ')

calc.interpret(' (3) ')
calc.interpret(' x = 03 ')
calc.interpret(' y = 4 * x ** (2 + 1) * 2')

print(table)

# print(calc.dumps())
calc1 = LALR.loads(calc.dumps(), globals())

calc1.interpret(' w = x + 1')

print(table)
Ejemplo n.º 9
0
# pprint(s1)
# pprint(s2)

# md = ast.parse(dedent(s1))
# pprint(md.body[0].__dict__)
# fd = md.body[0]
# fd.decorator_list = []
# ctx = {}
# co = compile(ast.Module([fd]), '<ast>', 'exec')
# exec(co, globals(), ctx)
# pprint(ctx)


pprint(E)

p = LALR(E)

# print(p.parse('3 + 2 * (5 + 1)'))
# print(p.interpret('3 + 2 * (5 + 1)'))

(p.parse('3 + 2 * (5 + 1)'))
(p.interpret('3 + 2 * (5 + 1)'))

s1 = dedent(getsource(E.semans[3]))

pprint(s1)
import dis
# dis.dis(E.semans[3].__code__)

def _fake_lex(*a):
    def dec(func):
Ejemplo n.º 10
0
import preamble
import metaparse as mp
from metaparse import LALR, END_TOKEN

p = LALR()

p.lexer.more(IGNORED=' ', PLUS='\+', TIMES='\*', LEFT='\(', RIGHT='\)')


@p.lexer(NUMBER='\d+')
def _(val):
    return int(val)


@p.rule
def expr(expr, PLUS, term):
    return expr + term


@p.rule
def expr(term):
    return term


@p.rule
def term(term, TIMES, factor):
    return term * factor


@p.rule
def term(factor):