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
R
E
E
E
E
E
E
E
E
E
R
E
E
E
E
E
R
E
E
E
E
J
JA
B
C
Z
alb
c
z
R
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
to
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
Write a Java program that will go through the following stages of compiler to translate the
above program
iteratively
line by line.
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
and
should go through all the seven
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
Lexical Analysis
Scanner
Syntax Analysis
Parser
Semantic Analysis
Syntactic Analysis
Intermediate Code Representation
ICR
Code Generation
CG
Code Optimization
CO
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 : COMPILER TECHNIQUES LEXICAL ANALYSISScanner
SYMBOL TABLE COMPRISING ATTRIBUTES AND TOKENS:
TOKEN# D identifier
TOKEN# Operator
TOKEN# A identifier
TOKEN# Operator
TOKEN# F identifier
TOKEN# Operator
TOKEN# B identifier
TOKEN# Operator
TOKEN# C identifier
Total number of Tokens:
STAGE : COMPILER TECHNIQUES SYNTAX ANALYSISParser
INPUT STRING: DAFBC D A F B C
INPUT STRING: DAFBC
The Grammar and Production rules:
EE RuleR
EEEEEEEEE R
EEEEE R
EEEEABCZabcz R
HENCE THE DERIVATION
EE R
EEE R
EEEE R
EEEEE R
EEEEEE R
EEEEEE R
EEEEEE R
EEEEEE R
EEEEEE R
EEEEEE R
EDEEEE R
EDAEEE R
EDAFEE R
EDAFBE R
EDAFBC R The final string
STAGE : COMPILER TECHNIQUES SEMANTIC ANALYSIS
CONCLUSIONThis expression: DAFBC is Syntactically and
Semantically correct.
STAGE : INTERMEDIATE CODE REPRESENTATION ICR
INPUT STRING: DAFBC
WE USE BEDMAS
TEMPORARY VARIABLES: WE USE: T T T T T
T DA
T TF
T BC
T TT
STAGE : CODE GENERATION CG WE USE: LDA, MUL, ADD, SUB, STR
INPUT STRING: DAFBC
LDA D
DIV A
STR T
LDA T
MUL F
STR T
LDA B
ADD F
STR T
LDA T
SUB T
STR T
STAGE : CODE OPTIMISATION CO WE USE: LDA, MUL, ADD, SUB, STR
INPUT STRING: DAFBC
DIV T A D
MUL T T F
ADD T B C
SUB T T T
STAGE : TARGET MACHINE CODE IN BINARY
INPUT STRING: DAFBC
