Qt program에 lex를 접목하려면?

thisrule의 이미지

현재 간단한 parsing 프로그램을 작성하고자 합니다.
그래서 token을 분리하는 부분은 lex를 사용하려 하고 문법적인 부분은 Qt로 하려고 합니다.

그런데 lexer.l 을 작성하려면

lex 규칙...
lex 규칙...

int main( int argc, char** argv )
{
    ...
    while( !feof( yyin ) )
    {
        yylex();
    }
    ...
    return 0;
}

와 같이 해야 하는데
Qt는 class base라 어떻게 접목해야 하는지 조금 난감합니다.
아시는 분들의 많은 조언 바랍니다.
cedar의 이미지

lex과 yacc 같은 툴은 별도의 문법 정의 스크립트를 사용하여
C 소스 코드를 생성하는 툴이므로 C++에서 사용하기가 불편합니다.

C++ 개발자라면 반드시 알아야 할 라이브러리인, Boost 라이브러리를 찾아보세요.
http://boost.org/libs/libraries.htm#String

단순한 토크나이저가 필요하다면 tokenizer 라이브러리를,
정규표현식이 필요하다면 regex(regex++라고도 표기)를 쓰시면 됩니다.
그리고 lex/yacc를 능가하는 강력한 LL parser framework인 spirit이란 것이 있는데, 이것은 C++ 소스코드에 직접 문법을 기술하는 방식으로 되어 있어서 사용이 매우 간편합니다.

thisrule의 이미지

cedar wrote:
C++ 개발자라면 반드시 알아야 할 라이브러리인, Boost 라이브러리를 찾아보세요.
http://boost.org/libs/libraries.htm#String

감사합니다.
상당히 방대한 양의 라이브러리이군요.
이 라이브러리 이해하는데만 해도 많은 시간이 걸리겠군요. 그런데 실제로 개발에 많이 쓰이나요?
한번 공부해보겠습니다.
thisrule의 이미지

cedar wrote:
C++ 개발자라면 반드시 알아야 할 라이브러리인, Boost 라이브러리를 찾아보세요.
http://boost.org/libs/libraries.htm#String

쭉 훑어보았습니다.
그런데 lex tool 처럼 간단하지가 않군요. compiler에 대한 기본 지식이 있어야 이해가 되겠습니다.

그냥 lex 처럼

/pattern1/ {
}

/pattern2/ {
}

pattern1일때는 이러한 일을, pattern2일때는 이러한 일을 할 수 있도록 지원하는
C++용 library나 tool은 없을까요?
시간이 부족한 상황에서 새로운 방대한 library를 배워 쓰려니 너무 막막합니다.
yielding의 이미지

spirit으로도 regular expression을 풀어서 BNF로 표현해서 쓸 수 있겠지만 말씀하신것 처럼 공부가 좀 필요하고요, boost string lib중에서 regex가 딱인거 같군요

Life rushes on, we are distracted

thisrule의 이미지

yielding wrote:
spirit으로도 regular expression을 풀어서 BNF로 표현해서 쓸 수 있겠지만 말씀하신것 처럼 공부가 좀 필요하고요, boost string lib중에서 regex가 딱인거 같군요

regex library를 이용하여 제가 원하는 program을 작성하려고 합니다.
어제 밤부터 오늘 아침까지 regex document를 훑어보았는데 제 C++ 실력이 모자라고, compiler 기초가 부족해서 아직 감이 잘 오지 않습니다.

그래서 KLDP 분들에게 조금의 hint를 얻기를 바랍니다.
제가 parsing하려는 대상 script는 아래와 같습니다.

Quote:
/* This is sample pattern program */
PATTERN 1;
SET F 000H ;
SET F 000L ;
REPEAT 20 SET F 111.;
SET F 000H ;
END;

문법자체는 간단합니다.
그래서 그냥 strtok()와 같은 함수를 이용해도 되나, 기존 parsing program이 lex와 C program으로 구현되어 있는데 이걸 Qt를 이용하여 upgrade시키고자 합니다.
전 단지, 처음의 명령어가 PATTERN, SET F, REPEAT, END 각각에 따라 어떤 행동을 하는 프로그램을 작성하고자 합니다.
line/block comment도 허용하고자 합니다.

regex를 사용하면 더 효율적이고 멋지게(?) 할 수 있을거 같은데...
힌트를 조금이라도 주시길 바랍니다.

cedar의 이미지

regex는 기본적인 lexical anaysis만 가능합니다.
스크립트 언어를 처리하려면,
아무리 간단한 문법일지라도, parser를 쓰실 것을 권합니다.

다음 Spirit을 사용한 예제 프로젝트들입니다.
http://spirit.sourceforge.net/repository/applications/show_contents.php

이중에 mini, slex, spirit_sql 정도가 가장 간단한 예제가 되겠네요.
BNF 표기법을 써서 문법을 정의하신 후, spirit 라이브러리를 써서 파서를 구현하실 것을 권합니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.