programming4us
programming4us
DESKTOP

Algorithms for Compiler Design: Implementation

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
7/24/2010 7:53:22 PM
4.2 IMPLEMENTATION
A top-down parser can be implemented by writing a set of recursive procedures to process the input. One procedure will take care of the left-most derivations for each nonterminal while processing the input. Each procedure should also provide for the storing of the input pointer in some local variable so that it can be reset properly when the parser backtracks. This implementation, called a "recursive descent parser," is a top-down parser for the above-described grammar that can be implemented by writing the following set of procedures:
S( )
{
if (input =='a' )
{
advance( );
if (A( ) != error)
if (input =='b')
{ advance( );
if (input == endmarker)
return(success);
else
return(error);
}
else
return(error);
}
else
return(error);
}

A( )
{
if (input =='c')
{
advance( );
if (input == 'd')
advance( ); } else return(error); } main( ) { Append the endmarker to the string w to be parsed;
Set the input pointer to the left most token of w;
if ( S( ) != error)
print f ("Successful completion of the parsing");
else
printf ("Failure");
}

where advance() is a routine that, when called, advances the input's pointer to the next occurrence of the symbol w.


Caution�

In a backtracking parser, the order in which alternatives are tried affects the language accepted by the parser. For example, in the above parser, if a production A c is tried before A cd, then the parser will fail to accept the string w = acdb, because it first expands S, as shown in Figure 1.

Click To expand
Figure 1: The parser first expands S and fails to accept w = acdb.

The first input symbol matches the left-most leaf; and therefore, the parser will advance the pointer to c and consider the nonterminal A for expansion in order to obtain the tree shown in Figure 2.

Click To expand
Figure 2: The parser advances to c and considers nonterminal A for expension.

The second input symbol also matches. Therefore, the parser will advance the pointer to d, the third input symbol, and consider the next leaf, labeled b in Figure 2. It finds that there is no match; and therefore, it will backtrack to S (as shown in Figure 2 by the thick arrow). But since there is no alternative to S that can be tried, the parser will return failure. Because the point of mismatch is the descendent of a node labeled by S, the parser will backtrack to S. It cannot backtrack to A. Therefore, the parser will not accept the string acdb. Whereas, if the parser tries the alternative A cd first and A c second, then the parser is capable of accepting the string acdb as well as acb because, for the string w = acb, when the parser encounters a mismatch, it is at a node labeled by d, which is a descendent of a node labeled by A. Hence, it will backtrack to A and try A c, and end up in the parse tree for acb. Hence, we conclude that the order in which alternatives are tried in a backtracking parser affect the language accepted by the compiler or parser.

EXAMPLE 1
Start example

Consider a grammar S aa | aSa. If a top-down backtracking parser for this grammar tries S aSa before S aa, show that the parser succeeds on two occurrences of a and four occurrences of a, but not on six occurrences of a.

End example

In the case of two occurrences of a, the parser will first expand S, as shown in Figure 2.

Click To expand
Figure 2: The parser first expands S.

The first input symbol matches the left-most leaf. Therefore, the parser will advance the pointer to a second a and consider the nonterminal S for expansion in order to obtain the tree shown in Figure 3.

Click To expand
Figure 3: The parser advances the pointer to a second occurrence of a.

The second input symbol also matches. Therefore, the parser will consider the next leaf labeled S and expand it, as shown in Figure 4.

Click To expand
Figure 4: The parser expands the next leaf labeled S.

The parser now finds that there is no match. Therefore, it will backtrack to S, as shown by the thick arrow in Figure 5. The parser then continues matching and backtracking, as shown in Figures 6 through 11, until it arrives at the required parse tree, shown in Figure 12.

Click To expand
Figure 5: The parser finds no match, so it backtracks.
Click To expand
Figure 6: The parser tries an alternate aa.
Click To expand
Figure 7: There is no further alternate of S that can be tried, so the parser will backtrack one more step.
Click To expand
Figure 8: The parser again finds a mismatch; hence, it backtracks.
Click To expand
Figure 9: The parser tries an alternate aa.
Click To expand
Figure 10: Since no alternate of S remains to be tried, the parser backtracks one more step.
Click To expand
Figure 11: The parser tries an alternate aa.
Click To expand
Figure 12: The parser arrives at the required parse tree.

