Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Nerate YACC Specification For A Few Syntactic Categories. A) Program To Recognize A Valid Arithmetic Expression That Uses Operator +, - , and

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

4.

Generate YACC specification for a


few syntactic categories.

a) Program to recognize a valid


arithmetic expression that uses
operator +, - , * and /.

Program name:arith_id.l

%{
/* This LEX program returns the
tokens for the expression */
#include “y.tab.h”
%}
%%
“=” {printf(“\n Operator is EQUAL”);}
“+” {printf(“\n Operator is PLUS”);}
“-“ {printf(“\n Operator is MINUS”);}
“/” {printf(“\n Operator is
DIVISION”);}
“*” {printf(“\n Operator is
MULTIPLICATION”);}
[a-z A-Z]*[0-9]* {
printf(“\n Identifier is %s”,yytext);
return ID;
}
return yytext[0];
\n return 0;
%%
int yywrap()
{
return 1;
}
Program Name : arith_id.y
%{
#include
/* This YYAC program is for
recognizing the Expression */
%}
%%
statement: A’=’E
|E{
printf(“\n Valid arithmetic
expression”);
$$ = $1;
};
E: E’+’ID
| E’-’ID
| E’*’ID
| E’/’ID
| ID
;
%%
extern FILE *yyin;
main()
{
do
{
yyparse();
}while(!feof(yyin));
}
yyerror(char*s)
{
}

OUTPUT:
[root@localhost]# lex arith_id.1
[root@localhost]# yacc –d arith_id.y
[root@localhost]# gcc lex.yy.c y.tab.c
[root@localhost]# ./a.out
x=a+b;

Identifier is x
Operator is EQUAL
Identifier is a
Operator is PLUS
Identifier is b

b) Program to recognise a valid


variable which starts with a letter
followed by any number of letters or
digits.

Program name: variable_test.l

%{
/* This LEX program returns the
tokens for the Expression */
#include "y.tab.h"
%}
%%
"int " {return INT;}
"float" {return FLOAT;}
"double" {return DOUBLE;}
[a-zA-Z]*[0-9]*{
printf("\nIdentifier is %s",yytext);
return ID;
}
return yytext[0];
\n return 0;
int yywrap()
{
return 1;
}

Program name: variable_test.y


%{
#include
/* This YACC program is for
recognising the Expression*/
%}
%token ID INT FLOAT DOUBLE
%%
D;T L
;
L:L,ID
|ID
;
T:INT
|FLOAT
|DOUBLE
;
%%
extern FILE *yyin;
main()
{
do
{
yyparse();
}while(!feof(yyin));
}
yyerror(char*s)
{
}

Output:

[root@localhost]# lex variable_test.I


[root@localhost]# yacc –d
variable_test.y
[root@localhost]# gcc lex.yy.c y.tab.c
[root@localhost]# ./a.out
int a,b;

Identifier is a
Identifier is b[root@localhost]#

You might also like