Javascript 词法分析器/分词器(Python 中?)

发布于 2024-08-16 09:15:41 字数 221 浏览 6 评论 0原文

有谁知道 Javascript 词法分析器或分词器(最好是 Python 中的?)

基本上,给定一个任意 Javascript 文件,我想获取标记。

例如

foo = 1

变成类似:

  1. 变量名 : "foo"
  2. 空白
  3. 运算符 : 等于
  4. 空白
  5. 整数 : 1

Does anyone know of a Javascript lexical analyzer or tokenizer (preferably in Python?)

Basically, given an arbitrary Javascript file, I want to grab the tokens.

e.g.

foo = 1

becomes something like:

  1. variable name : "foo"
  2. whitespace
  3. operator : equals
  4. whitespace
  5. integer : 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

×纯※雪 2024-08-23 09:15:41

http://code.google.com/p/pynarcissus/ 有一个。

我还制作了一个,但它不支持自动分号插入,因此对于您无法控制的 javascript 来说它几乎没有用(因为几乎所有现实生活中的 javascript 程序都缺少至少一个分号):) 这是我的:

http://bitbucket.org/santagada/jaspyon/src/tip/jaspyon/

语法位于 jsgrammar.txt 中,它由 PyPy 解析库进行解析(您必须从 pypy 源中下载并提取),并构建一个解析树,我在 astbuilder.py 上行走

但是如果您没有许可问题我会选择pynarcissus。这是查看代码的直接链接(从 narcissus 移植):

http://code.google.com/p/pynarcissus/source/browse/trunk/jsparser.py

http://code.google.com/p/pynarcissus/ has one.

Also I made one but it doesn't support automatic semicolon insertion so it is pretty useless for javascript that you have no control over (as almost all real life javascript programs lack at least one semicolon) :) Here is mine:

http://bitbucket.org/santagada/jaspyon/src/tip/jaspyon/

the grammar is in jsgrammar.txt, it is parsed by the PyPy parsing lib (which you will have to download and extract from the pypy source) and it build a parse tree which I walk on astbuilder.py

But if you don't have licensing problems I would go with pynarcissus. heres a direct link to look at the code (ported from narcissus):

http://code.google.com/p/pynarcissus/source/browse/trunk/jsparser.py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文