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

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

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