DONE IN C++ In this programming language, you should implement the lexical analy
ID: 647911 • Letter: D
Question
DONE IN C++
In this programming language, you should implement the lexical analysis task for the limited version (i.e., the depth of the nested loops) of a programming language. Lexical analysis is the first stage that compilers parse and detect the possible syntax errors.
Ideally, any new (programming) languages can be designed and analyzed in the similar manner. You will need to analyze a Pascal-and-C-like language in this programming assignment.
Given a segment of the source code, your C++ code should analyze the code and extract all tokens, which include:
Keywords: keywords are the words reserved by the language. They are all upper-case as "BEGIN", "END", "FOR" ...
Identiers: An identier is used to describe the variables, which are all lower-case...
Constants: Numbers like 10 ...
Operators: all arithmetic operations (i.e., +, -, *, and /), "++" and "="
Delimiters: like "," and ";"
Your C++ code should input a FIle from user, which contains the expression he/she wants the compilers to analyze. Then, your code should parse the input, detect the tokens, classify them, and print out the results.
With this assignment, you will get practice with the stack implementation which is one of the most widely used data structures. Besides, you will be familiar with stringprocessing and input parsing, which are of crucial importance in most C++ projects.
To reduce the dificulty, you will be given several lists containing all the keywords,delimiters, and operators which are possibly presented in the input language
Details
1. (Data Structures:) You need to implement a stack data structure to keep track of the processing and compute and depth of the nested loops. Adding elements to the stack (push) and removing objects from it (pop) are two essential methods that must be implemented. You can use any data structure to implement the stack, e.g., arrays, linked-lists, etc.
2. (Algorithms:) Once the input expression is given, your program should decide which character should be inserted to the stack, and when the result needs to be computed. You need to detect the possible syntax errors while tracing the depth of the nested loops.
3. (Keyword List:) "BEGIN", "END", "FOR", "WHILE", "IF", "ELSE", "ELSEIF".
4. (Delimiter List:) "(", ")", ";", ",".
5. (Operator List:) "+", "-" , "*", "/", "++","--", "=", "==", "<", " >" , "&&", "jj" , "!".
6. (Constant List:) Any number can be constant.
Explanation / Answer
#include #include #include #include using namespace std; /* The grammar follwed by this analyser is : you have to give a blank to differentite between two different entities, for eg instead of writing "int c=a+b;", write it as "int c = a + b ;". To execute this program first create an "input.txt" file where the program is written or specify the path of the file which has to be analysed, after compiling an "output.txt" file will be created where you have stored the program. */ bool keycheck(string str) { string keyword[] ={"int","main()","float","if"}; int flag=0; if(!(str[0]>=97 && str[0]Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.