Professional Documents
Culture Documents
Enrollment:03-134201-030
Q1. Identify the <token ,lexeme> pairs
Tokens Lexeme
For keyword
( symbols
int keyword
x identifier
= operators
0 constants
; symbols
x identifier
< operators
= operators
5 constants
; symbols
x identifier
+ operators
+ operators
) operators
2. B= (( c + a) * d ) / f
Tokens Lexeme
B Identifier
= operators
( symbol
( symbol
c identifier
+ operators
a identifier
) symbol
* operators
d Identifier
) symbol
/ symbol
f identifier
3. While ( a < 5 )
a= a+1
Tokens Lexeme
While keyword
( symbol
a identifier
< operators
5 constant
) symbol
a identifier
= operators
a identifier
+ operator
1 constant
4. Char MyCourse[5];
Tokens Lexeme
Char keyword
MyCourse identifier
[ symbol
5 constants
] symbol
; symbol
5. if ( a< b)
a=a*a;
else
b=b*b;
Tokens Lexeme
if keyword
( symbol
a identifier
< operators
b identifier
) symbol
a identifier
= operators
a identifier
* operators
a identifier
; symbol
else keyword
b identifier
= operators
b identifier
* operators
b identifier
; symbol
Q2. Write a program in C++ or Java that reads a source file and performs the followings operations:
int main() {
char ch, buffer[15], operators[] = "+-*/%=";
ifstream fin("program.txt");
int i, j = 0;
if (!fin.is_open()) {
cout << "error while opening the file\n";
exit(0);
}
while (!fin.eof()) {
ch = fin.get();
if (isalnum(ch)) {
buffer[j++] = ch;
}
else if ((ch == ' ' || ch == '\n') && (j != 0)) {
buffer[j] = '\0';
j = 0;
if (isKeyword(buffer) == 1)
cout << buffer << " is keyword\n";
else
cout << buffer << " is indentifier\n";
}
}
fin.close();
return 0;
}#include<iostream>
#include<string>
using namespace std;
int main()
{
char a;
//if the portion is symbol
bool isSymobol(char a)
{
if (a == ' ' || a == '+' || a == '-' || a == '*'
|| a == ' /' || a == '>' || a == '<' || a == '['
|| a == ']' || a == '{' || a == '}' || a == ','
|| a == ';' || a == '(' || a == ')')
return (true);
return (false);
}
bool isoperator(char a)
{
if ( a == '+' || a == '-' || a == '*'
|| a == ' /' || a == '>' || a == '<' || a == '%')
return (true);
return (false);
}
bool Identifier(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'|| isSymobol(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, "continue") || !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, "static")
|| !strcmp(str, "struct") || !strcmp(str, "goto"))
return (true);
return (false);
}
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);
}
void parse(char* str)
{
int left = 0, right = 0;
int len = strlen(str);
if (isKeyword(subStr) == true)
printf("'%s' IS A KEYWORD\n", subStr);
char a[100];
cout << "Lines of code";
cin >> a;
return (0);
}
Deadline:
Sunday, 20-March-2022 before 11:45 PM (Via LMS)