admin管理员组文章数量:1403522
I'm studying piler construction and naturally I'm also studying real world implementations of these concepts. One example of this is Babel's parser: Babylon.
I went through Babylon's code and it appears to be using a Top Down parser with embedded ad hoc semantic rules. src
I was expecting Babel to be using a member of the LR parsers and probably a definition file where the grammar productions are coupled together with semantic rules. Why? Well mostly because a bunch of other real world langs use lr parser generators such as Yacc, Bison, et al, that give you this exact interface, and seems to be a clearer and more maintainable way of representing these rules, and even more when you consider that Babel lives on the edge of the Javascript standard, implementing new things all the time.
I also have constructed both top down and bottom up (lr) parsers and I don't see a big implementation difficulty difference between the two (both are equally difficult :) )
So, why does Babel's parser uses a top down ad hoc syntax directed translations instead of what I see as a more structured approach? What are the design decisions behind that? What am I missing?
Thanks!
I'm studying piler construction and naturally I'm also studying real world implementations of these concepts. One example of this is Babel's parser: Babylon.
I went through Babylon's code and it appears to be using a Top Down parser with embedded ad hoc semantic rules. src
I was expecting Babel to be using a member of the LR parsers and probably a definition file where the grammar productions are coupled together with semantic rules. Why? Well mostly because a bunch of other real world langs use lr parser generators such as Yacc, Bison, et al, that give you this exact interface, and seems to be a clearer and more maintainable way of representing these rules, and even more when you consider that Babel lives on the edge of the Javascript standard, implementing new things all the time.
I also have constructed both top down and bottom up (lr) parsers and I don't see a big implementation difficulty difference between the two (both are equally difficult :) )
So, why does Babel's parser uses a top down ad hoc syntax directed translations instead of what I see as a more structured approach? What are the design decisions behind that? What am I missing?
Thanks!
Share Improve this question asked Oct 16, 2017 at 23:29 franleplantfranleplant 6391 gold badge7 silver badges19 bronze badges 3- Because English is read top to bottom. – StackSlave Commented Oct 16, 2017 at 23:36
- 4 @PHPglue That’s not what “top-down” refers to. What does English have to do with this? – Sebastian Simon Commented Oct 16, 2017 at 23:40
- This question probably isn't a good fit for SO since generally the answer is "because someone decided it should be". Also in this case Babylon started as a fork of github./ternjs/acorn so most architectural questions aren't Babylon-specific. – loganfsmyth Commented Oct 16, 2017 at 23:41
1 Answer
Reset to default 14I feel like you're really asking two (or maybe three) questions, so I'll address them separately
In general what are the advantages and disadvantages of different approaches to parsing
Top down vs. bottom up
For hand-written parsers the situation is actually pretty clear: Top-down parsers are much easier to write and maintain to the point that I've never even seen a hand-written bottom-up parser.
For parser generators the situation is less clear. Both types of parser generators exist (for example yacc and bison are bottom-up and ANTLR and JavaCC are top-down). Both have their advantages and disadvantages and I don't think there's much cause to say that one approach is clearly better than the other.
In fact I'd say it usually makes no sense to decide between top-down and bottom-up parsing. When hand-writing your parser, always go with the former. When using a parser generator, you should simply choose the tool whose features best fit your project, not based on whether it generates bottom-up or top-down parsers.
Hand-written parsers vs. parser generators
There are many reasons why one would hand-write parsers. These also depend on which parser-generators are even available for the language. One short-ing that parser generators often suffer from is that they make it hard to generate good error messages for syntax errors.
Another possible problem is that for non-context free languages you might need some dirty hacks to implement them using a parser generator or it might just not be possible at all.
How do these factors apply specifically to Babylon
Hand-written parsers vs. parser generators
The JavaScript grammar is quite plicated with a lot of special cases to resolve ambiguities. It would probably require extensive hacks when using a parser generator and might not be possible at all with the parser generators available for JavaScript.
I would also say that the parser generators available for JavaScript might not yet be production-ready and were even less so when the project was first created.
Top down vs. bottom up
As I said, I've never ever seen a hand-written bottom-up parser. So the decision to write a top-down parser is a no-brainer once you decide to go with a hand-written parser.
本文标签: javascriptWhy Babel uses a top down parserStack Overflow
版权声明:本文标题:javascript - Why Babel uses a top down parser? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744381783a2603541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论