0% found this document useful (0 votes)
10 views

Assignment 10.docx

Title nutrition or certainly not be able to attend the meeting with you and your family a

Uploaded by

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

Assignment 10.docx

Title nutrition or certainly not be able to attend the meeting with you and your family a

Uploaded by

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

Sandip Foundation’s

Sandip Institute of Engineering and Management,


Mahiravani, Trimbak Road, Nashik- 422 213

Group D
ASSIGNMENT NO: 10

TITLE:- Checking of parenthesis

AIM: In any language program mostly syntax error occurs due to unbalancing delimiter such as
(),{},[]. Write C++ program using stack to check whether given expression is well
Parenthesized or not.

PRE-REQUISITE:- Primitive operation of stack and recursion using stack.

LEARNING OBJECTIVES:- Identify balanced expression and importance of parenthesis in


programming language.

OUTCOMES:- Implementation of stack as ordered list and using recursion function.

THEORY:-

Stack Abstract Data Types:-

1.create(s):- create empty stack

int stack [100];

int top=-1;

The constructor must intialize the stack top, so as to represent an empty stack, to a value that
represent the top of the stack. We cannot initialize it to one of the values in the range of 0 to n-1
because these are the indices of stack array.

2.Empty Stack:-

Is_Empty(s)-return true if (s) empty otherwise return false

if(top==-1)
Sandip Foundation’s
Sandip Institute of Engineering and Management,
Mahiravani, Trimbak Road, Nashik- 422 213

then

return 1;

else

return 0;

3.Get_top(s):-

return the top most element of the stack

if(top==-1)

cout<<”Stack Underflow”;

else

return (stack(top));

4.push(i,s):-

Add the element ‘i’ on the stack ‘s’ and return the modified stack.

if(top==maxcapacity)

cout<<”Stack overflow”;

else

top++;

stack[top]=element;

5.pop(s):-

Remove the top most element from stack(s) and return the modified stack.

if(top==-1)

cout<<”Stack Empty”;

else
Sandip Foundation’s
Sandip Institute of Engineering and Management,
Mahiravani, Trimbak Road, Nashik- 422 213

return (stack(top));

top--;

Checking cporrectness of well form parenthesis:-

Consider a mathematical expression that includes several sets of nested parathesis .

For example:-

Z-((X*(X+Y)/5-2)+Y)/3)

To ensure that the paranthesis are nested correctly we need to check :-

i) There are equal number right nd left parathesis.

ii) Every right parenthesis is preceeded by matching left parathesis.

Expression such as ((X+Y) or (X+Y)) expression such as (X+Y)) (A-B) or (X-Y) breaks condition
(ii). To solve this problem let us define parathesis count at particular point in an expression as no. of
left parenthesis number of right parathesis that have been encontered in the left to right scanning of
expression at that particular point.

The two conditions that must hoold if the parathesis in an expression from pattern as below:-

i) The parathesis count at each point in expression is non-negative.

ii) The parenthesis count at the end of expression is zero.

A stack may also be used to keep track of parenthesis count. Whenever a left parenthesis is
encountered it is pushed in stack and whenever a right parathesis is encountered the stack is
examine. If the stack is empty then string is declared to be invalid. When end of string is reached the
stack must be empty otherwise string is declared to be invalid.

TEST CASES:-

No. Input Expected Output Remarks


T1 (A+B)*(C*(D-F)) Expression is well Success
parenthesized
Sandip Foundation’s
Sandip Institute of Engineering and Management,
Mahiravani, Trimbak Road, Nashik- 422 213

T2 {C-[A+B]*C] Expression is not well Success


parenthesized

CONCLUSION:-

The basic concept of stack operation push and pop able to check whether an expression is well
parenthesized or not.

You might also like