國立清華大學資訊工程學系
CS 3404 --- 編譯器設計
Assignment #2
Due: 5:00 p.m., April 14, 1999
In this assignment, you are going to use lex to implement the scanner
for a small Ada-like language named Macro. The lexical definition for
Macro is given below. You are supposed to read in the source file as
standard input and write the token types and values to the standard
output. For consistency, you are required to use the definition in
"
lexicon.h". Turn in your program in the same way as in the first
assignment.
- Comments:
Single line comment in Macro is delimited by -- (two hyphens)
and the end-of-line character. Moreover, we also have multi-line
comments like in C or Pascal. Multi-line comments are enclosed in a
pair of curly parenthesis, {}. Comments may appear anywhere in the
programs; they are ignored by the scanner. It could be tricky dealing
with end-of-file in comment.
- White spaces:
Spaces and tabs are considered white spaces, so is end-of-line
character. White spaces are to separate tokens, but they will not
appear as tokens.
- Case sensitivity:
Macro is case insensitive.
- Reserved words:
array, begin, body, else, elsif, end, exit, if, loop, procedure,
read, record, then, type, when, write, package, private, in, out, is,
of, integer, float, string, function, return
- Constants:
There are three kinds of constants: integers, float-point numbers and
strings. Integers are consisted of digits only. Float-point numbers
contain a decimal point, where at least one digit must appear at each
side of the decimal point (so we don't have 1. or .2). For simplicity,
we don't have exponential parts here. A string is enclosed in a pair of
double quotation marks, and is limited to within one line. Backslash
works as the escape character as in C.
- Identifiers:
An identifier begins with a character and is followed by a sequence of
letters and/or digits, excluding the reserved words.
- Operators and delimiters:
- Assignment: ":="
- Arithmetic: "+", "-", "*", "/"
- Relation: "<", "<=", ">", ">=", "==", "<>"
- Delimiters: "..", ":", ";", ",", ".", "(", ")", "[", "]"
Acknowledgement
This assignment is from CS 4410/6415 (Introduction to Compiler Design) of Georgia Institute of Technology, offered by Dr. Richard LeBlanc in Fall, 1998.