nearley icon indicating copy to clipboard operation
nearley copied to clipboard

Parsing context-sensitive grammars

Open jarble opened this issue 4 years ago β€’ 6 comments

I'm trying to write a parser for a "context-sensitive" grammar in Nearley, but I'm not sure if it's possible to do this yet.

For example, I would want this rule to only match patterns like "to be or not to be" or "to do or not to do:"

# "_" matches whitespace, "word" matches any word
phrase -> "to" _ word _ "or" _ "not" _ "to" _ word

Would it possible to make this rule "context-sensitive," so that the first word is identical to the second word?

jarble avatar Nov 10 '19 15:11 jarble

Would it be possible to make this grammar rule "context-sensitive" using a postprocessor?

jarble avatar Nov 10 '19 15:11 jarble

I can’t remember if there is a post-matching bit of code but I can imagine that if it let you accept a parse through a subroutine call or reject one then you could do the same thing as this context sensitive rule by checking that the two parses for the β€œword” are equal.

whitten avatar Nov 10 '19 16:11 whitten

Could we make a gist with this example using the fiddle for nearley ?

whitten avatar Nov 10 '19 16:11 whitten

You can use a post processor, the third argument is a reject thing that can be used for this.

phrase -> "to" _ word _ "or" _ "not" _ "to" _ word {% 
    (d, index, reject)  => {
      return d[2] === d.slice(-1)[0] ? reject : d
   }
%}

TheGrandmother avatar Dec 06 '19 15:12 TheGrandmother

Nearley is intended to parse context-free grammars, and your example isn’t context-free.

Depending on your grammar, perhaps you could generate every possible combination up front, for example by using a macro. Or you might be able to validate this when analysing the tree after parsing.

reject is deprecated, because it makes the grammar no longer context-free, and makes optimisation harder for us.

Sent with GitHawk

tjvr avatar Dec 14 '19 11:12 tjvr

I want to insert actions everywhere in the grammar, and i want to cut the backtracking by hand in the action like prolog does.

JIM-GLITCH avatar Jul 17 '22 14:07 JIM-GLITCH