少于 1 分钟阅读

Why factor?Permalink

Left factoring is a way to rewrite a CFG which further enables easier implementation of top-down parsing.

In general, if Aαβ1αβ2 are two A-productions, and the input begins with α, we won’t know whether to expand A to αβ1 or αβ2 unless we have an LL(|α|+1) parser.

Left factoring rewrites the two A-productions into:

AαAAβ1β2

and if we can choose between β1 and β2 by, say an LL(1) parser, we are on the right track.

That is to say, left factoring transforms a grammar into one more suitable for LL(k) parsing with smaller k.

AlgorithmPermalink

For each variable A, find the longest prefix αε common to two or more of A’s alternatives. Replace all of the A-productions Aαβ1αβ2αβnγ, by

AαAγAβ1β2βn

分类:

更新时间:

留下评论