CS544 Project Documentation



CS544 Compilers Project Documentation

[pic]

Javalet Compiler Project

Dave T. Tran

Document Change Control

|Rev |Date |Change Originator |Revised By |Section Changed |Description of the Change |

| | | | | | |

| | | | | | |

Table of Contents

1 Purpose/Scope 4

2 The Scanner 5

2.1 The Tokens 5

2.2 The Java File 6

2.3 Inputs 10

2.3.1 Input 1 10

2.3.2 Input 2 10

2.3.3 Input 3 10

2.3.4 Input 4 10

2.4 Outputs 11

2.4.1 Output 1 11

2.4.2 Output 2 12

2.4.3 Output 3 13

2.4.4 Output 4 13

3 Appendix 16

Purpose/Scope

This document provides all the information necessary to describe what has been done on the Javalet compiler project and how this compiler project was developed using Java 1.5. The project covers the stages the compiler design including the scanner, the parser, the AST and the code generator. The Java IDE that will be used to assist in the compiler design is IntelliJ IDEA 8.1.2. Using this and the power of the Java 1.5 language, a robust compiler will be created in order to support the Javalet language.

The following sections are broken up by the separate stages of the compiler design and go into greater technical design on what effort went into this project.

The Scanner

The Scanner is the first stage of the compiler design. This stage consists of reading the provided input, tokenizing the input, identifying the token class for each token and providing the resulting token translation as output.

Scanner Execution Instructions:

Dependencies: Java 1.5

Execute: java Javalet in order to execute the scanner.

1 The Tokens

The following is the list of tokens used in this project:

Type

"void", "int", “real”

Logical Operators

"!", "||", "&&", "!=", "==", "", "="

Numerical Operators

"+", "-", "*", "/", “=”

Punctuation

"{", "}", "(", ")", ",", ";"

Keywords

"if", "else", "while", "do", "for"

Names

Letter (Letter | Digit |_) * where a Letter is either an uppercase or lowercase letter and Digit is one of the digits from 0-9. There cannot be 2 consecutive underscores.

Integers

Sequences of 1 or more digits

Reals

Plus or minus followed by a number of digits followed by a dot ".", followed by a number of digits. Either the sequence before the dot can be null or the sequence after the dot can be null, but not both.

2 The Java File

This is the contents of the Java file used to construct the Scanner.

import java.io.*;

import java.util.Scanner;

import java.util.HashMap;

import java.util.ArrayList;

import java.util.regex.Pattern;

/**

* This class is for the CS544 Javalet compiler project.

*

* Created by trandt (davetran@)

* Created on Jun 1, 2009 at 9:42:54 PM.

* Version: 1.0

*/

