# Negative lookahead in LR parsing algorithm

Consider such a rule in grammar for an LR-family parsing generator (e.g YACC, BISON, etc.):

Nonterminal : [ lookahead not in {Terminal1, ..., TerminalN} ] Rule ;

It's an ordinary rule, except that it has a restriction: a phrase produced with this rule cannot begin with Terminal1, ..., TerminalN. (Surely, this rule can be replaced with a set of usual rules, but it will result in a bigger grammar). This can be useful for resolving conflicts.

The question is, is there a modification of LR table construction algorithm that accepts such restrictions? It seems to me that such a modification is possible (like precedence relations).

Surely, it can be checked in runtime, but I mean compile-time check (a check which is performed while building parsing table, like %prec, %left, %right and %nonassoc directives in yacc-compartible generators.)

• I'm not clear whether this a question about the tools in practice, or the LR parsing strategy in the abstract world. Also, the title mislead me: I thought you wanted like $-5$ symbols lookahead. – Raphael Apr 21 '13 at 14:29
• Seems that the phrase "negative lookahead" is common use in regard to regular expressions. The question is about abstract theory, but it would be very useful in real world parsing (e.g., consider ECMA-262 grammar, ecma-international.org/ecma-262/5.1, section 12.4). I would very much appreciate any formal or informal proof or practical experience whether it is possible to implement such restrictions and how to do that. – skvadrik Apr 21 '13 at 15:24
• Okay. You say yourself, "this rule can be replaced with a set of usual rules". What remains to say? – Raphael Apr 21 '13 at 15:26
• This fact neither means that negative lookahead in LR parsing is useless, nor that it is possible, so I don't see what you mean. :) – skvadrik Apr 21 '13 at 15:49
• You claim yourself that there is a (straight-forward) construction to unfold the "negative lookahead" into regular rules, so a parser-generator can just do that, can't it – Raphael Apr 21 '13 at 15:51