How to leave a factor without contextual grammar?

As I understand it, in the following case, factoring is required to create a parser from top to bottom. But it’s hard to understand how to do this? Can someone help me here? Thanks.

s = a | b b = cd c = (e | f) g e = a | h 
+4
source share
1 answer

Each nonterminal is referenced only once here, so we can combine the whole grammar in one expression:

 s = a | ((a | h | f) gd) 

So, we have two main options: a terminal, optionally followed by g, then d, or one of h or f, always followed by g, then d.

So we have

 s = b' | c' b' = a | agd c' = (h | f) gd 

or by pulling the overall gd sequence into your own production

 s = b' | c' b' = a | ae' c' = (h | f) e' e' = gd 

Then we can pull up like a regular start character in b 'by entering the parameter E (empty):

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

Grammar is now unambiguous.

+6
source

Source: https://habr.com/ru/post/1399465/


All Articles