Implement the lexical analysis task for a limited version

Assignment Help Computer Engineering
Reference no: EM132104925

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 upper- case. 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:

Text Case I

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

Test Case II

FOR (i, 10, ++))
BEGIN
sum=sum + i + j;
INPUT> Please enter the name of the input file: code.txt

OUTPUT> The depth of nested loop(s) is 0

Keywords: FOR BEGIN Identifier: sum i j Constant: 10 Operators: ++ = + Delimiter: ; ,

Syntax Error(s): END )

Text Case III

FOR (i, 10, ++) BEGIN FOR (j, 10, ++) BEGIN sum=sum + i + j; END FOR (k, 5, ++) BEGIN
mul=mul * k;
END
END
INPUT> Please enter the name of the input file: code.txt

OUTPUT> The depth of nested loop(s) is 2 Keywords: FOR BEGIN END
Identifier: sum i j mul k
Constant: 10 5

Operators: ++ = + * Delimiter: ; , Syntax Error(s): NA

Reference no: EM132104925

Questions Cloud

How many such matrices are there in each : In this problem, we are concerned with two dimensional arrays (matrices) that have 20 rows and 20 columns; further, each entry is a member of {1, 2, 3}.
When reviewing the roles of management accounting : When reviewing the roles of management accounting within a company, what is the most important role of management accounting, and how is that different than fin
Manufacturing costs for the period : West Co.'s manufacturing costs for the period just ended were as follows:
Allocation rate and the standard fixed overhead allocation : Compute the standard variable overhead allocation rate and the standard fixed overhead allocation.
Implement the lexical analysis task for a limited version : You should implement the lexical analysis task for a limited version (i.e., the depth of the nested loops) of a programming language.
Write an assembly program to read an integer : Write an assembly program to read an integer and count the number of ‘1' bits in it's binary representation. Label and print the output.
Write the sql code that will create the table structure : Write the SQL code that will create the table structure for EMPLOYEE table (Note that the JOB_CODE is the FK to JOB.)
A program to process customer requests to fly : The Flying Traveller Airline Company (FTAir) wants a program to process customer requests to fly from some origin city to some destination city.
Design a class named player with fields : Design a class named Player with fields for holding a Women's Basketball player's statistics. All fields should be private.

Reviews

Write a Review

Computer Engineering Questions & Answers

  What are the relevant issues surrounding firewall deployment

What are the relevant issues surrounding firewall deployments? How does one balance security requirements with usability requirements?

  Why has functional programming never become dominant

Imperative programming was the dominant paradigm from the dawn of computing until about 1990. Why has functional or logic programming never become dominant?

  Describe the software engineering process

Describe the software engineering process, the challenges in managing software development activities, and the potential interface issues from the software.

  Determine the high- and low-level noise immunities

Draw the circuit diagram of a three-input CMOS NOR gate with one input at 0 V and the other inputs at V, showing all the voltage levels.

  Thadvantages and disadvantages of using pass by reference

Discuss the pros and cons of static and dynamic allocation of memory in embedded applications. Be certain to address the circumstances under which there might be potential problems.

  Design the calculate button the accept button

You have been hired by an engineering company to develop software to perform advanced geometric calculation. Your new boss asked you to develop an application that allows engineers to calculate the surface area and the volume of a sphere. For this..

  What are the new innovation today in the given area

Microprocessor: Discuss what are the new innovation today in this area and what effect this area have on the computer field.

  What constitutes the foundation of internet communication

What constitutes the foundation of Internet communication? What is the role of the IP protocol? What is a protocol stack and why is it layered?

  How the products can be used in building the network

Discuss how those products can be used in building the network within an organization with respect to the advantages and disadvantages of each one.

  Write a program to perform the given operation

The Last National Bank has two branches, each of which uses a sequential containing a summary of customers' account. Write a program to perform this operation.

  What antimalware tools are available for windows

What antimalware tools are available for Windows? How do you select the best ones? What does it mean to 'quarantine' malware and other malicious software?

  What information will you need to find out

Describe how you would decide on the computing architecture for the new system, using the criteria presented in this chapter. What information will you need to find out before you can make an educated comparison of the alternatives?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd