Lab 04 - Lexemes, Identifiers, Keywords TASK 01:: Source Code

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Malik Hamza Ali Nasir 02-134191-099

LAB 04 – LEXEMES, IDENTIFIERS, KEYWORDS


TASK 01: Write a program to split input stream in words on the basis of white spaces (white space
characters: Enter, space, tab).

Source code:
#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> split_on_space(string input);

int main()
{
cout << "Enter a string seperated by whitespace" << '\n';
string input;
getline(cin, input);

auto list = split_on_space(input);

for (auto s : list)


{
cout << s << '\t';
}

cin.get();
return 0;
}

vector<string> split_on_space(string input)


{
vector<string> word_list;
string tmp;

for (char ch : input)


{
if (ch == ' ')
{
word_list.push_back(tmp);
tmp.erase();
}
else
{
tmp += ch;
}
}
word_list.push_back(tmp);

return word_list;
Malik Hamza Ali Nasir 02-134191-099

TASK 02: Use task one code to split input stream and then write a method to recognize valid identifiers
of C++.

Source code:
#include <iostream>
#include <string>
#include <vector>
#include <regex>

using namespace std;

bool is_valid_identifier(string id);


vector<string> valid_identifiers(string input);

int main()
{
cout << "Enter a string of identifiers seperated by whitespace" << '\n';
string input;
getline(cin, input);

auto list = valid_identifiers(input);

for (auto s : list)


{
cout << s << '\t';
}

cin.get();
return 0;
}

bool is_valid_identifier(string id)


{
return regex_match(id, regex{ R"([_[:alpha:]]\w*)" });
}

vector<string> valid_identifiers(string input)


{
vector<string> word_list;
string tmp;

for (char ch : input)


{
Malik Hamza Ali Nasir 02-134191-099

if (ch == ' ')


{
if (is_valid_identifier(tmp))
{
word_list.push_back(tmp);
}
tmp.erase();
}
else
{
tmp += ch;
}
}

if (is_valid_identifier(tmp))
{
word_list.push_back(tmp);
}

return word_list;
}

TASK 03: Write a method to recognize valid keywords of C++.


Source code:
#include <iostream>
#include <string>
#include <vector>
#include <regex>

using namespace std;

bool is_valid_identifier(string in);


bool is_valid_keyword(string in);
vector<string> split_on_space(string in);

int main()
{
cout << "Enter a string to identify valid keywords and identifiers" << '\n';
string in;
getline(cin, in);

vector<string> list = split_on_space(in);

for (string s : list)


Malik Hamza Ali Nasir 02-134191-099

{
if (is_valid_keyword(s))
{
cout << s << " is a valid keyword" << '\n';
}
else
{
if (is_valid_identifier(s))
{
cout << s << " is a valid identifier" << '\n';
}
else
{
cout << s << " is an invalid identifier" << '\n';
}
}
}

cin.get();
return 0;
}

bool is_valid_identifier(string in)


{
return regex_match(in, regex{ R"([_[:alpha:]]\w*)" });
}

bool is_valid_keyword(string in)


{
static vector<string> keywords{
"alignas", "decltype", "namespace", "struct", "alignof", "default",
"new", "switch", "and", "delete", "noexcept", "template", "throw", "eq",
"do", "not", "this", "asm", "double", "not_eq", "thread_local", "auto",
"dynamic_cast", "nullptr", "bitand", "else", "operator", "true",
"bitor", "enum", "or", "try", "bool", "explicit", "or_eq", "typedef",
"break", "export", "private", "typeid", "case", "extern", "protected",
"typename", "catch", "false", "public", "union", "char", "float",
"register", "unsigned", "char16_t", "for", "reinterpret_cast", "using",
"char32_t", "friend", "return", "virtual", "class", "goto", "short",
"void", "compl", "if", "signed", "volatile", "const", "inline",
"sizeof", "wchar_t", "constexpr", "int", "static", "while",
"const_cast", "long", "static_assert", "xor", "continue", "mutable",
"static_cast", "xor_eq"
};

for (string str : keywords)


{
if (str == in)
{
return true;
Malik Hamza Ali Nasir 02-134191-099

return false;
}

vector<string> split_on_space(string in)


{
vector<string> words;
string tmp;

for (char ch : in)


{
if (ch == ' ')
{
words.push_back(tmp);
tmp.erase();
}
else
{
tmp.push_back(ch);
}
}
words.push_back(tmp);

return words;
}

You might also like