0% found this document useful (0 votes)
54 views5 pages

Compiler Construction: Assignment # 02

This document contains an assignment submission for a compiler construction course. The assignment asks the student to write a LEX and YACC program to implement a simple arithmetic calculator. The student provides the source code for the LEX and YACC programs along with examples of expressions to test it. No output is shown, as the document states it is the last required submission without results.

Uploaded by

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

Compiler Construction: Assignment # 02

This document contains an assignment submission for a compiler construction course. The assignment asks the student to write a LEX and YACC program to implement a simple arithmetic calculator. The student provides the source code for the LEX and YACC programs along with examples of expressions to test it. No output is shown, as the document states it is the last required submission without results.

Uploaded by

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

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: 05

Obtained Marks:

Compiler Construction
Assignment # 02

Last date of Submission: 7 May 2021

Submitted To: Muhammad Nadeem Khokhar


_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Student Name: Muhammad Shehbaz Khan


_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Reg Number: 1812250


_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Compiler Construction BS(CS)-5B SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Instructions: Copied or shown assignments will be marked zero. Late submissions are not
entertained in any case.

Question
Write a LEX/FLEX and YACC/BISON program to implement a simple arithmetic calculator.
It should be able to recognize negative numbers and add, subtract, multiply, divide and group
sub-expressions. Sample expressions to test the program are given below. Give the source
code and the runtime screen.
2+2*2
(2+2)*2
12/2+3
2*25/3

Solution
Lex code
%{
/* Definition section */
#include<stdlib.h>
#include "y.tab.h"
extern int yylval;
%}

/* Rule Section */
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;

}
'<=' return LE;
'>=' return GE;
'!=' return NE;
'==' return EQ;
[\t] ;
[\n] return 0;

Compiler Construction BS(CS)-5B SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

. return yytext[0];
%%

Yacc code

%{
/* Definition section */
#include<stdio.h>
int flag=0;
%}

%token NAME NUMBER

%left GE LE EQ NE EE '<' '>'

%left '+' '-'

%left '*' '/' '%'

%left '(' ')'

%nonassoc UMINUS
/* Rule Section */
%%

ArithmeticExpression: E{

printf("Result=%d", $$);

return 0;

};
E:E '+' E {$$=$1+$3;}

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

Compiler Construction BS(CS)-5B SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

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

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

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

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

| NUMBER {$$=$1;}

|E GE E {$$=$1 >= $3 ;}

|E LE E {$$=$1 <= $3 ;}

|E NE E {$$=$1 != $3 ;}

|E EE E {$$=$1 == $3 ;}

|UMINUS E {$$=-$1 ;}
;

%%

//driver code
int main()
{
//printf("\nEnter the Expression:\n");
yyparse();
//if(flag==0)
//printf("\nEntered arithmetic expression is Valid\n\n");
// return 0;
}

int yyerror()
{

Compiler Construction BS(CS)-5B SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

// printf("\nEntered arithmetic expression is Invalid\n\n");


// flag=1;
}
int yywrap(){
return 1;
}

Output :

---
---
---

Last Required Result’s Output:

Compiler Construction BS(CS)-5B SZABIST-ISB

You might also like