Просмотр исходного кода

Initial commit: frontend.scheme

Mathias Landhäußer 11 лет назад
Родитель
Сommit
45e959895c

+ 27 - 0
jplag.frontend.scheme/.classpath

@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
+		<attributes>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="src" path="target/generated-sources/javacc"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>

+ 23 - 0
jplag.frontend.scheme/.project

@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>jplag.frontend.scheme</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+	</natures>
+</projectDescription>

+ 3 - 0
jplag.frontend.scheme/.settings/org.eclipse.core.resources.prefs

@@ -0,0 +1,3 @@
+eclipse.preferences.version=1
+encoding//src/main/java=UTF-8
+encoding/<project>=UTF-8

+ 12 - 0
jplag.frontend.scheme/.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,12 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.7
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
+org.eclipse.jdt.core.compiler.source=1.7

+ 4 - 0
jplag.frontend.scheme/.settings/org.eclipse.m2e.core.prefs

@@ -0,0 +1,4 @@
+activeProfiles=
+eclipse.preferences.version=1
+resolveWorkspaceProjects=true
+version=1

+ 127 - 0
jplag.frontend.scheme/pom.xml

@@ -0,0 +1,127 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<artifactId>scheme</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+
+	<repositories>
+		<repository>
+			<id>ipdNexus</id>
+			<name>Recaa Repository</name>
+			<url>https://bob.ipd.kit.edu/nexus/content/repositories/public/</url>
+		</repository>
+	</repositories>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.codehaus.mojo</groupId>
+				<artifactId>build-helper-maven-plugin</artifactId>
+				<version>1.7</version>
+				<configuration>
+					<sources>
+						<source>target/generated-sources/javacc</source>
+					</sources>
+				</configuration>
+				<executions>
+					<execution>
+						<id>add-javacc-source</id>
+						<phase>generate-sources</phase>
+						<goals>
+							<goal>add-source</goal>
+						</goals>
+
+					</execution>
+				</executions>
+			</plugin>
+			<plugin>
+				<groupId>org.codehaus.mojo</groupId>
+				<artifactId>javacc-maven-plugin</artifactId>
+				<version>2.6</version>
+				<configuration>
+					<grammarEncoding>${project.build.sourceEncoding}</grammarEncoding>
+					<isStatic>false</isStatic>
+					<debugparser>false</debugparser>
+				</configuration>
+
+				<executions>
+					<execution>
+						<id>javacc</id>
+						<goals>
+							<goal>javacc</goal>
+						</goals>
+					</execution>
+				</executions>
+
+				<dependencies>
+					<dependency>
+						<groupId>net.java.dev.javacc</groupId>
+						<artifactId>javacc</artifactId>
+						<version>3.2</version>
+					</dependency>
+				</dependencies>
+			</plugin>
+
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-jar-plugin</artifactId>
+				<configuration>
+					<archive>
+						<manifest>
+							<mainClass>jplag.scheme.Parser</mainClass>
+							<packageName>jplag.scheme</packageName>
+						</manifest>
+					</archive>
+				</configuration>
+			</plugin>
+		</plugins>
+		<pluginManagement>
+			<plugins>
+				<!--This plugin's configuration is used to store Eclipse m2e settings 
+					only. It has no influence on the Maven build itself. -->
+				<plugin>
+					<groupId>org.eclipse.m2e</groupId>
+					<artifactId>lifecycle-mapping</artifactId>
+					<version>1.0.0</version>
+					<configuration>
+						<lifecycleMappingMetadata>
+							<pluginExecutions>
+								<pluginExecution>
+									<pluginExecutionFilter>
+										<groupId>
+											org.codehaus.mojo
+										</groupId>
+										<artifactId>
+											javacc-maven-plugin
+										</artifactId>
+										<versionRange>
+											[2.6]
+										</versionRange>
+										<goals>
+											<goal>javacc</goal>
+										</goals>
+									</pluginExecutionFilter>
+									<action>
+										<ignore></ignore>
+									</action>
+								</pluginExecution>
+							</pluginExecutions>
+						</lifecycleMappingMetadata>
+					</configuration>
+				</plugin>
+			</plugins>
+		</pluginManagement>
+	</build>
+
+	<dependencies>
+		<dependency>
+			<groupId>edu.kit.ipd.jplag</groupId>
+			<artifactId>frontend-utils</artifactId>
+		</dependency>
+	</dependencies>
+	<parent>
+		<groupId>edu.kit.ipd.jplag.frontend</groupId>
+		<artifactId>frontend-parent</artifactId>
+		<version>0.3-SNAPSHOT</version>
+	</parent>
+</project>

+ 45 - 0
jplag.frontend.scheme/src/main/java/jplag/scheme/InsertStream.java

@@ -0,0 +1,45 @@
+package jplag.scheme;
+
+import java.io.*;
+
+public class InsertStream extends FileInputStream {
+  public InsertStream(File file) throws FileNotFoundException {
+    super(file);
+  }
+  public InsertStream(FileDescriptor fdObj) {
+    super(fdObj);
+  }
+  public InsertStream(String name) throws FileNotFoundException {
+    super(name);
+  }
+
+  boolean end_of_file = false;
+
+  public int read() throws IOException {
+    int result = super.read();
+    if (result != -1 || end_of_file) return result;
+    end_of_file = true;
+    return Character.LINE_SEPARATOR;
+  }
+
+  public int read(byte[] b) throws IOException {
+    int result = super.read(b);
+    if (result != -1 || end_of_file) return result;
+    end_of_file = true;
+    if (b.length > 1) {
+      b[0] = Character.LINE_SEPARATOR;
+      return 1;
+    }
+    return -1;
+  }
+
+  public int read(byte[] b, int off, int len) throws IOException {
+    int result = super.read(b,off,len);
+    if (result != -1 || end_of_file) return result;
+    end_of_file = true;
+    if (len > 0) {
+      b[off] = Character.LINE_SEPARATOR;
+      return 1;
+    } else return 0;
+  }
+}

+ 67 - 0
jplag.frontend.scheme/src/main/java/jplag/scheme/Language.java

@@ -0,0 +1,67 @@
+package jplag.scheme;
+
+import java.io.File;
+
+import jplag.ProgramI;
+
+public class Language implements jplag.Language {
+
+	public Language(ProgramI program) {
+		this.parser = new Parser();
+		this.parser.setProgram(program);
+
+	}
+
+	public int errorsCount() {
+		// TODO Auto-generated method stub
+		return this.parser.errorsCount();
+	}
+
+	private jplag.scheme.Parser parser;//noch nicht instanziert? siehe
+									   // Konstruktor
+
+	public String[] suffixes() {
+		String[] res = { ".scm", ".SCM" };
+		return res;
+	}
+
+	public String name() {
+		return "SchemeR4RS Parser [basic markup]";
+	}
+
+	public String getShortName() {
+		return "scheme";
+	}
+
+	public int min_token_match() {
+		return 13;
+	}
+
+	public boolean supportsColumns() {
+		return false;
+	}
+
+	public boolean isPreformated() {
+		return true;
+	}
+
+	public boolean usesIndex() {
+		return false;
+	}
+
+	public jplag.Structure parse(File dir, String[] files) {
+		return this.parser.parse(dir, files);
+	}
+
+	public boolean errors() {
+		return this.parser.getErrors();
+	}
+
+	public int noOfTokens() {
+		return jplag.scheme.SchemeToken.numberOfTokens();
+	}
+
+	public String type2string(int type) {
+		return jplag.scheme.SchemeToken.type2string(type);
+	}
+}

+ 39 - 0
jplag.frontend.scheme/src/main/java/jplag/scheme/Parser.java

@@ -0,0 +1,39 @@
+package jplag.scheme;
+
+import java.io.File;
+
+public class Parser extends jplag.Parser implements SchemeTokenConstants {
+	private String actFile;
+
+	private jplag.Structure struct;
+
+	public static void main(String args[]) {
+		Parser p = new Parser();
+		System.out.println(p.parse(new File("."), args).toString());
+	}
+
+	public jplag.Structure parse(File dir, String files[]) {
+		struct = new jplag.Structure();
+		errors = 0;
+		SchemeParser parser = null;// no worry it will be reinitialized
+		// in method parseFile(...)
+		for (int i = 0; i < files.length; i++) {
+			actFile = files[i];
+		    getProgram().print(null, "Parsing file " + files[i] + "\n");
+			if (!SchemeParser.parseFile(dir, files[i], parser, this))
+				errors++;
+			struct.addToken(new SchemeToken(FILE_END, actFile, 1));
+		}
+		this.parseEnd();
+		return struct;
+	}
+
+	public void add(int type, Token token) {
+		struct.addToken(new SchemeToken(type, actFile, token.beginLine));
+		/*
+		 * System.out.println(token.beginLine+"\t"+ (new
+		 * SchemeToken(0,null,0)).type2string(type)+"\t"+ token.image);
+		 */
+	}
+
+}

+ 71 - 0
jplag.frontend.scheme/src/main/java/jplag/scheme/SchemeToken.java

@@ -0,0 +1,71 @@
+package jplag.scheme;
+
+//import java.io.*;
+
+public class SchemeToken extends jplag.Token implements SchemeTokenConstants {
+  private int line;
+
+  public SchemeToken(int type, String file, int zeile) {
+    super(type,file,zeile);
+  }
+
+  public int getLine() { return line; }
+  public void setLine(int line) { this.line = line; }
+
+  public int getColumn() {return 0;}
+  public int getLength() {return 0;}
+
+  public static String type2string(int type) {
+    switch (type) {
+    case SchemeTokenConstants.FILE_END:
+                             return "********";
+    case S_BOOL:             return "BOOL    ";
+    case S_NUMBER:           return "NUMBER  ";
+    case S_CHAR:             return "CHAR    ";
+    case S_STRING:           return "STRING  ";
+    case S_ID:               return "ID      ";
+    case S_LIST_BEGIN:       return "(LIST   ";
+    case S_LIST_END:         return "LIST)   ";
+    case S_VECTOR_BEGIN:     return "(VECTOR ";
+    case S_VECTOR_END:       return "VECTOR) ";
+    case S_LITERAL:          return "LITERAL ";
+    case S_QUOT_BEGIN:       return "(QUOT   ";
+    case S_QUOT_END:         return "QUOT)   ";
+    case S_CALL:             return "CALL    ";
+    case S_LAMBDA_BEGIN:     return "(LAMBDA ";
+    case S_LAMBDA_END:       return "LAMBDA) ";
+    case S_FORMAL_BEGIN:     return "(FORMAL ";
+    case S_FORMAL_END:       return "FORMAL) ";
+    case S_BODY_BEGIN:       return "(BODY   ";
+    case S_BODY_END:         return "BODY)   ";
+    case S_IF_BEGIN:         return "(IF     ";
+    case S_IF_END:           return "IF)     ";
+    case S_ALTERN:           return "ALTERN  ";
+    case S_ASSIGN_BEGIN:     return "(ASSIGN ";
+    case S_ASSIGN_END:       return "ASSIGN) ";
+    case S_COND_BEGIN:       return "(COND   ";
+    case S_COND_END:         return "COND)   ";
+    case S_ELSE:             return "ELSE    ";
+    case S_CASE_BEGIN:       return "(CASE   ";
+    case S_CASE_END:         return "CASE)   ";
+    case S_DO_BEGIN:         return "(DO     ";
+    case S_DO_END:           return "DO)     ";
+    case S_COMMAND:          return "COMMAND ";
+    case S_DEF_BEGIN:        return "(DEF    ";
+    case S_DEF_END:          return "DEF)    ";
+    case S_BEGIN:            return "(       ";
+    case S_END:              return ")       ";
+    case S_AND:              return "AND     ";
+    case S_OR:               return "OR      ";
+    case S_LET:              return "LET     ";
+    case S_DELAY:            return "DELAY   ";
+    case S_VAR:              return "VAR     ";
+      
+    default:                 return "<?????> ";
+    }
+  }
+  
+  public static int numberOfTokens() { 
+    return NUM_DIFF_TOKENS;
+  } 
+}

