You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lark语法优先级问题求助:解析结果与预期不符

Fixing Lark Parser Matching Priority for "3xfoo"

The issue you're facing boils down to how PEG parsers (like Lark) prioritize rule matching—they try rules in the order they’re listed and greedily lock into the first valid match. In your original code, the set rule can already match the entire input "3xfoo" (since it includes an optional nb prefix), so the parser never gets the chance to split the nb and set into separate nodes via the set_mul rule.

Here's a straightforward fix:

Adjust Rule Order and Structure

Reorder the start rule to try set_mul first, and define set_mul as explicitly requiring an nb followed by a set. This ensures the parser prioritizes splitting the input into nb and set when possible, before falling back to the standalone set rule.

from lark import Lark
parser = Lark(r"""
start: set_mul | set
set_mul: nb set
set: [nb] "foo"  # Keep this if you still need `set` to accept an optional nb prefix
nb: INT "x"
%import common.INT
%import common.WS
%ignore WS
""", start='start')

input_str = "3xfoo"
p = parser.parse(input_str)
print(p.pretty())

Expected Output

start
  set_mul
    nb
      3
    set

Why This Works

  1. Rule Order: By placing set_mul before set in the start rule, the parser first attempts to match nb + set. For "3xfoo", it successfully matches nb (3x) and then set (foo), so it uses this interpretation instead of falling back to the set rule.
  2. Explicit set_mul Definition: Defining set_mul as nb set (rather than [nb] set) removes ambiguity—now it only matches when there’s an nb preceding a set, which aligns perfectly with your desired AST structure.

Alternative (If You Don’t Need set to Accept nb)

If you don’t actually require set to have an optional nb prefix, you can simplify the rules further to eliminate all ambiguity entirely:

from lark import Lark
parser = Lark(r"""
start: set_mul
set_mul: nb set | set
set: "foo"
nb: INT "x"
%import common.INT
%import common.WS
%ignore WS
""", start='start')

This version will still produce your desired output for "3xfoo" and cleanly handle standalone "foo" inputs as well.

内容的提问来源于stack exchange,提问作者Kypaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:04:44