PDA corresponding to CFG Theorem Corresponding to any

  • Slides: 20
Download presentation
PDA corresponding to CFG Theorem: Corresponding to any CFG there exists a PDA accepting

PDA corresponding to CFG Theorem: Corresponding to any CFG there exists a PDA accepting the language generated by the CFG. Since an algorithm has already been discussed to convert the CFG in CNF, so the PDA can be constructed corresponding to the CFG. As the CFG in CNF generates all the nonnull words of the corresponding CFL, so accepting the null string (if it is contained in the CFL), can be managed separately. Following is an example in this regard

Example Consider the following CFG which is in CNF and does not generate the

Example Consider the following CFG which is in CNF and does not generate the null string S SB|AB A CC B b C a The corresponding PDA will be

a b RD 1 RD 2 ST C PH S AT ∆ B ∆

a b RD 1 RD 2 ST C PH S AT ∆ B ∆ PP S S A PH B PH C PH S PH A PH C RD 3

Example continued … Here the STACK alphabet = {S, A, B, C}, where the

Example continued … Here the STACK alphabet = {S, A, B, C}, where the TAPE alphabet ={a, b} Note: It may be noted that when the POP state is entered either a nonterminal is replaced by two nonterminals at the top of the STACK accommodating a production, or a nonterminal is popped out from the top of the stack and a READ state is entered to read a specified letter from the TAPE or else the machine crashes.

Example continued … The choice of path taken at POP state to accommodate the

Example continued … The choice of path taken at POP state to accommodate the word belonging to the CFL can be determined by the left most derivation of the word. Consider the word aab with its left most derivation, as follows

Example continued … Working-String Generation Production Used S AB CCB aa. B aab S

Example continued … Working-String Generation Production Used S AB CCB aa. B aab S AB A CC C a B b step 1 step 2 step 3 step 4 step 5

Example continued … First of all the START state is entered STACK ∆… TAPE

Example continued … First of all the START state is entered STACK ∆… TAPE aab∆… The PUSH S state is entered STACK S TAPE aab∆…

Example continued … The POP state is entered and to accommodate the production S

Example continued … The POP state is entered and to accommodate the production S AB, PUSH B and PUSH A states are entered. STACK AB TAPE aab∆… Then the POP state is entered and to accommodate the production A CC, PUSH C states are entered

Example continued … STACK CCB TAPE aab The POP state is entered and to

Example continued … STACK CCB TAPE aab The POP state is entered and to accommodate the production C a, READ 1 is entered and the letter a is read from the TAPE.

Example continued … STACK CB TAPE aab The POP state is entered and to

Example continued … STACK CB TAPE aab The POP state is entered and to accommodate the production C a, READ 1 state is entered and the letter a is read from the TAPE STACK B TAPE aab

Example continued … The POP state is entered and to accommodate the production B

Example continued … The POP state is entered and to accommodate the production B b, READ 2 state is entered and the letter b is read from the TAPE STACK TAPE aab

Example continued … The shown in the STACK indicates that there are no nonterminals

Example continued … The shown in the STACK indicates that there are no nonterminals in the working string and is read from the STACK which leads to READ 3 state where the is read from the TAPE and the ACCEPT state is entered which shows that the word aab is accepted by the PDA. Following is the table showing all the observations discussed above, for the word aab

Example continued … Left most derivation S AB CCB STATE STACK TAPE START PUSH

Example continued … Left most derivation S AB CCB STATE STACK TAPE START PUSH S POP PUSH B PUSH A POP PUSH C POP S B AB B CB CB aab aab aab

Example continued … a. CB aab READ 1 POP READ 2 POP READ 3

Example continued … a. CB aab READ 1 POP READ 2 POP READ 3 ACCEPT CB B B aab aab Following is an example of building the PDA corresponding to the given CFG

Example Consider the following CFG S XY X a. X | b. X |a

Example Consider the following CFG S XY X a. X | b. X |a Y Ya | Yb | a First of all, converting the CFG to be in CNF, introduce the nonterminals A and B as A a B b The following CFG is in CNF

Example continued … S XY X AX | BX |a Y YA | YB

Example continued … S XY X AX | BX |a Y YA | YB | a A a B b The PDA corresponding to the above CFG will be

a a b RD 3 RD 4 a RD 1 RD 2 ST Y

a a b RD 3 RD 4 a RD 1 RD 2 ST Y X PH S B A AT ∆ ∆ PP S PH Y PH X X PH A X RD 5 Y Y PH X PH B PH A PH Y PH B PH Y

Example continued … The word aaab can be generated as Working-String Generation Production Used

Example continued … The word aaab can be generated as Working-String Generation Production Used S XY step 1 AXY X AX step 2 a. XY A a step 3 aa. Y X a step 4 aa. YB Y YB step 5 aaa. B Y a step 6 aaab B b step 7

Example continued … STACK TAPE (ST) ∆ aaab (PH S) S aaab (PP) ∆

Example continued … STACK TAPE (ST) ∆ aaab (PH S) S aaab (PP) ∆ aaab (PH Y) Y aaab (PH X) XY aaab (PP) Y aaab (PH X)XY aaab (PHA)AXY aaab (PP) XY aaab (RD 3)XY aaab STACK (PP) Y (RD 1) Y (PP) ∆ (PH B) B (PH Y)YB (PP) B (RD 2) B (PP) ∆ (RD 4) ∆ (PP) ∆ TAPE aaab aabb aaab ∆

Summing Up PDA corresponding to CFG, Examples of PDA corresponding to CFG

Summing Up PDA corresponding to CFG, Examples of PDA corresponding to CFG