Docsity

Programming Assignment 2 | Compiler Design | CIS 4930, Assignments of Computer Science

Toggle sidebar
bg1
Compiler Design (CIS 4930/6930) [Fall 2007]
Programming Assignment II
Objectives
1. To become familiar with flex, a popular program used to generate lexical analyzers.
2. To implement a lexical analyzer for programs written in DJ.
3. To understand which strings constitute valid tokens in DJ.
4. To practice writing regular expressions by specifying valid DJ tokens.
Due Date: Sunday, September 23, 2007 (at 11:59pm).
Machine Details
Complete this assignment by yourself on the following CSEE network computers:
c4labpc11, c4labpc12, ..., c4labpc29. These machines are physically located in the
Center 4 lab (Room 220). Do not use any server machines like grad, babbage, sunblast,
etc. You can connect to the C4 machines from home using SSH. (Example: Host name:
c4labpc11.csee.usf.edu Login ID and Password: <your login id and password>) You are
responsible for ensuring that your program executes properly on these machines.
Assignment Description
This assignment asks you to implement the lexical-analysis phase of our dj2dism
compiler. You will use flex (the current version of lex) to generate a lexical analyzer for
programs written in DJ.
Please begin by downloading the auxiliary file dj.y from
http://www.cse.usf.edu/~ligatti/compilers-07/as2/. In dj.y you will find an enumeration
of all possible DJ tokens.
Then, in a new file called dj.l (that is the letter “el”, which stands for “lex”, after the
period), write a flex input file that recognizes the token types enumerated in dj.y.
Whenever your lexer recognizes a DJ token, it should print the token to the screen
(example executions are shown below).
Big Hint
You will probably find it helpful to study the dism.l file (available at
http://www.cse.usf.edu/~ligatti/compilers-07/as1/dism/sim-dism/) and to use dism.l as a
reference for your own dj.l file. Your dj.l will probably be quite similar to dism.l, except
that dj.l will have the DEBUG flag set to 1 instead of 0, and dj.l will have different
tokens and regular expressions to match those tokens.
Behavior on Source-program Errors
For all assignments in this course, your program may exit immediately after detecting and
reporting the first error. Of course, this behavior is undesirable from a user’s perspective.
Hence, although it is not required, I recommend trying to implement all phases of
compilation such that as many errors as possible get reported before exiting the compiler.
1
pf2

Partial preview of the text

Download Programming Assignment 2 | Compiler Design | CIS 4930 and more Assignments Computer Science in PDF only on Docsity!

Compiler Design (CIS 4930/6930) [Fall 2007] Programming Assignment II Objectives

  1. To become familiar with flex, a popular program used to generate lexical analyzers.
  2. To implement a lexical analyzer for programs written in DJ.
  3. To understand which strings constitute valid tokens in DJ.
  4. To practice writing regular expressions by specifying valid DJ tokens. Due Date: Sunday, September 23, 2007 (at 11:59pm). Machine Details Complete this assignment by yourself on the following CSEE network computers: c4labpc11, c4labpc12, ..., c4labpc29. These machines are physically located in the Center 4 lab (Room 220). Do not use any server machines like grad, babbage, sunblast, etc. You can connect to the C4 machines from home using SSH. (Example: Host name: c4labpc11.csee.usf.edu Login ID and Password: ) You are responsible for ensuring that your program executes properly on these machines. Assignment Description This assignment asks you to implement the lexical-analysis phase of our dj2dism compiler. You will use flex (the current version of lex) to generate a lexical analyzer for programs written in DJ. Please begin by downloading the auxiliary file dj.y from http://www.cse.usf.edu/~ligatti/compilers-07/as2/. In dj.y you will find an enumeration of all possible DJ tokens. Then, in a new file called dj.l (that is the letter “el”, which stands for “lex”, after the period), write a flex input file that recognizes the token types enumerated in dj.y. Whenever your lexer recognizes a DJ token, it should print the token to the screen (example executions are shown below). Big Hint You will probably find it helpful to study the dism.l file (available at http://www.cse.usf.edu/~ligatti/compilers-07/as1/dism/sim-dism/) and to use dism.l as a reference for your own dj.l file. Your dj.l will probably be quite similar to dism.l , except that dj.l will have the DEBUG flag set to 1 instead of 0, and dj.l will have different tokens and regular expressions to match those tokens. Behavior on Source-program Errors For all assignments in this course, your program may exit immediately after detecting and reporting the first error. Of course, this behavior is undesirable from a user’s perspective. Hence, although it is not required, I recommend trying to implement all phases of compilation such that as many errors as possible get reported before exiting the compiler. 1

Compilation of the Lexer Compile your lexer with the following commands (where “>” is a command prompt).

flex dj.l bison dj.y gcc dj.tab.c –lfl –o dj-lex Example Executions The good1.dj program is: class C extends Object { } main { 0; } This good1.dj file is correctly tokenized as follows. ./dj-lex good1.dj CLASS ID(C) EXTENDS ID(Object) LBRACE RBRACE MAIN LBRACE NATLITERAL(0) SEMICOLON RBRACE ENDOFFILE

As another example, the good3.dj program is: //prints 4 main { var nat x; x=0; x=x+1; x=x+1; x=x+1; x=x+1; printNat(x); } This good3.dj file is correctly tokenized as follows.

./dj-lex good3.dj MAIN LBRACE VAR NATTYPE ID(x) SEMICOLON ID(x) ASSIGN NATLITERAL(0) SEMICOLON ID(x) ASSIGN ID(x) PLUS NATLITERAL(1) SEMICOLON ID(x) ASSIGN ID(x) PLUS NATLITERAL(1) SEMICOLON ID(x) ASSIGN ID(x) PLUS NATLITERAL(1) SEMICOLON ID(x) ASSIGN ID(x) PLUS NATLITERAL(1) SEMICOLON PRINTNAT LPAREN ID(x) RPAREN SEMICOLON RBRACE ENDOFFILE

Please note that we will test your lexer on DJ files that have not been distributed to the class. Submission Notes  Type the following pledge as an initial comment in your dj.l file: “I pledge my honor that I have not cheated on this assignment.” Type your name after the pledge. Not including this pledge will lower your grade 50%.  Upload and submit your dj.l file into the digital dropbox in Blackboard.  For every day that your assignment is late, your grade reduces 10%.  Your program must be commented appropriately. 2