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

SPCC Lab1

This C program defines several functions to classify characters and substrings in a given string as operators, keywords, identifiers, literals, constants, or special characters. It takes a string as input, uses these classification functions to parse it, and prints the classification of each substring separated by delimiters. It contains functions to check for delimiter, operator, keyword, identifier, constant, literal and special character classification, as well as a function to extract a substring between indices. These functions are used by the parse function to classify and print the substrings of a given input string.

Uploaded by

Sankalp Rane
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

SPCC Lab1

This C program defines several functions to classify characters and substrings in a given string as operators, keywords, identifiers, literals, constants, or special characters. It takes a string as input, uses these classification functions to parse it, and prints the classification of each substring separated by delimiters. It contains functions to check for delimiter, operator, keyword, identifier, constant, literal and special character classification, as well as a function to extract a substring between indices. These functions are used by the parse function to classify and print the substrings of a given input string.

Uploaded by

Sankalp Rane
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdbool.

h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
bool isDelimiter(char ch)
{
if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == ',' || ch == '>' ||
ch == '<' || ch == '=' || ch == '(' || ch == ')' ||
ch == '[' || ch == ']' || ch == '{' || ch == '}')
return (true);
return (false);
}
bool validIdentifier(char * str)
{
if (str[0] == 'a'|| str[0] =='b'|| str[0] == 'd'|| str[0] == 'c')
return (true);
return (false);
}
bool isOperator(char ch)
{
if (ch == '+' || ch == '-' || ch == '*' ||ch == '=')
return (true);
return (false);
}
bool isKeyword(char* str)
{
if ( !strcmp(str, "int")|| !strcmp(str, "float")|| !strcmp(str, "char"))
return (true);
return (false);
}
bool isSpecial(char *str)
{
if (str[0] == ';')
return (true);
return (false);
}
bool isConstant(char *str)
{
if (str[0]=='1')
return (true);
return (false);
}
bool isLiteral(char *str)
{
if (str[1] == 'z'||str[1] == 's')
return (true);
return (false);
}
char* subString(char* str, int left, int right)
{
int i;
char* subStr = (char*)malloc(
sizeof(char) * (right - left + 2));

for (i = left; i <= right; i++)


subStr[i - left] = str[i];
subStr[right - left + 1] = '\0';
return (subStr);
}

// Parsing the input STRING.


void parse(char* str)
{
int left = 0, right = 0;
int len = strlen(str);

while (right <= len && left <= right) {


if (isDelimiter(str[right]) == false)
right++;

if (isDelimiter(str[right]) == true && left == right) {


if (isOperator(str[right]) == true)
printf("'%c' IS AN OPERATOR\n", str[right]);

right++;
left = right;
} else if (isDelimiter(str[right]) == true && left != right
|| (right == len && left != right)) {
char* subStr = subString(str, left, right - 1);

if (isKeyword(subStr) == true)
printf("'%s' IS A KEYWORD\n", subStr);

else if (isSpecial(subStr) == true)


printf("'%s' IS AN SPECIAL CHARACTER", subStr);

else if (isConstant(subStr) == true)


printf("'%s' IS A CONSTANT\n", subStr);

else if (isLiteral(subStr) == true)


printf("'%s' IS A LITERAL\n", subStr);

else if (validIdentifier(subStr) == true


&& isDelimiter(str[right - 1]) == false)
printf("'%s' IS A IDENTIFIER\n", subStr);

left = right;
}
}
return;
}

int main()
{
int i;
char str[100];
printf("Enter the input \n");
for(i=0;i<5;i++)
{
fgets(str, sizeof(str), stdin);
printf("Line number %d \n", i+1);
parse(str);
}

return 0;
}

You might also like