Using a stack data structue, in C++, how would I write a program that takes in a
ID: 645080 • Letter: U
Question
Using a stack data structue, in C++, how would I write a program that takes in an input file by the user specifying the file path, and the file contains the expression he/she wants the compiler to analyze. Then, my code should parse the input, detect the tokens, classify them, and print out the results. I need to implement a stack data structure to keep track of the processing and compute and 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. I can use any data structure to implement the stack, e.g., arrays, linked-lists, etc. Once the input expression is given, the program should decide which character should be inserted to the stack, and when the result needs to be computed. I need to detect the possible syntax errors while tracing the depth of the nested loops.
I am given several lists containing all the keywords, delimiters, and operators which are possibly presented in the input language. Here they are. (Keyword List:) BEGIN, END, FOR, WHILE, IF, ELSE, ELSEIF. (Delimiter List:) (, ), ;, ,. (Operator List:) +, ?, *, /, ++, ??, =, ==, , &&, ||, !. (Constant List:) Any number can be constant.
Heres an example run:
Input File:
FOR (i, 10, ++)
BEGIN
FOR (j, 10, ++)
BEGAN
sum=sum + i + j;
END
END
> ./pa3.out
INPUT> Please enter the file path:
/user/test/code.txt
OUTPUT> The depth of nested loop(s) is 2
Keywords: FOR BEGIN END
Identifier: sum i j
Constant: 10
Operators: ++ = +
Delimiter: ; , ( )
Error(1): BEGAN
Input File:
i=3;
WHILE(i <100)
BEGIN
i=i*(i-1));
> ./pa3.out
INPUT> Please enter the file path:
/user/test/code.txt
OUTPUT> The depth of nested loop(s) is 1
Keywords: WHILE BEGIN END
Identifier: i
Constant: 3, 100
Operators: = < - *
Delimiter: ; , ( )
Error(1): Invalid parentheses.
Error(2): Missing END.
If nothing else, then any helpful advice for where to begin and for the process would be appreciated.
Explanation / Answer
Ans : I am unable to find the write meaning of your question but the I am providing will help you to reach your goal.
program:
example 1:
// using ofstream constructors.
#include <iostream.h>
#include <fstream.h>
using namespace std;
int main () {
ofstream outfile ("test.txt");
char buffer[] = "This buffer contains 34 characters ";
// >> i/o operations here <<
outfile.write (buffer, 35);
outfile.close();
return 0;
}
Example 2:
#include <iostream.h>
using namespace std;
// Write a simple C type program to open a file and write data to that
int main () {
FILE *pFile;
pFile = fopen ("writefile.txt", "wt");
char buffer[] = "This buffer contains 34 characters ";
char buffer2[] = "This is the second line ";
fwrite (buffer , 1 , 35 , pFile);
fwrite (buffer2, 1, 25, pFile);
close (pFile);
return 0;
cout << "End ";
}
example 3 from Let Us C++ By "Yashavant Kanetkar" page 423.
/* Count chars, spaces, tabs and newlines in a file */
# include "stdio.h"
main( )
{
FILE *fp ;
char ch ;
int nol = 0, not = 0, nob = 0, noc = 0 ;
fp = fopen ( "PR1.C", "r" ) ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
noc++ ;
if ( ch == ' ' )
nob++ ;
if ( ch == ' ' )
nol++ ;
if ( ch == ' ' )
not++ ;
}
fclose ( fp ) ;
printf ( " Number of characters = %d", noc ) ;
printf ( " Number of blanks = %d", nob ) ;
printf ( " Number of tabs = %d", not ) ;
printf ( " Number of lines = %d", nol ) ;
}
Example 4 from Let Us C++ By "Yashavant Kanetkar" page 431.
/* Writes records to a file using structure */
#include "stdio.h"
main( )
{
FILE *fp ;
char another = 'Y' ;
struct emp
{
char name[40] ;
int age ;
float bs ;
} ;
struct emp e ;
fp = fopen ( "EMP
LOYEE.DAT", "w" ) ;
if ( fp == NULL )
{
puts ( "Cannot open file" ) ;
exit( ) ;
}
while ( another == 'Y' )
{
printf ( " Enter name, age and basic salary: " ) ;
scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
fprintf ( fp, "%s %d %f ", e.name, e.age, e.bs ) ;
printf ( "Add another
record (Y/N) " ) ;
fflush ( stdin ) ;
another = getche( ) ;
}
fclose ( fp ) ;
}
/* Read records from a file using structure */
#include "stdio.h"
main( )
{
FILE *fp ;
struct emp
{
char name[40] ;
int age ;
float bs ;
} ;
struct emp e ;
fp = fopen ( "EMPLOYEE.DAT", "r" ) ;
if ( fp == NULL )
{
puts ( "Cannot open file" ) ;
exit( ) ;
}
while ( fscanf ( fp, "%s %d %f", e.name, &e.age, &e.bs ) != EOF )
printf ( " %s %d %f", e.name, e.age, e.bs ) ;
fclose ( fp ) ;
}
The answer is provided on the bases of my knowledge and information on the subject hope you had understand.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.