Texas Christian University
COSC 30403 Spring 2021
Project 1 Part C
Due: Apr 5th, Monday, at 11:30 PM
Submission: Compressed project folder
Create an analyzer for the easy calculator language. The analyzer should be called
“AnalysisListener.java” and should be in the package “easycalc”. “AnalysisListener” class
should extend “EasyCalcBaseListener” class. In addition, it should contain the following
methods.
• public String getSymbolTableString()
• public String getErrorMessageString()
The purposes of both methods are self-explained. For getting error messages, your program
should be able to catch the following kinds of errors.
• [identifier] undefined at [line]:[pos]
• redefinition of [identifier] at [line]:[pos]
• type clash at [line]:[pos]
• [operator] undefined for [type] at [line]:[pos]
Note that if an error occurs within an expression, it shouldn’t cause the error to be propagated up
to its parent expressions. For more detailed information about the output format, please refer to
the test cases at the end of this document. Also, remember to include sufficient comments in your
code.
Here are some tips that may help you to complete the project. These tips are not requirements.
1. You may want to override most of the “exit” methods from “EasyCalcBaseListener” like
in Part B.
2. You may consider using the following Java built-in data structures: List, SortedMap and
Stack.
3. Java built-in exception handling mechanism won’t help this project, and thus should
NOT be used.
A printer file “AnalysisApp.java” is provided. This file should be in the package “easycalc”, and
can show the results of your analyzer. Note that for the project, you only need to write code in
“AnalysisListener.java”, and you don’t want to change or modify anything else. Here are the test
cases you could use to test your results.
Test 1
int a;
a := (b + 2) * 3;
$$
→
a -> INT
b undefined at 2:7
Test 2
int a;
bool a;
$$
→
a -> INT
redefinition of a at 2:6
Test 3
bool a;
int b;
a := b;
$$
→
a -> BOOL
b -> INT
type clash at 3:1
Test 4
write 3 * 2.0;
$$
→
type clash at 1:7
Test 5
real a;
int b;
a := b * 4.0 – 3 * 2.0;
$$
→
a -> REAL
b -> INT
type clash at 3:6
Test 6
int a;
a := if true then 3.0 else 2;
$$
→
a -> INT
type clash at 2:19
Test 7
int a;
bool b;
a := 2 * b;
$$
→
a -> INT
b -> BOOL
* undefined for BOOL at 3:10
Test 8
int a;
bool b;
a := true + b / 3;
$$
→
a -> INT
b -> BOOL
/ undefined for BOOL at 3:13
Test 9
write 3.0 or 2;
$$
→
or undefined for REAL at 1:7
Test 10
write if 3 then 4 else 2;
$$
→
if undefined for INT at 1:10
Test 11
int a;
bool b;
a := to_int(b and false);
$$
→
a -> INT
b -> BOOL
to_int undefined for BOOL at
3:13
Test 12
real a;
write to_real(a + 2.0);
$$
→
a -> REAL
to_real undefined for REAL at
2:15
Test 13
int a;
a := (b + 2) * 3;
write 3 * 2.0;
$$
→
a -> INT
b undefined at 2:7
type clash at 3:7
Test 14
int a;
bool b;
a := (a + 4.0) * 3 – 2.0;
a := to_int(b and false);
$$
→
a -> INT
b -> BOOL
type clash at 3:7
to_int undefined for BOOL at
4:13
Test 15
int a;
bool b;
a := true + b / 3;
a := if true then 3.0 else 2;
$$
→
a -> INT
b -> BOOL
/ undefined for BOOL at 3:13
type clash at 4:19
学霸联盟