2012-09-17 2 views
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: firstSecond: second , 그러나 나는 주어진 :> 우선 순위 때문에 예상했다.

다른 우선 순위 조합을 시도했지만 예상 결과를 얻을 수 없었습니다. Ragel에서이 문제를 해결할 수있는 방법이 있습니까 (예 : 미리보기없이 해결할 수 있습니까?)?

답변

0

메인 시스템으로이 시도 :

two_idents = identifier >start_first %first_string . separator . (identifier >start_second %second_string);        

main := (two_idents | identifier >start_first %first_string) . (separator . keyword)?; 

말썽은 "두 번째 식별자"와 접두사, 그래서 보호 연결을 시도하는 "제 1 식별자"주 첫 번째 시스템 바로 가기 때문이다. 노조는 실제로 당신이하려는 성냥을 묘사합니다.

관련 문제