Created
February 17, 2014 03:33
-
-
Save Yamabiko/9044274 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './parser.rb' | |
def get_code | |
code = gets | |
return code.chomp | |
end | |
def lexical_analyze(str) | |
tokens = str.split(/\s+/) | |
res = [] | |
tokens.each do |token| | |
if token == '+' | |
res << ['+','+'] | |
elsif token == '-' | |
res << ['-','-'] | |
elsif token == '*' | |
res << ['*','*'] | |
elsif token == '/' | |
res << ['/','/'] | |
elsif token == '%' | |
res << ['%','%'] | |
elsif token == '(' | |
res << ['(','('] | |
elsif token == ')' | |
res << [')',')'] | |
elsif /[^0-9]/ =~ token | |
res << [:var,token] | |
else | |
res << [:int,token] | |
end | |
end | |
return res | |
end | |
def parse(tokens) | |
parser = SampleParser.new | |
return parser.parse(tokens) | |
end | |
p tokens = lexical_analyze(get_code) | |
p parse(tokens) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample.yを使ってparser.rbを生成してから実行してください。