+ 53 - 0
jplag.frontend.scheme/src/main/java/jplag/scheme/SchemeTokenConstants.java

@@ -0,0 +1,53 @@
+package jplag.scheme;
+
+public interface SchemeTokenConstants extends jplag.TokenConstants {
+  final static int FILE_END = 0;
+
+  // Used to optionally separate methods from each other
+  // with an always marked token
+  final static int SEPARATOR_TOKEN = 1;
+  
+  final static int S_BOOL = 2;
+  final static int S_NUMBER = 3;
+  final static int S_CHAR = 4;
+  final static int S_STRING = 5;
+  final static int S_ID = 6;
+  final static int S_LIST_BEGIN = 7;
+  final static int S_LIST_END = 8;
+  final static int S_VECTOR_BEGIN = 9;
+  final static int S_VECTOR_END = 10;
+  final static int S_LITERAL = 11;
+  final static int S_QUOT_BEGIN = 12;
+  final static int S_QUOT_END = 13;
+  final static int S_CALL = 14;
+  final static int S_LAMBDA_BEGIN = 15;
+  final static int S_LAMBDA_END = 16;
+  final static int S_FORMAL_BEGIN = 17;
+  final static int S_FORMAL_END = 18;
+  final static int S_BODY_BEGIN = 19;
+  final static int S_BODY_END = 20;
+  final static int S_IF_BEGIN = 21;
+  final static int S_IF_END = 22;
+  final static int S_ALTERN = 23;
+  final static int S_ASSIGN_BEGIN = 24;
+  final static int S_ASSIGN_END = 25;
+  final static int S_COND_BEGIN = 26;
+  final static int S_COND_END = 27;
+  final static int S_ELSE = 28;
+  final static int S_CASE_BEGIN = 29;
+  final static int S_CASE_END = 30;
+  final static int S_DO_BEGIN = 31;
+  final static int S_DO_END = 32;
+  final static int S_COMMAND = 33;
+  final static int S_DEF_BEGIN = 34;
+  final static int S_DEF_END = 35;
+  final static int S_BEGIN = 36;
+  final static int S_END = 37;
+  final static int S_AND = 38;
+  final static int S_OR = 39;
+  final static int S_LET = 40;
+  final static int S_DELAY = 41;
+  final static int S_VAR = 42;
+
+  final static int NUM_DIFF_TOKENS = 43;
+}

+ 280 - 0
jplag.frontend.scheme/src/main/java/jplag/scheme/test.scm

