CSE 40243 Fall 2023

Compilers and Language Design Course at the University of Notre Dame

Scanner Assignment

Objectives

The objectives of this assignment are:

Overview

The first step in building a compiler is to create a scanner.

You will use the Flex Scanner Generator to construct a scanner generator for the B-minor Language. It is up to you to carefully read this document and decide what all of the token types are, and define them carefully using flex regular expressions. Make sure that you define all reserved words, identifiers, operators, constants, statements, and any other program elements. If you think that the specification is not clear, be sure to ask for a clarification.

For token types that have a corresponding value, you should extract the necessary value from yytext. For string and character types, use the encoding functions that you wrote in the first assignment. You may make any necessary changes to fix bugs or otherwise adapt the code to this stage.

Make sure that you follow the general instructions for assignments, so that your work runs correctly on the student machines. We want you to have the benefit of using exactly the environment in which your work will be graded.

Requirements

Your program will be invoked as follows:

./bminor --scan sourcefile.bminor

It must print to the standard output the symbolic token types for each element of the input. For literal values (integers, strings, characters) and identifiers, you must also output the body of the token, with the quotes removed and any escape codes translated. If the input contains an invalid token, then the bminor should print a useful message to the standard error stream and exit with status one. Otherwise, it should indicate success by exiting with status zero.

For example, if the input looks like this:

string
1534
3.4
10e9
'a'
Notre Dame
"\'Notre Dame\'";
>=
@

then your output should be something like this:

STRING
INTEGER_LITERAL 1534
FLOAT_LITERAL 3.4
FLOAT_LITERAL 10e9
CHARACTER_LITERAL a
IDENTIFIER Notre
IDENTIFIER Dame
STRING_LITERAL 'Notre Dame'
SEMICOLON
GE
scan error: @ is not a valid character

Testing

As with the previous step, create ten good test cases named test/scanner/good[0-10].bminor that consist of valid B-minor tokens and ten bad test cases test/scanner/bad[0-10].bminor that contain at least one invalid token.

You can also try these example test cases that come with the textbook but note that they don’t cover the features specific to B-Minor 2023. We will evaluate your code using these and some other hidden test cases.

As always, exercise good style in programming by choosing sensible variable names, breaking complex tasks down into smaller functions, and using constructive comments where appropriate.

Ensure that make clean, make, and make test, and continue to work properly.

Turning In

To turn in via github, please review the General Instructions for Turning In. Make sure that your code is tagged as a release named “scanner”.

This assignment is due on Thursday, September 14th at 11:59PM Late assignments are not accepted.