public class Javalet {

//Discrete mapping for tokens

static HashMap discreteMap;

/**

* Population of the discrete mapping

*/

static {

discreteMap = new HashMap();

//type

discreteMap.put("void", "TYPE");

discreteMap.put("int", "TYPE");

discreteMap.put("real", "TYPE");

//logical operators

discreteMap.put("!", "NOT");

discreteMap.put("||", "OR");

discreteMap.put("&&", "AND");

discreteMap.put("!=", "NOTEQUIVALENT");

discreteMap.put("==", "EQUIVALENT");

discreteMap.put("", "GREATERTHAN");

discreteMap.put("=", "GREATERTHANEQUAL");

//numerical operators

discreteMap.put("+", "PLUS");

discreteMap.put("-", "MINUS");

discreteMap.put("*", "MULTIPLY");

discreteMap.put("/", "DIVIDE");

discreteMap.put("=", "EQUAL");

//punctuation

discreteMap.put("{", "OBRACE");

discreteMap.put("}", "EBRACE");

discreteMap.put("(", "OPARAM");

discreteMap.put(")", "EPARAM");

discreteMap.put(",", "COMMA");

discreteMap.put(";", "SEMICOLON");

//keywords

discreteMap.put("if", "IF");

discreteMap.put("else", "ELSE");

discreteMap.put("while", "WHILE");

discreteMap.put("do", "DO");

discreteMap.put("for", "FOR");

}

//Patterns (test at )

static Pattern namePattern = pile("[a-zA-Z0-9]*(_[a-zA-Z0-9]+)*");

static Pattern integerPattern = pile("[0-9]+");

static Pattern realPattern = pile("[-\\+]?[0-9]+.[0-9]+");

/**

* This is the main method.

*

* @param args (String[]) The arguments to provide.

* @throws IOException if a file can not be found.

*/

public static void main(String[] args) throws IOException {

//Check the input to see if a file was provided

if (null == args || args.length < 1) {

System.out.println("Please include the filename on the command line.");

System.exit(1);

}

//Check if this is a valid file

String filepath = args[0];

File file = new File(filepath);

if (!file.exists()) {

System.out.println("Please provide a valid filename on the command line.");

System.exit(1);

}

//Read in the file into a scanner

Scanner scanner = new Scanner(file);

//loop through each token

boolean foundSuffix, foundPrefix;

String token, suffix, prefix;

ArrayList suffixes, prefixes;

while (scanner.hasNext()) {

token = scanner.next();

prefixes = new ArrayList();

suffixes = new ArrayList();

//check if this token ends with a comma or semicolon

foundPrefix = true;

while(foundPrefix)

{

foundPrefix = false;

if(token.startsWith("!"))

{

prefixes.add("!");

foundPrefix = true;

token = token.substring(1, token.length());

}

else if(token.startsWith("("))

{

prefixes.add("(");

foundPrefix = true;

token = token.substring(1, token.length());

}

}

//check if this token ends with a comma or semicolon

foundSuffix = true;

while(foundSuffix)

{

foundSuffix = false;

if(token.endsWith(","))

{

suffixes.add(",");

foundSuffix = true;

token = token.substring(0, token.length() - 1);

}

else if(token.endsWith("("))

{

suffixes.add("(");

foundSuffix = true;

token = token.substring(0, token.length() - 1);

}

else if(token.endsWith(")"))

{

suffixes.add(")");

foundSuffix = true;

token = token.substring(0, token.length() - 1);

}

else if(token.endsWith(";"))

{

suffixes.add(";");

foundSuffix = true;

token = token.substring(0, token.length() - 1);

}

}

//check if the prefix needs to be processed

if(!prefixes.isEmpty())

{

int size = prefixes.size();

for(int i = 0; i < size; i++)

{

prefix = prefixes.get(i);

if(null != discreteMap.get(prefix))

{

System.out.println(discreteMap.get(prefix) + " ");

}

}

}

//translate each token

if (null != discreteMap.get(token))

{

System.out.println(discreteMap.get(token) + " ");

}

else if(namePattern.matcher(token).matches())

{

System.out.println("NAME: " + token + " ");

}

else if(integerPattern.matcher(token).matches())

{

System.out.println("INT: " + token + " ");

}

else if(realPattern.matcher(token).matches())

{

System.out.println("REAL: " + token + " ");

}

else

{

System.out.println("ERROR: " + token + " ");

}

//check if the suffix needs to be processed

if(!suffixes.isEmpty())

{

int size = suffixes.size();

for(int i = size - 1; i >= 0; i--)

{

suffix = suffixes.get(i);

if(null != discreteMap.get(suffix))

{

System.out.println(discreteMap.get(suffix) + " ");

}

}

}

}

//close the scanner

scanner.close();

}

}

3 Inputs

This section provides a list of inputs used to test out the scanner.

1 Input 1

void input_a() {

integer a, bb, xyz, b3, c, p, q;

real b;

a = b3;

b = -2.5;

xyz = 2 + a + bb + c - p / q;

a = xyz * ( p + q );

p = a - xyz - p;

}

2 Input 2

void input_b() {

if ( i > j )

i = i + j;

else if ( i < j )

i = 1;

}

3 Input 3

void input_c() {

while ( i < j && j < k ) {

k = k + 1;

while ( i == j )

i = i + 2;

}

}

4 Input 4

void input_d() {

int a = 0;

int j = 0;

boolean flag = false;

do

{

for ( int i = 10; i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download