Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to left factor a context-free grammar?

As I understand, in the following case left factoring is required to build top-down parser. But it's hard to understand how to do that? Can someone help me here? Thanks.

s = a | b
b = c d
c = (e | f) g
e = a | h
like image 498
Bee Avatar asked Mar 02 '12 20:03

Bee


People also ask

How do you do left factoring in grammar?

Left Factoring is a grammar transformation technique. It consists in "factoring out" prefixes which are common to two or more productions. Left Recursion is a property a grammar has whenever you can derive from a given variable (non terminal) a rhs that begins with the same variable, in one or more steps.

What is left recursion in context free grammar?

In terms of context-free grammar, a nonterminal is left-recursive if the leftmost symbol in one of its productions is itself (in the case of direct left recursion) or can be made itself by some sequence of substitutions (in the case of indirect left recursion).

What is left factoring write an algorithm for left factoring and explain with an example?

Left factoring transforms the grammar to make it useful for top-down parsers. In this technique, we make one production for each common prefixes and the rest of the derivation is added by new productions. Now the parser has only one production per prefix which makes it easier to take decisions.

What is the purpose of left factoring?

It is a process of factoring out the common prefixes of alternatives. It is used when it is not clear that which of the two alternatives is used to expand the non-terminal.


1 Answers

Every non-terminal is only referenced once here, so we can pull the entire grammar together in a single expression:

s = a | ((a | h | f) g d)

So we have two basic variations, the terminal a optionally followed by g then d, or else one of h or f always followed by g then d.

So we have

s =  b' | c'
b' = a | a g d
c' = (h | f) g d

or, pulling the common g d sequence into its own production

s =  b' | c'
b' = a | a e'
c' = (h | f) e'
e' = g d

We can then pull a up as a common starting symbol in b' by introducing an E (empty) option:

s =  b'' | c'
b'' = a (e' | E)
c' = (h | f) e'
e' = g d

The grammar is now unambiguous.

like image 177
500 - Internal Server Error Avatar answered Sep 16 '22 12:09

500 - Internal Server Error