Homework 3 – Parser

Homework 3 – Parser ∗

MUST FIT

Friday 2 December 2016

Abstract

The task of this assignment is to implement a recursive-descendant parser 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 parser. Especially, if you make some

∗A picture of a parse tree for a sentence of Englich, which is a natural language. We will write a parser that can compute a parse tree for a program of a computer programming language

1

syntax error in one of these file, then your parser should be able to detect it.

  • –  parser_files: A folder containing all of the source code files directly related to parsing. Especially, the file rd_parse.c has some missing functions that you need to provided.
  • –  scanner_files: A folder containing all of the .h files that are directly related to scanning.
  • –  utility_files: A folder containing all of .h files of the general utlities code that we used in the scanner homework.
  • –  parser_driver.c: The source code file that contains the main() function.
  • –  C-parser_FAQ.txt: A file explaining some aspects of the design of the parser.
  • –  mac: A folder designed for compiling the parser on MacOSX. it contains the following:
    • ∗  sample_parser_mac.out: A sample executable file that is com- piled on MacOSX. To run this parser at command line, with a source file in the provided cm folder, in the simplest way without any option, we can use the command:
                   ./sample_parser_mac.out ../cm/fileName.cm
      

      To know more options of running the parser, use the com- mand

                           ./sample_parser_mac.out -h
      
    • ∗  scanner_mac.o: A file generated by combining multiple .o

      files of the scanner project using the following command:

                   ld -r scan.o cminus_dfa.o -o scanner_mac.o
      
    • ∗  mac_gcc_util.o: The file generated by compiling
    • ∗  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

      2

2

  • –  linux: A folder containing the .o files and the makefile for compil- ing the parser program on linux using gcc. A sample executable file is also provided.
  • –  windows: A folder containing two folders:
    • ∗  gcc: The .o and files and makefile for compiling the parser program on windows using gcc. A sample executable file is provided. To provide the name of a file in the cm folder, you can type something in the command like:
                                     ../../cm/fact.cm
      
    • ∗  cl: The .obj files and the makefile for compiling the parser

      using the compiler cl. A sample executable file is provided. Especially, the file scanner_win_cl.lib is generated in the scanner project folder by the following command:

                   lib /OUT:scanner_win_cl.lib  scan.obj cminus_dfa.obj
      

      Tasks to do

      1. Read and understand the provided code.
      2. Provide all of the missing code in the files parser_files/rd_parser.c

      and parse_util.c. 3. Bonus (+20):

• Modify (extend) the grammar, so that the logical operations &&}}, ||}}, and ! are allowed in the C-Minus language. The precedence and associativity of these operators should be re- spected according to he standards of ANSI C (Please refer to the files in the folder c_operators).

  • Addthecorrespondingfunctionsinthefileparser_files/rd_parser.c to implement the changed grammar for the logical rules. Cor- respondingly, the scanner part of the compiler should also be changed to accept the logical operators.
  • Write some C-minus source code file where the logical operators appear, test it with the parser.

    Submitting your homework

    • Due time: 23 December 2016 Friday, 7:00pm. 3

3

  • 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 rd_parser.c and parse_util.c to an email. That is all you need to send.
  • 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 3, whose name Shan Li, whose student ID is 123-45, then the title of the email should be:

                         [Li, Shan][123-45][hmk3 parser]
    

    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/.

    4