0% found this document useful (0 votes)
44 views2 pages

Mehran University of Engineering and Technology, Jamshoro Deparment of Computer Systems Engineering Subject: Compiler Construction

The document describes a C program to implement a lexical analyzer. The program takes in an input string from the user, splits it into tokens and stores them in arrays. It then prints out the tokens identifying them as arithmetic operators, assignment operators, constants, identifiers or other. It also provides tasks to identify tokens in a given program and count prefixes, suffixes and substrings in a string.

Uploaded by

Rabia Jiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views2 pages

Mehran University of Engineering and Technology, Jamshoro Deparment of Computer Systems Engineering Subject: Compiler Construction

The document describes a C program to implement a lexical analyzer. The program takes in an input string from the user, splits it into tokens and stores them in arrays. It then prints out the tokens identifying them as arithmetic operators, assignment operators, constants, identifiers or other. It also provides tasks to identify tokens in a given program and count prefixes, suffixes and substrings in a string.

Uploaded by

Rabia Jiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Mehran University of Engineering and Technology, Jamshoro

Deparment of Computer Systems Engineering


Subject: Compiler Construction
LAB#4

OBJECT: IMPLEMENTATION OF LEXICAL ANALYZER


AIM: WRITE A C PROGRAM TO IMPLEMENT A LEXICAL ANALYZER
Algorithm:
1.
2.
3.
4.
5.
6.

Start the program


Declaraed the required variables
Read the expression from the keyboard or user
Split and store token in corresponding arrays using for loop and if else statement
Print all array values as per the sequence
Stop the program

#include <stdio.h>
#include <string.h>
Void main()
{ Char input[50], store[20];
Int i,j;
Printf(\neneter the input );
Scanf( %s,input);
Printf(%s,input);
Printf(\n);
For (i = 0 ; input[i] != NULL ;i++)
{
If(input[i] != : )
{
Store[i] = input[i];

If (store[i] ==+ || store[i] == - || store[i]== * || store[i]== /)


Printf( %c arithmatic operator \n, store[i]);
Else if (store[i] == =)
Printf(%c assignment operator \n, store[i]);
Else if (isdigit( store[i]))
Printf(%c

constant \n, store[i]);

Else
Printf(%c identifier \n ,store[i]);
}
Else printf(\n);
}
}
Tasks:
1. Identify the lexemes that makes up the tokens in the following program. Give reasonable
attribute values for the tokens
int max ( i , j ) int i , j;
{
Return i>j ? i:j;
}
2. In a string of length n, how many of the following are there?
a) Prefixes
b) Suffixes
c) Substrings

You might also like