Top Banner
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];
31
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: yacc lab

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];

Page 2: yacc lab

\n return 0;

%%

………………………………….

Yacc Specification

%{

#include<stdio.h>

%}

%token NUM ID

%left '+' '-'

%left '*' '/'

%nonassoc UMINUS

Page 3: yacc lab

%%

exp: exp '+' exp

| exp '-' exp

| exp '/' exp

| exp '*' exp

| '-' exp %prec UMINUS

| '(' exp ')'

| '{' exp '}'

| '[' exp ']'

| NUM

Page 4: yacc lab

| ID

;

%%

int main()

{

printf("\n Enter an expression:");

yyparse();

printf("\n valid expression");

return 0;

}

Page 5: yacc lab

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.

Page 6: yacc lab

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;}

Page 7: yacc lab

%%

…………………………….

Yacc specification

%{

#include<stdio.h>

int nlevel=0;

%}

%token IF STMT ELSE

%%

start:ifs {printf("\n valid statement\n");} ;

ifs:IF cond st {nlevel++;}

Page 8: yacc lab

| ELSE st {;}

;

st:simpst

| "{"compst"}"

| ifs

;

simpst:STMT";"

|";"

;

compst:simpst compst

Page 9: yacc lab

|ifs compst

|simpst

|ifs

;

cond:"("STMT")"

;

%%

int main()

{ printf("\n enter an expression\n");

yyparse();

Page 10: yacc lab

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

Page 11: yacc lab

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];

Page 12: yacc lab

\n return 0;

%%

Yacc Specification

…………………..

%{

#include<stdio.h>

%}

%token NUM

%left '+' '-'

%left '*' '/'

%nonassoc UMINUS

%%

Page 13: yacc lab

exp : exp '+' exp

| exp '-' exp

| exp '/' exp

| '-' exp %prec UMINUS

| exp '*' exp

| '(' exp ')'

| '{' exp '}'

| '[' exp ']'

| NUM

;

Page 14: yacc lab

%%

int main()

{

printf("\n Enter an arithmatic expression");

yyparse();

printf("\n Valid expression");

return 0;

}

void yyerror(){

printf("\n Invalid expression");

Page 15: yacc lab

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"

Page 16: yacc lab

%}

%%

[a-zA-Z]+ {return LETTER;}

[0-9]+ {return DIG;}

[\t] {;}

[\n] {return 0;}

. {return(yytext[0]);}

%%

Yacc Specification

…………………………….

%{

#include <stdio.h>

Page 17: yacc lab

%}

%token LETTER

%token DIG

%%

var: LETTER alpha

| LETTER

alpha: LETTER alpha

| DIG alpha

| LETTER

| DIG

Page 18: yacc lab

;

%%

main()

{

printf("type the variable\n");

if(yyparse()==0)

printf("valid variable\n");

return 0;

}

Page 19: yacc lab

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

Page 20: yacc lab

……………………………………………

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

Page 21: yacc lab

…………………………….

%{

#include<stdio.h>

#include<stdlib.h>

%}

%union {float fval;}

%token NUM

%type <fval> NUM

%type <fval> e

%type <fval> start

%left '+''-'

Page 22: yacc lab

%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;}

Page 23: yacc lab

|'(' e ')' {$$=$2;}

|'{' e '}' {$$=$2;}

|'[' e ']' {$$=$2;}

|NUM { $$=$1;}

;

%%

int main()

{

printf("\n enter aritmetic expression\n");

yyparse();

Page 24: yacc lab

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

Page 25: yacc lab

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;

Page 26: yacc lab

"b" return B;

%%

yacc Specification

………………………..

%{

#include<stdio.h>

%}

%token A B

%%

s : A s B

|

Page 27: yacc lab

;

%%

int yyerror()

{

printf("Invalid string\n");

exit(0);

}

int main()

Page 28: yacc lab

{

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

Page 29: yacc lab

aabbb

valid input