0% found this document useful (0 votes)
676 views14 pages

YAAC

The document contains descriptions of several programs that use lex and yacc to recognize different context-free grammars. The programs recognize expressions with arithmetic operators, nested if statements with different levels of nesting, variables starting with letters followed by letters and digits, strings that match the grammar anbn where n>=0, and the grammar anbn where n>=10.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
676 views14 pages

YAAC

The document contains descriptions of several programs that use lex and yacc to recognize different context-free grammars. The programs recognize expressions with arithmetic operators, nested if statements with different levels of nesting, variables starting with letters followed by letters and digits, strings that match the grammar anbn where n>=0, and the grammar anbn where n>=10.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

PROGRAM TO RECOGANIZE A VALID AIRTHMATIC EXPRESSION THAT USES

OPERATOR + -*/
%{
#include"y.tab.h"
%}
%%
[0-9] {return num;}
[+\-*/()] {return yytext[0];}
%%
%{
#include<stdio.h>
%}
%token num
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%%
st : exp {printf("valid\n");}
exp : exp '+' exp
| exp '-' exp
| exp '*' exp
| exp '/' exp
| '('exp')'
| num
;
%%
main()
{
printf("Enter expression\n");
yyparse();
}
yyerror() {printf("invalid\n");exit(0);}
OUTPUT
Enter expression
9+3
valid
[pavan@localhost ~]$ ./a.out
Enter expression
9+6+
invalid
PROGRAM TO EVALUATE A VALID AIRTHMATIC EXPRESSION THAT USES
OPERATOR + - * /
%{
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return num;}
[+\-*/()] {return yytext[0];}
%%
%{
#include<stdio.h>
%}
%token num
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%%
st : exp {printf("%d is valid",$1);}
exp : exp '+' exp {$$=$1+$3;}
| exp '-' exp {$$=$1-$3;}
| exp '*' exp {$$=$1*$3;}
| exp '/' exp {if($3==0)
{
printf("invalid input");
exit(0);
}
$$=$1/$3;}
| '('exp')'
| num
;
%%
main()
{
printf("Enter expression\n");
yyparse();
}
yyerror() {printf("invalid\n");exit(0);}
OUTPUT
Enter expression
9+6
15 is valid[pavan@localhost ~]$ ./a.out
Enter expression
10/5
2 is valid[pavan@localhost ~]$ ./a.out
Enter expression
10/0
invalid input[pavan@localhost ~]$
PROGRAM TO RECOGANIZE NESTEAD IF CONTROL STATEMENTS AND DISPLAY
NUMBER OF LEVELS OF NESTING
%{
#include"y.tab.h"
%}
%%
"if" return IF;
"then" return THEN;
"else" return ELSE;
"endif" return ENDIF;
[a-zA-Z()'"]+ return IDENT;
[0-9]+ return CONST;
">" return RELOP;
"<" return RELOP;
"=" return RELOP;
"!=" return RELOP;
">=" return RELOP;
"<=" return RELOP;
"&&" return AND;
"!" return NOT;
"||" return OR;
"\n" return 0;
%%
%{
#include<stdio.h>
int valid=1, i=0;
%}
%token IF THEN ELSE ENDIF
%token RELOP AND NOT OR CONST IDENT
%left OR
%left AND
%nonassoc RELOP
%%
SL :SL ';' S;
|S;
S : IFS
;
IFS : IF BEXP THEN IFS ST ENDIF {i++;}
;
| IF BEXP THEN ST ENDIF;
| IF BEXP THEN ST ELSE ST ENDIF;
BEXP : AEXP RELOP AEXP
| NOT BEXP %prec AND
| BEXP AND BEXP
| BEXP OR BEXP
| '('BEXP')';
AEXP : CONST
| IDENT
|
;
ST : IDENT
|
;
%%
main()
{
yyparse();
if(valid==1)
printf("Valid");
printf("the nested is=%d",i);
}
yyerror() {valid=0;printf("invalid\n");exit(0);}
OUT PUT
if a>c then pjhgj endif
Valid the nested is=0[pavan@localhost ~]$ ./a.out
if a>b then printf("sdf") endif
Validthe nested is=0[pavan@localhost ~]$
PROGRAM TO RECOGNIZE A VALID VARIABLE, WHICH STARTS WITH A
LETTER, FOLLOWED BY ANY NUMBER OF LETTERS OR DIGITS.
Yacc Program(vi p1.y):
%{
#include<stdio.h>
%}
%token letter digit
%%
st:letter exp {printf("Valid variable");}
exp:letter exp
| digit exp
|
;
%%
yyerror()
{
printf("Invalid variable");
}
main()
{
yyparse();
}
Lex Program(vi g1.l):
%{
#include"y.tab.h"
%}
%%
[0-9]+ {return digit;}
[a-z]+ {return letter;}
%%
OUTPUT:
[PAVAN@localhost ~]$ lex g1.l
[PAVAN@localhost ~]$ yacc -d p1.y
[PAVAN@localhost ~]$ cc lex.yy.c y.tab.c -lfl
[PAVAN@localhost ~]$ ./a.out
abc123
Valid variable
[PAVAN@localhost ~]$ ./a.out
123abc
Invalid variable

PROGRAM TO REGONIZE THE GRAMMER (an b,n>=10).
Yacc Program(vi p2.y):
#include<stdio.h>
%}
%token a1 b1
%%
st:exp {printf("Valid Input\n");}
exp:a1 a1 a1 a1 a1 a1 a1 a1 a1 a1 n b1
|
;
n:a1 n
|
;
%%
yyerror()
{
printf("Invalid input\n");
}
main()
{
printf("Enter input\n");
yyparse();
}
Lex Program(vi pg2.l):
%{
#include"y.tab.h"
%}
%%
[a] return a1;
[b] return b1;
%%

