1
다음 형식을 구문 분석하려고합니다 : (identifier/)?identifier(/keyword)?
첫 번째 식별자와 선택적 키워드가 있습니다. 키워드는 식별자로 사용할 수 없습니다. up
다음 키워드 인 경우, 예를 들어 :어떻게 Ragel로이 모호성을 해결할 수 있습니까?
simple
는 제로서 상기 제 2 식별자,first/second
일치 제 식별자first
및 제 것과second
,second/up
일치second
일치 식별자 및up
을 키워드로 사용합니다. 루비 Ragel 사용
는 I가 다음 FSM 정의 :
%%{
machine simple;
keyword = "up";
separator = '/';
ident_char = any - separator;
identifier = ident_char+ - keyword;
action start_string { $start_string = p }
action first_string { puts "First: #{get_string(data, p)}" }
action second_string { puts "Second: #{get_string(data, p)}" }
action keyword_string { puts "Keyword: #{get_string(data, p)}" }
main := (identifier >start_string %first_string separator)?
:> identifier >start_string %second_string
(separator keyword >start_string %keyword_string)?
;
}%%
%% write data;
def get_string(data, p)
data[$start_string...p].pack("c*")
end
def parse(data)
data = data.unpack("c*")
eof = pe = data.length
%% write init;
%% write exec;
end
parse("first/second")
puts("---")
parse("second/up")
이것은 다음 출력 준다 : 첫 번째 부분이어야하는,
$ ragel -R simple.rl ; ruby simple.rb
Second: first
---
Second: second
Keyword: up
잘못 First: first
Second: second
, 그러나 나는 주어진 :>
우선 순위 때문에 예상했다.
다른 우선 순위 조합을 시도했지만 예상 결과를 얻을 수 없었습니다. Ragel에서이 문제를 해결할 수있는 방법이 있습니까 (예 : 미리보기없이 해결할 수 있습니까?)?