Exemplo n.º 1
0
def test4():
    g = Grammar("start: '\(' name_list (COMMA MUL NAME)? '\)'; @name_list: NAME | name_list COMMA NAME ;  MUL: '\*'; COMMA: ','; NAME: '\w+'; ")
    l = g.parse('(a,b,c,*x)')

    g = Grammar("start: '\(' name_list (COMMA MUL NAME)? '\)'; @name_list: NAME | name_list COMMA NAME ;  MUL: '\*'; COMMA: ','; NAME: '\w+'; ")
    l2 = g.parse('(a,b,c,*x)')
    assert l == l2, '%s != %s' % (l,l2)
Exemplo n.º 2
0
def test3():
    # Multiple parsers and colliding tokens
    g = Grammar("start: B A ; B: '12'; A: '1'; ", auto_filter_tokens=False)
    g2 = Grammar("start: B A; B: '12'; A: '2'; ", auto_filter_tokens=False)
    x = g.parse('121')
    assert x.head == 'start' and x.tail == ['12', '1'], x
    x = g2.parse('122')
    assert x.head == 'start' and x.tail == ['12', '2'], x
Exemplo n.º 3
0
def test_python_parse2(n):
    g = Grammar(file(python_g_file))
    if n == 0:
        s = """
a = \\
        \\
        1\\
        +2\\
-3
print a
"""
    elif n == 1:
        s = "a=b;c=d;x=e\n"

    elif n == 2:
        s = r"""
@spam3 (\
this,\
blahblabh\
)
def eggs9():
    pass

"""
    else:
        assert False

    logging.debug( g.parse(s) )
Exemplo n.º 4
0
def test_python_with_filters():
    g = Grammar(file('python3.g'))
    #pprint(g.parse('f(1,2,3)\n'))
    #pprint(g.parse('if a:\n\tb\n'))
    #pprint(g.parse(FIB))
    #pprint(g.parse('a or not b and c\n'))
    pprint(g.parse(file('../plyplus.py').read()))
Exemplo n.º 5
0
def test2():
    g = Grammar("start: A+ B A@+ 'b' A*; B: 'b'; A: 'a';")
    #print g.parse('aaabaab')
    for x in g.parse('aaabaab'):
        if isinstance(x, TokValue):
            tok = x
            print 'tok #%d: %s = %s {%d:%d}' % (tok.index, tok.type, tok, tok.line, tok.column)
        else:
            print type(x), x
Exemplo n.º 6
0
def test_config_parser():
    g = Grammar(file('../grammars/config.g'), auto_filter_tokens=True)
    res = g.parse("""
        [ bla Blah bla ]
        thisAndThat = hel!l%o/
        one1111:~$!@ and all that stuff

        [Section2]
        whatever: whatever
        """)
    print res
Exemplo n.º 7
0
def test_python_lib_with_filters(path = PYTHON_LIB):
    import glob, os
    g = Grammar(file('python3.g'))

    files = glob.glob(path+'\\*.py')
    start = time.time()
    for f in files:
        f2 = os.path.join(path,f)
        print f2
        l = g.parse(file(f2).read())

    end = time.time()
    logging.info("Test3 (%d files), time: %s secs"%(len(files), end-start))
Exemplo n.º 8
0
def test_python_parse():
    g = Grammar(file(python_g_file))
    if 1:
        start = time.time()
        l = g.parse(file('python_sample1.py').read())
        l = g.parse(file('python_sample2.py').read())
        l = g.parse(file('../../examples/calc.py').read())
        l = g.parse(file('../grammar_lexer.py').read())
        l = g.parse(file('../grammar_parser.py').read())
        l = g.parse(file('../strees.py').read())
        l = g.parse(file('../grammars/python_indent_postlex.py').read())
    ##l = g.parse(file('parsetab.py').read())

        l = g.parse(file('../plyplus.py').read())

        l = g.parse("c,d=x,y=a+b\nc,d=a,b\n")
        end = time.time()
        logging.info("Time: %s secs " % (end-start))

    if PYTHON25_LIB:
        l = g.parse(file(PYTHON25_LIB + 'os.py').read())
        l = g.parse(file(PYTHON25_LIB + 'pydoc.py').read())
Exemplo n.º 9
0
def test_python_lib():
    import glob, os
    g = Grammar(file(python_g_file))

    path = PYTHON_LIB
    files = glob.glob(path+'/*.py')
    start = time.time()
    for f in files:
        f2 = os.path.join(path,f)
        logging.info( f2 )
        l = g.parse(file(f2).read())

    end = time.time()
    logging.info( "Test3 (%d files), time: %s secs"%(len(files), end-start) )
Exemplo n.º 10
0
def test():
    #p = grammar_parser.parse(file('sample_grammar.txt').read())
    #pt = ParseTree(p)
    #t = FlattenGrammar_Visitor().visit(p)
    #t = SimplifyGrammar_Visitor('simp_').visit(p)
    #pt.visit(FlattenGrammar_Visitor())

    g = Grammar(file('sample_grammar.txt').read())
    for x in g.parse(file('sample_input.txt').read())[1][1]:
        if isinstance(x, TokValue):
            tok = x
            #print 'tok #%d: %s = %s {%d:%d}' % (tok.index, tok.type, tok, tok.line, tok.column)
        else:
            #print type(x), x
            pass

    #g = Grammar("start: 'a' 'a' B* ('c'? de)+; B: 'b'; C: 'c'; @de: DE; DE: 'de'; ")
    #print g.parse('aabbbde')
    #print g.parse('aacdede')


    #g = Grammar("start: A | a -> A; a: B (A|B) -> B; A: 'a'; B: 'b';")
    #print g.parse('aaabaab')
    pass
Exemplo n.º 11
0
def test_python4ply_sample():
    g = Grammar(file(python_g_file))
    l = g.parse(file(r'python4ply-sample.py').read())
Exemplo n.º 12
0
Arquivo: dml.py Projeto: nnog/dml
    #Make substitutions
    docpreamble = docpreamble.replace('%_document_class_%', document_class)
    docpreamble = docpreamble.replace('%_document_class_options_%', document_class_options)
    docpreamble = docpreamble.replace('%_tikz_package_options_%', tikz_package_options)
    docpreamble = docpreamble.replace('%_tikz_libraries_%', ','.join(tikz_libraries))
    docpreamble = docpreamble.replace('%_additional_preamble_%', additional_preamble)
    tikzheader = tikzheader.replace('%_tikzpicture_env_options_%', tikzpicture_env_options)

    if arg.lex:
        for t in grammar.lex(source):
            print "line {0.lineno:<3}col {0.lexpos:<4}{0.type}:\t{0.value}".format(t)
        sys.exit(0)

    #Parse src file according to grammar
    ast = grammar.parse(source)
    ast.calc_parents()
    
    if arg.ast:
        print ast
        import pydot
        ast.to_png_with_pydot("ast.png")
        if transformer:
            ast = transformer.transform(ast)
            print "\npost transform:"
            print ast
            ast.to_png_with_pydot("ast_posttransform.png")
        sys.exit(0)
    
    if arg.latex:
        print docpreamble
Exemplo n.º 13
0
import sys, os
import logging

sys.path.insert(0, os.path.abspath(".."))
sys.path.insert(0, os.path.abspath("../grammars"))
from plyplus import Grammar, TokValue, STree
from selector import selector
from pprint import pprint

logging.basicConfig(level=logging.INFO)

tree_grammar = Grammar("start: branch; branch: name ('{' branch@* '}')?; name: '[a-z]';")

tree1 = tree_grammar.parse("a{b{cde}}")
tree2 = tree_grammar.parse("a{abc{bbab}c}")


def test_elem_head():
    assert len(selector("name").match(tree1)) == 5
    assert len(selector("branch").match(tree1)) == 5
    assert len(selector("name").match(tree2)) == 9
    assert len(selector("branch").match(tree2)) == 9


def test_elem_regexp():
    assert len(selector("/[a-c]$/").match(tree1)) == 3
    assert len(selector("/[b-z]$/").match(tree2)) == len("bcbbbc")


def test_elem_any():
    assert len(selector("*").match(tree1)) == 16
Exemplo n.º 14
0
"""Demonstrates the use of the permutation operator."""
from plyplus import Grammar

# Basic Permutations
perm1 = Grammar("""
start : a ^ b? ^ c ;

a : 'a' ;
b : 'b' ;
c : 'c' ;

WS: '[ \t]+' (%ignore);
""")

print perm1.parse(' c b a').pretty()
print perm1.parse('c a ').pretty()

# Permutations with a separator
perm2 = Grammar("""
start : a ^ (b|d)? ^ c ^^ ( COMMA | SEMI ) ;

a : 'a' ;
b : 'b' ;
c : 'c' ;
d : 'd' ;

COMMA : ',' ;
SEMI  : ';' ;

WS: '[ \t]+' (%ignore);
""")
Exemplo n.º 15
0
def test_auto_filtered_python():
    g = Grammar(file('python3.g'), auto_filter_tokens=True, expand_all_repeaters=True)
    r = g.parse(file('../plyplus.py').read())
    #pprint()
    from sexp import find
    print [x.tail[0] for x in find(r, 'decorator')]