OUTPUT:
[PAVAN@localhost ~]$ lex pg2.l
[PAVAN@localhost ~]$ yacc -d p2.y
[PAVAN@localhost ~]$ cc lex.yy.c y.tab.c -lfl
[PAVAN@localhost ~]$ ./a.out
Enter input
aaaaaaaaaab
Valid Input
[PAVAN@localhost ~]$ ./a.out
Enter input
aaaaaaaaaaaaaaaab
Valid Input
[PAVAN@localhost ~]$ ./a.out
Enter input
aaaab
Invalid input
[PAVAN@localhost ~]$ ./a.out
Enter input
ab
Invalid input
[PAVAN@localhost ~]$ ./a.out
Enter input
b
Invalid input
[PAVAN@localhost ~]$ ./a.out
Enter input
aaaaaaaaaa
Invalid input
PROGRAM TO RECOGNIZE STRINGS 'aaab','abbb','ab' AND 'a' USING THE
GRAMMER (an bn, n>=0).
%{
#include<stdio.h>
%}
%token as bs
%%
st:exp {printf("Valid input\n");}
exp:as A B
A:as A
|
;
B:bs B
|
;
%%
yyerror()
{
printf("Invalid input\n");
}
main()
{
printf("Enter the input\n");
yyparse();
}
%{
#include"y.tab.h"
%}
%%
[a] return as;
[b] return bs;
%%
OUTPUT:
[PAVAN@localhost ~]$ lex pg3.l
[PAVAN@localhost ~]$ yacc -d p3.y
[PAVAN@localhost ~]$ cc lex.yy.c y.tab.c -lfl
[PAVAN@localhost ~]$ ./a.out
Enter the input
aaab
Valid input
[PAVAN@localhost ~]$ ./a.out
Enter the input
abbb
Valid input
[PAVAN@localhost ~]$ ./a.out
Enter the input
ab
Valid input
[PAVAN@localhost ~]$ ./a.out
Enter the input
a
Valid input
[PAVAN@localhost ~]$ ./a.out
Enter the input
bbba
Invalid input
[PAVAN@localhost ~]$ ./a.out
Enter the input
b
Invalid input

You might also like