Showing posts with label Compiler Construction. Show all posts
Showing posts with label Compiler Construction. Show all posts

Saturday, May 12, 2012

Program to create Parser for IF-ELSE Statement using Flex(Lex) and Bison(Yacc)


'IF -ELSE' Statement is commonly used in all programming languages to implement various logical operations.
The control flows by checking a specific condition-if('cond) then expression if the condition is not satisfied the control flows 
to the else statement else-expression.


Parsing if-else statement is done by definitions of a grammar which uses Regular expressions.Compiler construction tools Flex(Lex) and Bison(Yacc) in Linux(Ubuntu) are used to define the grammar and basic operations .On compilation a c program is generated which is actually the parser for 'IF-ELSE' statement.A parser generates a Parse tree for Intermediate code generation.


Following are complete source codes for Flex and Bison programs to create a Parser for 'IF-ELSE' Statement which accepts valid 'IF-ELSE' Statements
 /*PARSER FOR IF ELSE STATEMENT*/  
 //c-madeeasy.blogspot.com  
 /*YACC PROGRAM*/  
 %{  
 #include<stdio.h>  
 #include<stdlib.h>  
 %}  
 %token num alpha LT GT EQ LE GE NE AND OR INC DEC END  
 %left '+''-'  
 %left '*''/'  
 %right '^'  
 %right '='  
 %nonassoc UMINUS  
 %nonassoc IF  
 %nonassoc ELSE  
 %left GE NE LT GT LE EQ  
 %left AND OR  
 %%  
 S:ST END{printf("\n Accepted\n");exit(0);}  
 ST:IF'('F')''{'ST'}'%prec IF  
  |IF'('F')''{'ST'}'ELSE'{'ST'}'  
  |E';'  
  |E';'ST  
  F:C LO C  
  |C  
 LO:AND  
  |OR  
  C:E RELOP E  
  |E  
  E:alpha '='E  
  |E'+'E   
  |E'-'E   
  |E'*'E   
  |E'/'E  
  |E'^'E  
  |'('E')'   
  |'-'E %prec UMINUS  
  |alpha  
  |num  
  |alpha INC  
  |alpha DEC  
 RELOP:LT  
    |GT  
    |EQ  
    |LE  
    |GE  
    |NE  
   ;  
 %%  
 #include"lex.yy.c"  
 int main()  
 {   
 yyparse();  
 yylex();  
 return END;  
 }  
 yyerror(char *s)  
 {  
  printf("\nError");  
 }  
 /*LEX PROGRAM*/  
 %{  
 #include"pgm4.tab.h"  
 %}  
 %%  
 "if" {return IF;}  
 "else" {return ELSE;}  
 "&&" {return AND;}  
 "||" {return OR;}  
 "<=" {return LE;}  
 ">=" {return GE;}  
 ">" {return GT;}  
 "<" {return LT;}  
 "!=" {return NE;}  
 "++" {return INC;}  
 "--" {return DEC;}  
 "==" {return EQ;}  
 [0-9]+ {return num;}  
 [a-zA-Z]+ {return alpha;}  
 [\t];  
 [\n];   
 "$" {return END;}  
 .  {return yytext[0];}  
 %%  
 /*OUTPUT  
 if(i>0)  
 {  
 a=0;  
 c++;  
 }  
 else  
 {  
 h;  
 }  
 $  
 Accepted  
 if(a>l)   
 {  
 w;  
 }  
 $  
 Accepted*/  


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...