Lab #2 Using
a vector of vectors for storing
tokens of a given source program and another vector of vectors for the
corresponding token category information
- Download and
examine the type definitions regarding perLineTokenVector,
vectOfTokenVects, tokenCategory,
perLineCategoryVector, and vectOfCategoryVects in the beginning of this .cpp file. Create a C++ project to compile this
file and add code in the main function to practice how we may store the
tokens of the two-line sample source program below and their category information in
terms of vectors of vectors as described in the following.
- "radius=2;
",
- "area=3.14*radius*radius;"
- Declare a vectOfTokenVects
object (i.e. a vector<perLineTokenVector>
object), say vOfT, for storing the tokens
of the sample source program above.
- Declare a perLineTokenVector
object (i.e. a vector<string> object), say T, and call this object T
to perform the push_back
method 4 times to store the 4 tokens "radius", "=",
"2", and ";" as 4 strings respectively
in it. Call vOfT to perform the push_back method and store a copy of T in the end of vOfT.
- Call this object T
to perform the clear method to
empty itself as an empty vector and then call this object T to perform the push_back
method 8 times to store the 8 tokens "area",
"=", "3.14", "*", "radius", "*",
and "radius", ";" as 8 strings
respectively in it. Call vOfT to
perform the push_back
method and store a copy of T in the end of vOfT.
- Write a 2-level nested loop to print out from vOfT all the
tokens of the two-line sample source program.
- Declare a vectOfCategoryVects
object (i.e. a vector< perLineCategoryVector
> object), say vOfC, for storing the
category information of the tokens of the sample source program above.
- Declare a perLineCategoryVector
object (i.e. a vector<tokenCategory>
object), say C, and call this
object C to perform the push_back
method 4 times to store the 4 tokenCategory symbols ID_NAME, ASSIGNMENT_OP,
NUMERICAL_LITERAL, and SEMICOLON corresponding to the categories of the
4 tokens in the first line of the sample program respectively into C. Call vOfC to perform the push_back method and
store a copy of C in the end of vOfC.
- Call this object C
to perform the clear method to
empty itself as an empty vector and then call this object C to perform the push_back
method 8 times to store the 8 tokenCategory symbols ID_NAME, ASSIGNMENT_OP,
NUMERICAL_LITERAL, NUMERICAL_OP, ID_NAME, NUMERICAL_OP,
ID_NAME, and SEMICOLON
corresponding to the categories of the 8 tokens in the second line of the
sample program respectively into C.
Call vOfC to perform the push_back method and store a copy of C in the end of vOfC.
- Write a 2-level nested loop to print out from vOfC the
category information of all the tokens of the two-line sample source
program.