@@ -0,0 +1,280 @@
+(define reload (lambda () (load "hw1.scm")))        ; Handy reload command.
+
+; Question #1 - rotate-n
+
+;   Procedure: rotate
+;
+;          In: List 'l'.
+;
+;         Out: List 'l' after single rotation.
+;
+; Description: Helper function for rotate-n.
+;
+(define rotate (lambda (l)                          ; Helper function performs
+		 (if (null? l)                      ; single rotation.
+		     '()
+		     (append (cdr l) (list (car l))))))
+
+;   Procedure: rotate-n
+;
+;          In: List 'l', integer 'n'.
+;
+;         Out: List 'l', rotated 'n' times.
+;
+; Description: Rotates list 'l' 'n' times.  Uses helper function rotate.
+;
+(define rotate-n (lambda (l n)
+		   (if (zero? n)
+		       l
+		       (rotate-n (rotate l) (- n 1)))))
+
+; Question #2 - split
+
+;   Procedure: filter
+;
+;          In: Function 'f', List 'l'.
+;
+;         Out: List of all items in 'l' for which function 'f' returns #t.
+;
+; Description: Helper function for split.
+;
+(define filter (lambda (f l)                        ; Filter returns a list
+		 (if (null? l)                      ; of items in 'l' for which
+		     '()                            ; function 'f' is true.
+		     (if (f (car l))
+			 (cons (car l) (filter f (cdr l)))
+			 (filter f (cdr l))))))
+;   Procedure: split
+;
+;          In: List 'l' of integers.
+;
+;         Out: List containing three sublists -- integers <, ==, and > 0.
+;
+; Description: Calls helper function filter.
+;
+                                                    ; Split filters list 'l'
+                                                    ; into three lists, of
+(define split (lambda (l)                           ; items <, ==, and > 0.
+		     (cons (filter (lambda (x) (< x 0)) l)
+			   (cons (filter (lambda (x) (eq? x 0)) l)
+				 (list (filter (lambda (x) (> x 0)) l))))))
+
+; Question #3 - sum
+
+;   Procedure: sum-helper
+;
+;          In: List 'l' of integers, and integer 'n'.
+;
+;         Out: Sum of 'n' and all other integers in 'l'.
+;
+; Description: Helper function for sum.
+;
+(define sum-helper (lambda (l n)                    ; Sum-helper passes along
+		     (if (null? l)                  ; the running total 'n' of
+			 n                          ; every other element
+			 (if (null? (cdr l))        ; as it recurses down 'l'.
+			     n
+			     (sum-helper (cddr l) (+ n (cadr l)))))))
+;   Procedure: sum
+;
+;          In: List 'l' of integers.
+;
+;         Out: Sum of all integers in 'l'.
+;
+; Description: Calls helper function sum-helper.
+;
+(define sum (lambda (l) (sum-helper l 0)))          ; Sum calls sum-helper
+                                                    ; with an initial total 0.
+
+; Question #4 - majority?
+
+
+;   Procedure: major-help?
+;
+;          In: List 'l', and integer 'n'.
+;
+;         Out: #t if any item in 'l' occurs more than n/2 times.
+;
+; Description: Determines majority? with the help of length paramter 'n'.
+;
+(define major-help? (lambda (l n)                   ; Major-help? detemines how
+		      (if (null? l)                 ; many items in list 'l'
+			  #f                        ; match the first item.
+			  (let* (                   ; If number is strictly >
+				 (h (car l))        ; than n/2, it returns #t,
+				 (l2 (filter        ; else recurses down 'l'.
+				      (lambda (x) (eq? x h))
+				      l))
+				 (n2 (* 2 (length l2))))
+			    (if (> n2 n)
+				#t
+				(major-help? (cdr l) n))
+			    ))))
+
+;   Procedure: majority?
+;
+;          In: List 'l'.
+;
+;         Out: #t if any item in 'l' occurs more than half the time.
+;
+; Description: Calls helper function majority-help? with length of list 'l'.
+;
+(define majority? (lambda (l)                       ; Majority? calls major- 
+		    (major-help? l (length l))))    ; help with length of
+                                                    ; list 'l'.
+
+; Question #5 - car&cdr
+
+;   Procedure: member*?
+;
+;          In: Atom 'x' and complex list 'l'.
+;
+;         Out: #t if atom 'x' appears in any part of 'l'.
+;
+; Description: Guides car&cdr down correct portion of complex list.
+;
+(define member*? (lambda (x lst)
+		   (if (null? lst)
+		       #f
+		       (if (list? (car lst))
+			   (or (member*? x (car lst))
+			       (member*? x (cdr lst)))
+			   (if (eq? x (car lst))
+			       #t
+			       (member*? x (cdr lst)))))))
+
+;   Procedure: car&cdr2
+;
+;          In: Atom 's', Complex List 'slst', Function list 'f'.
+;
+;         Out: Chain of car's and cdr's to reach 's' in complex list 'slst'.
+;
+; Description: Car&cdr2 recursively calls itself as it moves into the
+;              complex list 'slst' toward atom 's'.  At each call, it adds
+;              another link in the chain of car's and cdr's by cons-ing
+;              them on to the list of functions, 'f'.
+;
+(define car&cdr2 
+  (lambda (s slst f) 
+    (if (null? slst)                   ; Slst should never be null,
+	f                              ; but just in case...
+	(if (eq? (car slst) s)         ; If we've found the atom at the
+	    (list 'car f)              ; head, return (car ...).
+	    (if (list? (car slst))     ; If the head is a sublist, check to
+		(if (member*? s (car slst))  ; see if the atom is in the
+		    (car&cdr2 s (car slst) (list 'car f)) ; sublist,
+		    (car&cdr2 s (cdr slst) (list 'cdr f))) ; otherwise,
+		(car&cdr2 s (cdr slst) (list 'cdr f)) ; proceed down the
+		)))))                  ; tail of the list.
+;
+;   Procedure: car&cdr
+;
+;          In: Atom 's', Complex List 'slst', Error value 'errvalue'.
+;
+;         Out: Chain of car's and cdr's to reach 's' in complex list 'slst',
+;              or 'errvalue' if 's' is not in 'slst'.
+;
+; Description: Car&cdr determines if 's' is in 'slst' using helper function
+;              member*?.  If it is not, it returns 'errvalue'.  
+;              If it is, then it calls helper function car&cdr2 to 
+;              assemble the chain of car's and cdr's to reach 's'.
+;
+(define car&cdr (lambda (s slst errvalue)
+		  (if (member*? s slst)
+		      (list 'lambda '(lst)
+			    (car&cdr2 s slst 'lst))
+		      errvalue)))
+
+; Question #6 - matrix-multiply
+;
+; Procedure: matrix-multiply
+;
+;        In: Two lists of four integers each.
+;
+;       Out: A list of four integers representing the 2x2 matrix result
+;            of matrix-multiplying the two input matrices.
+
+(define matrix-multiply (lambda (m1 m2)
+			  (let ((a11 (car m1))
+				(a12 (cadr m1))
+				(a21 (caddr m1))
+				(a22 (cadddr m1))
+				(b11 (car m2))
+				(b12 (cadr m2))
+				(b21 (caddr m2))
+				(b22 (cadddr m2)))
+			  (cons (+ (* a11 b11)
+				   (* a12 b21))
+				(cons (+ (* a11 b12)
+					 (* a12 b22))
+				      (cons (+ (* a21 b11)
+					       (* a22 b21))
+					    (list (+ (* a21 b12)
+						     (* a22 b22)
+						     )
+						  )
+					    )
+				      )
+				)
+			  )
+			  )
+  )
+
+; Question 7 - extensional-eq?
+;
+; Procedure: extensional-eq?
+;
+;        In: Two functions (each of which take a single integer argument),
+;            and an integer, 'n'.
+;
+;       Out: #t if the two function-arguments return the same value for all
+;            integers from 0-n.  #f if they do not.
+
+(define extensional-eq? (lambda (f1 f2 n)
+			  (if (< n 0)
+			      #t
+			      (if (not (eq? (f1 n) (f2 n)))
+				  #f
+				  (extensional-eq? f1 f2 (- n 1))
+				  )
+			      )
+			  )
+  )
+
+; Question #8 - acc
+;
+;   Procedure: acc-helper
+;
+;          In: List of integers 'lst', and initial sum 'n'.
+;
+;         Out: List of integers, where each integer is the integer from the
+;              input list added to the summation of all previous integers
+;              in the list.
+;
+; Description: Helper function for acc.
+
+(define acc-helper (lambda (lst n)
+		     (if (null? lst)
+			 '()
+			 (cons (+ n (car lst))
+			       (acc-helper (cdr lst)
+					   (+ (car lst) n)
+					   )
+			       )
+			 )
+		     )
+  )
+
+;   Procedure: acc
+;
+;          In: List of integers
+;
+;         Out: List of integers, where each integer is the integer from the
+;              input list added to the summation of all previous integers
+;              in the list.
+;
+; Description: Calls acc-helper with initial list, and initial sum 0.
+
+(define acc (lambda (lst) (acc-helper lst 0)))
+
+; ...

+ 734 - 0
jplag.frontend.scheme/src/main/javacc/Scheme.jj

@@ -0,0 +1,734 @@
+/* Hey Emacs, this is -*- mode: java; page-delimiter: "^%%$"; -*- */
+
+/*
+ * Scheme.jj
+ * =========
+ * This is a parser for scheme written for JavaCC.
+ * The grammar is far from optimal (it uses a lot of lookaheads),
+ * but that is because it follows almost exactly the grammar given
+ * in "Revised4 Report on the Algorithmic Language Scheme" (R4RS).
+ *
+ * SKIP has been used for whitespaces, leaving out the lexemes for
+ * <intertoken space> and similar.
+ *
+ * The lexemes <identifier> and <syntactic keyword> has been written
+ * as production rules to avoid conflicting lexemes.
+ *
+ * The <sequence> production rule has been slighly altered as
+ * a workaround for some problems I had with the original
+ * production rule. (Is this a bug in JavaCC?)
+ * In R4RS the rule is
+ *   <sequence> --> <command>* <expression>
+ * here it is
+ *   <sequence> --> <expression>+
+ *
+ * The non-context-free grammar for quasiquotations has been written
+ * with use of semantic lookahead to handle the <template 0> production
+ * rule.
+ *
+ * The special construct (...)? has been used in several locations
+ * to join two or more alternatives into one without altering
+ * the structure of the grammar.
+ *
+ * To anyone who reads or uses this code, note the following:
+ *  - The code is public domain.
+ *  - It has not been heavily tested and I leave no guarantees
+ *    whatsoever that it works properly.
+ *  - If you make any changes, or find it useful in any way, I
+ *    would appreaciate to hear from you.
+ *
+ * Author: Lorens Younes (d93-hyo@nada.kth.se)
+ */
+
+options
+{
+  IGNORE_CASE = true;
+  DEBUG_PARSER = false;
+  DEBUG_LOOKAHEAD = false;
+  DEBUG_TOKEN_MANAGER = false;
+}
+
+
+PARSER_BEGIN(SchemeParser)
+package jplag.scheme;
+import java.io.*;
+
+public class SchemeParser implements SchemeTokenConstants {
+    /* used for context in the template production rule */
+    private static int templateParam;
+    private jplag.scheme.Parser parser2 ;
+    //static SchemeParser parser = null;
+
+    public static boolean parseFile(File dir, String fileName,
+        SchemeParser parser,jplag.scheme.Parser parserX)
+    {
+        File file=null;
+        try {
+            FileInputStream in = new FileInputStream(file=new File(dir,fileName));
+            if (parser==null) {
+                parser = new SchemeParser(in);
+            } else {
+                parser.ReInit(in);
+            }
+            parser.parser2=parserX;
+        } catch (FileNotFoundException e) {
+            System.out.println("Scheme Parser R4RS:  File " + fileName +
+                " not found.");
+            return false;
+        }
+        try {
+            parser.Program();
+        } catch (ParseException e) {
+            parserX.getProgram().addError("  Parsing Error in '" + fileName
+                + "':\n  " + e.getMessage() + "\n");
+            return false;
+        } catch (TokenMgrError e) {
+            parserX.getProgram().addError("  Scanning Error in '" + fileName
+                + "':\n  " + e.getMessage() + "\n");
+            return false;
+        }
+        return true;
+    }
+
+/*    public static void main(String args[]) {
+        parseFile(new File("."),args[0]);
+    }*/
+}
+
+PARSER_END(SchemeParser)
+
+
+/*
+ * Lexical structure
+ */
+
+SKIP : /* whitespace */
+{
+  " "
+| "\t"
+| "\n"
+| "\r"
+| "\f"
+}
+
+SPECIAL_TOKEN : /* comment */
+{
+  <COMMENT : ";" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>
+}
+
+TOKEN : /* syntactic keywords */
+{
+  <ELSE : "else">
+| <ARROW : "=>">
+| <DEFINE : "define">
+| <UNQUOTE : "unquote">
+| <UNQUOTE_SPLICING : "unquote-splicing">
+| <QUOTE : "quote">
+| <LAMBDA : "lambda">
+| <IF : "if">
+| <SET : "set!">
+| <BEGIN : "begin">
+| <COND : "cond">
+| <AND : "and">
+| <OR : "or">
+| <CASE : "case">
+| <LET : "let">
+| <LETSTAR : "let*">
+| <LETREC : "letrec">
+| <DO : "do">
+| <DELAY : "delay">
+| <QUASIQUOTE : "quasiquote">
+}
+
+TOKEN : /* variable */
+{
+  <VARIABLE :
+    <INITIAL> (<SUBSEQUENT>)*
+  | <PECULIAR_IDENTIFIER>
+  >
+| <#INITIAL :
+    <LETTER>
+  | <SPECIAL_INITIAL>
+  >
+| <#LETTER :
+    ["a"-"z"]
+  >
+| <#SPECIAL_INITIAL :
+    ["!","$","%","&","*","/",":","<","=",">","?","~","_","^"]
+  >
+| <#SUBSEQUENT :
+    <INITIAL>
+  | <DIGIT>
+  | <SPECIAL_SUBSEQUENT>
+  >
+| <#DIGIT :
+    ["0"-"9"]
+  >
+| <#SPECIAL_SUBSEQUENT :
+    [".","+","-"]
+  >
+| <#PECULIAR_IDENTIFIER :
+    ["+","-"]
+  | "..."
+  >
+}
+
+TOKEN : /* boolean */
+{
+  <BOOLEAN :
+    "#" ["t","f"]
+  >
+}
+
+TOKEN : /* character */
+{
+  <CHARACTER :
+    "#\\" ~[" ","\n"]
+  | "#\\" <CHARACTER_NAME>
+  >
+| <#CHARACTER_NAME :
+    "space"
+  | "newline"
+  >
+}
+
+TOKEN : /* string */
+{
+  <STRING :
+    "\"" (<STRING_ELEMENT>)* "\""
+  >
+| <#STRING_ELEMENT :
+    ~["\"","\\"]
+     | "\\" ["\"","\\","n"]      // addition by guido ("n")
+  >
+}
+
+TOKEN : /* number */
+{
+  <NUMBER :
+    <NUM_2>
+  | <NUM_8>
+  | <NUM_10>
+  | <NUM_16>
+  >
+| <#NUM_2 :
+    <PREFIX_2> <COMPLEX_2>
+  >
+| <#NUM_8 :
+    <PREFIX_8> <COMPLEX_8>
+  >
+| <#NUM_10 :
+    <PREFIX_10> <COMPLEX_10>
+  >
+| <#NUM_16 :
+    <PREFIX_16> <COMPLEX_16>
+  >
+| <#COMPLEX_2 :
+    <REAL_2> ("@" <REAL_2>)?
+  | (<REAL_2>)? ("+" | "-") (<UREAL_2>)? "i"
+  >
+| <#COMPLEX_8 :
+    <REAL_8> ("@" <REAL_8>)?
+  | (<REAL_8>)? ("+" | "-") (<UREAL_8>)? "i"
+  >
+| <#COMPLEX_10 :
+    <REAL_10> ("@" <REAL_10>)?
+  | (<REAL_10>)? ("+" | "-") (<UREAL_10>)? "i"
+  >
+| <#COMPLEX_16 :
+    <REAL_16> ("@" <REAL_16>)?
+  | (<REAL_16>)? ("+" | "-") (<UREAL_16>)? "i"
+  >
+| <#REAL_2 :
+    <SIGN> <UREAL_2>
+  >
+| <#REAL_8 :
+    <SIGN> <UREAL_8>
+  >
+| <#REAL_10 :
+    <SIGN> <UREAL_10>
+  >
+| <#REAL_16 :
+    <SIGN> <UREAL_16>
+  >
+| <#UREAL_2 :
+    <UINTEGER_2> ("/" <UINTEGER_2>)?
+  >
+| <#UREAL_8 :
+    <UINTEGER_8> ("/" <UINTEGER_8>)?
+  >
+| <#UREAL_10 :
+    <UINTEGER_10> ("/" <UINTEGER_10>)?
+  | <DECIMAL_10>
+  >
+| <#UREAL_16 :
+    <UINTEGER_16> ("/" <UINTEGER_16>)?
+  >
+| <#DECIMAL_10 :
+    <UINTEGER_10> <SUFFIX>
+  | "." (<DIGIT_10>)+ ("#")* <SUFFIX>
+  | (<DIGIT_10>)+ "." (<DIGIT_10>)* ("#")* <SUFFIX>
+  | (<DIGIT_10>)+ ("#")+ "." ("#")* <SUFFIX>
+  >
+| <#UINTEGER_2 :
+    (<DIGIT_2>)+ ("#")*
+  >
+| <#UINTEGER_8 :
+    (<DIGIT_8>)+ ("#")*
+  >
+| <#UINTEGER_10 :
+    (<DIGIT_10>)+ ("#")*
+  >
+| <#UINTEGER_16 :
+    (<DIGIT_16>)+ ("#")*
+  >
+| <#PREFIX_2 :
+    <RADIX_2> <EXACTNESS>
+  | <EXACTNESS> <RADIX_2>
+  >
+| <#PREFIX_8 :
+    <RADIX_8> <EXACTNESS>
+  | <EXACTNESS> <RADIX_8>
+  >
+| <#PREFIX_10 :
+    <RADIX_10> <EXACTNESS>
+  | <EXACTNESS> <RADIX_10>
+  >
+| <#PREFIX_16 :
+    <RADIX_16> <EXACTNESS>
+  | <EXACTNESS> <RADIX_16>
+  >
+| <#SUFFIX :
+    (<EXPONENT_MARKER> <SIGN> (<DIGIT>)+)?
+  >
+| <#EXPONENT_MARKER :
+    ["e","s","f","d","l"]
+  >
+| <#SIGN :
+    ("+" | "-")?
+  >
+| <#EXACTNESS :
+    ("#" ["i","e"])?
+  >
+| <#RADIX_2 :
+    "#b"
+  >
+| <#RADIX_8 :
+    "#o"
+  >
+| <#RADIX_10 :
+    ("#d")?
+  >
+| <#RADIX_16 :
+    "#x"
+  >
+| <#DIGIT_2 :
+    ["0","1"]
+  >
+| <#DIGIT_8 :
+    ["0"-"7"]
+  >
+| <#DIGIT_10 :
+    ["0"-"9"]
+  >
+| <#DIGIT_16 :
+    <DIGIT_10>
+  | ["a","b","c","d","e","f"]
+  >
+}
+
+void Identifier() :
+{}
+{
+  SyntacticKeyword()
+| <VARIABLE>                        { parser2.add(S_VAR,token); }
+}
+
+void SyntacticKeyword() :
+{}
+{
+  <ELSE>
+| <ARROW>
+| <DEFINE>
+| <UNQUOTE>
+| <UNQUOTE_SPLICING>
+| <QUOTE>
+| <LAMBDA>
+| <IF>
+| <SET>
+| <BEGIN>
+| <COND>
+| <AND>
+| <OR>
+| <CASE>
+| <LET>
+| <LETSTAR>
+| <LETREC>
+| <DO>
+| <DELAY>
+| <QUASIQUOTE>
+}
+
+/*
+ * External representations
+ */
+
+void Datum() :
+{}
+{
+  SimpleDatum()
+| CompoundDatum()
+}
+
+void SimpleDatum() :
+{}
+{
+  <BOOLEAN>                       { parser2.add(S_BOOL,token); }
+| <NUMBER>                        { parser2.add(S_NUMBER,token); }
+| <CHARACTER>                     { parser2.add(S_CHAR,token); }
+| <STRING>                        { parser2.add(S_STRING,token); }
+| Symbol()
+}
+
+void Symbol() :
+{}
+{
+  Identifier()                    { parser2.add(S_ID,token); }
+}
+
+void CompoundDatum() :
+{}
+{
+  List()
+| Vector()
+}
+
+void List() :
+{}
+{
+                                  { parser2.add(S_LIST_BEGIN,token); }
+  (
+   "(" ((Datum())+ ("." Datum())? ")" | ")")
+   | Abbreviation()
+  )                               { parser2.add(S_LIST_END,token); }
+}
+
+void Abbreviation() :
+{}
+{
+  AbbrevPrefix() Datum()
+}
+
+void AbbrevPrefix() :
+{}
+{
+  "'"
+| "`"
+| ","
+| ",@"
+}
+
+void Vector() :
+{}
+{
+  "#("                            { parser2.add(S_VECTOR_BEGIN,token); }
+  (Datum())* ")"                  { parser2.add(S_VECTOR_END,token); }
+}
+
+/*
+ * Expressions
+ */
+
+void Expression() :
+{}
+{
+  <VARIABLE>
+| LOOKAHEAD(2) Literal()
+| LOOKAHEAD(2) ProcedureCall()
+| LOOKAHEAD(2) LambdaExpression()
+| LOOKAHEAD(2) Conditional()
+| LOOKAHEAD(2) Assignment()
+| DerivedExpression()
+}
+
+void Literal() :
+{}
+{
+  (Quotation() | SelfEvaluating())
+                                  { parser2.add(S_LITERAL,token); }
+}
+
+void SelfEvaluating() :
+{}
+{
+  <BOOLEAN>                       { parser2.add(S_BOOL,token); }
+| <NUMBER>                        { parser2.add(S_NUMBER,token); }
+| <CHARACTER>                     { parser2.add(S_CHAR,token); }
+| <STRING>                        { parser2.add(S_STRING,token); }
+}
+
+void Quotation() :
+{}
+{
+  ("'" { parser2.add(S_QUOT_BEGIN,token); } Datum()
+ | "(" { parser2.add(S_QUOT_BEGIN,token); } <QUOTE> Datum() ")")
+                                  { parser2.add(S_QUOT_END,token); }
+}
+
+void ProcedureCall() :
+{}
+{
+  "(" Operator() { parser2.add(S_CALL,token); } (Operand())* ")"
+}
+
+void Operator() :
+{}
+{
+  Expression()
+}
+
+void Operand() :
+{}
+{
+  Expression()
+}
+
+void LambdaExpression() :
+{}
+{
+  "("                             { parser2.add(S_LAMBDA_BEGIN,token); }
+    <LAMBDA> Formals() Body() ")" { parser2.add(S_LAMBDA_END,token); }
+}
+
+void Formals() :
+{}
+{
+  "("                             { parser2.add(S_FORMAL_BEGIN,token); }
+    ((<VARIABLE>)+ ("." <VARIABLE>)? ")" | ")")
+                                  { parser2.add(S_FORMAL_END,token); }
+| <VARIABLE>
+}
+
+void Body() :
+{}
+{
+  (LOOKAHEAD(4) Definition())*     { parser2.add(S_BODY_BEGIN,token); }
+  Sequence()                       { parser2.add(S_BODY_END,token); }
+}
+
+void Sequence() :
+{}
+{
+( LOOKAHEAD(2) Expression()           // addition by guido:
+| "(" { parser2.add(S_DEF_BEGIN,token); } <DEFINE> <VARIABLE>
+               Expression() ")"      // bis hierher
+)+
+}
+
+void Command() :
+{}
+{
+                                   { parser2.add(S_COMMAND,token); }
+  Expression()
+}
+
+void Conditional() :
+{}
+{
+  "("                              { parser2.add(S_IF_BEGIN,token); }
+    <IF> Test() Consequent() Alternate()
+  ")"                              { parser2.add(S_IF_END,token); }
+}
+
+void Test() :
+{}
+{
+  Expression()
+}
+
+void Consequent() :
+{}
+{
+  LOOKAHEAD(2) Expression()
+| "(" ")"           // addition by guido           
+}
+
+void Alternate() :
+{}
+{
+  ( { parser2.add(S_ALTERN,token); } Expression() )?
+}
+
+void Assignment() :
+{}
+{
+  "("                              { parser2.add(S_ASSIGN_BEGIN,token); }
+    <SET> <VARIABLE> Expression()
+  ")"                              { parser2.add(S_ASSIGN_END,token); }
+}
+
+void DerivedExpression() :
+{}
+{
+  LOOKAHEAD(2) "(" { parser2.add(S_COND_BEGIN,token); }
+    <COND> (LOOKAHEAD(2) CondClause())*
+    (LOOKAHEAD(2) CondClause() | "(" { parser2.add(S_ELSE,token); } <ELSE> Sequence() ")")? ")" { parser2.add(S_COND_END,token); }
+
+| LOOKAHEAD(2) "(" { parser2.add(S_CASE_BEGIN,token); } <CASE> Expression()
+    (LOOKAHEAD(2) CaseClause())*
+    (LOOKAHEAD(2) CaseClause() | "(" <ELSE> { parser2.add(S_ELSE,token); }
+     Sequence() ")")? ")" { parser2.add(S_CASE_END,token); }
+
+| LOOKAHEAD(2) "(" { parser2.add(S_AND,token); } <AND> (Test())* ")"
+| LOOKAHEAD(2) "(" { parser2.add(S_OR,token); } <OR> (Test())* ")"
+| LOOKAHEAD(2) "(" { parser2.add(S_LET,token); } <LET> (<VARIABLE>)?
+    "(" (BindingSpec())* ")" Body() ")"
+| LOOKAHEAD(2) "(" { parser2.add(S_LET,token); } <LETSTAR>
+    "(" (BindingSpec())* ")" Body() ")"
+| LOOKAHEAD(2) "(" { parser2.add(S_LET,token); } <LETREC>
+    "(" (BindingSpec())* ")" Body() ")"
+| LOOKAHEAD(2) "(" { parser2.add(S_BEGIN,token); } <BEGIN> Sequence() ")"
+                   { parser2.add(S_END,token); } 
+| LOOKAHEAD(2) "(" { parser2.add(S_DO_BEGIN,token); } <DO>
+    "(" (IterationSpec())* ")" "(" Test() Sequence() ")" (Command())* ")"
+                   { parser2.add(S_DO_END,token); }
+| LOOKAHEAD(2) "(" { parser2.add(S_DELAY,token); } <DELAY> Expression() ")"
+| Quasiquotation()
+}
+
+void CondClause() :
+{}
+{
+  "(" Test() (Sequence() | <ARROW> Recipient())? ")"
+}
+
+void Recipient() :
+{}
+{
+  Expression()
+}
+
+void CaseClause() :
+{}
+{
+  "(" "(" (Datum())* ")" Sequence() ")"
+}
+
+void BindingSpec() :
+{}
+{
+  "(" <VARIABLE> Expression() ")"
+}
+
+void IterationSpec() :
+{}
+{
+  "(" <VARIABLE> Init() (Step())? ")"
+}
+
+void Init() :
+{}
+{
+  Expression()
+}
+
+void Step() :
+{}
+{
+  Expression()
+}
+
+/*
+ * Quasiquotations
+ */
+
+void Quasiquotation() :
+{}
+{
+  QuasiquotationD(1)
+}
+
+void Template(int d) :
+{ templateParam = d; }
+{
+  LOOKAHEAD({ templateParam == 0 }) Expression()
+| SimpleDatum()
+| LOOKAHEAD(2) ListTemplate(d)
+| VectorTemplate(d)
+| Unquotation(d)
+}
+
+void QuasiquotationD(int d) :
+{}
+{
+  "`" Template(d)
+| "(" <QUASIQUOTE> Template(d) ")"
+}
+
+void ListTemplate(int d) :
+{}
+{
+  LOOKAHEAD(2) "(" ((TemplateOrSplice(d))+ ("." Template(d))? ")" | ")")
+| "'" Template(d)
+| QuasiquotationD(d + 1)
+}
+
+void VectorTemplate(int d) :
+{}
+{
+  "#(" (TemplateOrSplice(d))* ")"
+}
+
+void Unquotation(int d) :
+{}
+{
+  "," Template(d - 1)
+| "(" <UNQUOTE> Template(d - 1) ")"
+}
+
+void TemplateOrSplice(int d) :
+{}
+{
+  LOOKAHEAD(2) Template(d)
+| SplicingUnquotation(d)
+}
+
+void SplicingUnquotation(int d) :
+{}
+{
+  ",@" Template(d - 1)
+| "(" <UNQUOTE_SPLICING> Template(d - 1) ")"
+}
+
+/*
+ * Programs and definitions
+ */
+
+void Program() :
+{}
+{
+  (CommandOrDefinition())* <EOF>
+}
+
+void CommandOrDefinition() :
+{}
+{
+  LOOKAHEAD(3) Command() | Definition()
+}
+
+void Definition() :
+{}
+{
+( LOOKAHEAD(3) "(" { parser2.add(S_DEF_BEGIN,token); } <DEFINE> <VARIABLE>
+               Expression() ")"
+| LOOKAHEAD(2) "(" { parser2.add(S_DEF_BEGIN,token); } <DEFINE>
+               "(" <VARIABLE> DefFormals() ")" Body() ")"
+| "(" { parser2.add(S_DEF_BEGIN,token); } <BEGIN> (Definition())* ")"
+) { parser2.add(S_DEF_END,token); }
+}
+
+void DefFormals() :
+{}
+{
+  ((<VARIABLE>)+ ("." <VARIABLE>)?)?
+}