0% found this document useful (0 votes)
21 views4 pages

Scanner Solution

This document discusses parsing numbers, comments, strings, and escape characters in a programming language. It includes code to: 1) Parse numbers with exponential notation and underscores in the number. 2) Parse multiline comments delimited by === and ===. 3) Parse strings allowing escape characters by replacing them during parsing.

Uploaded by

Gehad Omar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Scanner Solution

This document discusses parsing numbers, comments, strings, and escape characters in a programming language. It includes code to: 1) Parse numbers with exponential notation and underscores in the number. 2) Parse multiline comments delimited by === and ===. 3) Parse strings allowing escape characters by replacing them during parsing.

Uploaded by

Gehad Omar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1- Adding e to the number ➔➔➔ 8.5e+3 , 6.4e-2 , 25.

43e4
private void number() {
while (isDigit(peek())) {
advance();
}
if (peek() == '.' && isDigit(peekNext())) {
// Consume the "."
advance();
while (isDigit(peek())) {
advance();
}
if (peek() == 'e' && (peekNext() == '+' || peekNext() == '-') && isDigit(peekNextNext())) {
advance();
advance();
} else if (peek() == 'e' && isDigit(peekNext())) {
advance();
}
while (isDigit(peek())) {
advance();
}
}
String value = source.substring(start, current);
addToken(NUMBER, Double.parseDouble(value));
}
char peekNextNext()
{
if (current + 2 >= source.length()) {
return '\0';
}
return source.charAt(current + 2);
}
2- number have underscore (_) (only one underscore)

private void number() {


while (isDigit(peek()) || (peek()=='_' && isDigit(peekNext()))) advance();

// Look for a fractional part.


if (peek() == '.' && isDigit(peekNext())) {
// Consume the "."
advance();

while (isDigit(peek()) || (peek()=='_' && isDigit(peekNext()))) advance();


}
String value = source.substring(start,current);
value = value.replace("_", "");

addToken(NUMBER,
Double.parseDouble(value));
}
3- ===asd test asd=== // multible line Comment
case '=':

if (peek() == '=' && peekNext() == '=') {

multibleLineComment();

} else {

addToken(match('=') ? EQUAL_EQUAL : EQUAL);

break;

void multibleLineComment() {

advance();

advance();

while ((peek() != '=' || peekNext() != '=' || peekNextNext() != '=') && !isAtEnd()) {

if (peek() == '\n')

line++;

advance();

if (isAtEnd())

FirstProInBook.error(line, "Unterminated Comment.");

return;

advance(); advance(); advance();

char peekNextNext()

if (current + 2 >= source.length()) {

return '\0';

return source.charAt(current + 2);

}
4- String have escape Characters ➔ ➔ asd “test” asd
private void string() {

while ((peek() != '"'||previous()=='\\') && !isAtEnd()) {

if (peek() == '\n') {

line++;

advance();

if (isAtEnd()) {

FirstProInBook.error(line, "Unterminated string.");

return;

// The closing ".

advance();

// Trim the surrounding quotes.

String value = source.substring(start + 1, current - 1);

value = value.replace('\\', '\0');

addToken(STRING, value);

char previous()

return source.charAt(current-1);

You might also like