급질이예요.. 간단한 형태소 분석기 C++/C로 어떻게 짜야 할지..

khj2220의 이미지

Lexical Analyzer를 만드려고 하는데 어렵네요...
토큰사이에는 공백문자가 하나이상 들어갈수 있고 c에서 나오는 syntex error를 잡아주어야 하는데..(변수 이름이 반드시 알파벳으로 시작하고 x13은되지만 그외는 안되는 것등,,x_13,x@2 이런거 모두 안되여,,,,)그리고 변수 선언이 반드시 처음에 와야 하구요,,,사칙연산은 c언어 방식따르는데 우선순위는 고려 안해도 되구요..

(입출력 예)

코드를 입력하세요: int xy; xy= 3 * (4 + 5); print xy;

int: IntType
xy: Identifier
;: SemiColon
xy: Identifier
=: Assignment
3: Number
*: MulOperator
(: LeftBracket
4: Number
+: AddOperator
5: Number
): RightBracket
;: SemiColon
print: PrintComand
xy: Identifer
;: SemiColon

또다른 예
입력:
int x3x,3y3;
x3x=x3x+x3x;
print 3y3;

출력:
int: IntType
x3x: Identifier
,: Comma
3y3: Error (Variable Must Begin with Alphabet!)
;: Semicolon
x3x: Identifier
=: Assignment
x3x: Identifier
+: AddOp
x3x: Identifier
;: Semicolon
print: PrintFunc
3y3: Error (Variable Must Begin with Alphabet!)
;: Semicolon

그리구 선언안된 변수사용시에도 에러를 내야 하구요, .c와 같긴 한데,, 도무지.

관련 소스나 아시는 분은 소스좀 올려 주세요~~~ㅜ.,ㅜ

cedar의 이미지

자세한 내용은 오라일리에서 나온 'lex & yacc'을 사서 보세요.
한빛출판사에서 번역판도 나와있습니다.

vacancy의 이미지

변수 선언이 앞에 와야 한다는 조건은
Lexical Analysis 단계에서 할 수 있는게 아닌데요.

다른 툴을 써도 되는 거라면
http://www.gnu.org/software/flex/
http://www.gnu.org/software/bison/
을 참고하시고요.
( 전자는 Lexer, 후자는 Parser입니다. )

아니라고 해도 저 두 software의 매뉴얼 중에 (아마 bison쪽에)
Lex를 쓰지 않은 간단한 Lexer의 예가 들어있었던 것 같습니다.

댓글 달기

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