0% found this document useful (0 votes)
6 views

YACC

Compiler design

Uploaded by

naveenraj.kmns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

YACC

Compiler design

Uploaded by

naveenraj.kmns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Ex.No.

4 Generate YACC specification for a few syntactic categories

Ex.No: 4A Generate YACC Specification for Program to recognize a valid


arithmetic expression that uses operator +, - , * and /
AIM:
To recognize a valid arithmetic expression using YACC specification

ALGORITHM:
1. Accept the token generated in lex part as input
2. Specify order of procedure
3. Define rules with end point
4. Parse input string from standard input by calling yyparse() by main function
5. Print the result of any rules matches
6. If none of results defined matches print “invalid expression”
PROGRAM:
exp1.y

%token NUMBER ID NL
%left '+' '-'
%left '*' '/'
%%

stmt : exp NL { printf("Valid Expression"); exit(0);}


| exp '=' exp NL { printf("Valid Expression"); exit(0);}
;
exp : exp '+' exp
| exp '-' exp
| exp '*' exp
| exp '/' exp
| '(' exp ')'
| ID
| NUMBER
;
%%

int yyerror(char *msg)


{
printf("Invalid Expression\n");
exit(0);
}

main ()
{
print f("Enter t he expression\n");
yyparse();
}
yywrap()
{
return 1;
}

exp1.l

%{
#include "y.tab.h"
%}
%%
[0-9]+ { return NUMBER; }
[a-zA-Z][a-zA-Z0-9_]* { return ID; }
\n { return NL ;}
. { return yytext[0]; }
%%

OUTPUT

[ucea-cse@telnet ~] $ yacc -d exp1.y


[ucea-cse@telnet ~]$ lex exp1.l
[ucea-cse@telnet ~]$ cc y.tab.c lex. yy.c
[ucea-cse@telnet ~]$ ./a.out
Enter the expression
a=b
Valid Expression
[shapnarani-cse@telnet ~]$ ./a.out
Enter the expression
c=a+b
Valid Expression
[shapnarani-cse@telnet ~]$ ./a.out
Enter the expression
4+9
Valid Expression
[shapnarani-cse@telnet ~]$ ./a.out
Enter the expression
90-
Invalid Expression

RESULT:
Thus the program to recognize a valid arithmetic expression using YACC specification
has been successfully executed and verified.
Ex.No:4B Program to recognize a valid variable which starts
with a letter followed by any number of letters or
digits.

AIM:
To recognize a valid variable using YACC specification

ALGORITHM:
1. Accept the token generated in lex part as input
2. Specify order of procedure
3. Define rules with end point
4. Parse input string from standard input by calling yyparse() by main function
5. Print the result of any rules matches to find the variables
6. If none of results defined matches print “invalid variable”
PROGRAM:
var.y

%token DIGIT LETTER NL UND


%%
stmt : variable NL { printf(“Valid Identifiers\n”); exit(0);}
;
variable : LETTER alphanumeric
| LETTER|
;
alphanumeric: LETTER alphanumeric
| DIGIT alphanumeric
| UND alphanumeric
| LETTER
| DIGIT
| UND
;
%%
int yyerror(char *msg)
{
printf(“Invalid Expression\n”);
exit(0);
}
main ()
{
printf(“Enter the variable name\n”);
yyparse();
}
var.l
%{
#include “y.tab.h”
%}
%%
[a-zA-Z] { return LETTER ;}
[0-9] { return DIGIT ; }
[\n] { return NL ;}
[_] { return UND; }
. { return yytext[0]; }
%%

OUTPUT

[ucea-cse@telnet ~]$ yacc -d var.y


[ucea-cse@telnet ~]$ lex var.l
[ucea-cse@telnet ~]$ cc y.tab.c lex.yy.c
[ucea-cse@telnet ~]$ ./a.out
Enter the variable name
g
Valid Identifiers
[shapnarani-cse@telnet ~]$ ./a.out
Enter the variable name
lp
Valid Identifiers
[shapnarani-cse@telnet ~]$ ./a.out
Enter the variable name
u8
Valid Identifiers

RESULT:
Thus t he program to recognize valid variable which starts wit h a letter
fo llowed by any number of letters or digits has been successfully executed
and verified.
Ex.No: 4C Implementation of Calculator using LEX and YACC

AIM:
To write a program to implement the calculator using LEX and YACC.

ALGORITHM:
Step 1: Lex and yacc can be easily interfaced
Step 2: Lex compiler can be used to perform lexical analysis phase
Step 3: Create a lex source with lex specification with extension .l
Step 4: In lex code we check for letter ie. Alphabets and we check for digit ie.numeric
values
Step 5: Lex compiler will produce the tokens
Step 6: Tokens are used for yacc compiler for syntax analyzing
Step 7: In yacc compiler accepts a large class of CFG’s but requires a lower level
analyzer to recognize input tokens
Step 8: In yacc source specifies grammars.
i)write the grammar in .y file
eg:list:list stat|list error
ii)expr:’(‘expr’)’|expr ‘*’ expr|expr’(‘exprexpr|expr’%’expr.
Step 9: In main function yyparse() is used to call parser
Step 10:yyerror() method is used to handle the errors from yacc
Step 11: Compile the code produced by YACC as well as LEX
Step 12: Link the object files to appropriate libraries for executable parser
Step 13: Execute the (program) parser

PROGRAM:
cal.y

%{
#include<stdio.h>
int regs[26];
int base;
%}
%start list
%token DIGIT LETTER
%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS
%%
list:
|
list stat '\n'
|
list error '\n'
{
yyerror;
}
;
stat : expr
{
printf("%d\n",$1);
}
|
LETTER '=' expr
{
regs[$1]=$3;
}
;
expr :
'(' expr ')'
{
$$=$2;
}
|
expr '*' expr
{
$$=$1*$3;
}
|
expr '/' expr
{
$$=$1/$3;
}
|
expr '%' expr
{
$$=$1%$3;
}
|
expr '+' expr
{
$$=$1+$3;
}
|
expr '-' expr
{
$$=$1-$3;
}|
expr '&' expr
{
$$=$1&$3;
}
|
expr '|' expr
{
$$=$1|$3;
}
|
'-' expr %prec UMINUS
{
$$=$2;
}
|
LETTER
{
$$=regs[$1];
}
|
number
;
number : DIGIT
{
$$=$1;
base=($1==0)?8:10;
}
|
number DIGIT
{
$$=base*$1+$2;
}
;
%%
main()
{
return(yyparse());
}
yyerror(s)
char *s;
{
fprintf(stderr,"%s\n",s);
}
yywrap()
{
return(1);
}
cal.l
%{
#include<stdio.h>
#include"y.tab.h"
int c;
extern int yylval;
%}
%%
" ";
[a-z] {
c=yytext[0];
yylval=c-'a';
return (LETTER);
}
[0-9] {
c=yytext[0];
yylval=c-'0';
return (DIGIT);
}
[^a-z0-9\b] {
c=yytext[0];
return (c);
}

OUTPUT:
[ucea-cse@telnet ~]$ yacc -d cal.y
[ucea-cse@telnet ~]$ lex cal.l
[ucea-cse@telnet ~]$ cc y.tab.c lex.yy.c
[ucea-cse@telnet ~]$ ./a.out
9+8
17
(9*3)/3
9
a=9
b=4
a-b
5

RESULT:
Thus the program to implement the calculator using LEX and YACC has been
successfully executed and verified.

You might also like