Search
Desktop

Algorithms for Compiler Design: Implementation

Published 7 min readBy Programming4us Editorial Team
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.


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.


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

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.


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


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.


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.


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.


Figure 5: The parser finds no match, so it backtracks.


Figure 6: The parser tries an alternate aa.


Figure 7: There is no further alternate of S that can be tried, so the parser will backtrack one more step.


Figure 8: The parser again finds a mismatch; hence, it backtracks.


Figure 9: The parser tries an alternate aa.


Figure 10: Since no alternate of S remains to be tried, the parser backtracks one more step.


Figure 11: The parser tries an alternate aa.


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.


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.


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.


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.


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.


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.


Figure 18: The parser finds no match, so it backtracks.


Figure 19: The parser tries an alternate aa.


Figure 20: No alternate of S can be tried, so the parser will backtrack one more step.


Figure 21: Again finding a mismatch, the parser backtracks.


Figure : The parser then tries an alternate.


Figure 23: No alternate of S remains to be tried, so the parser will backtrack one more step.


Figure 24: The parser again finds a mismatch; therefore, it backtracks.


Figure 25: The parser tries an alternate aa.


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.


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.


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.


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.


Figure 31: The parser matches the third input symbol, considers the next leaf, and expands S.


Figure 32: The parser matches the fourth input symbol, considers the next leaf, and expands S.


Figure 33: A match is found for the fifth input symbol, so the parser considers the next leaf, and expands S.


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.


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.


Figure 36: The parser backtracks one more step.


Figure 37: The parser tries the alternate aa.


Figure 38: Again, a mismatch is found. So, the parser backtracks.


Figure 39: No alternate of S remains, so the parser will back-track one more step.


Figure 40: The parser tries an alternate aa.


Figure 41: Again, a mismatch is found. The parser backtracks.


Figure 42: The parser then tries an alternate aa.


Figure 43: A mismatch is found, and the parser backtracks.


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.