Professional Documents
Culture Documents
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)
body
# reading variables
read(x);
read(y);
# if-statement
(x >= y) ? # =, ~, <, <=, >, >=
body
x is x + 1;
write(z);
end
******** *********
*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