Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java program to process and display the following stages of compiler: Stage 1 : Lexical analysis ( Scanning ) Stage 2 : Syntax

Write a Java program to process and display the following stages of compiler:
Stage 1: Lexical analysis (Scanning)
Stage 2: Syntax Analysis (Parsing)
Stage 3: Semantic Analysis
Stage 4: Intermediate Code Representation (ICR)
Stage 5: Code Generation (CG)
Stage 6: Code Optimization (CO)
Stage 7: Target Machine Code (TMC) in Binary
VPL elearning lab to be done by all students: Students are to write Java codes for stages 1,2,3,4,5,6 and 7 of the compiler and submit on VPL/elearning for automatic grading.
Sample output:
A MINI COMPILER PROJECT FOR CTE711S
===================================
ENTER NEXT STRING === #1
-Every String/line must end with a semicolon (;)
-Enter String (Containing 0 to 9 and/or operators: +,/,*,-)
-Enter No Space in b/w chara & end the string with semicolon(;)ie 5-4+9*8/2; for Full(7 stages) results of compilation
-or Enter Space in b/w char with semicolon(;) at end of String ie 5-4+9*8/2 ; for the result of arithmetic expression
-Or Type 99 and press Enter to Quit:
7/3-9+6*2;
======STAGE1: COMPILER TECHNIQUES--> LEXICAL ANALYSIS-Scanner
SYMBOL TABLE COMPRISING ATTRIBUTES AND TOKENS:
TOKEN#17 identifier
TOKEN#2/ Operator
TOKEN#33 identifier
TOKEN#4- Operator
TOKEN#59 identifier
TOKEN#6+ Operator
TOKEN#76 identifier
TOKEN#8* Operator
TOKEN#92 identifier
TOKEN#10 ; symbol
Total number of Tokens: 10
GIVEN THE GRAMMAR: E=E1| E=E1*E2| E=E1+E2| E=digit | E={0,1,2,3,4,5,6,7,8,9}
======STAGE2: COMPILER TECHNIQUES--> SYNTAX ANALYSIS-Parser
GET A DERIVATION FOR : 7/3-9+6*2 ;
E7/ E3- E9+ E6* E2 ;
E7/ E3- E9+ E6* digit2 ;
E7/ digit3- E9+ E6* digit2 ;
E7/ digit3- E9+ digit6* digit2 ;
digit7/ digit3- E9+ digit6* digit2 ;
digit7/ digit3- digit9+ digit6* digit2 ;
digit7/ digit3- digit9+ digit6*2 ;
digit7/3- digit9+ digit6*2 ;
digit7/3- digit9+6*2 ;
7/3- digit9+6*2 ;
7/3-9+6*2 ;
======STAGE3: COMPILER TECHNIQUES--> SEMANTIC ANALYSIS
CONCLUSION-->This expression: 7/3-9+6*2 ; is Syntactically and Semantically correct
======STAGE4: COMPILER TECHNIQUES--> INTERMEDIATE CODE REPRESENTATION (ICR)
THE STRING ENTERED IS : 7/3-9+6*2
The ICR is as follows:
t1=7/3
t2=6*2
t3=9+t2
t4= t2-t3
CONCLUSION-->The expression was correctly generated in ICR
======STAGE5: CODE GENERATION (CG)
LDA 2
DIV 8
STR t1
LDA 9
MUL t1
STR t2
LDA 4
ADD t2
STR t3
LDA 5
SUB t3
STR t4
======STAGE6: CODE OPTIMISATION (CO)
DIV t1,2,8
MUL t2,9, t1
ADD t3,4, t2
SUB t4,5, t3
======STAGE7: TARGET MACHINE CODE (TMC)
001011111
0010101010
0010101111
00101101100
======END OF COMPILATION
======THE ORIGINAL INPUT STRING IS: 5-4+9*8/2 ;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions