0% found this document useful (0 votes)
56 views4 pages

Simple Calculator Program with YACC

1) The document describes a program for a simple calculator using YACC that implements a bottom-up parser for syntax analysis. 2) It includes the source code for the lexical analyzer and parser, with rules for tokens like numbers, operators, and parentheses. 3) The driver code prompts the user for an arithmetic expression and parses it, printing whether it is valid or invalid.

Uploaded by

Abhinav arora
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)
56 views4 pages

Simple Calculator Program with YACC

1) The document describes a program for a simple calculator using YACC that implements a bottom-up parser for syntax analysis. 2) It includes the source code for the lexical analyzer and parser, with rules for tokens like numbers, operators, and parentheses. 3) The driver code prompts the user for an arithmetic expression and parses it, printing whether it is valid or invalid.

Uploaded by

Abhinav arora
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

Abhinav Arora (08) 1714110101

Name: Abhinav Arora


CD ASSIGNMENT NO.4
Q) Write a program for Simple Calculator using YACC. Explain the different Bottom up
Parser for Syntax Analyzer

CODE FOR CALCULATOR-


Lexical Analyzer Source Code:
%{
/* Definition section */
#include<stdio.h>
#include "[Link].h"
extern int yylval;
%}
/* Rule Section */
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
Parser Source Code :
%{
/* Definition section */
#include<stdio.h>
int flag=0;
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
/* Rule Section */
%%

ArithmeticExpression:
E{
printf("\nResult=%d\n", $$);
return 0;
};
E:E'+'E {$$=$1+$3;}

|E'-'E {$$=$1-$3;}

|E'*'E {$$=$1*$3;}
Abhinav Arora (08) 1714110101
Name: Abhinav Arora
|E'/'E {$$=$1/$3;}

|E'%'E {$$=$1%$3;}

|'('E')' {$$=$2;}

| NUMBER {$$=$1;}

;
%%
//driver code
void main()
{
printf("\nEnter Any Arithmetic Expression which
can have operations Addition,
Subtraction, Multiplication, Division,
Modulus and Round brackets:\n");
yyparse();
if(flag==0)
printf("\nEntered arithmetic expression is Valid\n\n");
}
void yyerror()
{
printf("\nEntered arithmetic expression is Invalid\n\n");
flag=1;
}

OUTPUT-

You might also like