Posts

Showing posts from November, 2017

Program to identify valid indian phone number.

%{ #include<stdio.h> %} %% ([789][0-9]+{9}) {printf("it is valid phone number");} %% main() { yylex(); }

Program to identify valid emai id.

%{ #include<stdio.h> %} %% ([a-zA-Z0-9['.']+['@'][a-z]+['.'][a-z]+) {printf("it is valid email id");} %% main() { yylex(); }

Desk Calculator

//   Lex file: desk.l DIGIT [0-9]+\.?|[0-9]*\.[0-9]+ %% [ ] {DIGIT}    {yylval=atof(yytext);return NUM;} \n|.             {return yytext[0];} //   Yacc file: desk.y   %{     #include<ctype.h>     #include<stdio.h>     #define YYSTYPE double %} %token NUM %left '+' '-' %left '*' '/' %right UMINUS %% S         : S E '\n' { printf("Answer: %g \nEnter:\n", $2); }            | S '\n'            |            | error '\n' { yyerror("Error: Enter once more...\n" );yyerrok; }            ; E         : E '+' E    { $$ = $1 + $3; }            | E'-'E    { $$=$1-$3; }            | E'*'E    { $$=$1*$3; }            | E'/'E    { $$=$1/$3; }            | '('E')'    { $$=$2; }            | '-'E %prec UMINUS { $$= -$2; }            | NUM                     ; %% #include "lex.yy.c" int main() {     printf

Lex program for changing capital case to lower and vice versa

%{ #include<stdio.h> %} %% {lower} {printf("%c",yytext[0]-32);} {capital} {printf("%c",yytext[0]+32);} %% int main() { yylex(); return(0); }

Lex program for counting the size of string

%{ #include<stdio.h> %} %% [a-z A-Z 0-9 ]* {printf("%d \n ",yyleng);} %% int main() { yylex(); return 0; }

Lex program for recognizing character, digit or any special character

%{ #include<stdio.h> %} %% [a-z A-Z] {printf("%s-charcter \n ",yytext);} [0-9] {printf("%s-digit \n ",yytext);} [^a-zA-Z0-9] {printf("%s-special charcter \n ",yytext);} //not starting with %% int yywrap() //this functional is optional { return(1); } int main() { yylex(); return 0; }

Write a context free grammar for structure of a english sentence, where a pronoun is followed by a verb and verb is followed by determiner. Determiner is followed by noun and noun can be followed by noun. Statement can begin with noun or a proper noun. **following program is written for example sentence"I prefer a morning flight from New Delhi".

LEX FILE: %{ #include "y.tab.h" %} %% I {return pronoun}; prefer {return verb;} a{return determiner;} morning|flight {return noun;} from {return preposition;} NewDelhi {return propernoun;} %% YACC FILE: %{ #include<stdio.h> %} %token noun pronoun propernoun determiner verb preposition %% S : NP VP {printf ("Sentence accepted");}    ; NP : pronoun       | propernoun       | determiner nominal       ; nominal : noun nominal                | noun                ; VP : verb       | verb NP       | verb NP PP       | verb PP       ; PP : preposition NP       ; %% main() { yyparse(); } yyerror(char * arg) { printf("invalid sentence"); }

There are two kangaroos on an x-axis ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location x1 and moves at a rate of v1 meters per jump. The second kangaroo starts at location x2 and moves at a rate of v2 meters per jump. Given the starting locations and movement rates for each kangaroo, can you determine if they'll ever land at the same location at the same time?

Image
#include <iostream> using namespace std; int main(){     int x1, v1, x2, v2;     cin >> x1 >> v1 >> x2 >> v2;     // If one kangaroo is behind the other AND moving slower,     //    he/she will never catch up to the other one     if ((x1 < x2) && (v1 < v2)) cout << "NO";     else if ((x2 < x1) && (v2 < v1)) cout << "NO";     // Otherwise, move each kangaroo one jump at a time until     //     the one behind is no longer behind.     else {         if (x1 < x2) {             while (x1 < x2) {                 x1 += v1;                 x2 += v2;             }         } else {             while (x2 < x1) {                 x1 += v1;                 x2 += v2;             }         }         // Once he/she is no longer behind the other, check to see         //    if he/she is in the same position, or if he/she has passed         if (x1 == x2) cout << "YES&

Implementation of Symbol Table

Image
#include<bits/stdc++.h> using namespace std; // It has been seen if chain length is < 50 then some space is underutilized, // Theoretically seen if > 50, prime number is taken then chain takes moderate space. // So thats why 53 is used. #define CHAIN_LENGTH 53 #define M 128 // Two columns of the symbol table with name and class type. struct symbol_info{     char *name;     char *classtype;     struct symbol_info *next; } *block[CHAIN_LENGTH]; // Hashing position is calculated using sum of all character ascii values // Then performing Modulo operation to go to any bucket from 0 to CHAIN_LENGTH int cHash(char* name){     int idx = 0;     for(int i = 0; name[i]; ++i){         idx = idx + name[i];     }    return (idx % CHAIN_LENGTH); } // If there is no element in the chain then new element is added in front, // otherwise through hashing if we reach a chain or, bucket that contains an // element then we insert the new element at the beginning of t

A program to find FIRST of non-terminals of the given grammar

Image
#include<stdio.h> #include<conio.h> char array[10][20],temp[10]; int c,n; void fun(int,int[]); int fun2(int i,int j,int p[],int ); void main() {    int p[2],i,j;    printf("Enter the no. of productions :");    scanf("%d",&n);    printf("Enter the productions :\n");    for(i=0;i<n;i++)    scanf("%s",array[i]);    for(i=0;i<n;i++)    {     c=-1,p[0]=-1,p[1]=-1;     fun(i,p);     printf("First(%c) : [ ",array[i][0]);     for(j=0;j<=c;j++)     printf("%c,",temp[j]);     printf("\b ].\n");     getch();    } } int fun2(int i,int j,int p[],int key) {   int k;   if(!key)   {     for(k=0;k<n;k++)     if(array[i][j]==array[k][0])     break;     p[0]=i;p[1]=j+1;     fun(k,p);     return 0;   }   else   {     for(k=0;k<=c;k++)     {     if(array[i][j]==temp[k])     break;     }     if(k>c)return 1;