Question: The Program below is written in V language to check for errors, evaluate and print out P and R . START INTEGER M , N

The Program below is written in V language to check for errors, evaluate and print out P and R
.
START
INTEGER M
,
N
,
K
,
P
,
R
,
H
,
|
,
g
,
k
,
m
READ M
,
N
,
K
ASSIGN N
=
M
-
/
K
READ g
,
H
,
|
,
m
P
=
g
/
H
-
l
+
m
*
N
/
k
R
=
M
+
N
/
K
WRITE P
STOP
The Grammar and Production rules:
EE Rule
1
(
R
1
)
E
>
|
E
+
E
|
E
/
E
|
E
*
E
|
E
+
E
|
R
2
E
>
|
E
1
]
E
2
/
E
3
]
.
.
.
|
E
2
6
|
R
3
[
E
1
|
E
2
|
E
3
|
.
.
.
|
E
2
6
]
J
>
{
JA
|
B
|
C
|
.
.
.
]
Z
|
alb
]
c
]
.
.
.
|
z
]
}
R
4
Note the following additional conditions in the above program:
e Words in capital letters are Keywords
e Words in small letters are Identifiers. Words in single letters from A
Z and a to z are also Identifiers
e
+
,
-
,
/
,
*
are Operators
e Equals
=
,
and semi colon ; are Symbols
e Any string
/
line must contain: Keywords, Identifiers, Operators, or Symbols
e Symbols such as:
%
,
$
,
&
,
<
,
>
,
; not allowed and would give Semantic error
e Two operators must not be combined such as:
+
*
not allowed and would give Syntax error,
same with other operators that are combined, for example
-
/
,
*
/
,
*
+
e Semi colon ; at the end of a line not allowed and would give Syntax error
e Numbers
0
,
1
to
9
are not allowed and would give Syntax error
e The acceptable keywords are: INTEGER, ASSIGN, READ, WRITE, START, STOP.
e Misspelling in the keywords such as: WRITEE not allowed and would give Lexical error
e Any other character on the keyboard different from all the above will give Syntax error.
e Note that text with yellow background in the above program have errors
(
1
)
Write a Java program that will go through the following stages of compiler to translate the
above program
(
iteratively
)
-
line by line.
(
2
)
Write a Java program that will go through the following stages of compiler to translate the
above program
(
iteratively
)
all at once.
Note: Only lines
6
and
7
should go through all the seven
(
7
)
stages of compiler:
P
=
g
/
H
-
ltm
*
N
/
k
and R
=
M
+
N
/
K
The other lines should just check for errors and report the error
(
if any
)
OUR SEVEN STAGES OF COMPILER
1
)
Lexical Analysis
(
Scanner
)
2
)
Syntax Analysis
(
Parser
)
3
)
Semantic Analysis
(
Syntactic Analysis
)
4
)
Intermediate Code Representation
(
ICR
)
5
)
Code Generation
(
CG
)
6
)
Code Optimization
(
CO
)
7
)
Target Machine code
(
TMC
)
in Binary
Give me the full code in java
The output should follow the same concept like the one below
STAGE 1: COMPILER TECHNIQUES--> LEXICAL ANALYSIS-Scanner
SYMBOL TABLE COMPRISING ATTRIBUTES AND TOKENS:
TOKEN#1 D identifier
TOKEN#2/ Operator
TOKEN#3 A identifier
TOKEN#4* Operator
TOKEN#5 F identifier
TOKEN#6- Operator
TOKEN#7 B identifier
TOKEN#8+ Operator
TOKEN#9 C identifier
Total number of Tokens: 9
STAGE 2: COMPILER TECHNIQUES--> SYNTAX ANALYSIS-Parser
INPUT STRING: D/A*F-B+C D=4, A=1, F=6, B=2, C=3
2
INPUT STRING: D/A*F-B+C
The Grammar and Production rules:
EE Rule1(R1)
E|E+E|E/E|E*E|E-E| R2
E|E1|E2|E3|...|E26| R3
[E1|E2|E3|...|E26|]{|A|B|C|...|Z|a|b|c|...|z|} R4
HENCE THE DERIVATION =
EE R1
EE/E R2
EE/E*E R2
EE/E*E-E R2
EE/E*E-E+E R2
EE4/E*E-E+E R3
EE4/E1*E-E+E R3
EE4/E1*E6-E+E R3
EE4/E1*E6-E2+E R3
EE4/E1*E6-E2+E3 R3
ED/E1*E6-E2+E3 R4
ED/A*E6-E2+E3 R4
ED/A*F-E2+E3 R4
ED/A*F-B+E3 R4
ED/A*F-B+C R4 The final string
STAGE 3: COMPILER TECHNIQUES--> SEMANTIC ANALYSIS
CONCLUSION-->This expression: D/A*F-B+C is Syntactically and
Semantically correct.
STAGE 4: INTERMEDIATE CODE REPRESENTATION (ICR)
INPUT STRING: D/A*F-B+C
WE USE BEDMAS
TEMPORARY VARIABLES: WE USE: T1, T2, T3, T4, T5
T1= D/A
T2= T1*F
T3= B+C
T4= T2-T3
3
STAGE 5: CODE GENERATION (CG) WE USE: LDA, MUL, ADD, SUB, STR,
INPUT STRING: D/A*F-B+C
LDA D
DIV A
STR T1
LDA T1
MUL F
STR T2
LDA B
ADD F
STR T3
LDA T2
SUB T3
STR T4
STAGE 6: CODE OPTIMISATION (CO) WE USE: LDA, MUL, ADD, SUB, STR,
INPUT STRING: D/A*F-B+C
DIV T1, A, D
MUL T2, T1, F
ADD T3, B, C
SUB T4, T2, T3
STAGE 7: TARGET MACHINE CODE IN BINARY
INPUT STRING: D/A*F-B+C
010001

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!