Lab 3
unknown
c_cpp
4 years ago
8.3 kB
5
Indexable
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
bool isKeyword(char *str, int left, int right)
{
int state = 1;
while (left <= right)
{
switch (state)
{
case 1:
if (str[left] == 'i')
{
state = 2;
}
break;
case 2:
if (str[left] == 'f')
{
state = 3;
}
break;
case 3:
if (str[left] == ' ')
{
state = 4;
}
case 4:
printf("%s", "Token issued is <if>\n");
return true;
}
left += 1;
}
return (false);
};
bool isIdentifier(char *str, int left, int right)
{
int state = 0;
char ch;
while (left <= right)
{
ch = str[left];
switch (state)
{
case 0:
if (isalpha(ch))
{
state = 1;
}
break;
case 1:
if (isalnum(ch))
{
state = 1;
break;
}
else
{
state = 2;
}
case 2:
printf("%s", "Token issued is <id, 1>\n");
return true;
}
left += 1;
}
return false;
}
bool isNumber(char *str, int left, int right)
{
int state = 0;
char token[20] = "";
char ch;
while (left <= right)
{
ch = str[left];
switch (state)
{
case 0:
if (isdigit(ch))
{
state = 1;
}
break;
case 1:
if (isdigit(ch))
{
strncat(token, &ch, 1);
state = 1;
break;
}
else if (ch == '.')
{
strncat(token, &ch, 1);
state = 2;
break;
}
else if (ch == 'E')
{
strncat(token, &ch, 1);
state = 4;
break;
}
else
{
state = 8;
}
case 2:
if (isdigit(ch))
{
strncat(token, &ch, 1);
state = 3;
}
break;
case 3:
if (isdigit(ch))
{
strncat(token, &ch, 1);
state = 3;
}
else if (ch == 'E')
{
strncat(token, &ch, 1);
state = 4;
}
else
{
state = 9;
}
break;
case 4:
if (ch == '+' || ch == '-')
{
strncat(token, &ch, 1);
state = 5;
}
else if (isdigit(ch))
{
strncat(token, &ch, 1);
state = 6;
}
break;
case 5:
if (isdigit(ch))
{
strncat(token, &ch, 1);
state = 6;
}
break;
case 6:
if (isdigit(ch))
{
strncat(token, &ch, 1);
state = 6;
break;
}
else
{
state = 7;
}
case 7:
printf("Token issued is <%s>\n", token);
return true;
case 8:
printf("Token issued is <%s>\n", token);
return true;
case 9:
printf("Token issued is <%s>\n", token);
return true;
}
left += 1;
}
return (false);
}
bool isRelOp(char *str, int left, int right)
{
int state = 0;
while (left <= right)
{
switch (state)
{
case 0:
if (str[left] == '<')
{
state = 1;
}
else if (str[left] == '>')
{
state = 6;
}
else if (str[left] == '=')
{
state = 5;
}
break;
case 1:
if (str[left] == '=')
{
state = 2;
}
else if (str[left] == '>')
{
state = 3;
}
else
{
printf("%s", "Token issued is <RELOP, LT>\n");
return true;
}
break;
case 2:
printf("%s", "Token issued is <RELOP, LE>\n");
return true;
case 3:
printf("%s", "Token issued is <RELOP, NE>\n");
return true;
case 5:
printf("%s", "Token issued is <RELOP, EQ>\n");
return true;
case 6:
if (str[left] == '=')
{
state = 7;
}
else
{
printf("%s", "Token issued is <RELOP, GT>\n");
return true;
}
break;
case 7:
printf("%s", "Token issued is <RELOP, GE>\n");
return true;
}
left += 1;
}
return false;
}
bool isParentheses(char *str, int left, int right)
{
int state = 0;
while (left <= right)
{
switch (state)
{
case 0:
if (str[left] == '(')
{
state = 1;
}
else if (str[left] == ')')
{
state = 2;
}
else if (str[left] == '{')
{
state = 3;
}
else if (str[left] == '}')
{
state = 4;
}
else if (str[left] == '[')
{
state = 5;
}
else if (str[left] == ']')
{
state = 6;
}
break;
case 1:
printf("%s", "Token issued is <PAREN, SIMP_OPEN>\n");
return true;
case 2:
printf("%s", "Token issued is <PAREN, SIMP_CLOSE>\n");
return true;
case 3:
printf("%s", "Token issued is <PAREN, CURL_OPEN>\n");
return true;
case 4:
printf("%s", "Token issued is <PAREN, CURL_CLOSE>\n");
return true;
case 5:
printf("%s", "Token issued is <PAREN, SQ_OPEN>\n");
return true;
case 6:
printf("%s", "Token issued is <PAREN, SQ_CLOSE>\n");
return true;
}
left += 1;
}
return false;
}
bool isWhiteSpace(char *str, int left, int right)
{
int state = 1;
while (left <= right)
{
switch (state)
{
case 1:
if (str[left] == ' ')
{
state = 2;
}
break;
case 2:
printf("%s", "Whitespace token not issued <>\n");
return true;
}
left += 1;
}
return false;
}
int main()
{
/* code */
char str[20];
printf("Enter input : ");
gets(str);
// printf("All Tokens are : \n");
// printf("%s\n", str);
int left = 0, right = 0;
int len = strlen(str);
// printf("%d",len);
while (right <= len && left <= right)
{
if (isKeyword(str, left, right))
{
// left forward retract
left = right;
right -= 1;
}
else if (isIdentifier(str, left, right))
{
left = right;
right -= 1;
}
else if (isNumber(str, left, right))
{
left = right;
right -= 1;
}
else if (isRelOp(str, left, right))
{
left = right;
right -= 1;
}
else if (isParentheses(str, left, right))
{
left = right;
right -= 1;
}
else if (isWhiteSpace(str, left, right))
{
left = right;
right -= 1;
}
// else
// {
// printf("%s", "Unrecognized lexeme. No token issued.\n");
// }
right += 1;
}
return 0;
}
Editor is loading...