Created
June 17, 2018 08:29
-
-
Save maehrm/4c9f122316814cf4bf8a5f9a6b6a9db4 to your computer and use it in GitHub Desktop.
コンパイラつく〜ろ〜♪ 3番BNF記法 - pokotyamuさんのメモ帳 http://pokotyamu.hatenablog.com/entry/2015/05/15/005033
This file contains 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
# coding: utf-8 | |
# | |
# <数値> ::= <数字列> | <符号><数字列> | <数字列>.<数字列> | <符号><数字列>.<数字列> | |
# <数字列> ::= <数字> | <数字列> <数字> | |
# <数字> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
# <符号> ::= + | - | ± # ±は省略 | |
class MyNumeric | |
rule | |
target : numeric_num { result = 'ok' } | |
numeric_num : digit_string | |
| SIGN digit_string | |
| digit_string '.' digit_string | |
| SIGN digit_string '.' digit_string # 追加する必要があるのでは? | |
digit_string : NUMBER | |
| digit_string NUMBER | |
end | |
---- header | |
require 'strscan' | |
---- inner | |
def parse(str) | |
scanner = StringScanner.new(str) | |
@q = [] | |
until scanner.eos? | |
scanner.scan(/\A\s+/) ? nil : | |
scanner.scan(/\A\d/) ? @q << [:NUMBER, scanner.matched] : | |
scanner.scan(/\A[+-]/) ? @q << [:SIGN, scanner.matched] : | |
scanner.scan(/\A\./) ? @q << [scanner.matched, scanner.matched] : | |
nil | |
end | |
pp @q | |
do_parse | |
end | |
def next_token | |
@q.shift | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ racc -g -o numeric_parser.rb my_numeric.y; ruby numeric_parser.rb
parse error on value "." (".")