From 19d2651ab7618afab39811907fd5b0ae0e36ad41 Mon Sep 17 00:00:00 2001 From: Kleidi Bujari Date: Fri, 4 Oct 2024 17:40:22 -0400 Subject: lab 1 --- F2024/cps710/assignments/A1/HL.jj | 83 +++++++++++++++++ F2024/cps710/assignments/A1/IdBoolToken.java | 18 ++++ F2024/cps710/assignments/A1/IdNumToken.java | 18 ++++ F2024/cps710/assignments/A1/IdSetToken.java | 18 ++++ F2024/cps710/assignments/A1/NumberToken.java | 18 ++++ F2024/cps710/assignments/A1/StringToken.java | 23 +++++ F2024/cps710/assignments/A1/Token.java | 133 +++++++++++++++++++++++++++ 7 files changed, 311 insertions(+) create mode 100644 F2024/cps710/assignments/A1/HL.jj create mode 100644 F2024/cps710/assignments/A1/IdBoolToken.java create mode 100644 F2024/cps710/assignments/A1/IdNumToken.java create mode 100644 F2024/cps710/assignments/A1/IdSetToken.java create mode 100644 F2024/cps710/assignments/A1/NumberToken.java create mode 100644 F2024/cps710/assignments/A1/StringToken.java create mode 100644 F2024/cps710/assignments/A1/Token.java (limited to 'F2024/cps710/assignments/A1') diff --git a/F2024/cps710/assignments/A1/HL.jj b/F2024/cps710/assignments/A1/HL.jj new file mode 100644 index 0000000..54657cc --- /dev/null +++ b/F2024/cps710/assignments/A1/HL.jj @@ -0,0 +1,83 @@ +options { + IGNORE_CASE=false; +} + +PARSER_BEGIN(HL) + + public class HL { } + +PARSER_END(HL) + + TOKEN_MGR_DECLS : +{ + static int depth; +} + +SKIP : +{ + " " + | "\t" + | "\n" + | "\r" + | <"//" (~["\n","\r"])* ("\n" | "\r" | "\r\n")> + | "/'" { depth = 1; SwitchTo(COMMENT_BLOCK); } +} + + SKIP: +{ + < "/'" > { depth++; } + | < "'/" > { if (--depth == 0) SwitchTo(DEFAULT); } + | < ~[] > +} + +TOKEN : +{ + + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | "> + | ="> + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | + | +} + diff --git a/F2024/cps710/assignments/A1/IdBoolToken.java b/F2024/cps710/assignments/A1/IdBoolToken.java new file mode 100644 index 0000000..eadfdfe --- /dev/null +++ b/F2024/cps710/assignments/A1/IdBoolToken.java @@ -0,0 +1,18 @@ +class IdBoolToken extends Token { + public String value; + + public IdBoolToken(int kind, String image) { + super(kind, image); + this.value = image; + } + + @Override + public Object getValue() { + return value; + } + + @Override + public String toString() { + return value; + } +} diff --git a/F2024/cps710/assignments/A1/IdNumToken.java b/F2024/cps710/assignments/A1/IdNumToken.java new file mode 100644 index 0000000..602dac2 --- /dev/null +++ b/F2024/cps710/assignments/A1/IdNumToken.java @@ -0,0 +1,18 @@ +class IdNumToken extends Token { + public String value; + + public IdNumToken(int kind, String image) { + super(kind, image); + this.value = image; + } + + @Override + public Object getValue() { + return value; + } + + @Override + public String toString() { + return value; + } +} diff --git a/F2024/cps710/assignments/A1/IdSetToken.java b/F2024/cps710/assignments/A1/IdSetToken.java new file mode 100644 index 0000000..7d4e93c --- /dev/null +++ b/F2024/cps710/assignments/A1/IdSetToken.java @@ -0,0 +1,18 @@ +class IdSetToken extends Token { + public String value; + + public IdSetToken(int kind, String image) { + super(kind, image); + this.value = image; + } + + @Override + public Object getValue() { + return value; + } + + @Override + public String toString() { + return value; + } +} diff --git a/F2024/cps710/assignments/A1/NumberToken.java b/F2024/cps710/assignments/A1/NumberToken.java new file mode 100644 index 0000000..0ec1391 --- /dev/null +++ b/F2024/cps710/assignments/A1/NumberToken.java @@ -0,0 +1,18 @@ +class NumberToken extends Token { + public Integer value; + + public NumberToken(int kind, String image) { + super(kind, image); + this.value = Integer.parseInt(image); + } + + @Override + public Object getValue() { + return value; + } + + @Override + public String toString() { + return value.toString(); + } +} diff --git a/F2024/cps710/assignments/A1/StringToken.java b/F2024/cps710/assignments/A1/StringToken.java new file mode 100644 index 0000000..26580f6 --- /dev/null +++ b/F2024/cps710/assignments/A1/StringToken.java @@ -0,0 +1,23 @@ +class StringToken extends Token { + public String value; + + public StringToken(int kind, String image) { + super(kind, image); + this.value = image + .substring(1, image.length() - 1) + .replace("\\n", "\n") + .replace("\\t", "\t") + .replace("\\\"", "\"") + .replace("\\\\", "\\"); + } + + @Override + public Object getValue() { + return value; + } + + @Override + public String toString() { + return value; + } +} diff --git a/F2024/cps710/assignments/A1/Token.java b/F2024/cps710/assignments/A1/Token.java new file mode 100644 index 0000000..d271170 --- /dev/null +++ b/F2024/cps710/assignments/A1/Token.java @@ -0,0 +1,133 @@ +/* Generated By:JavaCC: Do not edit this line. Token.java Version 7.0 */ +/* JavaCCOptions:TOKEN_EXTENDS=,KEEP_LINE_COLUMN=true,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */ +/** + * Describes the input token stream. + */ + +public class Token implements java.io.Serializable { + + /** + * The version identifier for this Serializable class. + * Increment only if the serialized form of the + * class changes. + */ + private static final long serialVersionUID = 1L; + + /** + * An integer that describes the kind of this token. This numbering + * system is determined by JavaCCParser, and a table of these numbers is + * stored in the file ...Constants.java. + */ + public int kind; + + /** The line number of the first character of this Token. */ + public int beginLine; + /** The column number of the first character of this Token. */ + public int beginColumn; + /** The line number of the last character of this Token. */ + public int endLine; + /** The column number of the last character of this Token. */ + public int endColumn; + + /** + * The string image of the token. + */ + public String image; + + /** + * A reference to the next regular (non-special) token from the input + * stream. If this is the last token from the input stream, or if the + * token manager has not read tokens beyond this one, this field is + * set to null. This is true only if this token is also a regular + * token. Otherwise, see below for a description of the contents of + * this field. + */ + public Token next; + + /** + * This field is used to access special tokens that occur prior to this + * token, but after the immediately preceding regular (non-special) token. + * If there are no such special tokens, this field is set to null. + * When there are more than one such special token, this field refers + * to the last of these special tokens, which in turn refers to the next + * previous special token through its specialToken field, and so on + * until the first special token (whose specialToken field is null). + * The next fields of special tokens refer to other special tokens that + * immediately follow it (without an intervening regular token). If there + * is no such token, this field is null. + */ + public Token specialToken; + + /** + * An optional attribute value of the Token. + * Tokens which are not used as syntactic sugar will often contain + * meaningful values that will be used later on by the compiler or + * interpreter. This attribute value is often different from the image. + * Any subclass of Token that actually wants to return a non-null value can + * override this method as appropriate. + */ + public Object getValue() { + return null; + } + + /** + * No-argument constructor + */ + public Token() {} + + /** + * Constructs a new token for the specified Image. + */ + public Token(int kind) + { + this(kind, null); + } + + /** + * Constructs a new token for the specified Image and Kind. + */ + public Token(int kind, String image) + { + this.kind = kind; + this.image = image; + } + + /** + * Returns the image. + */ + @Override + public String toString() + { + return image; + } + + /** + * Returns a new Token object, by default. However, if you want, you + * can create and return subclass objects based on the value of ofKind. + * Simply add the cases to the switch for all those special cases. + * For example, if you have a subclass of Token called IDToken that + * you want to create if ofKind is ID, simply add something like : + * + * case MyParserConstants.ID : return new IDToken(ofKind, image); + * + * to the following switch statement. Then you can cast matchedToken + * variable to the appropriate type and use sit in your lexical actions. + */ + public static Token newToken(int ofKind, String image) + { + switch(ofKind) { + case HLConstants.NUMBER: return new NumberToken(ofKind, image); + case HLConstants.IDNUM: return new IdNumToken(ofKind, image); + case HLConstants.IDSET: return new IdSetToken(ofKind, image); + case HLConstants.IDBOOL: return new IdBoolToken(ofKind, image); + case HLConstants.STRING: return new StringToken(ofKind, image); + default : return new Token(ofKind, image); + } + } + + public static Token newToken(int ofKind) + { + return newToken(ofKind, null); + } + +} -- cgit 1.4.1