dongshan3806 2015-04-25 15:25 采纳率: 0%
浏览 28

我想写一个特定用途的解析器/词法分析器,但我感到不知所措。 你能用这个小例子帮我吗? [关闭]

So I'm writing a php templating engine called Slade inspired by Ruby Slim and laravel Blade.

And now many people are recommending me to rewrite it to a lexer / parser thing, instead of relying completely on regexes. So I googled about lexers and parsers and tried to learn how they work, and while I think I get the general idea, I still find it hard to start write one.

So I was hoping if someone would help me on my way, by showing how to do one example. How would I exactly lex (is that even a verb?) and parse this:

#wrapper.container.well first-attr="a literal attribute" second-attr=variable.name And here some text that will be the content of the div...

Into these nodes:

[
    'tagName' => 'div', // since there was no tagname, a div is assumed
    'attributes' => [
        'id' => 'wrapper',
        'class' => 'container well',
        'first-attr' => 'a literal attribute',
        'second-attr' => 'the value of the variable',
    ],
    'textContent' => 'And here some text that will be the content of the div...'
]

Of course I don't expect anyone to write out a function that 100% lexes / parses this, but I'd like to see the general pseudo code of how to go about this. Could anyone help me with this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启