Now, consider a string of four occurrences of a. The parser will first expand S, as shown in Figure 13.

Click To expand
Figure 13: The parser first expands S.

The first input symbol matches the left-most leaf. Therefore, the parser will advance the pointer to a second a and consider the nonterminal S for expansion, obtaining the tree shown in Figure 14.

Click To expand
Figure 14: The parser advances the pointer to a second occurrence of a.

The second input symbol also matches. Therefore, the parser will consider the next leaf labeled by S and expand it, as shown in Figure 15.

Click To expand
Figure 15: The parser considers the next leaf labeled by S.

The third input symbol also matches. So, the parser moves on to the next leaf labeled by S and expands it, as shown in Figure 16.

Click To expand
Figure 16: The parser matches the third input symbol and moves on to the next leaf labeled by S.

The fourth input symbol also matches. Therefore, the next leaf labeled by S is considered. The parser expands it, as shown in Figure 17.

Click To expand
Figure 17: The parser considers the fourth occurrence of the input symbol a.

Now it finds that there is no match. Therefore, it will backtrack to S (Figure 18) and continue backtracking, as shown in Figures 19 through 26, until the parser finally arrives at the successful generation of a parse tree for aaaa in Figure 27.

Click To expand
Figure 18: The parser finds no match, so it backtracks.
Click To expand
Figure 19: The parser tries an alternate aa.
Click To expand
Figure 20: No alternate of S can be tried, so the parser will backtrack one more step.
Click To expand
Figure 21: Again finding a mismatch, the parser backtracks.
Click To expand
Figure : The parser then tries an alternate.
Click To expand
Figure 23: No alternate of S remains to be tried, so the parser will backtrack one more step.
Click To expand
Figure 24: The parser again finds a mismatch; therefore, it backtracks.
Click To expand
Figure 25: The parser tries an alternate aa.
Click To expand
Figure 26: The parser then tries an alternate aa.

Figure 27: The parser successfully generates the parse tree for aaaa.

Now consider a string of six occurrences of a. The parser will first expand S, as shown in Figure 28.

Click To expand
Figure 28: The parser expands S.

The first input symbol matches the left-most leaf. Therefore, the parser will advance the pointer to the second a and consider the nonterminal S for expansion. The tree shown in Figure 29 is obtained.

Click To expand
Figure 29: The parser matches the first symbol, advances to the second occurrence of a, and considers S for expansion.

The second input symbol also matches. Therefore, the parser will consider next leaf labeled S and expand it, as shown in Figure 30.

Click To expand
Figure 30: The parser finds a match for the second occurrence of a and expands S.

The third input symbol also matches, as do the fourth through sixth symbols. In each case, the parser will consider next leaf labeled S and expand it, as shown in Figures 31 through 34.

Click To expand
Figure 31: The parser matches the third input symbol, considers the next leaf, and expands S.
Click To expand
Figure 32: The parser matches the fourth input symbol, considers the next leaf, and expands S.
Click To expand
Figure 33: A match is found for the fifth input symbol, so the parser considers the next leaf, and expands S.
Click To expand
Figure 34: The sixth input symbol also matches. So the next leaf is considered, and S is expanded.

Now the parser finds that there is no match. Therefore, it will backtrack to S, as shown by the thick arrow in Figure 35.

Click To expand
Figure 35: No match is found, so the parser backtracks to S.

Since there is no alternate of S that can be tried, the parser will backtrack one more step, as shown in Figure 36. This procedure continues (Figures 37 through 43), until the parser tries the sixth alternate aa (Figure 44) and fails to find a match.

Click To expand
Figure 36: The parser backtracks one more step.
Click To expand
Figure 37: The parser tries the alternate aa.
Click To expand
Figure 38: Again, a mismatch is found. So, the parser backtracks.
Click To expand
Figure 39: No alternate of S remains, so the parser will back-track one more step.
Click To expand
Figure 40: The parser tries an alternate aa.
Click To expand
Figure 41: Again, a mismatch is found. The parser backtracks.
Click To expand
Figure 42: The parser then tries an alternate aa.
Click To expand
Figure 43: A mismatch is found, and the parser backtracks.
Click To expand
Figure 44: The parser tries for the alternate aa, fails to find a match, and cannot generate the parse tree for six occurrences of a.

Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
programming4us
 
 
programming4us