0% found this document useful (0 votes)
44 views3 pages

Balanced Delimiter

The document discusses the application of stacks in checking balanced delimiters in programming languages, focusing on the parsing process during compilation. It explains how a stack is used to match opening and closing delimiters, with examples of valid and invalid delimiters. Additionally, it provides a C program implementation for checking balanced expressions using a stack data structure.
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)
44 views3 pages

Balanced Delimiter

The document discusses the application of stacks in checking balanced delimiters in programming languages, focusing on the parsing process during compilation. It explains how a stack is used to match opening and closing delimiters, with examples of valid and invalid delimiters. Additionally, it provides a C program implementation for checking balanced expressions using a stack data structure.
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/ 3

Data Structures

Unit II Stacks
Applications of stack

Balanced delimiter
The common application of Stack is delimiter checking, i.e., parsing that involves analyzing a source
program syntactically. It is also called parenthesis checking. When the compiler translates a source
program written in some programming language such as C, C++ to a machine language, it parses the
program into multiple individual parts such as variable names, keywords, etc. By scanning from left
to right. The main problem encountered while translating is the unmatched delimiters. We make use
of different types of delimiters include the parenthesis checking (,), curly braces {,} and square
brackets [,], and common delimiters /* and */. Every opening delimiter must match a closing
delimiter, i.e., every opening parenthesis should be followed by a matching closing parenthesis. Also,
the delimiter can be nested. The opening delimiter that occurs later in the source program should be
closed before those occurring earlier.

Valid Delimiter Invalid Delimiter


While ( i > 0) While ( i >
/* Data Structure */ /* Data Structure
{ ( a + b) - c } { ( a + b) - c

To perform a delimiter checking, the compiler makes use of a stack. When a compiler translates a
source program, it reads the characters one at a time, and if it finds an opening delimiter it places it
on a stack. When a closing delimiter is found, it pops up the opening delimiter from the top of the
Stack and matches it with the closing delimiter.

On matching, the following cases may arise.


o If the delimiters are of the same type, then the match is considered successful, and the process
continues.
o If the delimiters are not of the same type, then the syntax error is reported.
When the end of the program is reached, and the Stack is empty, then the processing of the source
program stops.
Example: let's consider the following expression.
[{(a -b) * (c -d)}/f]
Input left Characters read Stack contents
{(a -b) * (c -d)}/f] [ [
(a -b) * (c -d)}/f] { [{
a -b) * (c -d)}/f] ( [{(
-b) * (c -d)}/f] a [{(
b) * (c -d)}/f] - [{(
) * (c -d)}/f] b [{(
* (c -d)}/f] ) [{
(c -d)}/f] * [{
c -d)}/f] ( [{(
-d)}/f] c [{(
d)}/f] - [{(
)}/f] d [{(
}/f] ) [{
/f] } [
f] / [

Page | 1
Data Structures

] f [
]

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 20
struct stack
{
char stk[MAX];
int top;
}s;

void push(char item)


{
if (s.top == (MAX - 1))
printf ("Stack is Full\n");
else
{
s.top = s.top + 1; // Push the char and increment top
s.stk[s.top] = item;
}
}

void pop()
{
if (s.top == - 1)
{
printf ("Stack is Empty\n");
}
else
{
s.top = s.top - 1; // Pop the char and decrement top
}
}

int main()
{
char exp[MAX];
int i = 0;
s.top = -1;
printf("\n INPUT THE EXPRESSION : ");
scanf("%s", exp);
for(i = 0;i <strlen(exp);i++)
{
if(exp[i] == '(' || exp[i] == '[' || exp[i] == '{')
{
push(exp[i]); // Push the open bracket
continue;
}
else if(exp[i] == ')' || exp[i] == ']' || exp[i] == '}') // If a closed bracket is encountered
{
if(exp[i] == ')')

Page | 2
Data Structures

{
if(s.stk[s.top] == '(')
{
pop(); // Pop the stack until closed bracket is found
}
else
{
printf("\n UNBALANCED EXPRESSION\n");
break;
}
}
if(exp[i] == ']')
{
if(s.stk[s.top] == '[')
{
pop(); // Pop the stack until closed bracket is found
}
else
{
printf("\n UNBALANCED EXPRESSION\n");
break;
}
}
if(exp[i] == '}')
{
if(s.stk[s.top] == '{')
{
pop(); // Pop the stack until closed bracket is found
}
else
{
printf("\n UNBALANCED EXPRESSION\n");
break;
}
}
}
}

if(s.top == -1)
{
printf("\n BALANCED EXPRESSION\n");
}
}

Page | 3

You might also like