Posts

Showing posts with the label Compiler Design Lab

Program to implement Recursive Descent Parsing in C.

Image
  Code: #include<stdio.h> static char c[10]; char input; void E(),EPRIME(); int main() {     printf("Enter a String: ");     scanf("%s",c);       E();     if(c[input]=='$')         printf("Valid String\n");     else         printf("Invalid String\n");     return 0; }   void E() {     if (c[input] == 'i')         input++;     EPRIME(); }   void EPRIME()     {     if (c[input]== '+') {         input++;         if(c[input]=='i')             input++;         EPRIME();     }  ...

Write a Program to Implement a Calculator and Recognize a Valid Arithmetic Expression Using Lex and Yacc.

Image
  Examples: Input: 4+5 Output: Result=9 Entered arithmetic expression is Valid Input: 10-5 Output: Result=5 Entered arithmetic expression is Valid Input: 10+5- Output: Entered arithmetic expression is Invalid Input: 10/5 Output: Result=2 Entered arithmetic expression is Valid Input: (2+5)*3 Output: Result=21 Entered arithmetic expression is Valid Input: (2*4)+ Output: Entered arithmetic expression is Invalid Input: 2%5 Output: Result=2 Entered arithmetic expression is Valid Lexical Analyzer Source Code: %{  #include<stdio.h>  #include "y.tab.h"  extern int yylval;  %}  %%  [0-9]+ {  yylval=atoi(yytext);  return NUMBER;  }  [\t] ;  [\n] return 0;  . return yytext[0];  %%  int yywrap()  {  return 1;  } 

Write a program to add line number in input program

Image
  Input Text File #include<stdio.h> int main() { int a=10,b=20; int c=a+b; printf("Addition of a and b is:%d",c); return 0; } Code %{ #include<stdio.h> int line_number = 1; %} line .*\n %% {line} { printf("%d %s", line_number++, yytext); } %% int yywrap(){} int main() { extern FILE *yyin; yyin=fopen("SimpleCodeInput.txt","r"); if(yyin==NULL){ printf("File Not Found"); } yylex(); return 0; }

Write a program to count comment and copy this code in new file and without comment

Image
  Input Text File #include<stdio.h> int main() { int a,b; /*float c; */ //hi printf("hai"); /*printf("Hello");*/ } Create One Output Blank Text File Code %{ int com=0; %} %% "/*"[^\n]+"*/" {com++; fprintf(yyout," ");} "//".* {com++; fprintf(yyout," ");} %% int main() { extern FILE *yyin; yyin=fopen("CountCommentInput.txt","r"); if(yyin==NULL){ printf("File Not Found"); } yyout=fopen("CountCommentOutput.txt","w"); yylex(); printf("Comment=%d\n",com); return 0; } int yywrap(){}

Write A Program To Print HTML Tags

Image
  Input Text File <html> <head> <title>Introduction</title> </head> <body> <h1>My Name is Jay</h1> <p>I from India.</p> </body> </html> Code %{ #include<stdio.h> %} %% "<"[^>]*> {printf("VALUE:%s\n",yytext);} .|\n ; %% int main() { extern FILE *yyin; yyin=fopen("HTMLInput.txt","r"); if(yyin==NULL){ printf("File Not Found"); } yylex(); return 0; } yywrap() { return(1); }

Write A Program To Count How Many Positive, Negative, Positive Fraction And Negative Fraction Numbers In Our File

Image
  Input Text File 1,23,-4,5.3,-8.4,4,6,-8,3.5,-5.9 Code %{ #include<stdio.h> int p=0; int n=0; int pf=0; int nf=0; %} DIGIT [0-9] %% \+?{DIGIT}+ p++; [-]{DIGIT}+ n++; \+?{DIGIT}*\.{DIGIT}+ pf++; -{DIGIT}*\.{DIGIT}+ nf++; .; %% main() { extern FILE *yyin; yyin=fopen("Numbers.txt","r"); if(yyin==NULL){ printf("File Not Found"); } yylex(); printf("\n count of Positive Numbers:%d",p); printf("\n count of Negative Numbers:%d",n); printf("\n count of Positive Fractions Numbers:%d",pf); printf("\n count of Negative Fractions Numbers:%d",nf); } int yywrap(){ return (1); }

Write A Program To Count Uppercase And Lowercase Letters In File

Image
  Input Text File Increase Input Text File Code %{ #include<stdio.h> int U=0; int L=0; %} %% [A-Z] U++; [a-z] L++; %% main() { extern FILE *yyin; yyin=fopen("UpperLowerInput.txt","r"); if(yyin==NULL){ printf("File Not Found"); } yylex(); printf("\nTotal No of UpperCase Character=%d",U); printf("\nTotal No of LowerCase Character=%d",L); } int yywrap() { return 1; }

Write A Program To Print Hello World Using Lex

Image
  How to write lex program ? first write a lex program in notepad then save with .l extension. next step is open cmd(Command prompt) then open directory in which your program is saved. then 3 step to run lex program let's see flex filename.l gcc lex.yy.c a.exe Code %{ #include<stdlib.h> %} %% ("hi"|"HI")"\n" {printf("\nHello Sir,Good Morning\n");} ("bye"|"BYE")"\n" {printf("\nBye, Take Care\n");} . {yyerror();} %% int main() { yylex(); return 0; } int yywrap(void) { return 0; } int yyerror(){ printf("Enter hi,HI,bye or BYE"); }

Write A Program To Detect Tokens In C

Image
  Create one text file Input Text File int main() { int a=10,b=20; if(a>b) return a; else return b; } Code #include<stdio.h> #include<ctype.h> #include<string.h> #include<stdlib.h> void keyw(char *p); int i=0,id=0,kw=0,num=0,op=0,sp=0,ar=0,count=0,new_line=0; char keys[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"}; main() {     char ch,str[25],seps[20]=" \t\n,;(){}[]#\"<>",op...