Experiment no 1
Problem Statement : Write a
program in C/Cpp to identify keywords, identifiers and special symbols in a
given program.
Methodology:
1. Create a c
program.
2. Open the *.c
file/program in the read mode
4. Display each
data structure i.e. keywords, identifiers and special symbols.
Theory :
In
the fundamentals of language processing we know there are two steps
analysis and synthesis as,
Language
processing= Analysis of Source program + Synthesis of target program.
The first operand, The Analysis of
source program, consists of three components as
i) Lexical Analysis
ii) Syntax Analysis
iii) Semantic Analysis
The lexical analysis identifies the
operators, constants and identifiers. Let us take an example to see how a
lexical units are formed by the lexical analysis.
e.g.
percent_profit=(profit*100)/cost_price;
In the above statement lexical
analysis identifies
i) = ,
* ,
/ as operators
ii)100 as constant
iii) percent_profit , profit
& cost_price as identifiers.
Keywords:
In the program there are
some words which has special meaning already defined in the compiler , called
keywords.
e.g. int, char,
if , goto etc.
Identifiers:
Identifiers
are the lexical tokens we can call these as variables. There are some rules to
define the variable. Hence some variables are listed as follows:
e.g. int
a,b,c;
In the above
example a, b, c are three variables.
Special Symbols:
There may be some
symbols present in the statement, there may be
i) *,/,+,- which
are used in the program as a operator.
ii) ; to end the
statement( except if, else, for etc) etc.
For the lexical analysis we have
a *.c program as follows:
void
main()
{
int
a;
char
b;
float
c;
}
In the above program we can see
that there are three data types int, char & float along with a, b, c as
identifiers.
The program will give the
following output:
Keywords are int, char, float
Identifiers are a,b,c
Symbols are , ;;;
Frequently asked questions:
2. What is the difference between analysis
and synthesis?.
3.
What is mean lexical analysis?
4.
Write down different keywords, identifiers and symbols used in the
program.
CODE:
input files:
1. IN.txt
ADD BREG,C
MOVE AREG,A
MOVE BREG,B
SUB AREG,B
ADD BREG,='10'
2. KEY_IN.txt
MOVE
ADD
SUB
MUL
3.REG_IN.txt
AREG
BREG
CREG
DREG
4. MAIN program
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
# define MAX 80
# define SI 30
void main()
{
char str[SI],str1[SI],str2[SI],strc[SI];
clrscr();
fstream f,f1,f2,f3;
f.open("sp\\in.txt",ios::beg|ios::in);
while(f.getline(str,MAX))
{
strcpy(strc,str);
strcpy(str1,strtok(str," "));
f1.open("sp\\key_in.txt",ios::beg|ios::in);
while(f1.getline(str,MAX))
{
if(strcmp(str,str1)==0)
{
cout<<"\n"<<str<<"is Memonics";
break;
}
if(f1.eof())
break;
}
f1.close();
strcpy(str,strc);
strcpy(str,strtok(str," "));
strcpy(str,strtok(NULL," "));
strcpy(str2,strtok(str,","));
f1.open("sp\\reg_in.txt",ios::beg|ios::in);
while(f1.getline(str1,MAX))
{
if(strcmp(str1,str2)==0)
{
cout<<"\n"<<str<<"is Register";
break;
}
if(f1.eof())
break;
}
f1.close();
strcpy(str,strc);
strcpy(str,strtok(str," "));
strcpy(str,strtok(NULL," "));
strcpy(str2,strtok(str,","));
strcpy(str2,strtok(NULL,","));
if(!strstr(str2,"="))
{
cout<<"\n"<<str2<<"is Symbol";
}
else
cout<<"\n"<<str2<<"is Literal";
if(f.eof())
break;
}
f.close();
getch();
}
Comments
Post a Comment