Skip to main content

Posts

Showing posts from 2020

Testing a Program – An Example

Question: Write a set of test cases – specific set of data – to properly test a relatively Simple program. Create a set of test data for the program - data the program must handle correctly to be considered a successful program. Here’s a description of the program: “The program reads three integer values from an input dialog. The three values represent the lengths of the sides of a triangle. The program displays a message that states whether the triangle is scalene, isosceles, or equilateral”. Code: The function triangle takes three integer parameters that are interpreted as the lengths of the sides of a triangle. It returns whether the triangle is equilateral (three lengths equal), isosceles (two lengths equal), scalene (no lengths equal), or invalid (impossible lengths). Evaluate your test cases: Evaluate your set of test cases by using it to answer the following questions. Give yourself one point for each “YES” answer. Do you have a test case that represents a

Code Optimization | Code Optimization Techniques

Code Optimization- Code Optimization is an approach to enhance the performance of the code. The process of code optimization involves- Eliminating the unwanted code lines Rearranging the statements of the code Advantages- The optimized code has the following advantages- Optimized code has faster execution speed. Optimized code utilizes the memory efficiently. Optimized code gives better performance. Code Optimization Techniques- Important code optimization techniques are- Compile Time Evaluation Common sub-expression elimination Dead Code Elimination Code Movement Strength Reduction 1. Compile Time Evaluation- Two techniques that falls under compile time evaluation are- A) Constant Folding- In this technique, As the name suggests, it involves folding the constants. The expressions that contain the operands having constant values at compile time are evaluated. Those express

Three Address Code | Examples

Three Address Code- Three Address Code is a form of an intermediate code. The characteristics of Three Address instructions are- They are generated by the compiler for implementing Code Optimization . They use maximum three addresses to represent any statement. They are implemented as a record with the address fields. General Form- In general, Three Address instructions are represented as- a = b op c Here, a, b and c are the operands. Operands may be constants, names, or compiler generated temporaries. op represents the operator. Examples- Examples of Three Address instructions are- a = b + c c = a x b Common Three Address Instruction Forms- The common forms of Three Address instructions are- 1. Assignment Statement- x = y op z and x = op y Here, x, y and z are the operands. op represents the operator. It assigns the result obtained after solving the right side expression of the a

Grammar Ambiguity | Check Ambiguous Grammar

Grammar Ambiguity- Before you go through this article, make sure that you have gone through the previous article on Ambiguous Grammar . There exists no algorithm to check whether any given grammar is ambiguous or not. This general decision problem is undecidable- “Whether a grammar is ambiguous or not?” This is because it can be shown that this problem is equivalent to Post Correspondence Problem. General Approach To Check Grammar Ambiguity- To check whether a given grammar is ambiguous or not, we follow the following steps- Step-01: We try finding a string from the Language of Grammar such that for the string there exists more than one- parse tree or derivation tree or syntax tree or leftmost derivation or rightmost derivation Step-02: If there exists at least one such string, then the grammar is ambiguous otherwise unambiguous. PROBLEMS BASED ON CHECKING WHETHER GRAMMAR IS AMBIGUOUS- Problem-01:

Ambiguous Grammar | Grammar in Automata

Before you go through this article, make sure that you have gone through the previous article on Types of Grammar in Automata. On the basis of number of derivation trees, grammars are classified as- Ambiguous Grammar Unambiguous Grammar 1. Ambiguous Grammar- A grammar is said to ambiguous if for any string generated by it, it produces more than one- Parse tree Or derivation tree Or syntax tree Or leftmost derivation Or rightmost derivation Example- Consider the following grammar- E → E + E / E x E / id Ambiguous Grammar This grammar is an example of ambiguous grammar. Any of the following reasons can be stated to prove the grammar ambiguous- Reason-01: Let us consider a string w generated by the grammar- w = id + id x id Now, let us draw the parse trees for this string w. Since two parse trees exist for string w, therefore the grammar is ambiguous. Reason-02: Let us cons