Exemplo n.º 16
0
                need_else = True
                for i in elseifs:
                    if i[0]:
                        self.eval_code(i[1], Scope(scope, False, {}))
                        need_else = False
                        break
                if need_else:
                    if elsey is not None:
                        self.eval_code(elsey, Scope(scope, False, {}))
            elif parse.head == 'elseif_stmt' or parse.head == 'else_stmt':
                raise EvalError, 'Else or ElseIf without if'
            elif parse.head == 'while_stmt':
                self.eval_while(parse, scope)
                index_counter += 1
            elif parse.head == 'when_stmt':
                if func:
                    raise EvalError, "when inside function not allowed"
                self.eval_when_stmt(parse, scope)
                index_counter += 1
            else:
                raise EvalError, "notimplemented"

        return scope, None


if __name__ == "__main__":
    b = open('test.txt').read()
    c = grammar.parse(b)
    E = Evaluator(stdout_logger, debug_handler)
    print E.eval_code(c, None, False)
Exemplo n.º 17
0
COMPLEX = """
using Complex

z = a + b i
"""

HELLO_PEPPER = """
main() =
  pr("Hello world!")
"""

HELLO_PYTHON = """
def main():
  pr("Hello world!")

"""


# g = Grammar(file("python.g"))
# print g.parse(FIB_PYTHON)
g = Grammar(file("pepper2.g"), debug=True)

# try:
# pdb.set_trace()
ast = g.parse(HELLO_PEPPER)
print ast
compile.compile_ast(ast, "hello")
# print g.parse(HELLO)
#except:
#  pass
Exemplo n.º 18
0
def test_into():
    g = Grammar("start: '\(' name_list (COMMA MUL NAME => 2 3)? '\)' => ^1 ^-1; @name_list: NAME | name_list COMMA NAME => 1 3;  MUL: '\*'; COMMA: ','; NAME: '\w+'; ")
    assert g.parse('(a,b,c,*x)') == ['start', 'a', 'b', 'c', '*', 'x']
    assert g.parse('(a,b,c,x)') == ['start', 'a', 'b', 'c', 'x']
Exemplo n.º 19
0
import sys, os
import logging
sys.path.insert(0, os.path.abspath('..'))
sys.path.insert(0, os.path.abspath('../grammars'))
from plyplus import Grammar, TokValue, STree
from selector import selector
from pprint import pprint

logging.basicConfig(level=logging.INFO)

tree_grammar = Grammar("start: branch; branch: name ('{' branch* '}')?; name: '[a-z]';")

tree1 = tree_grammar.parse('a{b{cde}}')
tree2 = tree_grammar.parse('a{abc{bbab}c}')

def test_elem_head():
    assert len( selector('name').match(tree1) ) == 5
    assert len( selector('branch').match(tree1) ) == 5
    assert len( selector('name').match(tree2) ) == 9
    assert len( selector('branch').match(tree2) ) == 9

def test_elem_regexp():
    assert len( selector('/[a-c]$/').match(tree1) ) == 3
    assert len( selector('/[b-z]$/').match(tree2) ) == len('bcbbbc')

def test_elem_any():
    assert len( selector('*').match(tree1) ) == 16
    assert len( selector('*').match(tree2) ) == 28

def test_elem():
    test_elem_head()
Exemplo n.º 20
0
        #tokens = tokens +' '+token[2].lower()

    return tokens


path = 'Programas_MiniJava/programa1.java'

with open(path) as f:
    texto_arq = f.readlines()

#print(texto)
texto = ''
for i in texto_arq:
    texto = texto + i

texto = retornaTokensArquivo(path)
cont = 1
text = ''
for i in texto:
    text = text + i
    if i == ' ':
        #print(text, cont)
        cont += 1
        text = ''
#print(texto)
#texto = retornaTokensArquivo(path)

parser = Grammar(open("MiniJava.g"))
resp = parser.parse(texto)
print(resp)
Exemplo n.º 21
0
class Main_Strategy(bt.Strategy):
    params = ( ('strategy_expression_buy', None ), ('strategy_expression_sell', None ), ('strategies_parameters', None), )

    class Calc(STransformer):
        def __init__(self, indicators, strategies_parameters):
            self.indicators = indicators
            self.strategies_parameters = strategies_parameters
            self.strategies_functions = {
                "CCI_buy_Strategy": self.CCI_buy_Strategy,
                "CCI_sell_Strategy": self.CCI_sell_Strategy,
                "Crossover_SMA_Strategy": self.Crossover_SMA_Strategy
            }
        def CCI_buy_Strategy(self, parameter, indicator):
            if self.indicators[indicator] < parameter["value"]:
                return 1
            else:
                return 0

        def CCI_sell_Strategy(self, parameter, indicator):
            if self.indicators[indicator] > parameter["value"]:
                return 1
            else:
                return 0

        def Crossover_SMA_Strategy(self, parameter, indicator):
            if self.indicators[indicator] > 0:
                return 1
            else:
                return 0

        def _bin_operator(self, atom):
            arg1, operator_symbol, arg2 = atom.tail
            operator_func = { '&': op.and_, '|': op.or_ }[operator_symbol.tail[0]]
            return operator_func(arg1, arg2)

        def _bin_strategy(self, atom):
            s_p = self.strategies_parameters[atom.tail[0]]
            return self.strategies_functions[s_p["name"]](s_p["parameter"], int(atom.tail[0].replace("s","")))

        strategy = _bin_strategy
        atomic = _bin_operator

    def __init__(self):
        self.dataclose = self.datas[0].close
        self.indicators = []
        self.strategy_expression_buy = self.params.strategy_expression_buy
        self.strategy_expression_sell = self.params.strategy_expression_sell
        self.strategies_parameters = self.params.strategies_parameters
        for s, p in self.strategies_parameters.items():
            if p["name"] == "CCI_buy_Strategy":
                self.indicators.append(bt.indicators.CommodityChannelIndex())
            elif p["name"] == "CCI_sell_Strategy":
                self.indicators.append(bt.indicators.CommodityChannelIndex())
            elif p["name"] == "Crossover_SMA_Strategy":
                sma_fast, sma_slow = bt.ind.SMA(period=p["parameter"]["fast"]), bt.ind.SMA(period=p["parameter"]["slow"])
                self.indicators.append(bt.indicators.CrossOver(sma_fast, sma_slow))
        self.strategy_parser = Grammar("""
                                        start: exp ;
                                        @exp : strategy | atomic ;
                                        atomic: '\(' exp op exp '\)' | exp op exp ;
                                        strategy: 's\d+' ;
                                        op: '\&|\|' ;
                                        SPACES: '[ ]+' (%ignore) ;
                                    """)
        self.strategy_expression_buy = self.strategy_parser.parse(self.strategy_expression_buy)
        self.calc_buy = self.Calc(self.indicators, self.strategies_parameters)
        self.strategy_expression_sell = self.strategy_parser.parse(self.strategy_expression_sell)
        self.calc_sell = self.Calc(self.indicators, self.strategies_parameters)

    def next(self):
        buysig = self.calc_buy.transform(self.strategy_expression_buy).tail[0]
        sellsig = self.calc_sell.transform(self.strategy_expression_sell).tail[0]
        if not self.position:
            if buysig:
                self.buy()
        else:
            if sellsig:
                self.close()

        if len(self.dataclose) == (self.dataclose.buflen())-1:
            self.close()
Exemplo n.º 22
0
box_command : 'define' name 'box' number number color;
kill_command: 'kill' pattern ;

sleep_command: 'sleep' number;

spring_command: 'spring' pattern (number number number number)? (option)*;

pattern: name (name)* | regexp ;

option: '-' name;

geometry : number number | name (offset)?;
offset : '\+' number '-' number ;

color: simple_color ('\*' number)?;
simple_color: name | '\#[\da-f]{6,8}' | number ',' number ',' number (',' number)? | name '\*' number; 

name : '[A-Za-z][0-9A-Za-z]*' ;
number : '[\d\.]+' ;

regexp: 'r\'.+?\'' ;

eol : '\n' ;
SPACES : ' +' (%ignore) ;
""")

r=list_parser.parse("""define b1 box 100 100 cyan
""")
print(r)

Exemplo n.º 23
0
    rpp_type  = lambda self, node: node
    box_type  = lambda self, node: node
    rcc_type  = lambda self, node: node
    type = lambda self, node: node
    bodies  = lambda self, node: node
    operator  = lambda self, node: node
    expr  = lambda self, node: node
    zone_operation = lambda self, node: node
    zones  = lambda self, node: node
    material = lambda self, node: node
    media = lambda self, node: node
    file  = lambda self, node: node
    VALUE = lambda self, node: node
    SPH_T = lambda self, node: node
    RPP_T = lambda self, node: node
    BOX_T = lambda self, node: node
    RCC_T = lambda self, node: node
    SEPARATOR = lambda self, node: node
    MINUS = lambda self, node: node
    PLUS = lambda self, node: node
    DESCRIPTION = lambda self, node: node
    ZONE_NAME = lambda self, node: node

file = open("geo.dat")
content = file.read()
preprocessed_dat = convert_geo(content)
tree = json_grammar.parse(preprocessed_dat)

tree.to_png_with_pydot(r'tree.png')
print(Geo_parserTransformer().transform(tree))