C++ Lexical Analyzer Implementation

Job ID: 38629208

Budget: $10 – $30 USD

Implementing a Lexical Analyzer in C++
Objective:
In this assignment, you will implement a lexical analyzer that can tokenize a given source code written in a simple programming language. The lexical analyzer should identify and classify various token types such as keywords, identifiers, integer literals, float literals, operators, and punctuators.

Task:
Complete the implementation of the lexical analyzer by filling in the missing functions and implementations in the provided starter code. Specifically, you need to implement the following functions:

initKeywords(): Initialize the keywords map with the language keywords.
isWhitespace(char c): Check if a character is a whitespace character.
isAlpha(char c): Check if a character is an alphabetic character.
isDigit(char c): Check if a character is a digit.
isAlphaNumeric(char c): Check if a character is alphanumeric (either a letter or a digit).
getNextWord(): Extract the next word (identifier or keyword) from the input.
getNextNumber(): Extract the next number (integer or float) from the input.
tokenize(): Tokenize the input string and return a list of tokens.
getTokenTypeName(TokenType type): Convert the TokenType to a string for printing.
printTokens(const vector<Token>& tokens): Print all the tokens.
Requirements:
The lexical analyzer should handle the following keywords: int, float, main, return, if, else.
The analyzer should identify and classify the following token types:
Keywords
Identifiers
Integer literals
Float literals
Operators (+, -, *, /, etc.)
Punctuators (;, ,, (, ), {, etc.)
It should handle whitespace characters, including spaces, tabs, and newlines.
Testing:
The lexical analyzer will be tested with 4 test cases on CodePost.io, where it will check for the correct classification and identification of tokens. Ensure that your implementation correctly handles the given test inputs.

Test Case 1:
input : ./Lexical "int main() { float x = 3.14; float y=3.15; float z=x+y; return 0; }"
output: sourcecode:intmain(){floatx=3.14;floaty=3.15;floatz=x+y;return0;}tokensgeneratedbylexicalanalyzer:type:keyword,value:inttype:keyword,value:maintype:punctuator,value:(type:punctuator,value:)type:punctuator,value:{type:keyword,value:floattype:identifier,value:xtype:unknown,value:=type:float_literal,value:3.14type:punctuator,value:;type:keyword,value:floattype:identifier,value:ytype:unknown,value:=type:float_literal,value:3.15type:punctuator,value:;type:keyword,value:floattype:identifier,value:ztype:unknown,value:=type:identifier,value:xtype:operator,value:+type:identifier,value:ytype:punctuator,value:;type:keyword,value:returntype:integer_literal,value:0type:punctuator,value:;type:punctuator,value:}
Test case 2:
input: ./Lexical "(2+4)*3"
output: sourcecode:(2+4)*3tokensgeneratedbylexicalanalyzer:type:punctuator,value:(type:integer_literal,value:2type:operator,value:+type:integer_literal,value:4type:punctuator,value:)type:operator,value:*type:integer_literal,value:3
Test case 3:
A compilation test case
Test case 4:
input: ./Lexical "int foo(int x, float y) { int arr[5] = {1, 2, 3, 4, 5}; float avg = (x + y) / 2.0; if (avg > 10.0) { return 1; } else { return 0; } // This is a comment with a tricky character: /* }"
output: sourcecode:intfoo(intx,floaty){intarr[5]={1,2,3,4,5};floatavg=(x+y)/2.0;if(avg>10.0){return1;}else{return0;}//thisisacommentwithatrickycharacter:/*}tokensgeneratedbylexicalanalyzer:type:keyword,value:inttype:identifier,value:footype:punctuator,value:(type:keyword,value:inttype:identifier,value:xtype:punctuator,value:,type:keyword,value:floattype:identifier,value:ytype:punctuator,value:)type:punctuator,value:{type:keyword,value:inttype:identifier,value:arrtype:unknown,value:[type:integer_literal,value:5type:unknown,value:]type:unknown,value:=type:punctuator,value:{type:integer_literal,value:1type:punctuator,value:,type:integer_literal,value:2type:punctuator,value:,type:integer_literal,value:3type:punctuator,value:,type:integer_literal,value:4type:punctuator,value:,type:integer_literal,value:5type:punctuator,value:}type:punctuator,value:;type:keyword,value:floattype:identifier,value:avgtype:unknown,value:=type:punctuator,value:(type:identifier,value:xtype:operator,value:+type:identifier,value:ytype:punctuator,value:)type:operator,value:/type:float_literal,value:2.0type:punctuator,value:;type:keyword,value:iftype:punctuator,value:(type:identifier,value:avgtype:unknown,value:>type:float_literal,value:10.0type:punctuator,value:)type:punctuator,value:{type:keyword,value:returntype:integer_literal,value:1type:punctuator,value:;type:punctuator,value:}type:keyword,value:elsetype:punctuator,value:{type:keyword,value:returntype:integer_literal,value:0type:punctuator,value:;type:punctuator,value:}type:operator,value:/type:operator,value:/type:identifier,value:thistype:identifier,value:istype:identifier,value:atype:identifier,value:commenttype:identifier,value:withtype:identifier,value:atype:identifier,value:trickytype:identifier,value:charactertype:unknown,value::type:operator,value:/type:operator,value:*type:punctuator,value:}
Related categories: C++ Programming Programming