1.Program to test the validity of a simple expression involving operators +,-,* and /. Lex Specification ………………………………….. %{ #include "y.tab.h" %} %% [_a-zA-Z][_a-zA-Z0-9]* {return ID;} [0-9]+(\.[0-9]+)-> {return NUM;} [\t];
Sep 02, 2014
1.Program to test the validity of a simple expression involving operators +,-,* and /.
Lex Specification
…………………………………..
%{
#include "y.tab.h"
%}
%%
[_a-zA-Z][_a-zA-Z0-9]* {return ID;}
[0-9]+(\.[0-9]+)-> {return NUM;}
[\t];
. return yytext[0];
\n return 0;
%%
………………………………….
Yacc Specification
%{
#include<stdio.h>
%}
%token NUM ID
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%%
exp: exp '+' exp
| exp '-' exp
| exp '/' exp
| exp '*' exp
| '-' exp %prec UMINUS
| '(' exp ')'
| '{' exp '}'
| '[' exp ']'
| NUM
| ID
;
%%
int main()
{
printf("\n Enter an expression:");
yyparse();
printf("\n valid expression");
return 0;
}
void yyerror(){
printf("\n Invalid expression");
exit(0);
}
Output:
…………………………………………..
lex l1.l
yacc -d y1.y
cc lex.yy.c y.tab.c -ll
./a.out
Enter an expression:a+(b-c)/a
valid expression
………………………………………………………………………………………………………….
2.Program to recognise the nested IF control statements and display the number of levels of nesting.
Lex Specification
%{
#include "y.tab.h"
%}
WS [ \t]*
%%
{WS}"if"{WS} {return IF;}
{WS}"else"{WS} {return ELSE;}
[-+_A-Za-z0-9!=><]+ {return STMT;}
. {return yytext[0];}
\n {return 0;}
%%
…………………………….
Yacc specification
%{
#include<stdio.h>
int nlevel=0;
%}
%token IF STMT ELSE
%%
start:ifs {printf("\n valid statement\n");} ;
ifs:IF cond st {nlevel++;}
| ELSE st {;}
;
st:simpst
| "{"compst"}"
| ifs
;
simpst:STMT";"
|";"
;
compst:simpst compst
|ifs compst
|simpst
|ifs
;
cond:"("STMT")"
;
%%
int main()
{ printf("\n enter an expression\n");
yyparse();
printf("\n number of levels of nesting=%d\n",nlevel);
return 0;
}
void yyerror()
{ printf("\n invalid");
exit(0);
}
…………………………………..
Output:
lex lfile2.l
yacc -d yfile2.y
cc lex.yy.c y.tab.c -ll
./a.out
enter an expression
if(a==b)a=0;
valid statement
………………………………………………………………………………………………………………..
3.Program to recognize valid arithmatic expression that uses operators+,-,*and /.
Lex Specification
…………………………….
%{
#include "y.tab.h"
%}
%%
([0-9]+|[0-9]*\.[0-9]+) return NUM;
. return yytext[0];
\n return 0;
%%
Yacc Specification
…………………..
%{
#include<stdio.h>
%}
%token NUM
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%%
exp : exp '+' exp
| exp '-' exp
| exp '/' exp
| '-' exp %prec UMINUS
| exp '*' exp
| '(' exp ')'
| '{' exp '}'
| '[' exp ']'
| NUM
;
%%
int main()
{
printf("\n Enter an arithmatic expression");
yyparse();
printf("\n Valid expression");
return 0;
}
void yyerror(){
printf("\n Invalid expression");
exit(0);
}
………………………………….
Output
lex l3.l
yacc -d y3.y
cc lex.yy.c y.tab.c -ll
./a.out
Enter an arithmatic expression1+3*4
Valid expression
………………………………………………………………………………….
4. Program to recognize a valid variable.
Lex Specification
……………………….
%{
#include "y.tab.h"
%}
%%
[a-zA-Z]+ {return LETTER;}
[0-9]+ {return DIG;}
[\t] {;}
[\n] {return 0;}
. {return(yytext[0]);}
%%
Yacc Specification
…………………………….
%{
#include <stdio.h>
%}
%token LETTER
%token DIG
%%
var: LETTER alpha
| LETTER
alpha: LETTER alpha
| DIG alpha
| LETTER
| DIG
;
%%
main()
{
printf("type the variable\n");
if(yyparse()==0)
printf("valid variable\n");
return 0;
}
yyerror()
{
printf("invalid variable\n");
exit(0);
}
Output:
lex lfile4.l
yacc -d yfile4.y
cc lex.yy.c y.tab.c -ll
./a.out
Enter a variable
cn12
valid variable
……………………………………………
5.Evaluate the arithmatic expression +,-,*,/
Lex Specification
…………………………..
%{
#include "y.tab.h"
%}
%%
([0-9]+|[0-9]*\.[0-9]+)+ {yylval.fval=atof(yytext);return NUM;}
. return yytext[0];
\n return 0;
%%
Yacc Specification
…………………………….
%{
#include<stdio.h>
#include<stdlib.h>
%}
%union {float fval;}
%token NUM
%type <fval> NUM
%type <fval> e
%type <fval> start
%left '+''-'
%left '*''/'
%nonassoc UMINUS
%%
start:e {printf("=%2.2f",$$);}
;
e:e '+' e {$$=$1+$3;}
|e '-' e {$$=$1-$3;}
|e '*' e {$$=$1*$3;}
|e '/' e {if($3==0) yyerror("divided by zero"); else $$=$1/$3;}
|'-' e %prec UMINUS {$$=-$2;}
|'(' e ')' {$$=$2;}
|'{' e '}' {$$=$2;}
|'[' e ']' {$$=$2;}
|NUM { $$=$1;}
;
%%
int main()
{
printf("\n enter aritmetic expression\n");
yyparse();
printf("\n valid expression\n");
return 0;
}
void yyerror()
{ printf("\n invalid expression\n");
exit(0);
}
…………………………………
Output:
lex lfile5.l
yacc -d yfile.y
cc lex.yy.c y.tab.c -ll
./a.out
Enter aritmetic expression
1+5*4
=21.00
valid expression
……………………………………………………………………………………..
6. Program to recognize the string ‘abbb’,'ab’,'a’ using grammer (a^nb^n,n>=0).
Lex Specification
……………………
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
"a" return A;
"b" return B;
%%
yacc Specification
………………………..
%{
#include<stdio.h>
%}
%token A B
%%
s : A s B
|
;
%%
int yyerror()
{
printf("Invalid string\n");
exit(0);
}
int main()
{
printf("Enter the string:\n");
yyparse();
printf("Valid string\n");
return 0;
}
Output:
……………………….
lex lfile6.l
yacc -d yfile6.y
cc lex.yy.c y.tab.c -ll
./a.out
enter a string of A’s followed by B
for the following grammer a^nb^n,n,m>=0
aabbb
valid input