Question

In: Computer Science

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)

Solutions

Expert Solution

EBNF rules for while statement :

<while_stmt> -> WHILE ‘(‘ ( <arith_expr> | <logic_expr> ) ‘)’

<block> <block> -> <stmt> | ‘{‘ <stmt> {<stmt>} ‘}’

Source Code of the parsing of the given example :

import java.util.*;

public class ParseString

{

          public static void main(String args[])

          {

                       StringTokenizer st1 = new StringTokenizer("do { if ( number % 2 == 0 ) even ++ ; number = number + 1 ; } while ( number <= 10 ) ", " ");    // StringTokenizer Class allows string to break into tokens.

                       int count=0;

                       while (st1.hasMoreTokens())   // It test if more tokens are available or not.

                       {

                                count++;

                                System.out.println("Token "+count+" "+st1.nextToken()); //Returns next token in the string.

                        }

                       System.out.println("Total Tokens :: "+count);

          }

}

Output (Snapshot) :


Related Solutions

Very easy: Write an EBNF rule that describes the following for statement in Java for (...
Very easy: Write an EBNF rule that describes the following for statement in Java for ( counter = 1; counter <= 10; counter ++) { sum=sum+1; } Then test your EBNF rule in Java as a recursive-descent subprogram
Write EBNF descriptions for the following: • a Java For each statement Assume that EBNF descriptions...
Write EBNF descriptions for the following: • a Java For each statement Assume that EBNF descriptions for statement(s), type, array, white space are given. (statmnts, type, array, and whitespace respectively) You have to create EBNF descriptions for For each statement and also variable. Use the following Java variable naming rules to create EBNF description for variable. o All variable names must begin with a letter of the alphabet, an underscore ( _ ) , or a dollar sign ( $...
Write the EBNF for mini Java language based of the following information: Data Types Integer Int...
Write the EBNF for mini Java language based of the following information: Data Types Integer Int Long Double Boolean Char References Complex Data Structures Arrays int v[30]; Classes member variables class Name { int a; char b; char name[25]; } Methods Return data type Primitive data type Void Method Name Parameter list Could be empty Statement Block { Variable declarations Executable Statements } Program Variable Declarations Class Definitions Methods Only one method named "main" but must have one method named...
Q1)   Convert the following set of BNF rules to a single EBNF rule. <E> --> <E>...
Q1)   Convert the following set of BNF rules to a single EBNF rule. <E> --> <E> + <T> | <E> - <T> | <T> Q2)   Briefly explain how the expected type and actual type of <expr>             in the following two BNF rules are determined: <assign> --> <var> = <expr> (Rule 1) <expr> --> <var> + <var> (Rule 2)
Write a non recursive method to insert into an AVL tree in Java
Write a non recursive method to insert into an AVL tree in Java
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; //...
In Java, write a recursive function that accepts a string as its argument and prints the...
In Java, write a recursive function that accepts a string as its argument and prints the string in reverse order. Demonstrate the function in a driver program.
In java, Write a recursive function to calculate the sum of the nodes only on even...
In java, Write a recursive function to calculate the sum of the nodes only on even levels of the subtree. please do not add any parameters to do this function. private int sumEvenLevels(Node current){ //you can only pass in root. //code }
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT