0% found this document useful (0 votes)
8 views3 pages

Wa0011.

DSA

Uploaded by

srinithiravi27
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)
8 views3 pages

Wa0011.

DSA

Uploaded by

srinithiravi27
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/ 3

Ex.No.3a Generate YACC specification for a few syntactic categories.

a) Program to recognize a valid arithmetic expression that uses operator +, – , * and /.

AIM : To Write a program to implement a calculator using Yacc Tool.

ALGORITHM:
STEP 1: Define and include necessary ‘C’ declarations and token definitions
STEP 2: Define the translation rules and the input structure specifications for
the grammar
rules
STEP 3:Define the functions that are invoked in the rules
STEP 4:Read the input
STEP 5: If the given input matches with the defined rules , the yacc tool
executes the
respective actions.
STEP 6: End

PROGRAM:
//Program to implement calculator using yacc Tool

%{
#define YYSTYPE double
%}
%token NUMBER
%left '+' '-'
%left '*' '/'
%%
list: /*nothing*/
|list '\n'
|list expr '\n' {printf("\t%.8g\n",$2);}
;
expr: NUMBER {$$=$1;}
|expr '+' expr {$$=$1+$3;}
|expr '-' expr {$$=$1-$3;}
|expr '*' expr {$$=$1*$3;}
|expr '/' expr {$$=$1/$3;}
|'(' expr ')' {$$=$2;}
;
%%
#include<stdio.h>
#include<ctype.h>
char *progname;
int lineno=1;
main(argc,argv)
char *argv[];
{
progname=argv[0];
yyparse();
}
yylex()
{
int c;
while((c=getchar())==' '||c=='\t')
;
if(c==EOF)
return 0;
if(c=='.'||isdigit(c))
{
ungetc(c,stdin);
scanf("%lf",&yylval);
return NUMBER;
}
if(c=='\n')
lineno++;
return(c);
}

How to execute

$yacc ss.y (create a file called y.tab.c)


$cc y.tab.c –ly
$./a.out
SAMPLE INPUT & OUTPUT

Input:

5+6

Output:
11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy