0% found this document useful (0 votes)
9 views40 pages

STQA Lab Manual

The document outlines practical exercises for software testing techniques including Boundary Value Analysis (BVA) and Equivalence Class Partitioning, detailing the theory, execution steps, and examples for each method. It also discusses Cyclomatic Complexity, providing a code example and methods to calculate independent paths. Additionally, it includes a section on designing independent paths using DD-paths.

Uploaded by

mangamanga1101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views40 pages

STQA Lab Manual

The document outlines practical exercises for software testing techniques including Boundary Value Analysis (BVA) and Equivalence Class Partitioning, detailing the theory, execution steps, and examples for each method. It also discusses Cyclomatic Complexity, providing a code example and methods to calculate independent paths. Additionally, it includes a section on designing independent paths using DD-paths.

Uploaded by

mangamanga1101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Faculty of Engineering & Technology

Software Testing and Quality Assurance (203105396)


B. Tech CSE 4th Year 7th Semester

PRACTICAL: 01

AIM: Design test cases using Boundary value analysis.

Theory: -
 Boundary Value Analysis (BVA) is a Black-Box testing
technique used to check the errors at the boundaries of an input
domain.

 It is used to test boundary values because the input values near


the boundary have higher chances of error. Boundary values are
those that contain the upper and lower limit of a variable.

 BVA mainly focuses on testing both valid and invalid input


parameters for a given range of a software component.

 Whenever we do the testing by boundary value analysis, the


tester focuses on, while entering boundary value whether the
software is producing correct output or not.

Steps: -
1. Identify the boundaries.
2. Create valid and invalid partitions based on boundary values.
3. Create test cases for valid and invalid partitions.

Execution: -
 First Example
Let's consider an example where we need to test a function that
processes age input, which is valid for ages between 18 and 65
(inclusive).

BOUNDARY VALUE ANALYSIS

Valid
Invalid Invalid
(min, min + 1, max – 1,
(min – 1) (max + 1)
max)

17 18, 19, 55, 56 57

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
Page| 1
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

Valid Test Cases: -


1. Enter the value 18 which is min value.
2. Enter the value 19 which is min + 1 value.
3. Enter the value 55 which is max – 1 value.
4. Enter the value 56 which is max value.

Invalid Test Cases: -


1. Enter the value 17 which is min – 1 value.
2. Enter the value 57 which is max + 1 value.

 Second Example
Assume the valid password length is between 8 and 20
characters (inclusive).

BOUNDARY VALUE ANALYSIS

Valid
Invalid Invalid
(min, min + 1, max – 1,
(min – 1) (max + 1)
max)

7 8, 9, 19, 20 21

Valid Test Cases: -


1. Enter a password of length 8 (min value).
2. Enter a password of length 9 (min + 1 value).
3. Enter a password of length 19 (max – 1 value).
4. Enter a password of length 20 (max value).

Invalid Test Cases: -


1. Enter a password of length 7 (min – 1 value).
2. Enter a password of length 21 (max + 1 value).

 Third Example
Assume the passing marks are between 40 and 100 (inclusive).

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
Page| 2
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

BOUNDARY VALUE ANALYSIS

Valid
Invalid Invalid
(min, min + 1, max – 1,
(min – 1) (max + 1)
max)

39 40, 41, 99, 101 101

Valid Test Cases: -


1. Enter the value 40 which is the min value.
2. Enter the value 41 which is the min + 1 value.
3. Enter the value 99 which is the max – 1 value.
4. Enter the value 100 which is the max value.

Invalid Test Cases: -


1. Enter the value 39 which is the min – 1 value.
2. Enter the value 101 which is the max + 1 value.

 Forth Example
Assume the valid name length is between 3 and 50 characters
(inclusive).

BOUNDARY VALUE ANALYSIS

Valid
Invalid Invalid
(min, min + 1, max – 1,
(min – 1) (max + 1)
max)

2 3, 4, 49, 50 51

Valid Test Cases: -


1. Enter a name with 3 characters which is the min value.
2. Enter a name with 4 characters which is the min + 1 value.
3. Enter a name with 49 characters which is the max – 1 value.
4. Enter a name with 50 characters which is the max value.

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
Page| 3
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

Invalid Test Cases: -


1. Enter a name with 2 characters which is the min – 1 value.
2. Enter a name with 51 characters which is the max + 1 value.

 Fifth Example
Assume the valid amount is between ₹100 and ₹5000
(inclusive).

BOUNDARY VALUE ANALYSIS

Valid
Invalid Invalid
(min, min + 1, max – 1,
(min – 1) (max + 1)
max)

99 100, 101, 4999, 5000 5001

Valid Test Cases: -


1. Enter the value $100 which is the min value.
2. Enter the value $101 which is the min + 1 value.
3. Enter the value $4999 which is the max – 1 value.
4. Enter the value $5000 which is the max value.

Invalid Test Cases: -


1. Enter the value $99 which is the min – 1 value.
2. Enter the value $5001 which is the max + 1 value.

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
Page| 4
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 02

AIM: Design test cases using Equivalence class partitioning.

Theory: -
 Equivalence class partitioning is black box testing
technique that divides the input data of a software unit into
partitions of equivalent data from which test cases can be
derived. Here test cases are designed to cover each
partition at least once.

 Each and every condition of particular partition works as


same as other.

 If a condition in a partition is valid, other conditions are


valid too. If a condition in a partition is invalid, other
conditions are invalid too.

 It helps to reduce the total number of test cases from


infinite to finite. The selected test cases from these groups
ensure coverage of all possible scenarios.

Case: -
1. If the range condition is given as an input, then one valid and
two invalid equivalence classes are defined.

2. If a specific value is given as input, then one valid and two


invalid equivalence classes are defined.

3. If a member of set is given as an input, then one valid and


one invalid equivalence class is defined.

4. If Boolean no. is given as an input condition, then one valid


and one invalid equivalence class is defined.

Execution: -
Enrollment No.: 210303105168
CHAUDHARY UMANGKUMAR BALUBHAI
Page| 5
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

 Test Case – I: Range

Teenager: 13 – 17 years

EQUIVALENCE CLASS PARTIONING


Valid
Invalid Invalid
(any value between min and
(any value <= min 1) (any value >= max + 1)
max of the given range)
<= 12 13 17 >= 18

Valid Test Cases: -


1. Pick any one input data from 13 – 17.

Invalid Test Cases: -


1. Pick any one input test data less than or equal to 12.
2. Pick any one input test data more than or equal to 18.

 Test Case – II: Specific Value

Aadhar Card = 12 digits

EQUIVALENCE CLASS PARTIONING


Invalid Invalid
Valid
(any value <= specific value (any value >= specific value
(specific value)
1) + 1)
<= 11 digits 12 digits >= 13 digits

Valid Test Cases: -


1. Enter 12-digit Aadhar card number.

Invalid Test Cases: -


1. Enter Aadhar card number which has less than or equal to 11
digits.
2. Enter Aadhar card number which has more than or equal to
13 digits.
Enrollment No.: 210303105168
CHAUDHARY UMANGKUMAR BALUBHAI
Page| 6
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

 Test Case – III: Member of Set

Numbers ending with a 0

EQUIVALENCE CLASS PARTITIONING

Valid Invalid
(any number ending with a 0) (any number not ending with a 0)
10, 20, 30… 38, 95, 17, etc.

Valid Test Cases: -


1. Enter any number ending with a 0.

Invalid Test Cases: -


1. Enter any number not ending with a 0.

 Test Case – IV: Boolean Number

Orange is orange in colour. => True

EQUIVALENCE CLASS PARTITIONING

Valid Invalid
(true/1 OR false/0; based on your statement) (true/1 OR false/0; based on your statement)

