Nerate YACC Specification For A Few Syntactic Categories. A) Program To Recognize A Valid Arithmetic Expression That Uses Operator +, - , and
Nerate YACC Specification For A Few Syntactic Categories. A) Program To Recognize A Valid Arithmetic Expression That Uses Operator +, - , and
Nerate 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
%{
/* 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;
}
Output:
Identifier is a
Identifier is b[root@localhost]#