0% found this document useful (0 votes)
9 views6 pages

Cs-603 Activity: Abca-1 (Coding/Debugging) Compiler: Name - Divyansh Sharma Roll No. - 0905cs211055

asfsd

Uploaded by

ds29072002.1
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)
9 views6 pages

Cs-603 Activity: Abca-1 (Coding/Debugging) Compiler: Name - Divyansh Sharma Roll No. - 0905cs211055

asfsd

Uploaded by

ds29072002.1
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/ 6

CS-603 ACTIVITY: ABCA-1

(Coding/Debugging)
COMPILER
Name – Divyansh Sharma
Roll no. – 0905cs211055

Code –
#include <iostream>
#include <vector>
#include <cctype>

enum TokenType {
KEYWORD,
IDENTIFIER,
OPERATOR,
PUNCTUATION,
INTEGER_LITERAL,
COMMENT,
UNKNOWN
};

struct Token {
TokenType type;
std::string value;
};

class LexicalAnalyzer {
public:
LexicalAnalyzer(const std::string& input) : inputString(input) {}
std::vector<Token> analyze() {
std::vector<Token> tokens;
size_t currentPosition = 0;

while (currentPosition < inputString.size()) {


char currentChar = inputString[currentPosition];

if (std::isspace(currentChar)) {
// Ignore whitespace characters
currentPosition++;
} else if (std::isalpha(currentChar)) {
// Identify keywords and identifiers
std::string tokenValue;
while (std::isalnum(currentChar) || currentChar == '_') {
tokenValue += currentChar;
currentPosition++;
currentChar = inputString[currentPosition];
}

Token token;
if (isKeyword(tokenValue)) {
token.type = KEYWORD;
} else {
token.type = IDENTIFIER;
}
token.value = tokenValue;
tokens.push_back(token);
} else if (std::isdigit(currentChar)) {
// Identify integer literals
std::string tokenValue;
while (std::isdigit(currentChar)) {
tokenValue += currentChar;
currentPosition++;
currentChar = inputString[currentPosition];
}

Token token;
token.type = INTEGER_LITERAL;
token.value = tokenValue;
tokens.push_back(token);
} else if (currentChar == '/' && inputString[currentPosition + 1] == '/') {
// Identify comments
std::string tokenValue;
while (currentChar != '\n' && currentPosition < inputString.size()) {
tokenValue += currentChar;
currentPosition++;
currentChar = inputString[currentPosition];
}

Token token;
token.type = COMMENT;
token.value = tokenValue;
tokens.push_back(token);
} else {
// Identify operators and punctuation
Token token;
token.type = OPERATOR;
token.value = currentChar;
tokens.push_back(token);
currentPosition++;
}
}

return tokens;
}

private:
bool isKeyword(const std::string& word) {
// Simple check for keywords (you can extend this list as needed)
std::vector<std::string> keywords = {"int", "return", "if", "else"};
return std::find(keywords.begin(), keywords.end(), word) != keywords.end();
}

std::string inputString;
};

int main() {
std::string inputCode = R"(
#include <iostream>
int maximum(int x, int y) {
// This will compare two numbers
if (y > x)
return y;
else {
return x;
}
})";

LexicalAnalyzer lexer(inputCode);
std::vector<Token> tokens = lexer.analyze();

// Display the tokens


for (const auto& token : tokens) {
std::cout << "Type: " << token.type << ", Value: " << token.value << std::endl;
}

return 0;
}

Output
Type: PUNCTUATION, Value: #
Type: KEYWORD, Value: include
Type: PUNCTUATION, Value: <
Type: IDENTIFIER, Value: iostream
Type: PUNCTUATION, Value: >
Type: KEYWORD, Value: int
Type: IDENTIFIER, Value: maximum
Type: PUNCTUATION, Value: (
Type: KEYWORD, Value: int
Type: IDENTIFIER, Value: x
Type: PUNCTUATION, Value: ,
Type: KEYWORD, Value: int
Type: IDENTIFIER, Value: y
Type: PUNCTUATION, Value: )
Type: PUNCTUATION, Value: {
Type: COMMENT, Value: // This will compare two numbers
Type: KEYWORD, Value: if
Type: PUNCTUATION, Value: (
Type: IDENTIFIER, Value: y
Type: OPERATOR, Value: >
Type: IDENTIFIER, Value: x
Type: PUNCTUATION, Value: )
Type: KEYWORD, Value: return
Type: IDENTIFIER, Value: y
Type: PUNCTUATION, Value: ;
Type: KEYWORD, Value: else
Type: PUNCTUATION, Value: {
Type: KEYWORD, Value: return
Type: IDENTIFIER, Value: x
Type: PUNCTUATION, Value: ;
Type: PUNCTUATION, Value: }
Type: PUNCTUATION, Value: }

You might also like