Lab Assignment--Tokenizing and Parsing
Lab Code
To get the sample and exercise code, please use the following commands in the directory that you have created for this lab:
wget www.labs.cs.uregina.ca/330/Linux/Lab3.zip
unzip Lab3.zip
Part 1-Examine and Modify Existing Code
Use the starting code
(files: HALmod.cpp, HALmod.h, and main.cpp)
- Store the answer to the following questions as comments at the beginning
of main.cpp:
- What is Dr. Hilderman's code tokenizing based on (ie. what delimeter(s) separate(s) his words)?
- What are two reasons why his tokens are strings instead
of c strings
- Add functionality to the processCommand function so that
"lo" will also cause the shell to exit
Part 2-Extend the Code
We want to be able to turn the tokens array into an array of
c strings (In other words, in the format of char **).
Build the following functions and call them from main:
- char ** convertToC (string tokens [], int tokenCount);
- This converts the tokens into a c version of an array of words and returns the pointer to that array
- void cleanUpCArray (char ** cTokens, int tokenCount);
- This cycles through the c string version of the array and
frees up any memory that has been allocated
- void printReverse (char ** cTokens, int tokenCount);
- This cycles in reverse through the c string version of the words
and prints each word
A sample run might look something like the following
a049840[8]%./demo
HALshell> This is a test
The words in reverse are: test a is This
HALshell> to see what happens
The words in reverse are: happens what see to
HALshell> It is a success
The words in reverse are: success a is It
HALshell> lo
The words in reverse are: lo
HALshell: terminating ...
When you run your code through valgrind using:
valgrind --leak-check=yes ./demo
A message like the following should be displayed:
==23683==
==23683== HEAP SUMMARY:
==23683== in use at exit: 0 bytes in 0 blocks
==23683== total heap usage: 46 allocs, 46 frees, 992 bytes allocated
==23683==
==23683== All heap blocks were freed -- no leaks are possible
==23683==
==23683== For counts of detected and suppressed errors, rerun with: -v
==23683== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Deliverables:
Submit 4 files to URCourses:
- code for HALmod.cpp
- code for HALmod.h
- code for main.cpp
- Remember that in comments you should have answers to the questions in Part 1
- script of run using valgrind
Notes
- Do not create a zip file
- Submission is on UR Courses
- Submit your own work
- Be mindful that your assignment is due two hours before the start of your next lab