2013-12-10 31 views
1

我有问题一样here (nested function calls)pyparsing - 定义关键字 - 比较文字,词,关键词,并结合

我想也限制了仿函数是唯一的许多给出的单词(A,B,C)

一个

所以法律是:

a(dd, ee) 
b(a(1)) 

但不是:

aa(b(9)) - aa is invalid functor here 

我能做到这一点使用一个:

functor1 = Literal('a') | Literal('b') | Literal('c') 
functor2 = Word('a') | Word('b') | Word('c') 
functor3 = Keyword('a') | Keyword('b') | Keyword('c') 
functor4 = Combine(Keyword('a') | Keyword('b') | Keyword('c')) 

首先是容易的,但其余的是太暧昧了,我(尤其是Word有PARAM asKeyword,但它的代码不使用关键字类,反之亦然)。

请比较一下。

是OR列表工作作为Combine?

回答

1

这里是一些用于比较你的pyparsing表达式的测试代码。

from pyparsing import * 

functor1 = Literal('a') | Literal('b') | Literal('c') 
functor2 = Word('a') | Word('b') | Word('c') 
functor3 = Keyword('a') | Keyword('b') | Keyword('c') 
functor4 = Combine(Keyword('a') | Keyword('b') | Keyword('c')) 

functor1.setName("Literal('a') | Literal('b') | Literal('c')") 
functor2.setName("Word('a') | Word('b') | Word('c')") 
functor3.setName("Keyword('a') | Keyword('b') | Keyword('c')") 
functor4.setName("Combine(Keyword('a') | Keyword('b') | Keyword('c'))") 
functors = [functor1, functor2, functor3, functor4] 

tests = "a b c aaa bbb ccc after before".split() 
for func in functors: 
    print func 
    for t in tests: 
     try: 
      print t, ':', func.parseString(t) 
     except ParseException as pe: 
      print pe 
    print 

打印:

Literal('a') | Literal('b') | Literal('c') 
a : ['a'] 
b : ['b'] 
c : ['c'] 
aaa : ['a'] 
bbb : ['b'] 
ccc : ['c'] 
after : ['a'] 
before : ['b'] 

Word('a') | Word('b') | Word('c') 
a : ['a'] 
b : ['b'] 
c : ['c'] 
aaa : ['aaa'] 
bbb : ['bbb'] 
ccc : ['ccc'] 
after : ['a'] 
before : ['b'] 

Keyword('a') | Keyword('b') | Keyword('c') 
a : ['a'] 
b : ['b'] 
c : ['c'] 
aaa : Expected "a" (at char 0), (line:1, col:1) 
bbb : Expected "a" (at char 0), (line:1, col:1) 
ccc : Expected "a" (at char 0), (line:1, col:1) 
after : Expected "a" (at char 0), (line:1, col:1) 
before : Expected "a" (at char 0), (line:1, col:1) 

Combine(Keyword('a') | Keyword('b') | Keyword('c')) 
a : ['a'] 
b : ['b'] 
c : ['c'] 
aaa : Expected "a" (at char 0), (line:1, col:1) 
bbb : Expected "a" (at char 0), (line:1, col:1) 
ccc : Expected "a" (at char 0), (line:1, col:1) 
after : Expected "a" (at char 0), (line:1, col:1) 
before : Expected "a" (at char 0), (line:1, col:1) 

你应该能够使这些意见:

  • Literal将在给定的字符串匹配,即使它是 较大仅仅是开始串。

  • Word将匹配由其构造函数字符串中的字母组成的字符组字符 。

  • Keyword将 只有当它是不是一个更大的字 的一部分给定的字符串匹配(其次是空间,或由非单词字符)

  • Combine没有 做任何事情在这例。

Combine的目的是将多个匹配的标记合并到一个字符串中。举例来说,如果你定义一个社会安全号码为:

Word(nums,exact=3) + '-' + Word(nums,exact=2) + '-' + Word(nums,exact=4) 

然后解析“555-66-7777”会给你

['555', '-', '66', '-', '7777'] 

最有可能你想这是一个字符串,所以通过将分析器表达式打包到Combine

Combine(Word(nums,exact=3) + '-' + Word(nums,exact=2) + '-' + Word(nums,exact=4)) 

['555-66-7777']