Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

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

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M93092394

Have any Question?


Related Questions in Computer Engineering

1 explain why fukayama thinks we are at the end of history

1. Explain why Fukayama thinks we are at the "end of history". How do you respond to his contention? 2. Explain what information a Lorenz curve gives you. How is this information summarized by a Gini coefficient. 3. Expl ...

Sorting algorithms are one kind of algorithm whose

Sorting algorithms are one kind of algorithm whose performance may depend upon the data. Choose one of the sorting algorithms or any other algorithm and explain whether the there are any differences in the best, average ...

Task 1implement a queue on a char array do not use queue

Task 1 Implement a Queue on a char [] array. Do not use ::Queue:: class from the STD library for this example. The user will input a string and the program will return the string with each letter in the string duplicated ...

Question a simm has a capacity of 32mx64how many 32mx8

Question : A SIMM has a capacity of 32Mx64. How many 32Mx8 DRAMa are reqquired to make this SIMM? How many address lines does the SIMM require? How should the DRAMs Be connected ?

Discuss why a financial services organization would benefit

Discuss why a financial services organization would benefit from using one framework over another (COSO, COBIT,) -- choose a framework or frameworks that in your opinion would be most ideally suited for such an organizat ...

Lucinda buys a new gaming system fornbsp500 she receives

Lucinda buys a new gaming system for? $500. She receives consumer surplus of? $175 from the purchase. How much does Lucinda value her GPS? system? Please provide the explanation

One of the basic motivations behind the minimum spanning

One of the basic motivations behind the Minimum Spanning Tree Problem is the goal of designing a spanning network for a set of nodes with minimum total cost. Here we explore another type of objective: designing a spannin ...

A student finds that 2496g of water at 249 ordmc density

A student finds that 24.96g of water at 24.9 ºC (density= 0.9971 g/cm3 ) is required to completely fill an empty flask. The water is removed and completely dried; granular solid copper weighing 51.24g is then added to th ...

A scavenger hunt question from the assigned

A scavenger hunt question from the assigned readings.) Consider the statement if ((a! = null)&&(a.x()))a.y(); What feature of the a && b makes it classify as a control structure? Rewrite this as 2 nested if statements. W ...

Alpine company stock is selling for 3240 a share one 30

Alpine Company stock is selling for $32.40 a share. One $30 call is valued at $2.76 and one $29 put is valued at $.65. What is the value of three call option contracts (remember that one contract includes 100 options)?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As