Question

In: Computer Science

Show a trace of the recursive descent parser given in Section4.4.1 for the string a *...

Show a trace of the recursive descent parser given in Section4.4.1 for the string a * (b + c).

Please give detailed line by line explanation of the answer below.

Next token is: 11, Next lexeme is a

Enter <expr>

Enter <term>

Enter <factor>

Next token is: 23, Next lexeme is *

Exit <factor>

Next token is: 25, Next lexeme is (

Enter <factor>

Next token is: 11, Next lexeme is b

Enter <expr>

Enter <term>

Enter <factor>

Next token is: 21, Next lexeme is +

Exit <factor>

Exit <term>

Next token is: 11, Next 8 is c

Enter <term>

Enter <factor>

Next token is: 26, Next lexeme is )

Exit <factor>

Exit <term>

Exit <expr>

Next token is: -1, Next lexeme is EOF

Exit <factor>

Exit <term>

Exit <expr>

Solutions

Expert Solution

There is a subprogram for each nonterminal in the grammar, which can parse sentences that can be generated by that nonterminal. EBNF is preferably suited for being the basis for a recursive descent parser, because EBNF minimizes the number of nonterminal

Assume we have a lexical analyzer named lex, which puts the next token code in nextToken. The coding process when there is only one RHS: For each terminal symbol in the RHS, compare it with the next input token; if they match, continue, else there is an error. For each nonterminal symbol in the RHS, call its associated parsing subprogram

Lexeme match a character pattern which is associated with lexical category called token

/* Function expr Parses strings in the language generated by the rule*/

void expr()

{

/* Parse the first term */

term();

/* If the next token is * or +, call lex to get the next token and parse the next term */

while (nextToken == ADD_OP || nextToken == SUB_OP){ lex(); term(); }

}

This routine does not detect errors .

Convention: Every parsing routine leaves the next token in nextToken

A nonterminal that has more than one RHS requires an initial process to determine which RHS it is to parse. The correct RHS is chosen based on the next token of input. The next token is compared with the first token that can be generated by each RHS until a match is found. If no match is found, it is a syntax error

/* term Parses strings in the language generated by the rule:*/

void term()

{

/* Parse the first factor */

factor();

/* As long as the next token is ( or ), next token and parse the next factor */

while (nextToken == MULT_OP || nextToken == DIV_OP)

{ lex();

factor();

}

} /* End of function term */

/* Function factor Parses strings in the language generated by the rule: -> id | () */

void factor()

{ /* Determine which RHS */

if (nextToken) == ID_CODE || nextToken == INT_CODE)

/* For the RHS id, just call lex */ lex(); /*

If the RHS is () – call lex to pass over the left parenthesis, call expr, and check for the right parenthesis */

else if (nextToken == LP_CODE)

{

lex();

expr();

if (nextToken == RP_CODE)

lex();

else error();

}

/* End of else if (nextToken == ... */

else error();

/* Neither RHS matches */


Related Solutions

Parse string java code Write a recursive program that can calculate the value of a given...
Parse string java code Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x Using index of for -/+
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
5-Write an EBNF rules that describes the following while statement of Java. Then, write the recursive-descent...
5-Write an EBNF rules that describes the following while statement of Java. Then, write the recursive-descent subprogram in Java or C/C++ for the EBNF rule. Please summit your source code and a screen shot of the parsing of the following examples. do { if ( number % 2 == 0 ) even ++; number=number+1; } while (number <= 10)
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Need code in c#.
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write...
java/netbeans Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns an ArrayList in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the ArrayList and then reverse the...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’...
c++ using recursive no loops (for ,while ..ect)not allowed Write a recursive function ‘bool palindrome(string s)’ that returns true if s is a palindrome and false if not. #5: Write a recursive function 'void reverse(string &word)' that reverses the given input string. string name = "damian"; reverse(name); cout << name << endl; //should display "naimad". #7: Write a function 'int numTwos(int n)' which returns the number of 2's in the base-4 expansion of n. cout << numTwos(2170) << endl; //...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT