lex & yacc 질문드립니다.

jekai의 이미지

lex & yacc 을 사용해보았는데,, 잘안되는게 있어서 질문드립니다.

어떤 언어(임의언어) 구현에서 inline 을 만들고있거든요.. ( 허접하게나마..;;;)

예를들어

inline
{
주절주절...
주절주절...
}
이런 식으로 inline 을 lex가 파싱하면, '{' , '}' 이거를 파싱해서 안에있는 코드를 그대로 뿌려주는 역할입니다.
그런데,, 중첩 스코프가 잘 안되는데.. 이유를 모르겠습니다. 예를들어
inline
{
if(function())
{
주절주절...
}
"하하하하"
}

이게 계속 syntax error 가 나네요... 계속보다 일도해야하기 떄문에 고수님들에게 여쭤봅니다.
참고로 코드는 이렇게 했습니다.

lex.l

scope_start "{"
scope_end "}"

// 아래는 scope 에 들어왔을때는, 모든문자에 대해서 yacc 에게 PURE_STR 를 리턴합니다.
{.. } { yylval.s (char *)strdup(yytext);
if(EnterScope)? PURE_STR:yytext[0]; // or yytext 를 return
}
{scope_start} { if(isInline) ++EnterScope ;
yylval.s = (char *) strdup(yytext) ;
if(EnterScope > 1) return PURE_STR) ;
else return yytext[0] ;
}

{scope_end} { if(isInline) --Enterscope ;

if(EnterScope < 1) isInline = FALSE ;

yylval.s = (char *) strdup(yytext) ;

if(EnterScope < 1) return PURE_STR) ;
else return yytext[0] ;
}
{inline} { isInline = TRUE ;
yylval.s = (char *) strdup(yytext) ;
return INLINE ;
}

yacc.y

야크 문법은 이렇습니다. 간단하죠 ;;;

InlineStatement
: INLINE '{' Commands '}'

Commands
: Commands PURE_STR
| PURE_STR

어디가 문제죠? 고수님들 가르쳐주세요 ㅠ_ㅠ

neogeo의 이미지

이미 '{' , '}' 에 의해 { , } 자체가 Token 으로 인식되기 때문입니다.

애석하게도 inline 이란 문법을 수정하시던가 { 를 다른것으로 대체 하셔서 토큰에서 반환될때 다른 문자열로 변환해 주시는게 좋을 것 같습니다.

( 문법 수정은 recursive 하게 하시면 어느정도 해결이 됩니다. )

보통 함수 선언 같은 경우

compound_stmt
: '{' local_declarations statement_list '}'
;

이것을 다시

statement_list
: compound_stmt
;

이런식으로 recursive 하게 구현 하면 내부적인 {} 를 해결하시는데 도움이 되실 것 입니다.

또다른 방법은 { 에 의한 state 를 구현하여 { 이후의 모든 { 와 } 를 카운트 해서 짝을 맞추는 방법도 있습니다.

어떻게 문법적으로 구현해야 하는지는 잘 고민해보시면 방법이 나오겠지요 :)

Neogeo - Future is Now.

Neogeo - Future is Now.

댓글 달기

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