Thursday, 12 January 2017

Yacc Programs



1. Program to test validity of a sample expression involving operators +, -,* and /.

Calcval.y
%{
   #include<stdio.h>
   #include<stdlib.h>
%}
%token NUM
%%
start : S | S start ;
S : E'\n' {printf("Valid Expression");}
  | error '\n' {yyerror("Enter valid expression");}
  ;

E : E '+' T
  | E '-' T
  | T
  ;

T : T '*' F
  | T '/' F
  | F
  ;

F : '(' E ')'
  | '-' F
  | NUM
  ;

%%

yyerror(char *msg)
{
printf("Invalid Expression : %s\n",msg);
}



void main()
{
printf("Enter Expression...> \n");
yyparse();
}



Call.l
%{
#include<stdio.h>
#include<stdlib.h>
#include "y.tab.h"
%}
%%
[0-9]+(\.[0-9]+)?([eE][0-9]+)? {yylval=atof(yytext);return NUM;}
[-+()*/]|\n {return yytext[0];}
[ \t] {;}
%%




 2. Program to evaluate an arithmetic expression involving operators +, -, * and /.



Calc.y
%{
   #include<stdio.h>
   #include<stdlib.h>
%}
%union {
  float f;
 
}

%token <f> NUM
%type <f> E T F

%%
start : S | S start ;
S : E'\n' {printf("%f\n",$1);}
  | error '\n' {yyerror("Enter valid expression");}
  ;

E : E '+' T {$$ = $1 + $3;}
  | E '-' T {$$ = $1 - $3;}
  | T {$$ = $1;}
  ;

T : T '*' F {$$ = $1 * $3;}
  | T '/' F {$$ = $1 / $3;}
  | F {$$ = $1;}
  ;

F : '(' E ')' {$$ = $2;}
  | '-' F {$$ = -$2;}
  | NUM {$$ = $1;}
  ;

%%

yyerror(char *msg)
{
printf("Invalid Expression : %s\n",msg);
}

void main()
{
printf("Enter Expression...> \n");
yyparse();
}





Caal.l
%{
#include<stdio.h>
#include<stdlib.h>
#include "y.tab.h"
%}
%%
[0-9]+(\.[0-9]+)?([eE][0-9]+)? {yylval.f=atof(yytext);return NUM;}
[-+()*/]|\n {return yytext[0];}
[ \t] {;}
%%





3. Program to recognize the grammar (An B, n >=10 ) .


Anb.y
%{
#include<stdio.h>
%}
%token A B
%%
start        :      variable | variable start ;
variable     :      A A A A A A A A A A var B '\n' {printf("\nValid variable \n");}
              |      error '\n' {yyerror("\nInvalid variable \n");}
              ;
var           :      A var
              |      {;}
              ;
%%
main()
{
printf("Enter the variable:\n");
yyparse();
}
yyerror(char *s)
{
 printf("%s",s);
}

Anb.l
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[aA]         {return A;}
[bB]         {return B;}
.|\n           {return yytext[0];}
%%

 



4. Program to recognize a valid variable, which starts with a letter, followed by any number of letters or digits.

Vari.y
%{
   #include<stdio.h>
   #include<ctype.h>
   #include<stdlib.h>
%}
%token let dig
%%
start : TERM | TERM start ;
TERM:  XTERM '\n'       {printf("\nAccepted\n");}
    |  error  '\n'         {yyerror ("\nRejected\n");}
    ;
XTERM: XTERM let
     | XTERM dig
     | let
     ;
%%
main()
{
  printf("Enter a variable:");
  yyparse();
}
yyerror(char *s)
{
  printf("%s",s);
}

Vari.l
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[a-zA-Z_]         {return let;}
[0-9]         {return dig;}
.|\n           {return yytext[0];}
%%



 5. Program to recognize nested IF control statements and display the number of levels of

nesting.

Nestif.y
%{
#include "stdio.h"
int level=0;
%}
%token IF SIMP OP
%%
stmt: if_stmt    {printf("\nValid levels=%d\n",level);}
    | error      { printf("---ERROR---\n");}
    ;
if_stmt: IFCOND SIMP
       | IFCOND if_stmt
       | IFCOND '{' EXP '}'
       | IFCOND '{' if_stmt '}'
IFCOND:  IF '(' EXP ')' {level++;}
      ;
EXP: | SIMP OP SIMP
     | SIMP
     ;
%%
yyerror(char *s)
{
  printf("Invalid");
}  
Nestif.l
%{
#include "y.tab.h"
%}
%%
"if"       return IF;
"=="       return OP;
[<>+]      return OP;
[{()}]     return *yytext;
[a-zA-Z]*  return SIMP;
" "        {;}
.          { }
%%
void main()
{
  yyin=fopen("a.c","r");
  yyparse();
}





6. Program to recognize strings ' aaab', ' abbb', 'ab' and 'a' using the grammar.

%{
   #include<stdio.h>
   #include<stdlib.h>
%}
%token a b
%%
start : S start | S ;
S      :      A '\n' {printf("\nString belongs to grammar..\n");}
       |      error '\n'            {yyerror("\nstring does not belong to grammar..\n");}
       ;
A      :      a a a b
       |      a b b b
       |      a b
       |      a
       ;
%%
main()
{
 printf("Enter String for GRAMMAR:\n");
 yyparse();
}
yylex()
{
 char ch;
 while((ch=getchar())==' ');
 if(ch=='a'||ch=='A')
   return a;
 if(ch=='b'||ch=='B')
   return b;
 return ch;
}
yyerror(char *s)
{
 printf("%s",s);
}


4 comments: