Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need a lexical scanner for this \"snowflake\" language in java Snowflake is a

ID: 3679374 • Letter: I

Question

I need a lexical scanner for this "snowflake" language in java

Snowflake is a subset of the Snobol 4 programming language. Snowflake is a string manipulation language. The only data type is a string. There are no numbers. Snowflake is ine oriented". A complete Snowflake statement must fit on a line and only one Snowflake statement can be on a line. Therefore it is important that you recognize the end of line characters. Snowflake is case insensitive so variables can be either upper or lower case and still be considered the same. String constants inside single quotes can be either upper or lower case and must remain the case entered. The default operator in Snowflake is concatenation. If you write two variables or string constants constants are surrounded by single quotes') with one or more spaces between them, the strings will be concatenated. Concatenation is the only operation allowed on the right side of the equals sign. Pattern matching is the major operation in Snowflake. A pattern can be specified after the variable on the left. cat (cow I tree") dog rat The variable cat will be searched to see if the value of the variable cow or the string "tree" can be found. The first value that matches is used. The value of the variable dog must be found immediately after the match of the first part. If there is an expression to the right of the equals sign, then this value will be substituted for the characters that matched the pattern. A pattern or any part of a pattern may be followed by a dollar sign and a variable. If the pattern matches, the matching string will be stored in the specified variable The dog bit the man cat cat ("rat" I "dog") mouse In the above program, the variable mouse will receive the string "dog" as that portion ofthe pattern matches There are two special variables, input and output. If you reference input in a program, it will read a line from the keyboard. For example cat nput ABC The variable cat will have the value of what was typed on the keyboard concatenated with ABC. When a value is assigned to the variable output, the string will be written to the screen. The variable output cannot appear on the right side ofan equals sign and the variable input cannot appear on the left. Comments in Snowflake have the same format as Java and C++ comments, both */and Difference between Snowflake and Snobol Snowflake pattern matching does not do any back tracking. Alternate patterns are matched in the order they are given. The only functions supported for patterns are span and break. There are no numbers or tables

Explanation / Answer

import java.io.*;
import java.util.Vector;
import java.util.regex.*;

public class StringLexer
{   
public static class StringLocation
{
public final int line;
public final int col;
  
public StringLocation(int line, int col)
{
this.line=line;
this.col=col;
}
  
public String toString()
{
return line+" : "+col;
}
}

public static class StringToken
{
public StringToken(String img, int id, StringLocation begin, StringLocation end)
{
this.id=id;
this.img=img;
this.begin=begin;
this.end=end;
}
  
public final int id;
public final String img;
public final StringLocation begin;
public final StringLocation end;
  
public String toString()
{
return "['"+img+"' id="+id+" "+begin+".."+end+"]";
}
}

private CharSequence inp;
private int loc = 0;
private final int[] lineBeginOffset;


private StringLocation locOf(int stringOffset)
{
for(int ln = 0; ln < lineBeginOffset.length; ++ln)
{
int current = lineBeginOffset[ln];
if(current == stringOffset)
return new StringLocation(ln, stringOffset - current);

if(current > stringOffset)
{
int col0 = lineBeginOffset[ln-1];
return new StringLocation(ln-1, stringOffset - col0);
}
}
return null;
}

public StringLexer(String str)
{
inp = str;
  
Vector<Integer> integers = new Vector<Integer>();
integers.add(0);
  
CharSequence cs = inp;
for(int stringOffset = 0; stringOffset < cs.length(); ++stringOffset)
{
char c = cs.charAt(stringOffset);
if(c != ' ')
continue;

integers.add(stringOffset+1);
}
  
integers.add(cs.length());
  
this.lineBeginOffset = new int[integers.size()];
int ln = -1;
for(int current : integers)
{
++ln;
lineBeginOffset[ln] = current;
}
}

public StringLexer(InputStream inpStream) throws IOException
{
this(new InputStreamReader(inpStream));
}

private static String makeString(Reader r) throws IOException
{
StringBuilder sb = new StringBuilder();
  
while(true)
{
int n = r.read();
if(n<0)
break;

char c = (char) n;
sb.append(c);
}
  
return sb.toString();
}

public StringLexer(Reader r) throws IOException
{
this(makeString(r));
}
  
public StringToken nextString(Pattern patt)
{
Matcher mat = patt.matcher(inp);
boolean b = mat.find();
if(!b)
return null;
  
MatchResult matRes = mat.toMatchResult();
if(mat.start() != 0)
return null;
  
String str = inp.subSequence(matRes.start(), matRes.end()).toString();
StringToken result = new StringToken(str, 0, locOf(loc + matRes.start()),
locOf(loc + matRes.end()));
  
inp = inp.subSequence(matRes.end(), inp.length());
loc += matRes.end();
  
return result;
}

public StringToken nextString(String regexp)
{
return nextString(Pattern.compile(regexp));
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote