Reinforce lexical analysis concepts
Description
Unformatted Attachment Preview
/**
* @author dgayler
*
*/
public class LexicalAnalyzer
{
private List code;
private int rowNum;
private int index;
private String current;
private boolean sentEOF;
private boolean sentLastEOLN;
/************************************************
* if n is a character in the string then these
* boolean flags are not needed.
*/
/**
* @param code – list of lines of code
* @throws LexicalError if there is an error in
* analyzing the code
*/
public LexicalAnalyzer(List code) throws LexicalError
{
this.code = code;
this.rowNum = 1;
this.current = code.get(0);
this.index = 0;
this.sentEOF = false;
this.sentLastEOLN = false;
}
/**
* skips white space leaving current character at next
* non-white space character
* @throws LexicalError
*/
private void skipWhiteSpace() throws LexicalError
{
while (index < current.length() &&
Character.isWhitespace(current.charAt(index)))
index++;
}
/**
* @return next token
* @throws LexicalError if error occurs generating next token
*/
public Token getToken() throws LexicalError
{
Token tok = null;
if (sentEOF)
throw new LexicalError (“attempt to read past end of program”);
skipWhiteSpace();
if (index == current.length())
{
if (sentLastEOLN)
{
tok = new Token (TokenType.EOS, “”, rowNum, index+1);
sentEOF = true;
}
else
{
tok = new Token (TokenType.EOLN, “”, rowNum, index+1);
if (rowNum == code.size())
sentLastEOLN = true;
else
{
current = code.get(rowNum);
rowNum++;
index = 0;
}
}
}
else
{
switch (current.charAt(index))
{
case ‘=’:
tok = new Token(TokenType.EQUAL, “=”, rowNum,
index+1);
index++;
break;
case ‘+’:
tok = new Token(TokenType.PLUS, “+”, rowNum,
index+1);
index++;
break;
case ‘-‘:
tok = new Token(TokenType.MINUS, “-“, rowNum,
index+1);
index++;
break;
case ‘*’:
tok = new Token(TokenType.TIMES, “*”, rowNum,
index+1);
index++;
break;
case ‘/’:
tok = new Token(TokenType.DIVIDE, “/”, rowNum,
index+1);
index++;
break;
case ‘(‘:
tok = new Token(TokenType.LPAREN, “(“, rowNum,
index+1);
index++;
break;
case ‘)’:
tok = new Token(TokenType.RPAREN, “+”, rowNum,
index+1);
index++;
break;
case ‘^’:
tok = new Token(TokenType.EXPONENT, “^”, rowNum,
index+1);
index++;
break;
default:
if (Character.isDigit(current.charAt(index)))
{
int begin = index;
String first = getDigitSequence();
if (index < current.length() && current.charAt(index)
== ‘.’)
{
index++;
String second = getDigitSequence();
tok = new Token(TokenType.FLOATLIT,
first + ‘.’ + second, rowNum, begin);
}
else
tok = new Token(TokenType.INTLIT, first,
rowNum, begin);
}
else if (current.charAt(index) == ‘.’)
{
int start = index;
index++;
String s = getDigitSequence();
tok = new Token(TokenType.FLOATLIT, s,
rowNum, start);
}
else
throw new LexicalError(rowNum, index+1,
current.charAt(index));
}
}
return tok;
}
/**
* @return longest string containing strictly digits starting at current character
* @throws LexicalError
*/
private String getDigitSequence() throws LexicalError
{
int start = index;
while (index < current.length() && Character.isDigit(current.charAt(index)))
index++;
return current.substring(start, index);
}
}
Assignment Info:
The purpose of this assignment is to expose students to a new language and to
reinforce lexical analysis concepts. Specifically, the assignment is to implement the
lexical analyzer in Pascal. Pascal is covered in one of the recorded lectures. My java
version of the lexical analyzer is accessible on canvas. YOU DO NOT HAVE TO USE
MY VERSION! You can u
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."
Our Service Charter
1. Professional & Expert Writers: Essay Noon only hires the best. Our writers are specially selected and recruited, after which they undergo further training to perfect their skills for specialization purposes. Moreover, our writers are holders of masters and Ph.D. degrees. They have impressive academic records, besides being native English speakers.
2. Top Quality Papers: Our customers are always guaranteed of papers that exceed their expectations. All our writers have +5 years of experience. This implies that all papers are written by individuals who are experts in their fields. In addition, the quality team reviews all the papers before sending them to the customers.
3. Plagiarism-Free Papers: All papers provided by Essay Noon are written from scratch. Appropriate referencing and citation of key information are followed. Plagiarism checkers are used by the Quality assurance team and our editors just to double-check that there are no instances of plagiarism.
4. Timely Delivery: Time wasted is equivalent to a failed dedication and commitment. Essay Noon are known for the timely delivery of any pending customer orders. Customers are well informed of the progress of their papers to ensure they keep track of what the writer is providing before the final draft is sent for grading.
5. Affordable Prices: Our prices are fairly structured to fit in all groups. Any customer willing to place their assignments with us can do so at very affordable prices. In addition, our customers enjoy regular discounts and bonuses.
6. 24/7 Customer Support: At Essay Noon, we have put in place a team of experts who answer all customer inquiries promptly. The best part is the ever-availability of the team. Customers can make inquiries anytime.