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

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
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 }
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...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
write a recursive method that returns the product of all elements in java linked list
write a recursive method that returns the product of all elements in java linked list
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT