0% found this document useful (0 votes)
2 views2 pages

Pseudo Code for Scan Method

The document contains pseudo code for a scan method that tokenizes a program string. It loops through each character, checks the character type, and prints the corresponding token if it is a symbol, operator, letter, digit or string.

Uploaded by

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

Pseudo Code for Scan Method

The document contains pseudo code for a scan method that tokenizes a program string. It loops through each character, checks the character type, and prints the corresponding token if it is a symbol, operator, letter, digit or string.

Uploaded by

Corey Watt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// This is the pseudo code for the scan method in Q2

n = [Link]()
index = 0
lineNumber = 1

while (index < n) {

ch = [Link](index)
// only one of the following six methods calls can be "not null" or "true"
for a character
whiteSpace = isWhiteSpace(ch)
newline = isLineBreak(ch)
sym = getSymbol(ch)
op = getOp(ch)
letter = isLetter(ch)
digit = isDigit(c)

if (ch is white space) { // simply skip white space

index ++
continue

} else if (newline) { // new line

increase line number by one


index ++
continue

} else if (ch is a symbol) { // find a symbol

print lineNumber, sym, ch


index ++
continue

} else if (ch is an operator) { // find an operator

// How to extend the code below to also identify two-character


operators (e.g., <=,==)?
print lineNumber, op, ch
index ++
continue

} else if (ch is a letter) { // find a letter

word = ""
word += ch
index ++
while (index < n) { // use this while loop to identify e.g. the
variable name
ch = [Link](index)
if (ch is a letter) {
word += ch
index ++
}
else { break }
}
// the word can be variable name (i.e. IDENTIFIER) or keyword (e.g.,
KEYWORD_INT) or Klingon method (e.g., KLINGON_ADD)
// how to decide?
print lineNumber, token type, word
continue

} else if (ch is a digit) { // find the first digit of a number

number = ""
number += ch
index ++
while (index < n) {
ch = [Link](index)
if (ch is a digit) {
number += ch
index ++
}
else { break }
}
print ...
continue

} else if (ch == '\"') { // find the beginning of a string literal e.g.,


"abc"

str = ""
str += ch
index ++

// to add a while loop similar to the one when ch is a digit or letter

print lineNumber, [Link], str


continue

} else {

print Encountered something not expected: ch


index ++
continue

You might also like