0% found this document useful (0 votes)
2K views

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

The document describes programs to recognize valid arithmetic expressions and variables. It provides Lex and YACC programs that: 1) Define tokens for operators, identifiers, and data types in Lex rules. 2) Specify grammar rules to recognize statements with expressions and declarations with types and identifiers in YACC. 3) Were compiled and run to validate sample input code.

Uploaded by

sudhakarm13
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

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

The document describes programs to recognize valid arithmetic expressions and variables. It provides Lex and YACC programs that: 1) Define tokens for operators, identifiers, and data types in Lex rules. 2) Specify grammar rules to recognize statements with expressions and declarations with types and identifiers in YACC. 3) Were compiled and run to validate sample input code.

Uploaded by

sudhakarm13
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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