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

  The checksum field in a single parity bit scheme

What will be the value of the checksum field b for the case of a two-dimensional parity scheme? Your answer should be such that a minimum-length checksum is used.

  Create a wizard that will guide users through steps

Use Travel Expenses.xlsx to create a wizard that will guide users through a few steps to enter some information used to calculate travel expenses.

  Write down a program which compute the average of a group

write a program that calculate the average of a group of test scores where the lowest score is dropped. it should use

  Determine whether version with or without sentinel is faster

Determine whether the version with or without a sentinel is faster. For what value of n is the extra time needed to insert a sentinel at the end of a list.

  Why should uninitialized pointers be set to null

Give two reasons why dynamic memory allocation is valuable. Why should uninitialized pointers be set to NULL? What is an alias and why is it dangerous?

  Write a program that reads in a set of countries

Design and implement a class Country that stores the name of the country, its population, and its area. Then write a program that reads in a set of countries.

  Give instruction to draw a string consisting of the letter V

Give instructions to draw a letter V by drawing two line segments. Give instructions to draw a string consisting of the letter V.

  Who needs to be on the incident response team

In today's highly connected world, cyber incidents happen on a daily basis. Every corporation from the very large to small and medium (SME) companies need.

  How are software components organized to perform functions

An OS acts as both client and server. How are software components organized to perform both functions at the same time?

  Define doppler spread and multipath spread

Repeat Illustrative Problem for a channel with bandwidth W multipath spread Tm = 10 µs and a Doppler spread Bd = 10 Hz.

  Develop the micro operations needed to manage the buffer

Assume that ASC memory is built using eight interleaved blocks of 8 Kwords each. Include an instruction buffer eight words long into the ASC control unit.

  Discuss specific current enterprise strategic requirements

Discuss the specific current or future enterprise strategic requirements that could benefit from the emerging technologies. Discuss the potential benefits.

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