问题标签 [nearley]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
35 浏览

nearley - Nearley at least one

I have a grammar where I want to have some whitespace (including newlines) in between two terms. There should be some whitespace, i.e. it should fail if the two terms are touching, however there can be as much whitespace as desired. The issue I'm coming across is that whitespace and newlines are different tokens. I can't work out how to generally make "at least one" in nearley.

0 投票
0 回答
225 浏览

javascript - 如何对大文件使用 moo lexer(和 nearley)

我试图找到可以解析非常大的文件(基本上是 PGN 文件)的东西。我开始使用 antlr4,但即使他们声称他们的类是“流”,但它们不是。antlr4 拿了我的 5,457,518 游戏测试文件,并试图将整个 1.7G 文件加载成一个巨大的字符串,然后解析它,导致内存不足崩溃。因此,我把它扔掉了,现在正在尝试 moo/nearley。

好吧,我似乎也有类似的问题。尽管 moo 和 nearley 都提供了将所谓的“块”作为参数的方法,但 moo 尤其没有意识到它位于字符串的末尾,并且可以在下一个 moo.feed 上获得更多。

例如,我的测试程序尝试将其发送给 moo,一次发送两个字节:[Abcde "bc def"]. 它LBRACKET正确吐出但随后它A作为一个符号吐出。如果我做 a ,它就会作为第二个符号moo.reset(next_two)吐出。bc

所以我的问题是,你,主词法分析器/解析器,到底是怎么做的?我应该回到antlr4吗?我应该以不同的方式使用 moo/nearley 吗?那里有更好的词法分析器/解析器吗?我真的不想从头开始编写自己的,但我真的开始怀疑是否有其他方法。

0 投票
1 回答
262 浏览

grammar - (E)BNF 如何匹配直到下一个非终结规则?

我正在尝试使用RIS 格式的内容编写语法

文件示例:

文件*.ris总是以 tag 开始,以 tagTY结束ER。在两者之间可以有许多其他标签,如KW(关键字)。

规范说单个KW语句可以跨越多行。

所以这:

相当于:

我正在努力想出一个这样的语法:

关键字以开头,KW后跟 -以下之一:

  • 直到行尾的字母
  • 字母直到行尾和任何其他行直到下一个关键字

无论我尝试什么,最终都会“吞下”所有其他语句,例如,第一个多行关键字捕获它之后的所有其他内容。

你会怎么写这个规则?我不一定对特定的答案感兴趣。任何能触发我“啊哈”时刻的事情都可以!

0 投票
2 回答
240 浏览

regex - 语法 - 如何在单词前后匹配可选和必需的空格?

我正在使用nearley 和 moo来提出一个相当复杂的语法。除了我的空白要求之外,它似乎工作正常。我需要在需要时需要空格,并在不需要时允许它,同时保持语法明确。

例如:

我需要在单词之间使用空格,但允许在逗号周围使用空格。所以以下也是有效的:

下面是一个试图做到这一点的快速近乎语法。如果您不了解语法,则很容易弄清楚。

语法可能有问题,但想法是一样的。这变成了一个模棱两可的语法。如何定义明确的语法,期望可选和必需的空格?

0 投票
1 回答
202 浏览

javascript - [Nearley]:如何解析匹配的开始和结束标签

我正在尝试使用 nearley 解析一种非常简单的语言:您可以在匹配的开始标签和结束标签之间放置一个字符串,并且可以链接一些标签。它看起来像一种 XML,但使用[而不是<, 标记总是 2 个字符长,并且没有嵌套。

但是我似乎无法正确解析这一点,因为我the grammar should be unambiguous一有多个标签就得到了。

我现在拥有的语法:

并且相关,理想情况下我希望标签不区分大小写(例如[bc]TESt[/BC]有效)

有谁知道我们该怎么做?我找不到一个近乎 XML 解析器示例。

0 投票
1 回答
90 浏览

javascript - 用于解析开始和结束标签的 Nearley 解析器语法

假设我有一种简单的语言来解析,它只是由字符串组成。"this is a string"

但是,该字符串可以在花括号内包含代码。为了简单起见,我们只说code只能是另一个字符串。"this is a string with {"code"}"

如何在 Nearley 中定义新字符串以包含code定义?我不断得到大量结果,因为chars可以匹配一个或多个字符。

理想情况下,我可以将这样的东西"chars {"code"} chars chars {"code"} chars"变成一个数组["chars ", "code", " chars chars ", "code", " chars"]

也许只能按照这个答案中的建议使用正则表达式和 moo 来做到这一点?(在这个例子中,开始和结束标签不那么模棱两可了,我没有遇到同样的问题。)[Nearley]:如何解析匹配的开始和结束标签

0 投票
1 回答
14 浏览

nearley - 如何将回车添加到近乎可能的空白值?

我正在使用nearley,但在某些 CRLF 文件上运行时失败。现在我已经通过评论空白内置导入解决了它,而是用 \r 重做它,如下所示:

能够做类似的事情wschar -> [ \t\n\r\v\f] {% id %}并从内置文件中建立它会很棒,但这样做会创建一个无限循环。

谢谢,任何帮助表示赞赏。

0 投票
1 回答
73 浏览

parsing - Generating a parser for expressions

I am trying to generate a javascript parser for the ebnf grammar described in this Microsoft article. The ebnf specified in the article does not work when I use it as its written, so I have tried to simplify it to make it work with the REx parser generator.

The goal is in Javascript to be able to parse and evaluate expressions like these to True or False:

  • AttributeA > 2 - The value of AttributeA is greater than 2
  • HasCategory(Assembly) - The node has Category Assembly
  • Assembly - The node has Category Assembly
  • HasValue(AttributeA) - The attribute AttributeA has a value. Its not undefined.
  • AttributeA < AttributeB - The value of attribute AttributeA is less than the value of attribute Attribute B
  • IsReference - The value of the attribute IsReference is True
  • AttributeA + 2 > 5 and AttributeB - 5 != 7
  • AttributeA * 1.25 >= 500

I am using the REx parser generator online here: https://bottlecaps.de/rex/. If you have suggestions for other parser generators that produce JavaScript I would appreciate some links to where I can find them.

The issue I'm struggling with is the definition of the MethodCall. I have tried a lot of different definitions but all fail. When I remove the MethodCall and MethodArgs definition the REx parser generator produces a parser.

So I would appreciate any help to crack this problem a lot.

Below is the grammar as far as I have been able to get.

Here are some of the different versions for the MethodCall definition I have tried but all of them fail.

I have tried to get inspiration from a number of other languages to see how they do it, but with no luck yet so far:

Update

Just wanted to let you know how this turned out. I struggled to get the EBNF grammar to do what I needed it to do, so I took a look at Nearley like @rici suggested and converted my grammar into the Nearley grammar syntax and got the tooling to work. It was just a much better choice for my project. The documentation is very good, the tooling is also great and the error messages is very helpful. So a huge thanks to @rici for suggesting Nearley.

Below is the grammar I have implemented. I have tested with the following inputs:

'2 + 4', '2 + 4 - 6', '(2 + 4)', '!true', '!(true)', 'hasCategory(test)', 'hasCategory(test,test2)', 'hasCategory( test , test2 )', 'hasCategory(test,test2, test3)', 'IsReference', 'IsReference()', '2 * 4', '(2 / 4)', '2 * 4 + 2', '(2 / 4) + 2', '2 > 4', '2 >= 2', '2 = 4', '2 == 2', '2 != 4', '2 !== 2', '(2 * 4 + 2) > 4', '(2 * 4 + 2) > (4 + 10)', 'true', 'true or false', 'true || false', 'true and false', 'true && false', '(true or false)', '!(true or false)', '2 != 1+1', '2 != (1+1)', '2 != (1+2)', '(2 > 2 or (2 != 1+1))',

0 投票
0 回答
15 浏览

javascript - Nearley 后处理正在删除有效的空格

Nearley 语法的新手,不知道这里出了什么问题。这是我的语法,用于获取放在左括号和右括号内的任何内容。

  • 测试输入 -(hello, " ", world)
  • 预期结果 -{type: "inside_brackets", argumentstring: "hello, " ", world"}
  • 实际结果 -{type: "inside_brackets", argumentstring: "hello, " ", world"}

注意 - 实际结果是将 4 个空格压缩为 1 个空格。不知道为什么会这样。

有什么想法吗?仅供参考 - 我正在使用Nearley Playground在 Safari v15 浏览器上测试我的语法。

0 投票
1 回答
25 浏览

parsing - 用 moo 生成的数据结构理解 Nearley

我有这个语法:

现在,当我向 nearley 提供以下输入时://\n//\n//\n//\n//我得到了这个结果:

我不太明白为什么生成的数组嵌套如此之深,以及是否有办法让每个元素都保持平坦。相同语义级别的类似注释应该是一个数组的一部分,而不是嵌套的。