0% found this document useful (0 votes)
27 views5 pages

Pathakexp 4

ISDH File Program 3 BTEC

Uploaded by

vabesi8625
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)
27 views5 pages

Pathakexp 4

ISDH File Program 3 BTEC

Uploaded by

vabesi8625
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/ 5

Experiment No: 4

Aim: Program to design a lexical analyser.

Source Code:

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

bool isDelimiter(char ch)


{
if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == ',' || ch == ';' || ch == '>' ||
ch == '<' || ch == '=' || ch == '(' || ch == ')' ||
ch == '[' || ch == ']' || ch == '{' || ch == '}')
return (true);
return (false);
}

bool isOperator(char ch)


{
if (ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == '>' || ch == '<' ||
ch == '=')
return (true);
return (false);
}

bool validIden fier(char *str)


{
if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||
str[0] == '3' || str[0] == '4' || str[0] == '5' ||
str[0] == '6' || str[0] == '7' || str[0] == '8' ||
str[0] == '9' || isDelimiter(str[0]) == true)
return (false);
return (true);
}

bool isKeyword(char *str)


{
if (!strcmp(str, "if") || !strcmp(str, "else") ||
!strcmp(str, "while") || !strcmp(str, "do") ||
!strcmp(str, "break") ||
!strcmp(str, "con nue") || !strcmp(str, "int") || !strcmp(str, "double") ||
!strcmp(str, "float") || !strcmp(str, "return") || !strcmp(str, "char") ||
!strcmp(str, "case") || !strcmp(str, "char") || !strcmp(str, "sizeof") ||
!strcmp(str, "long") || !strcmp(str, "short") || !strcmp(str, "typedef") ||
!strcmp(str, "switch") || !strcmp(str, "unsigned") || !strcmp(str, "void") ||
!strcmp(str, "sta c") || !strcmp(str, "struct") || !strcmp(str, "goto"))
return (true);
return (false);
}

bool isInteger(char *str)


{
int i, len = strlen(str);

if (len == 0)
return (false);
for (i = 0; i < len; i++)
{
if (str[i] != '0' && str[i] != '1' && str[i] != '2' && str[i] != '3' && str[i] != '4'
&& str[i] != '5' && str[i] != '6' && str[i] != '7' && str[i] != '8' && str[i] != '9' ||
(str[i] == '-' && i > 0))
return (false);
}
return (true);
}
bool isRealNumber(char *str)
{
int i, len = strlen(str);
bool hasDecimal = false;
if (len == 0)
return (false);
for (i = 0; i < len; i++)
{
if (str[i] != '0' && str[i] != '1' && str[i] != '2' && str[i] != '3' && str[i] != '4'
&& str[i] != '5' && str[i] != '6' && str[i] != '7' && str[i] != '8' && str[i] != '9' &&
str[i] != '.' ||
(str[i] == '-' && i > 0))
return (false);
if (str[i] == '.')
hasDecimal = true;
}
return (hasDecimal);
}

char *subString(char *str, int le , int right)


{
int i;
char *subStr = (char *)malloc(
sizeof(char) * (right - le + 2));
for (i = le ; i <= right; i++)
subStr[i - le ] = str[i];
subStr[right - le + 1] = '\0';
return (subStr);
}

void parse(char *str)


{
int le = 0, right = 0;
int len = strlen(str);
while (right <= len && le <= right)
{
if (isDelimiter(str[right]) == false)
right++;
if (isDelimiter(str[right]) == true && le == right)
{
if (isOperator(str[right]) == true)
prin ("'%c' IS AN OPERATOR\n", str[right]);
right++;
le = right;
}
else if (isDelimiter(str[right]) == true && le != right || (right == len && le
!= right))
{
char *subStr = subString(str, le , right - 1);
if (isKeyword(subStr) == true)
prin ("'%s' IS A KEYWORD\n", subStr);
else if (isInteger(subStr) == true)
prin ("'%s' IS AN INTEGER\n", subStr);
else if (isRealNumber(subStr) == true)
prin ("'%s' IS A REAL NUMBER\n", subStr);
else if (validIden fier(subStr)==true&&isDelimiter(str[right - 1]) == false)
prin ("'%s' IS A VALID IDENTIFIER\n", subStr);
else if (validIden fier(subStr)==false&&isDelimiter(str[right - 1]) == false)
prin ("'%s' IS NOT A VALID IDENTIFIER\n", subStr);
le = right;
}
}
return;
}

int main()
{
cout<<"Input the string "<<endl;
char str[2000] ;
scanf("%[^\n]s",str);
parse(str);
return (0);
}
Output of Experiment 4:

You might also like