ruby语法错误,意外的keyword_end,期望输入结束(SyntaxError)

时间:2016-05-18 06:44:20

标签: ruby syntax

运行我的代码时,我不断收到此错误(语法错误,意外的keyword_end,期望输入结束)。我看起来似乎所有的目的都匹配。我似乎错过了一些东西。任何帮助将不胜感激。

$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

1 个答案:

答案 0 :(得分:0)

第132行中的do不应该在那里。代码中还有许多其他语法错误。