Due: Thursday, October 2 2025, 11:59 pm
The first thing we have to do with any programming language is build a lexical analyzer that converts a text string into a series of tokens. There are lots of tools for handling this. For this assignment, you'll make use of the Flex lexical analysis tool and build a few different parsers.
The computers in the robotics lab have flex installed already, so if you have difficulty installing and using flex on your machine, you are welcome to use one of these. You can either physically access or remotely SSH to these machines. To SSH to one of the machines, use the command line ssh yourColbyUserName@machineName where machineName is in the range d122-01 to d122-15. Assume Ying picks d122-03, the command line would be ssh yingli@d122-03
If you want to use your computer for this project and your computer doesn't have Flex installed, here are some resources for Flex installation.
apt-get to install flex. To check whether you machine has flex install, you can use the command line flex --version. If your machine has flex, you should get some information to indicate the flex version (e.g., flex 2.6.4).
Flex is an open source lexical analysis tool. The way it works is you first create an input file that defines symbols and production rules that describe how to parse a text file. Then you run Flex and it produces a C file. You can then compile the C file and run it on some input.
As an example, consider the following Hello World program
for Flex. The line beginning with blah defines a rule that
says to replace instances of blah with hello
world.
/**
* Hello World: replace "sadness" with "happiness""
*/
%%
sadness printf("happiness");
%%
int main ( int argc, char *argv[] ) {
yylex();
return 0;
}
If you type the above into a file called replace.yy and then execute the following commands, you get the resulting behavior.
$ flex replace.yy
$ gcc -o repl lex.yy.c -ll
$ echo "sadness and more sadness" | ./repl
happiness and more happiness
If you'd like to explore beyond the sample code discussed in class, the documentation for flex provides many more examples and a description of the syntax.
Note that flex is based on regular expressions, which are similar to the extended BNF notation we've gone over in class. There are lots of documentation for regular expressions, and they can be very useful in many situations.
Using Flex, make a program called encode that takes any character in a-z or A-Z and shifts it 13 spaces forward in the alphabet, with wraparound from z back to a. Run it on a text file of your choice. You can test that it is working correctly by running the output of encode through encode again. The result should be the original document. Make use of the fact that you can get the ASCII value of any character by wrapping it in single quotes (e.g. 'a' is the value 97). You can treat it as an integer and do math.
Report: Your goal is to describe your pattern-matching code and demonstrate that it works correctly.
Using Flex, make a program that reads in a text file and tells you not only the number of rows and characters, but also how many of each vowel [a, e, i, o, u] are in the file.
Report: Your goal is to describe your pattern-matching code and demonstrate that it works correctly.
Using Flex, make a program that makes your prose slightly more exciting and less numeric. You can think of this as an editor who wants to bring your prose up a level in terms of conventions (use "one" instead of "1") and more emphatic words. But we are going to be very simplistic with the emphatic words. We simply make every "cat" a "lion".
cat within a longer word. You should not care what case the letters are and you should handle both "cat" and "cats".
Report: Your goal is to describe your pattern-matching code and demonstrate that it works correctly.
Implement a parser for Clite. For now, the output of your program should just be a sequence of strings, one per line. Please use the following strings.
| Integer-<value> | A sequence of one or more digits. |
| Float-<value> | An integer, a dot, and another integer. |
| Keyword-<word> | if | else | while | for | int | float |
| Identifier-<name> | Legal names/identifiers. |
| Assignment | = |
| Comparison-<symbol> | == | < | > | <= | >= |
| Operator-<symbol> | + | - | * | / |
| Open-bracket | { |
| Close-bracket | } |
| Open-paren | ( |
| Close-paren | ) |
Ideally, your program should also ignore comments. You can test your program on this example. You should get something like this result.
Report: Your goal is to describe your pattern-matching code and demonstrate that it works correctly.
Select one language that you don't know and want to learn this semester from list A in the Languages section in the syllabus page (Note that it can be the same A language you picked in Project 1 but that it cannot be Python or Java, if you took CS231 and CS25X at Colby).
If you want to learn other selected languages from B/C list, you are encouraged to do so, and it can count your extension. See the Extensions section below for more details.
Organize your Google Sites home page. It should be a top-level page for the course from which a user can navigate to the selected language and other non-C languages you picked as extensions. There should be a section for each language that should have a link to each project page.
Make sure there is a link to your project 1 on your top level page for the course.
Having a properly organized home page is worth 3 points in this project.
Here is a sample page for your reference.
Extensions are a way for you to customize the assignment and learn something of interest to you. Pick a task and dig deeper. Pick a different language and learn something about it. Do something interesting. The following are possible extensions, but you can choose your own.
Please note that it is your responsibility to explicitly indicate the extensions you have undertaken. If you complete extensions for the parsers, please indicate explicitly in the codes and README file. If you complete extensions in selected languages, please indicate explicitly in your Google Sites report. Because extensions can be individual or joint, you need to indicate this along with your description of the extension. If no indication is provided, the grader will assume the extensions were completed jointly.
The submission of this project has two components:
Required Files:
.md filename extension. Each project should have a README file along with all your source codes. The README file will contain:
tree command to generate this.You can follow the instructions for the README file from Project 1.
Please do NOT include: Executable files. On Windows, these have the .exe extension. On Mac and Linux, there is no e xtension. We have found that graders often have difficulty downloading submissions if they contain executable files.
To check whether you've made your report well organized, you may ask yourself the following questions:
Here is a recommended layout.
Ying Li CS333 (Home Page) |_ P1 (subpage) | |_Ying’s Explorations in R (a section on the home page) | | | |_P1 (listed in the section with a link to a subpage for P1) | |_P2 (listed in the section with a link to a subpage for P2) | |_... | |_Ying’s Notes in JS (a section on the home page) | | | |_P1 (listed in the section with a link to a subpage for P1) | |_P2 (listed in the section with a link to a subpage for P2) | |_...
Here is a sample home page that reflects the above layout for your reference. Please log in to Google Sites using your Colby account before clicking the links.
© 2025 Ying Li. Page last modified: .