Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Monday, May 7, 2012

Program to Construct Interpreter for Arthemetic Expression Evaluation using Bison and Flex


Arithmetic expressions are common in programming languages,this program is a sample implementation of an Interpreter to evaluate basic arithmetic expressions like a*b,a+b etc .This is accomplished by using compiler construction tools Flex(Lex) and Bison(Yacc) in Linux operating system environment.The following are complete source codes for two  Flex and Bison Programs to create an evaluator for Arithmetic Expressions

 /*YACC PROGRAM*/  
 %{  
 #include<stdio.h>  
 %}  
 %token num alpha END  
 %left '+''-'  
 %left '*''/'  
 %right '^'  
 %nonassoc UMINUS  
 %%  
 S:E END{printf("\n The given expression is valid\n");exit(0);}  
 E:E'+'E  
  |E'-'E  
  |E'*'E  
  |E'/'E  
  |'-'E %prec UMINUS  
  |'('E')'  
  |E'^'E  
  |num  
  |alpha  
  ;  
 %%  
 #include"lex.yy.c"  
 int main()  
 {  
  yyparse();  
  yylex();  
  return 0;  
 }  
  yyerror(char *s)  
  {  
  printf("\nerror\n");  
  }  
 /*LEX PROGRAM*/  
 %{  
 #include "pgm2.tab.h"  
 %}  
 %%  
 [0-9]+ {return num;}  
 [a-z]+ {return alpha;}  
 [ \t]  ;  
 [\n]  ;  
 "$"  {return END;}  
 .    {return yytext[0];}  
 %%  
 /*OUTPUT  
 r+x  
 $  
 The given expression is valid  
 d++c  
 $  
 error*/  

Tuesday, May 1, 2012

Program to create Lexical Analyser for C Programming Language using Lex/Flex

Flex/Lex is a compiler construction tool that can be used to design a compiler.In this example flex is used to create a sample lexical analyzer for c programming language,it can recognize the valid symbols in c programming language including valid programming constructs.
The following is a complete source code Flex/Lex program to implement a Lexical analyzer for C programming language
 letter [a-zA-Z]  
 digit[0-9]  
 %%  
 {digit}+("E"("+"|"-")?{digit}+)? printf("\n%s\tis real number",yytext);  
 {digit}+"."{digit}+("E"("+"|"-")?{digit}+)? printf("\n%s\t is floating pt no ",yytext);  
 "if"|"else"|"int"|"char"|"scanf"|"printf"|"switch"|"return"|"struct"|"do"|"while"|"void"|"for"|"float" printf("\n%s\t is keywords",yytext);  
 "\a"|"\\n"|"\\b"|"\t"|"\\t"|"\b"|"\\a" printf("\n%s\tis Escape sequences",yytext);  
 {letter}({letter}|{digit})* printf("\n%s\tis identifier",yytext);  
 "&&"|"<"|">"|"<="|">="|"="|"+"|"-"|"?"|"*"|"/"|"%"|"&"|"||" printf("\n%s\toperator ",yytext);  
 "{"|"}"|"["|"]"|"("|")"|"#"|"'"|"."|"\""|"\\"|";"|"," printf("\n%s\t is a special character",yytext);  
 "%d"|"%s"|"%c"|"%f"|"%e" printf("\n%s\tis a format specifier",yytext);  
 %%  
 int yywrap()  
 {  
 return 1;  
 }  
 int main(int argc,char *argv[])  
 {  
 yyin=fopen(argv[1],"r");  
 yylex();  
 fclose(yyin);  
 return 0;  
 }  
 /*INPUT PROGRAM  
 #include<stdio.h>  
 void main()  
 {  
  printf("\nhai\n");  
 }  
/*
OUTPUT

#  is a special character
include is identifier
< operator 
stdio is identifier
.  is a special character
h is identifier
> operator 

void  is keywords 
main is identifier
(  is a special character
)  is a special character

{  is a special character
 
printf  is keywords
(  is a special character
"  is a special character
\n is Escape sequences
hai is identifier
\n is Escape sequences
"  is a special character
)  is a special character
;  is a special character

}  is a special character
*/

Which is the Best Photo Watermarking Software

Photo Theft is becoming more and more common in the web with the outburst of social websites like Facebook,Google Plus and Image sharing se...