정규식 state 를 알아낼수 잇나요?

litdream의 이미지

제목이 이상하게 되었네요.
내용인 즉슨,

PCRE pattern 이 주어졌다고 할때, 내부적으로 state graph 를 만들수 있을것으로 생각되는데요,
( 가령, pcre_dfa_exec() 처럼, DFA state 를 만드는것처럼요. 혹은, python 의 re.compile 도 재사용시 효율적이라고 하는걸로 봐서 graph 를 만들듯 한데요.)

이 diagram 을 human readable 로 뽑아낼수 있을까요?
꼭 human readable 일 필요는 없지만, regex 의 토큰의 특성상 character 로 표현하자면 XML 정도로 만들수 있지 않을까 하는 억측(?)을 해봅니다만...

libpcre 가 해줄수 있지 않을까 봤지만, 제가 잘 모르는지 안되는것 같더군요.
혹시 좋은 방법 아시나요?

aero의 이미지

온라인상으로 하는건
http://osteele.com/tools/reanimator/
가 있고

와 같은 형식으로 그려주는 Perl 프로그램인 re_graph라는게 있네요
문서: http://www.oualline.com/sw/re/re_graph.html
소스: http://www.oualline.com/sw/re/re-graph_02.tar

그리고 Graph::Regexp
http://search.cpan.org/dist/Graph-Regexp/
라는 Perl 모듈도 정규표현식을 각종 포멧으로 뽑아내어
Graph::Easy 모듈
http://search.cpan.org/dist/Graph-Easy/
http://bloodgate.com/perl/graph/manual/index.html
을 통해 그래프를 그릴 수 있도록 해줍니다.

위 프로그램/모듈등은 모두 Perl 정규표현식 debug모드에서
출력되는 메시지를 parsing하여 그래프를 그립니다.
예)

perl -Mre=debug -e '/just|another|perl|hacker/'

결과
Freeing REx: `","'
Compiling REx `just|another|perl|hacker'
size 15 Got 124 bytes for offset annotations.
 
   1: BRANCH(4)
   2:   EXACT <just>(15)
   4: BRANCH(8)
   5:   EXACT <another>(15)
   8: BRANCH(11)
   9:   EXACT <perl>(15)
  11: BRANCH(15)
  12:   EXACT <hacker>(15)
  15: END(0)
minlen 4
Offsets: [15]
        0[0] 1[4] 0[0] 5[1] 6[7] 0[0] 0[0] 13[1] 14[4] 0[0] 18[1] 19[6] 0[0] 0[0] 25[0]
Freeing REx: `"just|another|perl|hacker"'

다음은 위 결과를 Graph::Regexp 모듈을 사용하여 Graphviz 포멧으로 출력하여 그려진것입니다.

댓글 첨부 파일: 
첨부파일 크기
Image icon regexp.gif4.14 KB
김일영의 이미지

역으로 해주는게 있다면 킹왕짱일듯. 그죠?
제가 식견이 짧아 못 찾은건지도...

litdream의 이미지

바로 제가 찾던것이 이것 맞습니다.
궁극적으로는 test data 를 random 으로 만들고 싶어서 필요한것이었습니다.
특히 boundary case 를 테스트하려고 했는데, perl -e 코드를 막상 perl 로 만들려니 잘 모르겠네요 -_-;
제가 perl 이 약해서...

perl -Mre=debug -e '/just|another|perl|hacker/'

같은 결과를 얻기위해 코드로 쓰면 어떻게 되나요?

삽질의 대마왕...

삽질의 대마왕...

aero의 이미지

r.pl

#!/usr/bin/env perl
use strict;
use warnings;
use re 'debug';
 
open(STDERR, ">&STDOUT");
eval $ARGV[0];

perl r.pl '/just|another|perl|hacker/'

이렇게 정규식을 인자로 넘겨주면
표준출력으로 돌려줍니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.