我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用pyparsing.StringEnd()。
def __init__(self, calc = SimpleCalculator()): self.exprStack = [] def pushStack(s, l, t): self.exprStack.append(t[0]) integer = Word(nums).addParseAction(pushStack) addop = Literal('+') | Literal('-') mulop = Literal('*') | Literal('/') lpar = Literal('(') rpar = Literal(')') expr = Forward() atom = integer | lpar + expr + rpar term = atom + ZeroOrMore((mulop + atom).addParseAction(pushStack)) expr << term + ZeroOrMore((addop + term).addParseAction(pushStack)) self.expr = expr + StringEnd() self.opfun = { '+' : (lambda a, b: calc.add(a,b)), '-' : (lambda a, b: calc.sub(a,b)), '*' : (lambda a, b: calc.mul(a,b)), '/' : (lambda a, b: calc.div(a,b)) }
def get_language(): """Create or retrieve the parse tree for defining a sensor graph.""" global sensor_graph, statement if sensor_graph is not None: return sensor_graph _create_primitives() _create_simple_statements() _create_block_bnf() sensor_graph = ZeroOrMore(statement) + StringEnd() sensor_graph.ignore(comment) return sensor_graph