$25
Installing Flex: sudo apt-get update sudo apt-get install flex
Editor for writing Lex/F1ex program: Use any text editor
How to compile/ execute: Check the lecture notes shared related to LEX/Flex tool.
SECTION 1
Q 1.1 Write a LEX/Flex program that recognizes binary strings containing odd number of O's.
Q 1.2 [Optional] Write a LEX/Flex program that recognizes binary strings containing odd number of Us and even number of I's.
Q 1.3 Write a LEX/Flex program that recognizes binary strings whose integer equivalent is divisible by 3.
SECTION 2
Q2. We had discussed about the lexical analyzer generator Lex/ Flex. Consider the example grammar for branching statements discussed in the class given below:
stmt -+ if expr then stmt if expr then stmt else stmt
exprterm relop term term termid number
The patterns for the tokens in the language are described below (that we have also considered in the previous assignment):
digit [0-9) digits number digits digits)? ( E 1+—1? digits )?
letter letter ( letter I digit
then else
Q 2.1. Write a Lex/Flex program to describe the tokens of the above grammar, and generate a lexical analyzer using the Lex/Flex tool.
Q 2.2. Test the lexical analyzer with some input strings (You should show and explain the output of the lexical analyzer for the considered examples).
SECTION 3
Q 3. Construct a lexical analyzer for the following simple "C" like language using the Lex/Flex tool.
Data Type : integer (INT/int), floating point (FLOAT/float)
Condition constructs: if
Loop Constructs: for, while
Input / Output Constructs:read(x) - Read into variable x
print(x) - Write variable x to output
Relational operators, assignment and arithmetic operators
Only function is main(), there is no other function.
You may test it using the below example:
Example Input:
main ( )
INT 1=0;
INT sum=0; INT count;
read (count) ; for (1=0; 1<10; i++)
read (x) ; sum+=x; print (sum) ;