True/1 based on the given statement False/0 based on the given statement

Valid Test Cases: -


1. Enter the value true/1 [based on the given statement].

Invalid Test Cases: -


1. Enter the value false/0 [based on the given statement].

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
Page| 7
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 03

AIM: Design independent paths by calculating cyclomatic


complexity using date problem.

Theory: -
 Cyclomatic complexity of a code section is the quantitative
measure of the number of linearly independent paths in it.
 It is a software metric useful for structured or White box
testing.
 Mathematically, for a structured program, the directed graph
inside control flow is the edge joining two basic blocks of the
program as control may pass from first to second.
So, cyclomatic complexity M would be defined as,
M=E–N+2
E = the number of edges in the control flow graph
N = the number of nodes in the control flow graph

Code: -
#include <stdio.h>
int isLeapYear(int year) {
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
return 1;
} else {
return 0;
}
} else {
return 1;
}
} else {
return 0;
}
}
int main() {
int year, month, days;
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
Enrollment No.: 210303105168
CHAUDHARY UMANGKUMAR BALUBHAI
Page| 8
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

31};
printf("Enter year: ");
scanf("%d", &year);
printf("Enter month (1-12): ");
scanf("%d", &month);
if (month < 1 || month > 12) {
printf("Invalid month. Please enter a value between 1 and
12.\n");
return 1;
}
if (month == 2 && isLeapYear(year)) {
days = 29;
} else {
days = monthDays[month 1];
}
printf("Number of days in month %d of year %d is: %d\n",
month, year, days);
return 0;
}

Control Flow Graph:

Cyclomatic complexity of code using control flow graph is:


Enrollment No.: 210303105168
CHAUDHARY UMANGKUMAR BALUBHAI
Page| 9
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

Method 1:
Edge nodes + 2 => 34 30 + 2 = 6
Method 2:
Predicate nodes +1 = 5+1 = 6 (6,7,10,17,20 are predicate nodes)
Method 3:
Total regions = 6
(Here r1, r2, r3, r4, r5 & r6 are the all regions)
V(G) = 6 and is same by all the three methods.
Identification of Independent paths (Basic Path set) from the
control flow graph:
1) 1-6-7-8-9-10-11-12-16-26-30
2) 1-6-7-8-9-10-13-15-16-26-30
3) 1-6-7-17-18-19-26-30
4) 1-6-7-17-20-21-22-26-30
5) 1-6-7-17-20-23-24-26-30
6) 1-6-27-28-29-30

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 10
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 05

AIM: Design independent paths by taking DD-path using date


problem.

Control Flow Graph:

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 11
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

DD Path Graph for above Control Flow Graph:

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 12
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 04

AIM: Design test cases using Decision table.

Decision Table: -

Valid Cases: -
1. Case 5: The user inserts a valid card, enters a valid PIN, has
sufficient balance, the card is not rejected, does not need to
re-enter the PIN, and the ATM dispenses cash.

Invalid Cases: -
1. Case 1: The user inserts an invalid card, the card is rejected,
does not need to re-enter the PIN, and the ATM does not
dispense cash.

2. Case 2: The user inserts a valid card, enters an invalid PIN,


has fewer than three invalid PIN attempts, no balance info,
the card is not rejected, needs to re-enter the PIN, and the
ATM does not dispense cash.

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 13
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

3. Case 3: The user inserts a valid card, enters a valid PIN, has
three invalid PIN attempts, no balance info, the card is not
rejected, does not need to re-enter the PIN, and the ATM
does not dispense cash.

4. Case 4: The user inserts a valid card, enters a valid PIN, has
fewer than three invalid PIN attempts, insufficient balance,
the card is not rejected, does not need to re-enter the PIN, and
the ATM does not dispense cash.

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 14
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 06

AIM: Design independent paths by taking DD path using date


problem.

Theory: -
1. Selenium
 Functions and Features:
 Used for web application testing.
 Supports multiple browsers (Chrome, Firefox, Safari, etc.).
 Compatible with various platforms (Windows, Mac,
Linux).
 Integrates with several programming languages (Java, C#,
Python, Ruby, etc.).
 Provides Selenium WebDriver for browser automation.

 Advantages:
 Open-source and free to use.
 Wide community support and extensive documentation.
 Supports parallel test execution.
 Easily integrates with other tools (e.g., TestNG, JUnit).

 Disadvantages:
 Requires programming skills.
 Limited support for handling complex web elements.
 Initial setup and configuration can be time-consuming.

 Usage:
 Ideal for regression testing and cross-browser testing.
 Widely used in continuous integration/continuous delivery
(CI/CD) pipelines.

2. JUnit
 Functions and Features:
 Primarily used for Java applications.
 Provides annotations to identify test methods.
 Supports assertions to check expected results.
 Integrates well with build tools like Maven and Gradle.
Enrollment No.: 210303105168
CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 15
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

 Advantages:
 Simple and easy to use.
 Well suited for unit testing.
 Facilitates test driven development (TDD).

 Disadvantages:
 Limited to Java applications.
 Not suitable for testing non-Java applications.

 Usage:
 Best for unit testing and small-scale integration testing in
Java projects.

3. TestNG
 Functions and Features:
 Inspired by JUnit but offers more flexibility and features.
 Supports parallel testing and data driven testing.
 Provides detailed test reports and logs.
 Integrates well with Selenium and Jenkins.

 Advantages:
 Powerful and flexible testing framework.
 Supports a wide range of test configurations.
 Enhances test organization with groups and dependencies.

 Disadvantages:
 Requires more configuration compared to JUnit.
 Has a steeper learning curve for beginners.

 Usage:
 Suitable for complex test scenarios and large-scale testing
projects.
 Often used in combination with Selenium for web
application testing.

4. Cucumber

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 16
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

 Functions and Features:


 Uses Behavior Driven Development (BDD) approach.
 Allows writing tests in Gherkin language, which is easy to
read and write.
 Integrates with Selenium, Appium, and other testing tools.

 Advantages:
 Promotes collaboration between technical and non-
technical team members.
 Enhances readability and maintainability of test cases.
 Supports reusable step definitions.

 Disadvantages:
 Requires additional effort to write Gherkin scenarios.
 Slower execution compared to traditional test scripts.

 Usage:
 Ideal for BDD projects where collaboration and
communication are key.
 Suitable for web and mobile application testing.

5. Appium
 Functions and Features:
 Used for mobile application testing on iOS and Android.
 Supports native, hybrid, and mobile web applications.
 Based on Selenium WebDriver, providing similar features.

 Advantages:
 Open source and free to use.
 Supports multiple programming languages.
 Can automate any mobile app from any language.

 Disadvantages:
 Complex setup and configuration.
 Performance issues with large test suites.

 Usage:

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 17
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

 Best for cross platform mobile application testing.


 Suitable for regression testing on mobile devices.

6. Jenkins
 Functions and Features:
 An open-source automation server for CI/CD.
 Provides numerous plugins for integration with various
tools.
 Automates the build, test, and deployment processes.

 Advantages:
 Highly extensible with a large plugin ecosystem.
 Supports distributed builds for better performance.
 Facilitates continuous integration and delivery.

 Disadvantages:
 Can be resource intensive.
 Requires proper configuration and maintenance.

 Usage:
 Widely used in CI/CD pipelines to automate testing and
deployment.
 Integrates with tools like Selenium, JUnit, TestNG, and
more.

7. Katalon Studio
 Functions and Features:
 An all-in-one test automation solution.
 Supports web, API, mobile, and desktop application
testing.
 Provides a user-friendly interface and built in keywords
for test creation.

 Advantages:
 Easy to use, even for beginners.
 Comprehensive reporting and analytics.
 Integrates with CI/CD tools like Jenkins and Azure

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 18
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

DevOps.

 Disadvantages:
 Limited customization options compared to coding-based
frameworks.
 Performance issues with very large test suites.

 Usage:
 Ideal for teams looking for an out of the box solution.
 Suitable for a wide range of application testing needs.

8. Postman
 Functions and Features:
 Widely used for API testing.
 Offers a user-friendly interface for designing, testing, and
documenting APIs.
 Supports automated testing with Postman collections and
Newman CLI.

 Advantages:
 Simple and intuitive interface.
 Facilitates API documentation and sharing.
 Extensive community support and documentation.

 Disadvantages:
 Limited to API testing.
 Performance issues with very large collections.

 Usage:
 Best for API functional and regression testing.
 Suitable for developers and testers in API centric projects.

9. SoapUI
 Functions and Features:
 Used for testing SOAP and RESTful web services.
 Offers functional, security, and load testing.
 Provides comprehensive test creation and execution

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 19
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

features.

 Advantages:
 Supports complex and advanced API testing scenarios.
 Integrates with CI tools like Jenkins and Azure DevOps.
 Extensive support for data driven testing.

 Disadvantages:
 Can be complex for beginners.
 Requires a paid license for the Pro version to access
advanced features.

 Usage:
 Ideal for comprehensive API testing, including functional,
security, and performance testing.
 Suitable for projects with complex API interactions.

10. Robot Framework
 Functions and Features:
 A generic test automation framework.
 Uses keyword driven testing approach.
 Supports various external libraries (e.g., Selenium,
Appium).

 Advantages:
 Highly extensible and flexible.
 Supports both functional and acceptance testing.
 Encourages reusable and readable test cases.

 Disadvantages:
 Requires some initial learning to understand the keyword
driven approach.
 Performance issues with very large test suites.

 Usage:
 Suitable for a wide range of applications, including web,
mobile, and desktop.

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 20
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

 Often used in combination with other tools for


comprehensive test automation.
 Equivalence class partitioning is black box testing
technique that divides the input data of a software unit into
partitions of equivalent data from which test cases can be
derived. Here test cases are designed to cover each
partition at least once.

 Each and every condition of particular partition works as


same as other.

 If a condition in a partition is valid, other conditions are


valid too. If a condition in a partition is invalid, other
conditions are invalid too.

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 21
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 07

AIM: Write a program to Implement LALR Parsing in C.

Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void push(char *,int *,char);


char stacktop(char *);
void isproduct(char,char);
int ister(char);
int isnter(char);
int isstate(char);
void error();
void isreduce(char,char);
char pop(char *,int *);
void printt(char *,int *,char [],int);
void rep(char [],int);
struct action
{
char row[6][5];
};

const struct action A[12]=


