parsinglark-parser

Lark: how to inline rule. DRY


I dont want the func rule here, it's merely to avoid repetition.

func: monad | dyad | builtin
dyad: "{" dyad* func+ "}" | func+ ":"
monad: "(" monad* func+ ")" | func+ "."
builtin: NAME

I could inline it

dyad: "{" dyad* (monad | dyad | builtin)+ "}" | (monad | dyad | builtin)+ ":"
monad: "(" monad* (monad | dyad | builtin)+ ")" | (monad | dyad | builtin)+ "."
builtin: NAME

but this is really annoying, when I am tinkering, not knowing what I am doing, and can't get my grammar right. Have to change thing in multiple places.

Is there a way to do this in lark?


Solution

  • You can use ?func

    ?func: monad | dyad | builtin
    dyad: "{" dyad* func+ "}" | func+ ":"
    monad: "(" monad* func+ ")" | func+ "."
    builtin: NAME
    

    There is also an issue about adding _func https://github.com/lark-parser/lark/issues/822