程序代写代做代考 FTP compiler Homework 4 – Semantic Analyzer ∗

Homework 4 – Semantic Analyzer ∗
Zhiyao Liang MUST FIT zyliang@must.edu.mo
Sunday 18 December 2016
Abstract
The task of this assignment is to implement a semantic analyzer for the C-minus language. [2] [1].
1 Preparation
• Download the provided code files for this homework from the ftp site. Read and understand the provided files. The code folder contains the following:
– cm: A folder containing some C-minus programs. These programs will be used to test your analyzer. There are two kind of programs included in this folder:
∗A picture of a decorated Christmas tree. This semantic analyzer uses a parse tree as its input. It will compute a symbolic table that is connected with the parse tree. The process is similar to decorating a tree.
1

∗ The correct .cm files. They should pass the checking of the analyzer.
∗ The wrong .cm files. They have semantic errors. The com- ments in these files describe the errors and the associated error number. The analyzer should be able to find these er- rors and print out the corresponding error messages.
– scanner_files: A folder containing all of the .h files that are directly related to scanning.
– parser_files: A folder containing all of the .h files that are needed by the code of the analyzer.
– utility_files: A folder containing all of the source code files that are generally applicable tools. Note that a function safe_malloc() is added.
– analyzer_files: A folder containing all of the source code files that implements the analyzer.
– analyzer_test.c: The source code file that contains the main() function.
– mac: A folder designed for compiling the parser on MacOSX. it contains the following:
∗ sample_analyzer_mac.out: A sample executable file that is compiled on MacOSX. To run this parser at command line, use the command:
./sample_analyzer_mac.out
During a run of the parser, when you choose to provide a file as the input of the parser, you can choose a file in the cm folder, say fact.cm, by typing:
../cm/fact.cm
∗ scanner_mac.o: This file is the same one that is used in the
parser project. We explain again the command that gener- ates this file by combining multiple .o files of the scanner project:
ld -r scan.o cminus_dfa.o -o scanner_mac.o
∗ mac_gcc_util.o: A file that is copied from the scanner project.
It is obtained by compiling the
∗ util.c file in the scanner project.
∗ parser_mac.o: A file that is generated by combining the .o
files of the parser project using the ld -r command, just 2

2
like how scanner_mac.o is created. Details of this command
is described in the makefile in the parser project.
∗ makefile: The file that is needed to by the make command. Read this file to know the commands that can be used to compile each .c files. To compile the whole program, use the
following command at command line:
make all
Or, to remove the generated files of compiling, use the com- mand:
make clean
– linux: A folder containing the .o files and the makefile for compil- ing the analyzer program on linux using gcc. A sample executable file is also provided.
– windows: A folder containing two folders:
∗ gcc: The .o files and a makefile for compiling the analyzer program on windows using gcc, and a sample executable file are provided here. During a run of the analyzer, when you wan to provide the name of a file in the cm folder, you can type something like:
../../cm/fact.cm
Tasks to do
1. Read the c-minus.pdf in the ftp site of this course, which provides the step-by-step details of how to implement the analyzer.
2. Read and understand the provided code. 3. Provide the missing code in two files:
• In the file symbol_table.c, provide the code for the functions st_lookup(), st_insert_dcl(), and st_insert_ref().
• In the file s_analyzer.c provide the code for the three functions pre_proc(), post_proc() and check_main().
• Bonus (15%):
(a) When an array is declared, the number of elements should be a constant expression (no varialbe is allowed to appear in the expression), otherwise, it is an error. For example. The following statement is wrong.
3

3
• •
• •



int arr[x+6]; // wrong
The following statement is correct.
int arr[3+6-7]; // correct
Extend the semantic analyzer so that this error can be de-
tected and reported.
(b) Describe a new semantic error of CMinus that is not doc-
mented in this homework, and extend the analyzer to detect and report this new semantic error.
Submitting your homework
Due time: 09 January 2016 Monday, 7:00pm.
At the top of the every file that you changed or provided content (maybe just the .c files), record your name and student ID and date as comments.
Attach the finished symbol_table.c and s_analyzer.c to an email.
If you implement the analyzer by your own original design, or if you have changed some other source code files, please attach all of them to the email, possibly zip them into a single file. Any information about how to run your code will be helpful.
Attention: Do not attach any binary files (.o, .obj, .exe, .out), since google will consider them virus. Just send the source code and textual documents.
Send your email to:
zyliangstu@gmail.com
The title (subject) of your email should be like:
[name][studentID][hmkNum info]
For example, if a student want to submit homework 5, whose name Shan Li, whose student ID is 123-45, then the title of the email should be:
[Li, Shan][123-45][hmk4 analyzer]
4

References
[1] Andrew W. Appel. Modern Compiler Implementation in C: Basic Tech- niques. Cambridge University Press, New York, NY, USA, 1997.
[2] Kenneth C. Louden. Compiler Construction Principles and Practice. PWS Publishing Company, 1997. ISBN 0-534-93972-4 http://www.cs.sjsu.edu/~louden/cmptext/.
5