Thursday, November 24, 2011

Infix to Prefix

Share Orkut

LEX Program



%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
%}
DIGIT [0-9]+
ID [a-zA-Z][a-zA-Z0-9]*
op "+"|"-"|"*"|"/"
%%
{ID} { strcpy(yylval.str,yytext); return id; }
{DIGIT} { strcpy(yylval.str,yytext); return no; }
{op} { return (int) yytext[0]; }
\n { return(0); }
. { return err; }
%%


YACC Program



%{
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char result[50];
int f=0;
%}
%union{
  char str[50];
};
%token <str> id
%token <str> no
%token <str> err
%type <str> exp
%left '-' '+'
%left '*' '/'
%%
input: /* empty string */
   | input exp {strcpy(result,$2);}
   | error {f=1;}
   ;
exp: exp '+' exp { sprintf($$,"+%s %s",$1,$3); }
   | exp '-' exp {sprintf($$,"-%s %s",$1,$3); }
   | exp '*' exp { sprintf($$,"*%s %s",$1,$3); }
   | exp '/' exp {sprintf($$,"/%s %s",$1,$3);}
   | id {strcpy($$,$1);}
   | no {strcpy($$,$1);}
   ;
%%
int main()
{
   printf("\nEnter an arithmetic expression:\n\n");
   yyparse();
   printf("\n");
   if(f==1)
     printf("Invalid Expression\n");
   else
     puts(result);
   return 0;
}
int yywrap()
{
   return 1;
}
int yyerror(char *mes) {
   return 0;
}

OUTPUT



Order of execution--(multicharacter identifier and numbers)

Share Orkut

LEX Program



%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"
%}
DIGIT [0-9]+
ID [a-zA-Z][a-zA-Z0-9]*
op "+"|"-"|"*"|"/"
%%
{ID} { strcpy(yylval.str,yytext); return id; }
{DIGIT} { strcpy(yylval.str,yytext); return no; }
{op} { return (int) yytext[0]; }
\n { return(0); }
. { return err; }
%%


YACC Program



%{
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char result[50];
int f=0;
%}
%union{
  char str[50];
};
%token <str> id
%token <str> no
%token <str> err
%type <str> exp
%left '-' '+'
%left '*' '/'
%%
input: /* empty string */
   | input exp {strcpy(result,$2);}
   | error {f=1;}
   ;
exp: exp '+' exp { sprintf($$,"(%s + %s)",$1,$3); }
   | exp '-' exp {sprintf($$,"(%s - %s)",$1,$3); }
   | exp '*' exp { sprintf($$,"(%s * %s)",$1,$3); }
   | exp '/' exp {sprintf($$,"(%s / %s)",$1,$3);}
   | id {strcpy($$,$1);}
   | no {strcpy($$,$1);}
   ;
%%
int main()
{
   printf("\nEnter an arithmetic expression:\n\n");
   yyparse();
   printf("\n");
   if(f==1)
     printf("Invalid Expression\n");
   else
     puts(result);
   return 0;
}
int yywrap()
{
   return 1;
}
int yyerror(char *mes) {
   return 0;
}


OUTPUT

Wednesday, November 23, 2011

Order of execution

Share Orkut

LEX Program



%{
#include <stdio.h>
#include "y.tab.h"
%}
op "+"|"-"|"*"|"/"
%%
[a-z] { yylval=*yytext; return id; }
{op} { return (int) yytext[0]; }
\n { return(0); }
. { return err; }
%%

LEX Program



%{
#include <stdio.h>
#include <ctype.h>
#define YYSTYPE char
void push(char);
void attach(char);
int f=0;
struct node{
      char str[30];
};
struct node stack[20];
int stkPtr=-1;
%}
%token id err
%left '-' '+'
%left '*' '/'
%%
input: /* empty string */
      | input exp {}
      | error {f=1;}
   ;
exp: exp '+' exp { attach('+'); }
      | exp '-' exp { attach('-'); }
      | exp '*' exp { attach('*');}
      | exp '/' exp {attach('/');}
      | id { push($1); }
   ;
%%
void push(char c){
struct node newNode;
      newNode.str[0]=c;
      newNode.str[1]='\0';
      stack[++stkPtr]=newNode;
}
void attach(char op){
      struct node newNode;
      struct node temp1=stack[stkPtr--];
      struct node temp2=stack[stkPtr--];
      sprintf(newNode.str,"(%s %c %s)",temp2.str,op,temp1.str);
     stack[++stkPtr]=newNode;
}
int main()
{
     printf("\nEnter an arithmetic expression:\n\n");
      yyparse();
      printf("\n");
      if(f==0)
           printf("%s\n",stack[stkPtr].str);
      else
           printf("Invalid Expression\n");
      return 0;
}
int yywrap()
{
      return 1;
}
int yyerror(char *mes) {
      return 0;
}

Infix to Postfix using lex and yacc

Share Orkut

LEX Program



%{
#include <stdio.h>
#include "y.tab.h"
%}
op "+"|"-"|"*"|"/"
%%
[a-z] { yylval=*yytext; return id; }
{op} { return (int) yytext[0]; }
\n { return(0); }
. { return err; }
%%

YACC Program



%{
#include <stdio.h>
#include <ctype.h>
#define YYSTYPE char
int f=0;
%}
%token id err
%left '-' '+'
%left '*' '/'
%%
input: /* empty string */
     | input exp {}
     | error {f=1;}
   ;
exp: exp '+' exp { printf("+"); }
     | exp '-' exp { printf("-"); }
     | exp '*' exp { printf("*"); }
     | exp '/' exp { printf("/");}
     | id { printf("%c",yylval); }
   ;
%%
int main()
{
   printf("\nEnter an arithmetic expression:\n\n");
   yyparse();
   printf("\n");
   if(f==1)
     printf("Invalid Expression\n");
   return 0;
}
int yywrap()
{
   return 1;
}
int yyerror(char *mes) {
   return 0;
}