Posts

Showing posts with the label Lex Program Example

How To Install LEX and YACC in Linux or Ubuntu

Image
To program LEX programs and yacc programs you need to install two packages in Ubuntu 1:Flex -to make the Lex program work 2.Bison -to make the yacc programs work To install,Open terminal and do the following...  1.To install Flex sudo apt-get upgrade sudo apt-get install flex 2.To install Lex sudo apt-get upgrade sudo apt-get install bison And its done ..!!!

Lex Program to find the Syntax of Scanf()

Image
Lex Program to find the Syntax of Scanf() Source Code:- 1:  %{   2:  #include<stdio.h> 3:  #include<ctype.h> 4:  int i=0,j=0; 5:  %} 6:  a "%d"|"%f"|"%c"|"%s" 7:  id [a-zA-Z][a-zA-Z0-9]* 8:  %% 9:  scanf\((\"({a}*|.*)*\"(,\&{id})*\))\; {for(i=0;i<yyleng;i++){if(yytext[i]=='%') 10:  j++; 11:  if(yytext[i]==',') 12:  j--; 13:  } 14:  if(j==0) 15:  printf("\ncorrect");   16:  else   17:  printf("incorrect"); 18:  } 19:  %% 20:  main() 21:  { yyin=fopen("file13.c","r"); 22:  yylex(); 23:  } 24:  int yywrap() 25:  {return 1; 26:  }      

Lex Program TO find Syntax of Printf()

Image
Lex Program TO find Syntax of Printf() Source Code:- 1:  %{   2:  #include<stdio.h>   3:  #include<ctype.h>   4:  int i=0,j=0;   5:  %}   6:  a "%d"|"%f"|"%c"|"%s"   7:  id [a-zA-Z][a-zA-Z0-9]*   8:  %%   9:  printf\((\"({a}*|.*)*\"(,{id})*\))\; {for(i=0;i<yyleng;i++){if(yytext[i]=='%')   10:  j++;   11:  if(yytext[i]==',')   12:  j--;   13:  }   14:  if(j==0)   15:  printf("\ncorrect");    16:  else    17:  printf("incorrect");   18:  }   19:  %%   20:  main()   21:  { yyin=fopen("file12.c","r");   22:  yylex();   23:  }   24:  int yywrap()   25:  {return 1;   26:  }                    

Lex Program To Check The Syntax of For Loop

Image
Lex Code To check the syntax of For loop:- Source code:- 1:  %{   2:  #include<stdio.h> 3:  #include<ctype.h> 4:  int c; 5:  %} 6:  op "++"|"--" 7:  rop "<"|">"|"<="|">="|"=="|"!=" 8:  id [a-zA-Z][a-zA-Z0-9]* 9:  no [0-9]* 10:  pp [\n\t" "] 11:  %% 12:  for\({id}=({no}|{id})\)?(,{id}=({no}|{id}))*\;({id}{rop}({id}|{no}))?\;({id}{op})?(,{id}{op})*\)\;?{pp}*\("{"(.*\n)*.*"}") {printf("correct");c=0;} 13:  .*    {printf("\nwrong");} 14:  %% 15:  main() 16:  { yyin=fopen("file11.c","r"); 17:  yylex(); 18:  } 19:  int yywrap() 20:  {return 1; 21:  }