C++代写:CS211LexicalAnalysis


代写一个精简版的词法解析器,能够对简单语言进行解析。

Problem Description

In this problem, you should implement the lexical analysis task for a 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 uppercase. In the case of this simplified language, the keywords are restricted to the set { "BEGIN", "END", "FOR" }
  • Identifiers: An identifier 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 text 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 string processing and input parsing, which are of crucial
    importance in most C++ projects.

Details

  1. (Data Structures) You need to implement a stack data structure to keep track of the processing and compute the 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.

Example Run

Try to keep your output as close to the given format as possible:
In this example, the input file is “code.txt”
It contains the code segment below:
FOR (i, 10, ++)
BEGIN
FOR (j, 10, ++)
BEGAN
sum=sum + i + j;
END
END
—|—
> ./pa3.out
INPUT> Please enter the name of the input file:
code.txt
OUTPUT> The depth of nested loop(s) is 1
Keywords: FOR BEGIN END
Identifier: sum i j
Constant: 10
Operatros: ++ = +
Delimiter: ; ,
Syntax Error(s): BEGAN

Hints

  1. You should get your stack data structure working well before implementing the lexical analysis task.
  2. The string processing to parse the input is an essential part of this assignment. You should make sure that you parse the input correctly, and you take care of all edge cases, e.g., more than one spaces between characters, no spaces, etc.

Grading Criteria

  1. Successful Compilation: Your source code should be able to compile using g++ -Wall command without any error or warning. The output should be a valid executable. Please note that we will be using g++ compiler on Linux to grade your programs. If you are using other compilers or IDE (e.g., Visual C++), it is recommended that you test the source codes with g++ before the CANVAS submission (i.e., make sure there is no warning).
  2. Program Correctness: The executable should be able to run correctly by giving
    out the required output.
  3. Programming Style: Good coding style is a key to efficient programming. We
    encourage you to write clear and readable codes. You should adopt a sensible
    set of coding conventions, including proper indentation, necessary comments
    and more. Here are some guidelines of good programming style.

Final Notes

Again, remember to start the programming assignments as soon as possible.
Unlike the conventional assignments, programming assignments sometimes take
un-predictable amount of time to finish. Thus, have the code running first,
then polish it later with the extra time before the deadline.


文章作者: SafePoker
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 SafePoker !
  目录