CD Lab Programs
CD Lab Programs
Source Code/Program:
#include<stdio.h>
#include<string.h>
int main(){
char com[30];
int len;
printf("\nenter comment : ");
gets(com);
len = strlen(com);
if(com[0] == '/' && com[1] == '/'){
printf("it is a single line comment");
}
else if(com[0] == '/' && com[1] == '*' && com[len - 1] == '/' && com[len - 2] ==
'*' ){
printf("it is a multi line comment");
}
else{
printf("it is not a comment");
}
}
Experiment 2:
2. Write a C program to design a lexical analyser for given language, which should ignore the
redundant spaces, tabs, new lines, find the tokens and also count the number of lines using
C program
Procedure:
It is the first phase of the compiler. It gets input from the source program and
produces tokens as output.
It reads the characters one by one, starting from left to right and forms the
tokens.
Token: It represents a logically cohesive sequence of characters such as keywords, operators,
identifiers, special symbols etc.
Example: a+b=20
Here, a,b,+,=,20 are all separate tokens.
Group of characters forming a token is called the Lexeme.
The lexical analyzer not only generates a token but also enters the lexeme into the symbol
table if it is not already there. Its main task is to read the input characters and produce as
output a sequence of tokens that the parser uses for syntax analysis. Upon receiving a "get
next token" command from the parser, the lexical analyzer reads input characters until it can
identify the next token.
Logic/Algorithm:
1. Read the C program as input and stores in a file.
2. Check all the characters from the file from left to right whether character is alphabet or
digit or special symbol.
3. If the input is operator prints as special symbol.
4. If the input is number prints as number.
5. If the input is identifier prints as identifier.
6. If the input is keyword prints as keyword.
7. Print no lines of code
int main()
{
FILE *f1,*f2,*f3;
char c, str[10];
int num[100], lineno=0, tokenvalue=0,i=0,j=0,k=0;
printf("\n Enter the c program : ");/*gets(st1);*/
f1=fopen("input","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("input","r");
f2=fopen("identifier","w");
f3=fopen("specialchar","w");
while((c=getc(f1))!=EOF)
{
if(isdigit(c))
{
tokenvalue=c-'0';
c=getc(f1);
while(isdigit(c))
{
tokenvalue*=10+c-'0';
c=getc(f1);
}
num[i++]=tokenvalue;
ungetc(c,f1);
}
else
if(isalpha(c))
{
putc(c,f2);
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
putc(c,f2);
c=getc(f1);
}
putc(' ',f2);
ungetc(c,f1);
}
else
if(c==' '||c=='\t')
printf(" ");
else
if(c=='\n')
lineno++;
else
putc(c,f3);
}
fclose(f2);
fclose(f3);
fclose(f1);
printf("\n The no's in the program are :");
for(j=0; j<i; j++)
printf("%d", num[j]);
printf("\n");
f2=fopen("identifier", "r");
k=0;
printf("The keywords and identifiers are:");
while((c=getc(f2))!=EOF)
{
if(c!=' ')
str[k++]=c;
else
{
str[k]='\0';
keyword(str);
k=0;
}
}
fclose(f2);
f3=fopen("specialchar","r");
printf("\n Special characters are : ");
while((c=getc(f3))!=EOF)
printf("%c",c);
printf("\n");
fclose(f3);
printf("Total no. of lines are:%d", lineno);
}
Experiment 3:
Write a C program to design a lexical analyzer for given language, which should ignore the
redundant spaces, tabs, new lines, find the tokens and also count the number of lines using lex
tool.
Procedure:
Contents of a lex program:
Declarations
%%
Translation rules
%%
Auxiliary functions
1. The declarations section can contain declarations of variables, manifest constants, and
regular definitions. The declarations section can be empty.
2. The translation rules are each of the form pattern {action}
• Each pattern is a regular expression which may use regular definitions defined in the
declarations section.
• Each action is a fragment of C-code.
3. The auxiliary functions section starting with the second %% is optional. Everything in this
section is copied directly to the file lex.yy.c and can be used in the actions of the
translation rules.
Output:
$ lex lexp.l
$ cc lex.yy.c
$ ./a.out test.c
Procedure:
1. By using finite automata for the given regular expression, we can verify the given input
is accepted or not?
2. If the state recognizes the given pattern rule. Then print string is accepted under
a*|abb.
3. Else print string is not accepted.