Compiler Lab Assignment 3- Lexical Analysis (LEX/Flex) Solved

35.00 $

Category:

Description

5/5 - (1 vote)

Lab Assignment 3- Lexical Analysis (LEX/Flex)

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.

  1. Data Type : integer (INT/int), floating point (FLOAT/float)
  2. Condition constructs: if
  3. Loop Constructs: for, while
  4. Input / Output Constructs:
    1. read(x) – Read into variable x
    2. print(x) – Write variable x to output
  5. Relational operators, assignment and arithmetic operators
  6. 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) ;

  • lab3_Lexical_Analysis_FLEX-ilmhnq.zip