{
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","emp","acc"},
{"emp","rc","sh","emp","rc","rc"},
{"emp","re","re","emp","re","re"},
{"sf","emp","emp","se","emp","emp"},
{"emp","rg","rg","emp","rg","rg"},
{"sf","emp","emp","se","emp","emp"},
{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","sl","emp"},
{"emp","rb","sh","emp","rb","rb"},
Enrollment No.: 210303105168
CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 22
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

{"emp","rb","rd","emp","rd","rd"},
{"emp","rf","rf","emp","rf","rf"}
};
struct gotol
{
char r[3][4];
};
const struct gotol G[12]=
{
{"b","c","d"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"emp","emp","emp"},
{"i","c","d"},
{"emp","emp","emp"},
{"emp","j","d"},
{"emp","emp","k"},
{"emp","emp","emp"},
{"emp","emp","emp"},
};

char ter[6]={'i','+','*',')','(','$'};
char nter[3]={'E','T','F'};
char states[12]={'a','b','c','d','e','f','g','h','m','j','k','l'};
char stack[100];
int top= 1;
char temp[10];
struct grammar
{
char left;
char right[5];
};
const struct grammar rl[6]=
{
{'E',"e+T"},
{'E',"T"},
{'T',"T*F"},

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 23
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

{'T',"F"},
{'F',"(E)"},
{'F',"i"},
};

void main()
{
char inp[80],x,p,dl[80],y,bl='a';
int i=0,j,k,l,n,m,c,len;
printf(" Enter the input :");
scanf("%s",inp);
len=strlen(inp);
inp[len]='$';
inp[len+1]='\0';
push(stack,&top,bl);
printf("\n stack \t\t\t input");
printt(stack,&top,inp,i);
do
{
x=inp[i];
p=stacktop(stack);
isproduct(x,p);
if(strcmp(temp,"emp")==0)
error();
if(strcmp(temp,"acc")==0)
break;
else
{
if(temp[0]=='s')
{
push(stack,&top,inp[i]);
push(stack,&top,temp[1]);
i++;
}
else
{
if(temp[0]=='r')
Enrollment No.: 210303105168
CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 24
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

{
j=isstate(temp[1]);
strcpy(temp,rl[j 2].right);
dl[0]=rl[j 2].left;
dl[1]='\0';
n=strlen(temp);
for(k=0;k<2*n;k++)
pop(stack,&top);
for(m=0;dl[m]!='\0';m++)
push(stack,&top,dl[m]);
l=top;
y=stack[l 1];
isreduce(y,dl[0]);
for(m=0;temp[m]!='\0';m++)
push(stack,&top,temp[m]);
}
}
}
printt(stack,&top,inp,i);
}
while(inp[i]!='\0');
if(strcmp(temp,"acc")==0)
printf(" \n accept the input ");
else
printf(" \n do not accept the input ");
}

void push(char *s,int *sp,char item)


{
if(*sp==100)
printf(" stack is full ");
else
{
*sp=*sp+1;
s[*sp]=item;
}
}

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 25
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

char stacktop(char *s)


{
char i;
i=s[top];
return i;
}
void isproduct(char x,char p)
{
int k,l;
k=ister(x);
l=isstate(p);
strcpy(temp,A[l 1].row[k 1]);
}
int ister(char x)
{
int i;
for(i=0;i<6;i++)
if(x==ter[i])
return i+1;
return 0;
}
int isnter(char x)
{
int i;
for(i=0;i<3;i++)
if(x==nter[i])
return i+1;
return 0;
}
int isstate(char p)
{
int i;
for(i=0;i<12;i++)
if(p==states[i])
return i+1;
return 0;
}

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 26
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

void error()
{
printf(" error in the input ");
exit(0);
}
void isreduce(char x,char p)
{
int k,l;
k=isstate(x);
l=isnter(p);
strcpy(temp,G[k 1].r[l 1]);
}
char pop(char *s,int *sp)
{
char item;
if(*sp== 1)
printf(" stack is empty ");
else
{
item=s[*sp];
*sp=*sp 1;
}
return item;
}
void printt(char *t,int *p,char inp[],int i)
{
int r;
printf("\n");
for(r=0;r<=*p;r++)
rep(t,r);
printf("\t\t\t");
for(r=i;inp[r]!='\0';r++)
printf("%c",inp[r]);
}
void rep(char t[],int r)
{
char c;

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 27
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

c=t[r];
switch(c)
{
case 'a': printf("0");
break;
case 'b': printf("1");
break;
case 'c': printf("2");
break;
case 'd': printf("3");
break;
case 'e': printf("4");
break;
case 'f': printf("5");
break;
case 'g': printf("6");
break;
case 'h': printf("7");
break;
case 'm': printf("8");
break;
case 'j': printf("9");
break;
case 'k': printf("10");
break;
case 'l': printf("11");
break;
default :printf("%c",t[r]);
break;
}
}

Output:

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 28
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 29
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 08

AIM: To Study about Lexical Analyzer Generator (LEX) and


Flex (Fast Lexical Analyzer).

Explanation:
Lex A Lexical Analyzer Generator:
Lex is a program generator designed for lexical processing
of character input streams. It accepts a high level, problem
oriented specification for character string matching, and
produces a program in a general purpose language which
recognizes regular expressions. The regular expressions
are specified by the user in the source specifications given
to Lex. The Lex written code recognizes these expressions
in an input stream and partitions the input stream into
strings matching the expressions.

The grammar in the above diagram is a text file you create


with a text edtior. Yacc will read your grammar and
generate C code for a syntax analyzer or parser. The
syntax analyzer uses grammar rules that allow it to analyze
tokens from the lexical analyzer and create a syntax tree.
The syntax tree imposes a hierarchical structure the tokens.
For example, operator precedence and associativity are
apparent in the syntax tree. The next step, code generation,
does a depth first Lexical Analyzer Syntax Analyzer a = b
+ c * d id1 = id2 + id3 * id4 = + * id1 source code tokens
syntax tree id2 id3 id4 load id3 mul id4 add id2 store id1
Code Generator generated code Lex Yacc patterns
grammar 5 walk of the syntax tree to generate code. Some
compilers produce machine code, while others, as shown
above, output assembly language.

What is Flex?
Enrollment No.: 210303105168
CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 30
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

 Flex is a powerful, open source application framework


which allows to build traditional applications for browser,
mobile and desktop using the same programming model,
tool, and codebase.
 Flex provides FLEX SDK consisting of the Flex class
library (ActionScript classes), the Flex compilers, the
debugger, the MXML and ActionScript programming
languages, and other utilities to build expressive and
interactive rich internet applications (RIA)
 Flex takes care of the user interface (UI) or the client side
functionality of a web application. Server side
functionality dependent on server side components written
in a traditional scripting language

How to use FLEX?


FLEX (Fast Lexical analyzer generator) is a tool for
generating scanners. Instead of writing a scanner from
scratch, you only need to identify the vocabulary of a
certain language (e.g. Simple), write a specification of
patterns using regular expressions (e.g. DIGIT [0 9]), and
FLEX will construct a scanner for you. FLEX is generally
used in the manner depicted here:

First, FLEX reads a specification of a scanner either from


an input file *.lex, or from standard input, and it generates
as output a C source file lex.yy.c. Then, lex.yy.c is
compiled and linked with the " lfl" library to produce an
executable a.out. Finally, a.out analyzes its input stream
and transforms it into a sequence of tokens.

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 31
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

*.lex is in the form of pairs of regular expressions and C


code. (sample1.lex, sample2.lex) lex.yy.c defines a routine
yylex () that uses the specification to recognize tokens.
a.out is actually the scanner!

How to Compile & Run LEX / YACC Programs on Windows?


If you are installing Ubuntu (or any Linux based OS) on
your system either through Virtual Box or by making your
system multi bootable, just to execute your Lex & Yacc
programs; then you might be wasting your HDD space &
your valuable time. You can easily skip this annoying
process and run your programs in Windows OS without
any hassles.

Here's how you can do it:

Installing Software’s:
1. Download Flex 2.5.4a
2. Download Bison 2.4.1
3. Download DevC++
4. Install Flex at "C:\GnuWin32"
5. Install Bison at "C:\GnuWin32"
6. Install DevC++ at "C:\Dev Cpp"
7. Open Environment Variables.
8. Add "C:\GnuWin32\bin;C:\Dev Cpp\bin;" to path.

Compilation & Execution of your Program:


1. Open Command prompt and switch to your working
directory where you have stored your lex file (".l") and
yacc file (".y")
2. Let your lex and yacc files be "hello.l" and "hello.y". Now,
follow the preceding steps to compile and run your
program.
1. For Compiling Lex file only:
 flex hello.l
 gcc lex.yy.c
2. For Compiling Lex & Yacc file both:
 flex hello.l
Enrollment No.: 210303105168
CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 32
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

 bison dy hello.y
 gcc lex.yy.c y.tab.c
3. For Executing the Program
 a.exe

Code:
EXAMPLE: HELLO.L FILE
%{
#include "y.tab.h"
int yyerror(char *errormsg);
%}
%%
("hi"|"oi")"\n" { return HI; }
("tchau"|"bye")"\n" { return BYE; }
. { yyerror("Unknown char"); }
%%
int main(void)
{
yyparse();
return 0;
}
int yywrap(void)
{
return 0;
}

int yyerror(char *errormsg)


{
fprintf(stderr, "%s\n", errormsg);
exit(1);
}

HELLO.Y FILE
%{

#include <stdio.h>

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 33
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

#include <stdlib.h>
int yylex(void);
int yyerror(const char *s);

%}

%token HI BYE

%%

program:
hi bye
;

hi:
HI { printf("Hello World\n"); }
;
bye:
BYE { printf("Bye World\n"); exit(0); }
;
Output:

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 34
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 09

AIM: Create a Lexer to take input from text file and count no of
characters, no. of lines & no. of words.

Code:
%{
#include<stdio.h>
int lines=0, words=0,s_letters=0,c_letters=0, num=0,
spl_char=0,total=0;
%}

%%
\n { lines++; words++;} [\t ' '] words++;
[A Z] c_letters++;
[a z] s_letters++;
[0 9] num++;
. spl_char++;
%%

main(void)
{
yyin= fopen("practical9.txt","r");
yylex();
total=s_letters+c_letters+num+spl_char;
printf(" This File contains ...");
printf("\n\t%d lines", lines);
printf("\n\t%d words",words);
printf("\n\t%d small letters", s_letters);
printf("\n\t%d capital letters",c_letters);
printf("\n\t%d digits", num);
printf("\n\t%d special characters",spl_char);
printf("\n\tIn total %d characters.\n",total);
}
int yywrap()
{

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 35
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

return(1);
}
Practical9.txt FILE :
Hii
I'm Umang From Cse

Output:

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 36
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 10

AIM: Write a Lex program to count number of vowels and


consonants in a given input string.

Code:
%{
int vow_count=0; int const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;} [a zA Z] {const_count++;}
%%
main()
{
yyin= fopen("practical8.txt","r"); yylex();
printf("The number of vowels are: %d\n",vow_count);
printf("The number of consonants are: %d\n",const_count);
return 0;
}
yywrap()
{
return 1;}
Practical10.txt FILE
Hii
I'm Umang From Cse

Output:

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 37
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 11

AIM: Write a Lex program to print out all numbers from the
given file.

Code:
%{
#include<stdio.h> int num=0;
%}
%%
[0 9] num++; ECHO;
%%

main(void)
{
yyin= fopen("practical11.txt","r");
yylex();
printf("\n\t%d digits", num);}
int yywrap()
{
return(1);}

Practical11.txt FILE
23332
23
Sbfjehbcc

Output:

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 38
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

PRACTICAL: 12

AIM: Write a Lex program to printout all HTML tags in file.

Code:
%{
#include<stdio.h>
%}

%%
\<[^>]*\> printf("%s\n",yytext);
.|\n;
%%

int yywrap()
{
return 1;
}

int main()
{
yyin=fopen("practical10b.txt","r");
yylex();
return 0;
}

Practical10b.txt FILE
<html>
<body>
<h1> Hello</h1>
<br>
</body>
</html>

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 39
Div: 25(CSE)
Faculty of Engineering & Technology
Software Testing and Quality Assurance (203105396)
B. Tech CSE 4th Year 7th Semester

Output:

Enrollment No.: 210303105168


CHAUDHARY UMANGKUMAR BALUBHAI
P a g e | 40
Div: 25(CSE)

You might also like