示例#1
0
文件: generator.py 项目: bukzor/pijnu
def formula():
    print ("=== sample1: formula ===")
    formulaGrammar = """\
formula
<toolset>
def doAdd(node):
    (n1,n2) = (node[0].value,node[1].value)
    node.value = n1 + n2
def doMult(node):
    (n1,n2) = (node[0].value,node[1].value)
    node.value = n1 * n2
<definition>
digit       : [0..9.]
number      : digit+                                        : join toReal
ADD         : '+'                                           : drop
MULT        : '*'                                           : drop
LPAREN      : '('                                           : drop
RPAREN      : ')'                                           : drop
mult        : (grup/number) MULT (grup/mult/number)         : @
add         : (grup/mult/number) ADD (grup/add/mult/number) : @
grup        : LPAREN (add / mult) RPAREN                    : @ liftNode
formula     : add / mult / digit
"""
    makeParser(formulaGrammar)
    # test
    from formulaParser import formulaParser
    formulaParser.test("9*8+01*2.3+45*67*(89+01.2)")
    print 9*8+01*2.3+45*67*(89+01.2)
    print formulaParser.match("9*8+01*2.3+45*67*(89+01.2)")
    Node.TREE_VIEW=True
    print formulaParser.match("9*8+01*2.3+45*67*(89+01.2)")
示例#2
0
def formula():
    print("=== sample1: formula ===")
    formulaGrammar = """\
formula
<toolset>
def doAdd(node):
    (n1,n2) = (node[0].value,node[1].value)
    node.value = n1 + n2
def doMult(node):
    (n1,n2) = (node[0].value,node[1].value)
    node.value = n1 * n2
<definition>
digit       : [0..9.]
number      : digit+                                        : join toReal
ADD         : '+'                                           : drop
MULT        : '*'                                           : drop
LPAREN      : '('                                           : drop
RPAREN      : ')'                                           : drop
mult        : (grup/number) MULT (grup/mult/number)         : @
add         : (grup/mult/number) ADD (grup/add/mult/number) : @
grup        : LPAREN (add / mult) RPAREN                    : @ liftNode
formula     : add / mult / digit
"""
    makeParser(formulaGrammar)
    # test
    from formulaParser import formulaParser
    formulaParser.test("9*8+01*2.3+45*67*(89+01.2)")
    print 9 * 8 + 01 * 2.3 + 45 * 67 * (89 + 01.2)
    print formulaParser.match("9*8+01*2.3+45*67*(89+01.2)")
    Node.TREE_VIEW = True
    print formulaParser.match("9*8+01*2.3+45*67*(89+01.2)")