Friday, 26 January 2018

Write the following programs with LEX Utility Tool System Programming GTU 2150708



a) Write a LEX program to check whether input symbol is number or character.

Program:-
%{
            #include<stdio.h>
%}

%%
       [a-z] printf("It's a Character...\n");
       [0-9] printf("It's a Number...\n");
%%
int yywrap()
{
            return 1;
}
int main()
{
            yylex();
            return 0;
}

Output:-

[root@SAR117 Desktop]#lex p1.lex
[root@SAR117 Desktop]#gcc lec.yy.c
[root@SAR117 Desktop]#./a.out
d
It's a Character...

6
It's a Number…

b) Write a LEX program to check whether input is multi-digit number or string.

Program:-
%{
            #include<stdio.h>
%}

%%
[a-zA-Z]+ printf("It's  a  Multichar String...");
[0-9]+ printf("It's  a  Multidigit Number...");
%%
int yywrap()
{
            return 1;
}
int main()
{
            yylex();
            return 0;
}




Output:-

[root@SAR117 Desktop]#lex p2.lex
[root@SAR117 Desktop]#gcc lec.yy.c
[root@SAR117 Desktop]#./a.out
hello
It's  a  Multichar String...

226
It's  a  Multidigit Number...


c) Write a LEX program to check whether inputted character is vowel or not.

Program:-
%{
            #include<stdio.h>
%}
%%
[a|A,e|E,i|I,O|o,u|U]+ printf("The Character is a vowel\n");
\n
[^a|A,e|E,i|I,O|o,u|U]+ printf("The Character is not a vowel\n");
%%
int yywrap()
{
return 1;
}
int main()
{
yylex();
return 0;
}



Output:-

[root@SAR117 Desktop]#lex p3.lex
[root@SAR117 Desktop]#gcc lec.yy.c
[root@SAR117 Desktop]#./a.out
A
The Character is a vowel

dd
The Character is not a vowel




d) Write a LEX program to check whether a word starts with vowel or not.

Program:-
%{
            #include<stdio.h>
%}

%%
[aeiouAEIOU][a-zA-Z]+ printf("Start’s  with a  vowel");
[a-zA-Z^aeiouAEIOU]+ printf("Does not start with a vowel");

%%

int yywrap()
{
            return 1;
}
int main()
{
            yylex();
            return 0;
}



Output:-

[root@SAR117 Desktop]#lex p4.lex
[root@SAR117 Desktop]#gcc lec.yy.c
[root@SAR117 Desktop]#./a.out
Hey
Does not start with a vowel

Ink
Start’s  with a  vowel


e) Write a LEX program to count the number of characters, words and lines from a given input file.

Program:-
%{
#include<stdio.h>
int w=0,l=0,c=0;
%}
%%
[a-z]+[ .] {
w++;
c+=yyleng;
}
[\n] l++;
. c++;
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("inp.txt","r");

yylex();

printf("Characters: %d",c);
printf("Words: %d",w);
printf("Lines: %d",l);
return 0;
}

The File contains:-
Hi
How are you
I am good

Output:-

[root@SAR117 Desktop]#lex p5.lex
[root@SAR117 Desktop]#gcc lec.yy.c
[root@SAR117 Desktop]#./a.out
Characters:22
Words:7
Lines:3

Write a program to implement Lexical Analyzer. System Programming GTU 2150708


Program:-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int isKeyword(char buffer[]){
    char keywords[32][10] = {"auto","break","case","char","const","continue","default",
                            "do","double","else","enum","extern","float","for","goto",
                            "if","int","long","register","return","short","signed",
                            "sizeof","static","struct","switch","typedef","union",
                            "unsigned","void","volatile","while"};
    int i, flag = 0;
   
    for(i = 0; i < 32; ++i){
        if(strcmp(keywords[i], buffer) == 0){
            flag = 1;
            break;
        }
    }
   
    return flag;
}

int main(){
    char ch, buffer[15], operators[] = "+-*/%=";
    FILE *fp;
    int i,j=0;
   
    fp = fopen("abc.txt","r");
   
    if(fp == NULL){
        printf("error while opening the file\n");
        exit(0);
    }
   
    while((ch = fgetc(fp)) != EOF){
           for(i = 0; i < 6; ++i){
               if(ch == operators[i])
                   printf("%c is operator\n", ch);
           }
          
           if(isalnum(ch)){
               buffer[j++] = ch;
           }
           else if((ch == ' ' || ch == '\n') && (j != 0)){
                   buffer[j] = '\0';
                   j = 0;
                                     
                   if(isKeyword(buffer) == 1)
                       printf("%s is keyword\n", buffer);
                   else
                       printf("%s is indentifier\n", buffer);
           }
          
    }
   
    fclose(fp);
   
    return 0;
}








Output:-


[root@SAR126 Desktop]# gedit lexicalanay.c
[root@SAR126 Desktop]# gcc lexicalanay.c
[root@SAR126 Desktop]# ./a.out
+ is operator
= is operator
abc is indentifier
[root@SAR117 Desktop]# cat abc.txt
a+b=c

Write a program to study the use of strtok(). System Programming GTU 2150708


Program:-

#include<string.h>
#include<stdio.h>
int main()
{
            char str[80]="A /&string/@of/%$^tokens";
            const char s[12]="/,!@#$%^&* ";
            char *token;
            token =strtok(str, s);
            while(token!=NULL)
            {
                        printf("%s\n",token);
                        token=strtok(NULL, s);
            }
            return 0;
}




Output:-

A
string
of
tokens

It's time To increase blogging capability. To have a chance to contribute in digital world. Any Interested People who want to make t...