Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the syntax of C, write a recursive descent subprogram that corresponds to

ID: 3808118 • Letter: U

Question

Using the syntax of C, write a recursive descent subprogram that corresponds to the following EBNF production (taken from the specification of Java):

<local_variable_declaration_statement>

[final] <type> <variable_declarator> {,<variable_declarator> };

{, }, [, and ] are metasymbols. Assume that the token codes for final, the comma, and the semicolon are FINAL_CODE, COMMA_CODE, and SEMICOLON_CODE, respectively. Also assume that recursive - descent subprograms named type and variable_declarator already exist.

Explanation / Answer

According to the question, write recursive descent subprogram. I want to write a recursive descent subprogram that corresponds to the EBNF rule. { and } are metasymbols unless they are enclosed in quotes. Here is the following code:

<local_variable_declaration_statement>

[final] <type> <variable_declarator> {,<variable_declarator> };


<initializer> -> "{" <designator> { <designator> } = <expression> "}"

From my understanding from reading my book..for this code:
<expr> -> <term> { ( + | - ) <term> } the recursive descent subprogram would be

int expr ( ) {

/* Parse the first term */

int success =   term ( ) ;

if ( success )
{
// Got a term! Could be a +/- next, if so, must be a term following!
while(success && sign () )
{
// Got a sign too! Must be a term following.
success = term ();
}
}
return success;
}

/* As long the next token is + or -, call lex to get the next token, and parse the next

term */

while (nextToken == FINAL_CODE || nextToken == COMMA_CODE || nextToken== SEMICOLON_CODE) {

lex ( );
term ( );
}
}


I have discuss first the simpler problem of recognizing a sequence of tokens. Recognition is like parsing, except that no parse tree is built. Instead, the result of recognition is a simple boolean value: true if the input stream is a syntactically correct program, and false otherwise. Turning a recognizer into a parser is simply a matter of adding tree-building code to each method, and returning the tree rather than a boolean value.

Each nonterminal in the EBNF is represented by a single method (or function, if you are using a non-object-oriented language) in the recursive descent parser. Typically, the current token is passed in as a parameter, though it may instead be maintained as a global variable. We will assume a Token type, with accessible fields type (containing a flag that indicates to which of several categories the token belongs) and value (containing the actual characters that make up the token). We will further assume that we have a Tokenizer class with a method nextToken() to get the next Token from some input stream.

When a method for a nonterminal succeeds, it should "consume" (use up) exactly those tokens that constitute the nonterminal. If the method fails (does not recognize the expected nonterminal), it should return without consuming any tokens--this may involve "putting back" some tokens. Failure is not, in general, an error, since we may need to test for several nonterminals before finding the correct one.


For more understanding, here I have express the same method using another way.

code :
<initializer> -> "{" <designator> { <designator> } = <expression> "}"

int initializer ()
{
// "{"
int success = openBrace ();
if ( success )
{
// <designator>
if ( success = designator () )
{
// { <designator> }
// Eat any more designators. Ignore failures as we've got the first one.
while ( designator () ) {};
// =
if ( success = equals () )
{
// <expression> "}"
// Got the equals, must be an expression then a close brace.
success = expression () && closeBrace ();
}

}
}
return success;
}

Here I have added the code as per the assignments, if any doubt related to the code I am happy to give your answer, please let me know for any doubt, my mail id pratap_mishra@outlook.com.