Cs-603 Activity: Abca-1 (Coding/Debugging) Compiler: Name - Divyansh Sharma Roll No. - 0905cs211055
Cs-603 Activity: Abca-1 (Coding/Debugging) Compiler: Name - Divyansh Sharma Roll No. - 0905cs211055
(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;
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();
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: }