CMPE 13 Winter 18 LAB 02

Due on JAN 25 @11:59pm

High Level Description:

● Build a scanner for a “calculator” like language with support for comments

Lab Description:

● Create a lab02 directory at gitlab

● Write a scanner for C-like expressions a = 3+4*(123+a != b)


foo = (33^0x33|123) && a==3 || b<3

● Support C++ style comments // and /* foo /* bar */ */

● Tokens in the commented region are not reported

●  For each token found, call add_token(char *start, int tok, int len)

● Google test inside lab02/tests/`test1.cpp`

● Must have a lab02/makefile

● Code inside lab02/src/`calculator.cpp` and lab02/src/calculator.h

● Commit only the needed files (extra files will result in point loses)

● For the test1.cpp, the add_token should be used for testing the functionality of calculator. The TAs will provide other add_token code for testing, so make sure to keep the function in test1.cpp


● int calculator(char *text)

● Inside calculator.cpp, returns the number of tokens found

● Int add_token(char *start, int tok, int len)

○ Inside test1.cpp, used for testing

○ Start is the beginning position of the token (char *)

○ Tok is the token type:

■ 0 (not valid)

■ 1 variable

■ 2 number

■ 3 operator (,),*,-,+,<,==,>=,<=,/,%,^,&&,&,|,||

■ 4 assign =

○ Len is the number of characters for the token

○ variables is a continuous sequence of alphanumeric (alnum) characters separated by spaces or non alphanumeric characters. The first character must not be numeric

○ number is also a continuous sequence of alnum characters, but the first character must be numeric (0-9)

Example execution:

char *tst1 = "a = 3+3	*588 foo"; 

int tst1_n_tokens = calculator(tst1);
// Calls:
// add_token(a,1,1)
// add_token(=,4,1)
// add_token(3,2,1)
// add_token(+,3,1)
// add_token(3,2,1)
// add_token(*,3,1)
// add_token(588,2,3)
// add_token(foo,1,3) assert(tst1_n_tokens==8);```

High level pseudo-code:

// You can do a different algorithm or structure. This is a hint to better understand the

// expected functionality and to provide a starting point for your program.

// A direct translation of this pseudo-code to C will NOT work. It is a guide, it does

// not cover corner cases. E.g: it does not handle end of file, or it is vague about spaces

// or the “set token based on”

calculator(text) in_nested_comments = 0
for each character in text if beginning_comment
call add_token if in_numeric, in_variable clear In_numeric and in_variable
call add_token if token_next token_next = 0
else if end of comment in_nested_comment--
else if in_numeric and character is alnum still in_numeric
else if in_variable and character is alnum still in_variable
else if in_two_char_token_option if valid_second_char
call add_token with new token else
call add_token with old single_character_token token_next = set token based on second_character_token
call add_token if in_numeric, in_variable clear In_numeric and in_variable
token_next = set token based on current_character_token if token_next can be 2 token set in_two_char_token_option```

Submission: You will submit the code using gitlab. As mentioned above, Lab is due on JAN 25 @ 11:59PM. We will clone all repos at midnight and will not use git tags. As a result if you wish to make and fail at a last minute change and push (hence only submitting code changes made hours/days before), it’s on you

Evaluation/Review: Your repos will be graded by the TAs for:

1) Containing the aforementioned files and directory structure(same file and dir names)

2) Proper working of make, make clean, make test (points will be deducted for any errors, compilation, runtime, extra files, or other mistakes)

3) Inspection of code (this includes checking for comments, code style and cheating)

4) Test quality (comments, different tokens…)

5) Being able to pass TAs tests (google test)

6) The evaluation rubric and points you get will be posted on canvas