为SQLite3生成JavaScript SQL解析器(使用Lemon?ANTLR3?) [英] Generating a JavaScript SQL parser for SQLite3 (with Lemon? ANTLR3?)

查看:319
本文介绍了为SQLite3生成JavaScript SQL解析器(使用Lemon?ANTLR3?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几周里,我一直在深入探讨将SQL语句解析成可管理的东西,但却发现我可能需要一个完整的词法分析器/解析器才能正确处理所有允许的令牌/格式做同样的事情。

For the last couple of weeks i've been diving into the pretty world of parsing SQL statements into something managable, only to find out that i'll probably need a full lexer/parser to properly handle all the allowed tokens/formats to do the same thing.

我最感兴趣的是创建表语句,但是一个完整的通用解析器会更好,因为网上没有人似乎有这还没有。

I'm mostly interested in the create table statements, but a full generic parser would be even nicer, since nobody on the web seems to have this yet.

我不是电脑毕业生,而是一个自学成才的人,所以这对我来说是一个很好的学习曲线。
我采取的步骤是:

I'm no computer graduate, but a self-taught man, so this is quite the learning curve for me. The steps I took were:


  1. 使用正则表达式解析sql

  2. 失败,修复正则表达式

  3. 更糟糕的是,深入了解SQLite源代码,发现它使用的是Lemon解析器,一个特定于SQLite的项目

  4. 试着去Lemon + PHP解析器的工作思路我可以手动将其转换为js。 (失败)

  5. 尝试让Emscripten在C中运行Lemon生成的解析器(依赖地狱,失败。)

  6. 搜索其他解析器/词法分析器生成器,请注意ANTLR3

  7. 全天候尝试获取 Sqljet语言文件通过更改输出格式和回溯错误转换为javascript。

  1. Parse sql with regexes
  2. That fails, fix regex
  3. That fails worse, dig through the SQLite source to find out that it uses the Lemon parser, an SQLite specific project
  4. Try to get Lemon + PHP parser working thinking i can convert that manually to js. (failed)
  5. Try to get Emscripten working on the Lemon generated parser in C (dependency hell, failed.)
  6. Search for other parser/lexer generators, note ANTLR3
  7. Try all day and night to get the Sqljet language file converted to javascript by changing output format and backtracing errors.

我一直在使用优秀的 AntlrWorks GUI试图弄清楚出了什么问题,但我不确定它是否是Javascript堆栈,Java堆栈,或者.g格式是v2的旧格式。

I have been using the excellent AntlrWorks GUI to try and figure out what is going wrong, but i'm unsure if it's the Javascript stack that's breaking, the Java stack, or that the .g format is in an old format for v2.

有没有人有解析器/词法分析器生成器经验可以指出我正确的方向来生成一个正确的可重用Sqlite解析器?
我似乎能够在javascript中为mysql和pl / sql生成解析器。这是否意味着sqlite .g格式需要更新?

Is there anyone with parser / lexer generator experience that can point me to the correct direction for generating a proper reusable Sqlite parser? I seem to be able to generate the parsers in javascript for both mysql and pl/sql. Does that mean that the sqlite .g format is in need of updating?

推荐答案

能够在JavaScript中使用语法目标,您必须:

To be able to use a grammar with the JavaScript target, you must:


  • 更改选项中的目标语言块: options {language = JavaScript; }

  • 更改 .g 语法文件中的所有嵌入代码(<$ c之间的内容使用JavaScript代码的$ c> {和} )。请注意,ANTLR 转换此代码,具体取决于您在选项中定义的内容'语言值,你必须自己做!

  • 在生成解析器时,不要使用ANTLRWorks,而是在命令行中使用,并使用大量的堆与java的 -Xmx 参数:SQL语法是需要大量内存的大型野兽

  • change the target language in the options block: options { language=JavaScript; }
  • change all embedded code in the .g grammar file (the stuff between { and }) with JavaScript code. Note that ANTLR does not convert this code depending on what you define in the options' language value, you'll have to do that yourself!
  • when generating a parser, don't use ANTLRWorks but do so on the command line and use a large amount of heap with java's -Xmx argument: SQL grammar are large beasts that need a large amount of memory

这是以前的Q& A,它展示了如何结合JavaScript目标使用(并运行)ANTLR生成的解析器: antlr3 - 生成解析树

Here's a previous Q&A that shows how to use (and run) an ANTLR generated parser in combination with the JavaScript target: antlr3 - Generating a Parse Tree

HTH

这篇关于为SQLite3生成JavaScript SQL解析器(使用Lemon?ANTLR3?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