Ian Wilder Ian Wilder - 6 months ago 56
Ruby Question

ruby syntax error, unexpected keyword_end, expecting end-of-input (SyntaxError)

I keep getting this error (syntax error, unexpected keyword_end, expecting end-of-input) when running my code. I've looked and it seems like all the ends match up. I seem to be missing something. Any help would be appreciated.

$charClass = $charClass.to_i
$lexeme= Array.new(100)
$nextChar = $nextChar.to_s
$lexLen = $lexLen.to_i
$token = $token.to_i
$nextToken = $nextToken.to_i
$input = File.read('user input.txt')

class String
def valid_integer?
true if Integer(self) rescue false
end
end

LETTER = 0
DIGIT = 1
UNKNOWN = 99

INT_LIT = 10
IDENT = 11
ASSIGN_OP = 20
ADD_OP = 21
SUB_OP = 22
MULT_OP = 23
DIV_OP = 24
LEFT_PAREN = 25
RIGHT_PAREN = 26


def lookup (ch)
case ch
when '('
addChar
$nextToken = LEFT_PAREN

when ')'
addChar
$nextToken = RIGHT_PAREN

when '+'
addChar
$nextToken = ADD_OP

when '-'
addChar
$nextToken = SUB_OP

when '*'
addChar
$nextToken = MULT_OP

when '/'
addChar
$nextToken = DIV_OP

else
addChar
$nextToken = EOF;
end
return $nextToken
end

def addChar
if $lexLen <= 98
$lexeme[$lexLen+1] = $nextChar;
$lexeme[$lexLen] = 0
else
puts "Error - $lexeme is too long"
end
end

def getChar()
if (($nextChar = $input.split("")) != EOF)
if $nextChar.match(/^[[:alpha:]]$/)
$charClass = LETTER
elsif $nextChar.valid_integer?
$charClass = DIGIT
else $charClass = UNKNOWN
end
$charClass = EOF
end
end

def getNonBlank()
while $nextChar =~ /^\s*$/
getchar()
end
end

def lex
$lexLen = 0
getNonBlank
case $charClass

when LETTER
addChar
getChar
while $charClass == LETTER || $charClass == DIGIT
addChar
getChar
end
$nextToken = IDENT

when DIGIT
addChar
getChar
while $charClass == DIGIT
addChar
getChar
end
next$token = INT_LIT

when UNKNOWN
lookup($nextChar)
getChar

when EOF
$nextToken = EOF
$lexeme[0] = 'E'
$lexeme[1] = 'O'
$lexeme[2] = 'F'
$lexeme[3] = 0
end
puts "Next $token is #{$nextToken}, Next $lexeme is #{$lexeme}"
return $nextToken
end

if $input.nil?
puts "ERROR = cannot open input"
else
getChar()
do lex()
while ($nextToken != EOF)
end
end
end

Answer

The do in line 132 is not supposed to be there. There are many other syntax errors in the code.