nearley
nearley copied to clipboard
Parsing context-sensitive grammars
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
?
Would it be possible to make this grammar rule "context-sensitive" using a postprocessor?
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.
Could we make a gist with this example using the fiddle for nearley ?
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
}
%}
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
I want to insert actions everywhere in the grammar, and i want to cut the backtracking by hand in the action like prolog does.