Showing posts with label YACC. Show all posts
Showing posts with label YACC. Show all posts

Friday, November 18

Lex and Yacc in Windows 7 - Compile and Run

Hi Friend, I was in search for a long time to get a platform in windows-7 to run lex and Yacc ,for academic purposes. Many of my friends also had the same problem and often forced to choose Linux only for merely executing Lex & Yacc programs..!!! At last, I found one solution and its my pleasure to share it with all of you.!!  :) Its very simple.You need to download two very small files. You have to download two executable( i.e.,   .exe files). In total,its size may have about 370KB. To download them click the links below. Link 1:   Flex Link 2:  Bison Save both files in same place....

Saturday, October 8

Calculator using YACC program

Here is the Yacc program implementing a simple calculator: %{ #include<stdio.h> #include<ctype.h> %} %token num %left '+''-' %left '*''/' %right '^' %% s:e'\n'{printf("%d",$1);} e:    e '+' e{$$=$1+$3;}  |e '-' e{$$=$1-$3;}  |e '*' e{$$=$1*$3;}  |e '/' e{$$=$1/$3;}  |e '^' e {   int i,j=$1;   for(i=1;i<$3;i++)   {   j=j*$1;   $$=j;   }   }  |'('e')'{$$=$2;}  |num1; num1:num1 num{$$ = $1*10 + $2;}  |num  ; %% yylex() { int c; c=getchar(); if(isdigit(c)) { yylval=c-'0'; return num; } return c; } int...

YACC program to convert infix

%{#include<stdio.h> #include<ctype.h> %} %token num %left '+''-' %left '*' '/' %% s:e'\n'{} e:e'+'e{printf("+");} |e'-'e{printf("-");} |e'/'e{printf("/");} |e'*'e{printf("*");} |num1{printf("%d",$1);} num1:num1 num {$$=$1*10+$2;} |num ; %% yylex() {  int c;  c=getchar(); if(isdigit(c)) { yylval=c-'0';  return num; }return c; } int main() {  yyparse(); return 1; } int yyerror() { return 1; } int yywrap() {  return 1;...

Yacc program to evaluate POSTFIX expression

%{#include<stdio.h> #include<ctype.h> %} %token num %left '+''-' %left '*' '/' %right '^' %% s:e'\n'{printf("\n%d",$1);} e:e e'+'{$$=$1+$2;} |e e'-'{$$=$1-$2;} |e e'*'{$$=$1*$2;} |e e'/'{$$=$1/$2;} |num ; %% yylex() { int c; c=getchar(); if(isdigit(c)) { yylval=c-'0';  return num; }return c; } int main() {  yyparse(); return 1; } int yyerror() { return 1; } int yywrap() {  return 1; } Illustraion: Evaluation of postfix expression can be done very easily. 34+ 3--> store value in $1 4--> store value in $2 +--> add $1 and $2 and store result in $$ For more Yacc programs click here.....