generator - Negative lookahead in LR parsing algorithm -


consider such rule in grammar lr-family parsing generator (e.g yacc, bison, etc.):

nonterminal : [ lookahead not in {terminal1, ..., terminaln} ] rule ; 

it's ordinary rule, except has restriction: phrase produced rule cannot begin terminal1, ..., terminaln. (surely, rule can replaced set of usual rules, result in bigger grammar). can useful resolving conflicts.

the question is, there modification of lr table construction algorithm accepts such restrictions? seems me such modification possible (like precedence relations).

surely, can checked in runtime, mean compile-time check (a check performed while building parsing table, %prec, %left, %right , %nonassoc directives in yacc-compartible generators.)

i don't see why shouldn't possible, don't see obvious reason why useful. have example in mind?

the easiest way grammar transform mention in parentheses. make larger grammar, won't artificially increase number of lr states.

the basic transformation, bit of hand-waving:

for production terminal restrictions:

  • if production starts non-nullable non-terminal, replace non-terminal terminal-restricted version.

  • if production starts terminal in terminal restriction list, remove production

  • if production starts terminal not in terminal restriction list, no change necessary.

if production starts nullable non-terminal, have create 2 versions of nullable non-terminal, 1 of null, , other of non-nullable; , create 2 versions of production, 1 starting each of new non-terminals. apply above transforms, interpreting "starts with" mean "starts after always-null non-terminals."

you don't need modify grammar, since above transformations can done on fly during construction of underlying slr machine, @ least lr(0) , lalr(1) constructions.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -