Question

1. Design and code a state diagram to recognize the floating-point literals, integer literals, string literals...

1. Design and code a state diagram to recognize the floating-point literals, integer literals, string literals and variable names of your favorite programming language.

2. Write an EBNF or BNF for boolean expressions, assignment statements, and mathematical/logical operations in Java.

3. Find an example of a loop, show the steps to prove its correctness. Explain in detail why each step is necessary.

4. In a letter to the editor of CACM, Rubin (1987) uses the following code segment as evidence that the readability of some code with gotos is better than the equivalent code without gotos. This code finds the first row of an n by n integer matrix named x that has nothing but zero values.

for (i = 1; i <= n; i++) {

for (j = 1; j <= n; j++)

if (x[i][j] != 0)

goto reject;

println (’First all-zero row is:’, i);

break;

reject:

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. State Diagram:

2. EBNF or BNF in Java

<constant expression> ::= <expression>

<expression> ::= <assignment expression>

<assignment expression> ::= <conditional expression> | <assignment>

<assignment> ::= <left hand side> <assignment operator> <assignment expression>

<left hand side> ::= <expression name> | <field access> | <array access>

<assignment operator> ::= = | *= | /= | %= | += | -= | <<= | >>= | >>>= | &= | ^= | |=

<conditional expression> ::= <conditional or expression> | <conditional or expression> ? <expression> : <conditional expression>

<conditional or expression> ::= <conditional and expression> | <conditional or expression> || <conditional and expression>

<conditional and expression> ::= <inclusive or expression> | <conditional and expression> && <inclusive or expression>

<inclusive or expression> ::= <exclusive or expression> | <inclusive or expression> | <exclusive or expression>

<exclusive or expression> ::= <and expression> | <exclusive or expression> ^ <and expression>

<and expression> ::= <equality expression> | <and expression> & <equality expression>

<equality expression> ::= <relational expression> | <equality expression> == <relational expression> | <equality expression> != <relational expression>

<relational expression> ::= <shift expression> | <relational expression> < <shift expression> | <relational expression> > <shift expression> | <relational expression> <= <shift expression> | <relational expression> >= <shift expression> | <relational expression> instanceof <reference type>

<shift expression> ::= <additive expression> | <shift expression> << <additive expression> | <shift expression> >> <additive expression> | <shift expression> >>> <additive expression>

<additive expression> ::= <multiplicative expression> | <additive expression> + <multiplicative expression> | <additive expression> - <multiplicative expression>

<multiplicative expression> ::= <unary expression> | <multiplicative expression> * <unary expression> | <multiplicative expression> / <unary expression> | <multiplicative expression> % <unary expression>

<cast expression> ::= ( <primitive type> ) <unary expression> | ( <reference type> ) <unary expression not plus minus>

<unary expression> ::= <preincrement expression> | <predecrement expression> | + <unary expression> | - <unary expression> | <unary expression not plus minus>

<predecrement expression> ::= -- <unary expression>

<preincrement expression> ::= ++ <unary expression>

<unary expression not plus minus> ::= <postfix expression> | ~ <unary expression> | ! <unary expression> | <cast expression>

<postdecrement expression> ::= <postfix expression> --

<postincrement expression> ::= <postfix expression> ++

<postfix expression> ::= <primary> | <expression name> | <postincrement expression> | <postdecrement expression>

<method invocation> ::= <method name> ( <argument list>? ) | <primary> . <identifier> ( <argument list>? ) | super . <identifier> ( <argument list>? )

<field access> ::= <primary> . <identifier> | super . <identifier>

<primary> ::= <primary no new array> | <array creation expression>

<primary no new array> ::= <literal> | this | ( <expression> ) | <class instance creation expression> | <field access> | <method invocation> | <array access>

<class instance creation expression> ::= new <class type> ( <argument list>? )

<argument list> ::= <expression> | <argument list> , <expression>

<array creation expression> ::= new <primitive type> <dim exprs> <dims>? | new <class or interface type> <dim exprs> <dims>?

<dim exprs> ::= <dim expr> | <dim exprs> <dim expr>

<dim expr> ::= [ <expression> ]

<dims> ::= [ ] | <dims> [ ]

<array access> ::= <expression name> [ <expression> ] | <primary no new array> [ <expression>]

3.

Loop invariants

Induction is the method of choice for analyzing properties of algorithms with loops. A typical such algorithm:

Initialization The algorithm initializes some variables based on the inputs.

Loop Then the loop starts. Each iteration of the loop changes variables ac- cording to some loop instructions until a guard condition fails.

Produce Output When the guard condition fails, the algorithm then pro- duces some output based on the values of the variables.

There are many things we want to know about such algorithms. How does the output depend on the input, and why does it meet the correctness property in the problem specification? Does the loop always terminate? How many iterations might the loop make, and how much total time does it take?

All of these are about global behavior of the algorithm, properties of the entire run. But what the algorithm description gives us is local behavior, what happens in each step. Loop invariants allow us to bridge this gap.

To understand the global behavior, when all we are given explicitly is the local, we usually need to prove something stronger than what happens at the end. We need to understand what happens in the middle, something about the values of the variables after t steps. Sometimes there’s an explicit formula telling us this. But more typically, the art of finding the right loop invariant is to get a property that is simple enough to understand but captures the reason why the algorithm is making progress towards terminating with the correct answer.

So far we’ve been very abstract. Let’s translate this to a specific example.

Here’s a very simple algorithm that computes the ceiling of the log of x.

LogRounded (x: integer): integer

1. i ← 0

2. y ← 1

3. IF x ≤ 0then Return “error”

4. While y < x do:

5. ii + 1

6. y ← 2 ∗ y

7. Return i

We want to show that the program outputs log2x rounded up to the nearest integer. But to do this we need to prove a stronger claim about the values y and i have throughout the loop. In this case, there is an explicit formula. Each time we go through the loop, i is incremented, and y doubles. So y will always be a power of 2. We will show y = 2i at all times. Note that this loop invariant isn’t just a translation of the definition of correctness. But it really is why the algorithm was designed the way it was, the explanation for why we are following these steps.

The proof breaks down into three basic parts, based on the three parts of a loop: Initialization, Looping, Returning Output.

Stating the invariant In this example, we are claiming that, if the loop exe- cutes at least t times, after the t’th iteration, y = 2i.

The base case Before the loop starts, i.e., after t = 0 iterations, y = 1 and i = 0. Since 20 = 1, the invariant is true at the start.

Induction step Let yB and iB be the values of y and i at the end of the t’th interation (i.e, at the beginning of the t + 1’st iteration). B stands for “before” the t + 1’st iteration. Let yA and iA be the values of y and i at the end of the t+1’st interation. A stands for “after” the t+1’st iteration.

Assume the loop invariant holds at the end of the t’th iteration, that is, that yB = 2iB . This is the induction hypothesis.

In that iteration, y is doubled and i is incremented, so the new value of y is yA = 2yB and the new value of i is iA = iB + 1.

Then, from the algorithm, we have

yA = 2yB

= 2 ∗ 2iB

= 2iB+1

= 2iA

Thus, at the end of the t + 1’st iteration, y = 2i as desired.

Summary of induction argument Since the invariant is true after t = 0 iterations, and if it is true after t iterations it is also true after t + 1 iterations, by induction, it will remain true after any number of iterations until the loop ends.

Using the invariant to prove correctness The guard condition is that y <x. Consider the last iteration of this loop, and as before let yB, iBbe the values of y and i before this iteration, and yA= 2yBand iA= iB+ 1 be the values after this iteration. Before the last iteration , yB < x because the guard condition is still true. Afterwards, the new value of y, yAhas yAx because this is the last time through the loop, which means the guard condition is false when it ends. Since yA= 2yB, yB < x ≤ 2yB. Using the loop invariant, yB= 2iB . So 2iB < x ≤ 2iB +1. Since iB+1 = iA, this means 2iA−1  < x ≤ 2iA . Taking logs, iA− 1 < log2x < iA. Thus, iAis the closest integer greater than log2 x. The algorithm then returns iA= |log2 x|.

4. Question is incomplete.

Add a comment
Know the answer?
Add Answer to:
1. Design and code a state diagram to recognize the floating-point literals, integer literals, string literals...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use the JAVA code attached as an input to the program that must be created...

    Please use the JAVA code attached as an input to the program that must be created IN JAVA. Instructions of the program: java code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * */ import java.util.Random; public class Rand_Z3_Exp { /** * @param args the command line arguments */ public static void main(String[] args) {...

  • Please use the JAVA code attached as an input to the program that must be created IN JAVA. Instru...

    Please use the JAVA code attached as an input to the program that must be created IN JAVA. Instructions of the program: java code: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * */ import java.util.Random; public class Rand_Z3_Exp { /** * @param args the command line arguments */ public static void main(String[] args) {...

  • IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. •...

    IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. • Declare a global enum variable that will support 10 values – each representing an ant colony {A, B, C, D, E, F, G, H, I, J}. • In the main function, o Declare a 2-dimensional array of size the global integer constant, SIZE. The number of rows and columns should be equal to SIZE, which would make this a square matrix. This array will...

  • Here is the code I have so far. I'm trying to figure out how to implement...

    Here is the code I have so far. I'm trying to figure out how to implement a boolean and use precedence. The 2nd expression should be 14 but it comes out as 28 so I'm definitely not understanding. #include <stack> #include <iostream> #include <string> using namespace std; // Function to find precedence of // operators. int precedence(char op) {    if (op == '+' || op == '-')        return 1;    if (op == '*' || op ==...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT