Choose a statement for a programming language of your choice and write a BNF production that defines the syntax of that statement. Clearly distinguish between the terminal and nonterminals symbols.
Then write a Java method that will parse that statement using recursive descent. You may assume that a method getNextToken is available to get the next terminal symbol and that there are methods available to parse each of the nonterminals.
<expression> ::= <number> | "(" <expression>
<Operator> <expression> ")"
<Operator> ::= "+" | "-" | "*" | "/"
Where <number> refers to any non negative real number. An example of a fully parenthesized expression is " (((34-17*8) + (2*7))".
Since every operator corresponds to pair of parentheses, there is no ambiguity about the order in which the operators are to be applied. Suppose we want a program that will read and evaluate such expressions.
To apply recursive decent parsing parsing, we need a subroutine for each rule in the grammar.
Corresponding to the rule for <operator>, we get a subroutine that reads an operator. The operator can be a choice of any of four things. Any other input will be an error.
/**
* If the next character in the input is one of the legal operators, read it and return it, otherwise, through a ParseError.
*/
Static char getoperator () throws ParseError{
TextIO.skipBlanks();
Char op = textIO.peek(); //look ahead at the next char, without reading it
if (op== '+' || op=='-' || op == '*' || op == '/' ) {
TextIO.getAnyChar(); // read the operation, to remove it from the input
return op;
}
else if
(op=='\n') through new ParseError ("Missing operator at end of line");
else
through new ParseError("Missing Operator. Found \ "" + op + "\" instead of +, -,*, or /. ");
}// end getOperator().
}
/**
* An object of type ParseError represents a syntax error found in the user's input.
*/
Private static class ParseError extends Exception {
ParseError(Stiring message){
Super(message);
}
}//end of nested class ParseError.
Choose a statement for a programming language of your choice and write a BNF production that...
(20 pts) To understand the value of recursion in a programming language: implement the binary search algorithm first as a recursive function and again using a conditional loop. Your program should create an array of characters holding the letters ‘A’ – ‘Z’ and find the index in the array where the letter ‘K’ is stored. You may use any programming language that supports recursion. (5pts) Define syntax and semantics and give an example. (5pts) Why is it important for a...
(8) (3 marks) Write BNF grammar rules for a language that is comprised only of sentences described as follows: symbol, fol symbols orjust onéx symbol, A sequence of one or more occurrences of an a lowed by either zero or morez followed by a sequence of one or more b symbols. (9) (1 mark) The following fragment of grammar describes the syntax of the exponentiation operator. What is the associativity ot the operator as detined here? factor | expr factor...
Write the code in java programming language To get some practice with recursion. You can do all this in one driver program. Write a recursive method to compute the result of the Fibonacci sequence: Fibonacci(N) = Fibonacci(N -1) + Fibonacci(N-2) N == 0 is 0 N == 1 is 1 Testing: Display the result for Fibonacci(N) and the number of function calls for N = 2, 5 and 10.
Using your choice of language, C#, Java, or pseudocode, write a recursive method that receives only one parameter, an int n, and prints the cubed of the numbers from 1 to n3 in ascending order. Do not use any global variables or stacks. For example, passing this method the value 5, the output would be “1 8 27 64 125”. Because 13 = 1, 23 = 8, 33 = 27, 43 = 64 and so on
Write a parser program for cSub using the method of recursive descent. The main program here will effectively be the main program of the compiler as a whole. The input to the program will be a Csub source file, specified on the command line. The program will construct a parse tree for the program, with one interior node for every nonterminal in the derivation of the program, and one leaf node for each of the id, num, and real tokens...
In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...
Q 4(b) 6 Marks] Write the code for a single Java class of your own choice that demonstrates the use of the following features in the Java language: Method overloadingg The use of super and this Identify clearly where in your code each of these points is being demonstrated Q 4(c) [6 Marks] Write an abstract Student class in Java which has a name, id_number, year and programme_code as member variables. Add a constructor to your class and a display()...
JAVA you will design and write a class that represents a real-world object of your choice. In the second part, you will write a program that demonstrates the use of the class. Part I: Select a "real-world" object that has not been used in class lecture and/or the textbook. The object you choose must be defined by at least: Have at least two characteristics (attributes). Have at least two behaviors (operations). The class that you write to represent the object...
Programming language: Java
Design an application that has three classes. The first one, named Book describes book object and has title and number of pages instance variables, constructor to initialize their values, getter methods to get their values, method to String to provide String representation of book object, and method is Long that returns true if book has over 300 pages, and returns false othewise. Provide also method char firstChard) that returns first cahracter of the book's title. The second...
Using a programming language of your choice, write a complete and fully functional program that uses reference and pointer types to swap two integer numbers. The two numbers are read in separately by the program’s user. Use a proper prompt for each number read in. a. Use one function that uses formal parameter reference type to swap the two numbers b. Use another function that uses formal parameter pointer type to swap the two numbers. c. In the main or...