几十行极简的flex

154 views
Skip to first unread message

风间星魂

unread,
Nov 3, 2012, 12:28:06 PM11/3/12
to pyth...@googlegroups.com
class pytoken:
    pattern = ""
    idtype = ""
    def __init__(self, pt, idtype = ""):
        self.pattern = pt
        self.idtype = idtype

    def call(self, tokstr):
        print tokstr
   
class pyflex:
    strcode = ""
    tokens = []
    def __init__(self, s = "", tok=[]):
        self.strcode = s
        self.tokens = tok

    def lexfork(self, matchlist):
        "二义性规则"
        count = len(matchlist)
        if count == 1:
            return matchlist[0]
        if count > 1:
            return max(matchlist, key=lambda x : x["re"].end())


    def parse(self):
        while len(self.strcode) > 0:
            matchlist = []
            for p in self.tokens:
                r = re.search(p.pattern, self.strcode)
                if r != None and r.start() == 0:
                    m = {"tok" : p, "re" : r}
                    matchlist.append(m)

            if len(matchlist) > 0:
                l = self.lexfork(matchlist)
                p = l["tok"]
                r = l["re"]
                p.call(r.group())
                self.strcode = self.strcode[r.end():]
            else:
                self.strcode = self.strcode[1:]


def main():
    fun = "word1 + 0.15 += 2*(30+20)"
    num = pytoken("[0-9]+", "NUM")
    f = pytoken("[0-9]?\.[0-9]+")
    ids = pytoken("[a-zA-Z][a-zA-Z0-9]*")
    add = pytoken("\+")
    mul = pytoken("\*")
    assign = pytoken("\=")
    addassign = pytoken("\+=")
    space = pytoken("[ \t]+")
    op = pytoken("\(")
    cp = pytoken("\)")

    lex = pyflex(fun, [num, f, ids, add, mul, assign, addassign, space, op, cp])
    lex.parse()


--
求武汉任意unix职位,2年ios经验。。


技术博客地址:fengjian.sinaapp.com

Earthson

unread,
Nov 13, 2012, 1:47:24 AM11/13/12
to pyth...@googlegroups.com
看名字吓了一跳,还以为是一个简短的DFA引擎呢~
好吧,内部是re~~


--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
发言: pyth...@googlegroups.com
退订: python-cn+...@googlegroups.com (向此发空信即退!)
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp



--


Perfection is achieved 
not when there is nothing more to add
 but when there is nothing left to take away

风间星魂

unread,
Nov 13, 2012, 2:36:02 AM11/13/12
to pyth...@googlegroups.com
有看见过一个真正只有几十行的DFA引擎。。。。
很复杂的递归。


2012/11/13 Earthson <earthso...@gmail.com>



--
求武汉任意unix职位,2年ios经验。。


技术博客地址:fengjian.sinaapp.com

Earthson

unread,
Nov 13, 2012, 2:39:51 AM11/13/12
to pyth...@googlegroups.com
Orz~
求链接:)

~~~
直接搜都很少有看到dfa实现的说~

风间星魂

unread,
Nov 13, 2012, 3:08:43 AM11/13/12
to pyth...@googlegroups.com

lua那个lstrlib.c 里的match 几十行函数。相关的加起来也很少,压缩一下恶心点几十行感觉也能做到。


2012/11/13 Earthson <earthso...@gmail.com>

Earthson

unread,
Nov 13, 2012, 3:14:08 AM11/13/12
to pyth...@googlegroups.com
Orz~ 这东西看起来压力略大啊~ 

Wang Xuerui

unread,
Nov 13, 2012, 3:20:24 AM11/13/12
to pyth...@googlegroups.com
2012/11/13 风间星魂 <fengjia...@gmail.com>:
> 有看见过一个真正只有几十行的DFA引擎。。。。
> 很复杂的递归。

直接给跪。。昨天晚上才写了个不支持状态的lexer,近百行,实在无力优化

依云

unread,
Dec 1, 2012, 4:06:30 AM12/1/12
to pyth...@googlegroups.com
我修改了下自己用,在这里:
https://github.com/lilydjwg/winterpy/blob/master/pylib/simplelex.py

许可证之类的需要注明下么?
Best regards,
lilydjwg

Linux Vim Python 我的博客:
http://lilydjwg.is-programmer.com/
--
A: Because it obfuscates the reading.
Q: Why is top posting so bad?

范煜

unread,
Dec 1, 2012, 7:13:20 AM12/1/12
to pyth...@googlegroups.com
我有个小的正则项目,最近三个月以来正在慢慢重写。。再过个两周瓦就填的差不多了


--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
发言: pyth...@googlegroups.com
退订: python-cn+...@googlegroups.com (向此发空信即退!)
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp



--
我的github: github.com/fy0

风间星魂

unread,
Dec 2, 2012, 6:41:13 AM12/2/12
to pyth...@googlegroups.com
本来从没想过这档事情,但既然你提到了。。。
允许我装一次b。
声明为BSD许可证。。。
你这个用在邮件分拣上的?


2012/12/1 依云 <lily...@gmail.com>
--
--
邮件来自: `CPyUG`华蟒用户组(中文Python技术邮件列表)
规则: http://code.google.com/p/cpyug/wiki/PythonCn
发言: pyth...@googlegroups.com
退订: python-cn+...@googlegroups.com (向此发空信即退!)
详情: http://code.google.com/p/cpyug/wiki/CpyUg
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp


依云

unread,
Dec 2, 2012, 7:03:03 AM12/2/12
to pyth...@googlegroups.com
OK, 下次更新就有版本信息了。

是全部都处理,清理邮件主题用的。

依云

unread,
Dec 2, 2012, 7:04:29 AM12/2/12
to pyth...@googlegroups.com
有错别字 -_-|||
s/本/权/
Reply all
Reply to author
Forward
0 new messages