Commit 9bb2f28e authored by Johannes Roith's avatar Johannes Roith

Initial import of CupParser.

parent f65088d2
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="lib"/>
<classpathentry kind="lib" path="lib/java-cup-11b-runtime.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CupParser</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
<dictionary>
<key>LaunchConfigHandle</key>
<value>&lt;project&gt;/.externalToolBuilders/CupAntBasedBuilder.launch</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
<project name="Compiler" default ="compile" basedir=".">
<property name="cup" location="src/cup" />
<property name="jflex" location="src/jflex" />
<property name="java" location="src" />
<property name="classes" location="bin/cls" />
<property name="result" location="bin/jar" />
<property name="lib" location="lib" />
<property name="tools" location="tools" />
<taskdef name="jflex" classname="JFlex.anttask.JFlexTask" classpath="${tools}/JFlex.jar" />
<taskdef name="cup" classname="java_cup.anttask.CUPTask" classpath="${tools}/java-cup-11b.jar" />
<target name="generate">
<jflex file="${jflex}/Lexer.jflex" destdir="${java}" />
<cup srcfile="${cup}/Parser.cup" destdir="${java}"
parser="Parser" interface="true" locations="false" />
</target>
<path id="libraries"> <files includes="${lib}/java-cup-11b-runtime.jar" /> </path>
<target name="compile" depends="generate">
<mkdir dir="${classes}"/>
<mkdir dir="${result}"/>
<javac srcdir="${java}" destdir="${classes}">
<classpath refid="libraries" />
</javac>
<jar destfile="${result}/CupParser.jar" basedir="${classes}">
<manifest>
</manifest>
</jar>
</target>
<target name="clean">
<delete file="${java}/Parser.java" />
<delete file="${java}/sym.java" />
<delete file="${java}/Scanner.java" />
<delete dir="${classes}" />
<delete dir="${result}" />
</target>
</project>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
package java_cup;
import java_cup.runtime.*;
public final class Utility {
// TODO: refactor this.
private static int error_count;
public static int getErrorCount() {
return error_count;
}
public static void Warning(String msg, Symbol sym) {
Warning("HELLO WARNING: " + "," + msg);
}
public static void Warning(String msg) {
System.out.println("HELLO WARNING: " + "," + msg);
}
public static void Fatal(String msg, Symbol sym) {
Fatal("HELLO FATAL: " + "," + msg);
}
public static void Fatal(String msg) {
System.out.println("HELLO FATAL: " + "," + msg);
}
public static void Error(String msg, Symbol sym) {
Error (msg + "," + sym);
}
public static void Error(String msg) {
System.out.println("HELLO ERROR: " + msg);
error_count++;
}
}
package java_cup;
/**
* This class represents a part of a production which contains an
* action. These are eventually eliminated from productions and converted
* to trailing actions by factoring out with a production that derives the
* empty string (and ends with this action).
*
* @see java_cup.production
* @version last update: 11/25/95
* @author Scott Hudson
*/
public class action_part extends production_part {
/*-----------------------------------------------------------*/
/*--- Constructors ------------------------------------------*/
/*-----------------------------------------------------------*/
/** Simple constructor.
* @param code_str string containing the actual user code.
*/
public action_part(String code_str)
{
super(/* never have a label on code */null);
_code_string = code_str;
}
/*-----------------------------------------------------------*/
/*--- (Access to) Instance Variables ------------------------*/
/*-----------------------------------------------------------*/
/** String containing code for the action in question. */
protected String _code_string;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** String containing code for the action in question. */
public String code_string() {return _code_string;}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Set the code string. */
public void set_code_string(String new_str) {_code_string = new_str;}
/*-----------------------------------------------------------*/
/*--- General Methods ---------------------------------------*/
/*-----------------------------------------------------------*/
/** Override to report this object as an action. */
public boolean is_action() { return true; }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Equality comparison for properly typed object. */
public boolean equals(action_part other)
{
/* compare the strings */
return other != null && super.equals(other) &&
other.code_string().equals(code_string());
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Generic equality comparison. */
public boolean equals(Object other)
{
if (!(other instanceof action_part))
return false;
else
return equals((action_part)other);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Produce a hash code. */
public int hashCode()
{
return super.hashCode() ^
(code_string()==null ? 0 : code_string().hashCode());
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Convert to a string. */
public String toString()
{
return super.toString() + "{" + code_string() + "}";
}
/*-----------------------------------------------------------*/
}
package java_cup;
/** A specialized version of a production used when we split an existing
* production in order to remove an embedded action. Here we keep a bit
* of extra bookkeeping so that we know where we came from.
* @version last updated: 11/25/95
* @author Scott Hudson
*/
public class action_production extends production {
/** Constructor.
* @param base the production we are being factored out of.
* @param lhs_sym the LHS symbol for this production.
* @param rhs_parts array of production parts for the RHS.
* @param rhs_len how much of the rhs_parts array is valid.
* @param action_str the trailing reduce action for this production.
* @param indexOfIntermediateResult the index of the result of the previous intermediate action on the stack relative to top, -1 if no previous action
*/
public action_production(
production base,
non_terminal lhs_sym,
production_part rhs_parts[],
int rhs_len,
String action_str,
int indexOfIntermediateResult)
throws internal_error
{
super(lhs_sym, rhs_parts, rhs_len, action_str);
_base_production = base;
this.indexOfIntermediateResult = indexOfIntermediateResult;
}
private int indexOfIntermediateResult;
/**
* @return the index of the result of the previous intermediate action on the stack relative to top, -1 if no previous action
*/
public int getIndexOfIntermediateResult(){
return indexOfIntermediateResult;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** The production we were taken out of. */
protected production _base_production;
/** The production we were taken out of. */
public production base_production() {return _base_production;}
}
package java_cup;
/* Defines integers that represent the associativity of terminals
* @version last updated: 7/3/96
* @author Frank Flannery
*/
public class assoc {
/* various associativities, no_prec being the default value */
public final static int left = 0;
public final static int right = 1;
public final static int nonassoc = 2;
public final static int no_prec = -1;
}
\ No newline at end of file
package java_cup;
import java.util.Stack;
public final class emit {
public static int num_conflicts;
public static Boolean nowarn = false;
public static Boolean _xmlactions = true;
public static String package_name;
public static String action_code;
public static String parser_code;
public static String init_code;
public static String scan_code;
public static String parser_class_name;
public static String symbol_const_class_name;
public static Stack<String> import_list = new Stack<String>();
public static production start_production;
public static Boolean lr_values() {
// TODO?!
return false;
}
public static Boolean locations() {
// TODO?!
return false;
}
public static String pre(String str) {
return str;
}
}
package java_cup;
import java.io.FileInputStream;
import java_cup.runtime.*;
class hello
{
public static void main(String[] blah) throws Exception {
Parser p = new Parser();
FileInputStream is = new FileInputStream("/Users/jroith/testcup/test.cup");
p.setInputStream(is);
p.parse();
for (String item : emit.import_list)
System.out.println("import: " + item);
System.out.println("parser code: " + emit.parser_code);
System.out.println("init code: " + emit.init_code);
System.out.println("scan code: " + emit.scan_code);
// terminal
// non terminal
// precedence left
production prod = emit.start_production;
System.out.println(prod);
System.out.println("done.");
}
}
package java_cup;
/** Exception subclass for reporting internal errors in JavaCup. */
public class internal_error extends Exception
{
/** Constructor with a message */
public internal_error(String msg)
{
super(msg);
}
/** Method called to do a forced error exit on an internal error
for cases when we can't actually throw the exception. */
public void crash()
{
Utility.Fatal("JavaCUP Internal Error Detected: "+getMessage());
printStackTrace();
System.exit(-1);
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
package java_cup;
/** This class represents a transition in an LALR viable prefix recognition
* machine. Transitions can be under terminals for non-terminals. They are
* internally linked together into singly linked lists containing all the
* transitions out of a single state via the _next field.
*
* @see java_cup.lalr_state
* @version last updated: 11/25/95
* @author Scott Hudson
*
*/
public class lalr_transition {
/*-----------------------------------------------------------*/
/*--- Constructor(s) ----------------------------------------*/
/*-----------------------------------------------------------*/
/** Full constructor.
* @param on_sym symbol we are transitioning on.
* @param to_st state we transition to.
* @param nxt next transition in linked list.
*/
public lalr_transition(symbol on_sym, lalr_state to_st, lalr_transition nxt)
throws internal_error
{
/* sanity checks */
if (on_sym == null)
throw new internal_error("Attempt to create transition on null symbol");
if (to_st == null)
throw new internal_error("Attempt to create transition to null state");
/* initialize */
_on_symbol = on_sym;
_to_state = to_st;
_next = nxt;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Constructor with null next.
* @param on_sym symbol we are transitioning on.
* @param to_st state we transition to.
*/
public lalr_transition(symbol on_sym, lalr_state to_st) throws internal_error
{
this(on_sym, to_st, null);
}
/*-----------------------------------------------------------*/
/*--- (Access to) Instance Variables ------------------------*/
/*-----------------------------------------------------------*/
/** The symbol we make the transition on. */
protected symbol _on_symbol;
/** The symbol we make the transition on. */
public symbol on_symbol() {return _on_symbol;}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** The state we transition to. */
protected lalr_state _to_state;
/** The state we transition to. */
public lalr_state to_state() {return _to_state;}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Next transition in linked list of transitions out of a state */
protected lalr_transition _next;
/** Next transition in linked list of transitions out of a state */
public lalr_transition next() {return _next;}
/*-----------------------------------------------------------*/
/*--- General Methods ---------------------------------------*/
/*-----------------------------------------------------------*/
/** Convert to a string. */
public String toString()
{
String result;
result = "transition on " + on_symbol().name() + " to state [";
result += _to_state.index();
result += "]";
return result;
}
/*-----------------------------------------------------------*/
}
This diff is collapsed.
This diff is collapsed.
package java_cup;
/** This class represents a shift/reduce nonassociative error within the
* parse table. If action_table element is assign to type
* nonassoc_action, it cannot be changed, and signifies that there
* is a conflict between shifting and reducing a production and a
* terminal that shouldn't be next to each other.
*
* @version last updated: 7/2/96
* @author Frank Flannery
*/
public class nonassoc_action extends parse_action {
/*-----------------------------------------------------------*/
/*--- Constructor(s) ----------------------------------------*/
/*-----------------------------------------------------------*/
/** Simple constructor.
*/
public nonassoc_action() throws internal_error
{
/* don't need to set anything, since it signifies error */
}
/*-----------------------------------------------------------*/
/*--- General Methods ---------------------------------------*/
/*-----------------------------------------------------------*/
/** Quick access to type of action. */
public int kind() {return NONASSOC;}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Equality test. */
public boolean equals(parse_action other)
{
return other != null && other.kind() == NONASSOC;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Generic equality test. */
public boolean equals(Object other)
{
if (other instanceof parse_action)
return equals((parse_action)other);
else
return false;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Compute a hash code. */
public int hashCode()
{
/* all objects of this class hash together */
return 0xCafe321;
}
/** Convert to string. */
public String toString()
{
return "NONASSOC";
}
/*-----------------------------------------------------------*/
}
package java_cup;
/** This class serves as the base class for entries in a parse action table.
* Full entries will either be SHIFT(state_num), REDUCE(production), NONASSOC,
* or ERROR. Objects of this base class will default to ERROR, while
* the other three types will be represented by subclasses.
*
* @see java_cup.reduce_action
* @see java_cup.shift_action
* @version last updated: 7/2/96
* @author Frank Flannery
*/
public class parse_action {
/*-----------------------------------------------------------*/
/*--- Constructor(s) ----------------------------------------*/
/*-----------------------------------------------------------*/
/** Simple constructor. */
public parse_action()
{
/* nothing to do in the base class */
}
/*-----------------------------------------------------------*/
/*--- (Access to) Static (Class) Variables ------------------*/
/*-----------------------------------------------------------*/
/** Constant for action type -- error action. */
public static final int ERROR = 0;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Constant for action type -- shift action. */
public static final int SHIFT = 1;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Constants for action type -- reduce action. */
public static final int REDUCE = 2;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Constants for action type -- reduce action. */
public static final int NONASSOC = 3;
/*-----------------------------------------------------------*/
/*--- General Methods ---------------------------------------*/
/*-----------------------------------------------------------*/
/** Quick access to the type -- base class defaults to error. */
public int kind() {return ERROR;}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Equality test. */
public boolean equals(parse_action other)
{
/* we match all error actions */
return other != null && other.kind() == ERROR;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Generic equality test. */
public boolean equals(Object other)
{
if (other instanceof parse_action)
return equals((parse_action)other);
else
return false;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Compute a hash code. */
public int hashCode()
{
/* all objects of this class hash together */
return 0xCafe123;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Convert to string. */
public String toString() {return "ERROR";}
/*-----------------------------------------------------------*/
}
This diff is collapsed.
This diff is collapsed.
package java_cup;
/** This class represents one row (corresponding to one machine state) of the
* reduce-goto parse table.
*/
public class parse_reduce_row {
/*-----------------------------------------------------------*/
/*--- Constructor(s) ----------------------------------------*/
/*-----------------------------------------------------------*/
/** Simple constructor. Note: this should not be used until the number
* of terminals in the grammar has been established.
*/
public parse_reduce_row()
{
/* make sure the size is set */
if (_size <= 0 ) _size = non_terminal.number();
/* allocate the array */
under_non_term = new lalr_state[size()];
}
/*-----------------------------------------------------------*/
/*--- (Access to) Static (Class) Variables ------------------*/
/*-----------------------------------------------------------*/
/** Number of columns (non terminals) in every row. */
protected static int _size = 0;
/** Number of columns (non terminals) in every row. */
public static int size() {return _size;}
//Hm Added clear to clear all static fields
public static void clear() {
_size = 0;
}
/*-----------------------------------------------------------*/
/*--- (Access to) Instance Variables ------------------------*/
/*-----------------------------------------------------------*/
/** Actual entries for the row. */
public lalr_state under_non_term[];
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.