You are on page 1of 4

Course Title Compiler Design Course Code CSCI 372

Semester Spring 2024 Date January 22, 2024

Project (Phase 1: Scanner)

Instructions:
 Please submit your project as mentioned in deliverables section below.
 The deadline for submitting the project is February 16, 2024.

Learning Objectives:
 Design a new programming language.
 Implement a scanner (lexical analyzer) for it.

Programming Language
 C
 Lex, Flex, Win_Flex

Deliverables
 Scanner (Lex file).
 Project report (PDF) mentioning:
o The names of the team members with university ID
o Scanning examples (2) without lexical errors (screenshots).
o Scanning examples (2) with lexical errors (screenshots).

Project Description
The aim of this project is to design a scanner for a new programming language (let's call it BETA). The student
should use a LEX tool to generate the scanner.
1) The scanner reads the input program and output the following tokens with their corresponding lexemes:
 FLOAT, INT
 IDENTIFIER, CONSTANT
 COMMA, SEMICOLON, LEFTPAR, RIGHTPAR
 PROGRAM, BODY, END, ENDPROGRAM
 READ, WRITE
 FOR, IS, IN
 PLUS, MINUS, MULTIPLY, DIVIDE
 COMPARE, QUESTIONMARK
 QUOTATION, STRING
2) The scanner should ignore blanks, tabs, and lines that start with # (comments).
3) The scanner should check for lexical errors and point out their position (line number and column number).
Examples of lexical errors:
 Appearance of illegal characters (&, @, !, ...).
 Illegal identifiers (do not start with a letter like 3employees).
 Illegal constants (e.g., 1.23.45)

Here is an example of an input program written in BETA language: (case insensitive)

# Global variable declarations


float x, y; # real numbers
int z; # integer

# Main program declarations


program
# local variables
int k;
float q;

body
# reading variables
read(x);
read(y);

# assignment and expressions


z is x + y – 2*3.5/(x + 2);

# if-statement
(x >= y) ? # =, ~, <, <=, >, >=
body
x is x + 1;
write(z);
end

# loops with number of iterations


for i in (1,n)
body
write(“Hello World!”);
x is x + 1;
end
end
endprogram

Here is an example of a scanner's output after scanning the above program:

******** *********
*Tokens* *Lexemes*
******** *********
FLOAT
IDENTIFIER x
COMMA
IDENTIFIER y
SEMICOLON
INT
IDENTIFIER z
SEMICOLON
PROGRAM
INT
IDENTIFIER k
SEMICOLON

FLOAT
IDENTIFIER q
SEMICOLON
BODY
READ
LEFTPAR
IDENTIFIER x
RIGHTPAR
SEMICOLON
READ
LEFTPAR
IDENTIFIER y
RIGHTPAR
SEMICOLON
IDENTIFIER z
IS
IDENTIFIER x
PLUS
IDENTIFIER y
MINUS
CONSTANT 2
MULTIPLY
CONSTANT 3.5
DIVIDE
LEFTPAR
IDENTIFIER x
PLUS
CONSTANT 2
RIGHTPAR
SEMICOLON
LEFTPAR
IDENTIFIER x
COMPARE >=
IDENTIFIER y
RIGHTPAR
QUESTIONMARK
BODY
IDENTIFIER x
IS
IDENTIFIER x
PLUS
CONSTANT 1
SEMICOLON
WRITE
LEFTPAR
IDENTIFIER z
RIGHTPAR
SEMICOLON
END
FOR
IDENTIFIER i
IN
LEFTPAR
CONSTANT 1
COMMA
IDENTIFIER n
RIGHTPAR
BODY
WRITE
LEFTPAR
QUOTATION
STRING HELLO WORLD!
QUOTATION
SEMICOLON
IDENTIFIER x
IS
IDENTIFIER x
PLUS
CONSTANT 1
SEMICOLON
END
END
ENDPROGRAM

Statistics:
***********

Number of FLOAT : 2
Number of IDENTIFIER: 20
Number of COMMA : 2
Number of SEMICOLON: 11
Number of INT : 1
Number of PROGRAM: 1
Number of BODY: 3
Number of READ: 2
Number of LEFTPAR: 7
Number of RIGHTPAR: 7
Number of IS: 3
Number of PLUS : 4
Number of MINUS : 1
Number of CONSTANT: 6
Number of MULTIPLY: 1
Number of DIVIDE : 1
Number of COMPARE: 1
Number of QUESTIONMARK: 1
Number of WRITE: 2
Number of END: 3
Number of FOR: 1
Number of IN: 1
Number of QUOTATION: 2
Number of STRING: 1
Number of ENDPROGRAM: 1

You might also like