eclipse.com 에서 plugin 중 sftp를 검색 하면 아래 사이트의 plugin이 점수가 높다.

http://aptana.com/plugins 에서 sftp로 검색 하면 아래부분이 나온다.


SFTP support (unsupported). This version of SFTP is the old version of SFTP as included with the beta version of Aptana Studio. Aptana Studio 1.0+/Eclipse 3.2 with Aptana Studio 1.0+ Plugin Users:

  1. Go to the Update Manager in Aptana Studio via Help->Software Updates->Find and Install...
  2. Add a new Remote Update site to the following url: http://update.aptana.com/install/sftp_deprecated/3.2/
  3. Install the unsupported SFTP feature from that update site
Eclipse 3.3 with Aptana Studio 1.0+ Plugin Users:
  1. Go to the Update Manager in Aptana Studio via Help->Software Updates->Find and Install...
  2. Add a new Remote Update site to the following url: http://update.aptana.com/install/sftp_deprecated/3.3/
  3. Install the unsupported SFTP feature from that update site

여기서 3.3 버젼에 맞는

http://update.aptana.com/install/sftp_deprecated/3.3/ 를 업데이트 하면 sftp를 사용할수 있다.

image

Windows->Show View->Others 에서  Aptana Standard Views의 File 을 선택 하면

image

sftp를 사용할 수 있다.

필자는 http://update.aptana.com/install/3.2/ 를 통채로 업데이트를 한 상황이라 화면이 다를 수도 있음.

그럼 즐프~


출처 : http://prelooks.tistory.com/85

참고로 저걸 하기전에 http://www.aptana.com/docs/index.php/Plugging_Aptana_into_an_existing_Eclipse_configuration
여기부터 업데이트를 하는게 좋을꺼 같다... orz

#include <stdio.h>

void main()
{
     char str[] = "10.3454";         //string
     float a;

     sscanf(str, "%f", &a);         // string to float

     printf("string = %s\n", str);
     printf("converting float = %f\n", a);
}


쓰이는곳... 내가 못찾아서 그런거일지도 모르지만...
sql 쿼리에서 받는 값이 모두 string이라 float 값을 받아와도 string으로 리턴들어옴..

C언어는 날짜/시간을 구할 때 하나의 함수로만 되는 것이 아니라, 다음과 같이 약간 복잡합니다.

time() 함수로, 현재 경과된 초(sec), 즉 "유닉스 시간"을 구한 후, 그것을 localtime() 함수로 연월일 시분초로 분리하여 구조체에 저장합니다.

C에서, 오늘 시각/날짜 (현재 날짜, 시간) 출력 예제


파일명: 0.cpp
#include <stdio.h>
#include <time.h>


void main(void) {
  time_t timer;
  struct tm *t;

  timer = time(NULL); // 현재 시각을 초 단위로 얻기

  t = localtime(&timer); // 초 단위의 시간을 분리하여 구조체에 넣기


  printf("유닉스 타임 (Unix Time): %d 초\n\n", timer); // 1970년 1월 1일 0시 0분 0초부터 시작하여 현재까지의 초

  printf("현재 년: %d\n",   t->tm_year + 1900);
  printf("현재 월: %d\n",   t->tm_mon + 1);
  printf("현재 일: %d\n\n", t->tm_mday);

  printf("현재 시: %d\n",   t->tm_hour);
  printf("현재 분: %d\n",   t->tm_min);
  printf("현재 초: %d\n\n", t->tm_sec);

  printf("현재 요일: %d\n", t->tm_wday); // 일요일=0, 월요일=1, 화요일=2, 수요일=3, 목요일=4, 금요일=5, 토요일=6
  printf("올해 몇 번째 날: %d\n", t->tm_yday); // 1월 1일은 0, 1월 2일은 1
  printf("서머타임 적용 여부: %d\n", t->tm_isdst); // 0 이면 서머타임 없음

}



실행 결과 화면:
D:\Z>cl 0.cpp && 0.exe
0.cpp
유닉스 타임 (Unix Time): 1160032094 초

현재 년: 2006
현재 월: 10
현재 일: 5

현재 시: 16
현재 분: 8
현재 초: 14

현재 요일: 4
올해 몇 번째 날: 277
서머타임 적용 여부: 0
D:\Z>



time.h 에 시간 구조체가 다음과 같이 정의되어 있습니다.

struct tm {
  int tm_sec;   /* Seconds */
  int tm_min;   /* Minutes */
  int tm_hour;  /* Hour (0--23) */
  int tm_mday;  /* Day of month (1--31) */
  int tm_mon;   /* Month (0--11) */
  int tm_year;  /* Year (calendar year minus 1900) */
  int tm_wday;  /* Weekday (0--6; Sunday = 0) */
  int tm_yday;  /* Day of year (0--365) */
  int tm_isdst; /* 0 if daylight savings time is not in effect) */

};




업데이트: 비주얼C 2005 이상에 최적화: ▶▶ C언어] localtime_s 함수 사용법: 비주얼 Visual C 2005 이상에서

출처 : http://mwultong.blogspot.com/2006/10/c-current-date-time.html

'C/C++언어' 카테고리의 다른 글

이클립스로 SFTP 사용하기  (0) 2008.06.24
C에서 string 문자열을 float 로 바꾸기  (1) 2008.06.15
LEX & YACC  (0) 2008.06.07
Lex와 Yacc의 사용법 강좌  (0) 2008.06.07
고정 소수점 (C++언어 버젼)  (0) 2008.05.09

Lex and yacc은

   ·컴파일러 또는 인터프리터를 작성하기 위한 도구

   ·패턴을 찾기 위한 응용분야에서 사용하기 쉽다.

특징

    ·rapid prototyping

    ·easy modification

    ·simple maintenance

Lex 및 Yacc가 이용된 예

   ·desktop calculator bc

   ·typesetting tools eqn, pic, xfig

   ·C compilers PCC, GCC

   ·menu compiler

   ·A SQL processors

   ·the lex itself

 

 

Availability of Lex and Yacc

 

Yacc was the first of the two,developed by Stephen C.Johnson. Lex was designed to work with yacc by M.E.Lesk and E.Schmidt. Both lex and yacc have been standard UNIX utilities since Version 7, and no significant differences between Berkeley and System V versions.

 

AT&T version lex and yacc

BSD version Berkeley yacc

GNU bison

GNU flex

MS-DOS, OS/2 versions

 

1.Lex and Yacc

 

The Simplest Lex Program

 

간단한 lex입력

%%

.|\n           ECHO;

%%

UNIX의 cat과 같은 기능

lex에 의하여 생성된 프로그램

#include <stdio.h>

#include <stdlib.h>

# define U(x) x

중략

yylex(){

int nstr; extern int yyprevious;

#ifdef __cplusplus

/* to avoid CC and lint complaining yyfussy not being used ...*/

static int __lex_hack = 0;

if (__lex_hack) goto yyfussy;

#endif

while((nstr = yylook()) >= 0)

yyfussy: switch(nstr){

     case 0:

             if(yywrap()) return(0); break;

     case 1:

# line 2 "ch1-1.l"

             ECHO;

             break;

     case -1:

             break;

     default:

             (void)fprintf(yyout,"bad switch yylook %d",nstr);

   } return(0); }

/* end of yylex */

생략

 

lex specification의 구성

definition section

%%

rule section

        pattern(regular expression)     action(C source code)

%%

user subroutine section(C source code)

 

 

Lex로 단어 인식 프로그램 만들기

 

인식할 단어:

is        am        are       were

was     be         being    been

do       does      did        will

would  should  can        could

has     have     had        go

 

단어 인식기 - LEX SPEC.

%{

/*

 * this sample demonstrates (very) simple recognition:

 * a verb/not a verb.

 */

%}

%%

[\t ]+         /* ignore white space */ ;

is |

am |

are |

were |

was |

be |

being |

been |

do |

does |

did |

will |

would |

should |

can |

could |

has |

have |

had |

go             { printf("%s: is a verb\n", yytext); }

[a-zA-Z]+      { printf("%s: is not a verb\n", yytext); }

.|\n           { ECHO; /* normal default anyway */ }

%%

 

main()

{

        yylex();

}

 

 

코드생성 및 실행

% lex ch-01.l

% ls

lex.yy.c

% cc lex.yy.c -o first -ll

% ls

first  lex.yy.c

% first

did I have fun?

did: is a verb

I: is not a verb

have: is a verb

fun: is not a verb

?

^D

%

 

 

문장의 다른 단어도 인식하기 - LEX SPEC

 

%{

/*

 * We expand upon the first example by adding recognition of some other

 * parts of speech.

 */

%}

%%

[\t ]+         /* ignore white space */ ;

is |

am |

are |

were |

was |

be |

being |

been |

do |

does |

did |

will |

would |

should |

can |

could |

has |

have |

had |

go             { printf("%s: is a verb\n", yytext); }

very |

simply |

gently |

quietly |

calmly |

angrily        { printf("%s: is an adverb\n", yytext); }

to |

from |

behind |

above |

below |

between |

below          { printf("%s: is a preposition\n", yytext); }

if |

then |

and |

but |

or             { printf("%s: is a conjunction\n", yytext); }

their |

my |

your |

his |

her |

its            { printf("%s: is an adjective\n", yytext); }

I |

you |

he |

she |

we |

they           { printf("%s: in a pronoun\n", yytext); }

 

[a-zA-Z]+ {

         printf("%s:  don't recognize, might be a noun\n", yytext);

        }

.|\n           { ECHO; /* normal default anyway */ }

%%

main()

{

        yylex();

}

 

 

 

Symbol Tables

단어의 품사를 선언하고 입력되는 단어의 품사를 구분하기

definition section

%{

/*

 * Word recognizer with a symbol table.

 */

enum {

        LOOKUP = 0, /* default - looking rather than defining. */

        VERB, /* 동사 */

        ADJ,  /* 형용사 */

        ADV,  /* 부사  */

        NOUN, /* 명사 */

        PREP, /* 전치사 */

        PRON, /* 대명사 */

        CONJ  /* 접속사 */

};

int state;

int add_word(int type, char *word);

int lookup_word(char *word);

%}

 

 

rule section

%%

\n     { state = LOOKUP; }    /* end of line, return to default state */

^verb  { state = VERB; }

^adj   { state = ADJ; }

^adv   { state = ADV; }

^noun  { state = NOUN; }

^prep  { state = PREP; }

^pron  { state = PRON; }

^conj  { state = CONJ; }

[a-zA-Z]+  {

                /* a normal word, define it or look it up */

             if(state != LOOKUP) {

                /* define the current word */

                add_word(state, yytext);

             } else {

                switch(lookup_word(yytext)) {

                case VERB: printf("%s: verb\n", yytext); break;

                case ADJ: printf("%s: adjective\n", yytext); break;

                case ADV: printf("%s: adverb\n", yytext); break;

                case NOUN: printf("%s: noun\n", yytext); break;

                case PREP: printf("%s: preposition\n", yytext); break;

                case PRON: printf("%s: pronoun\n", yytext); break;

                case CONJ: printf("%s: conjunction\n", yytext); break;

                default:

                        printf("%s:  don't recognize\n", yytext);

                        break;

                }

            }

          }

.      /* ignore anything else */ ;

%%

 

 

user subroutine section

main()

{

        yylex();

}

/* define a linked list of words and types */

struct word {

        char *word_name;

        int word_type;

        struct word *next;

};

struct word *word_list; /* first element in word list */

extern void *malloc();

int

add_word(int type, char *word)

{

        struct word *wp;      

        if(lookup_word(word) != LOOKUP) {

                printf("!!! warning: word %s already defined \n", word);

                return 0;

        }

       

        /* word not there, allocate a new entry and link it on the list */

        wp = (struct word *) malloc(sizeof(struct word));

        wp->next = word_list;

        /* have to copy the word itself as well */

       

        wp->word_name = (char *) malloc(strlen(word)+1);

        strcpy(wp->word_name, word);

        wp->word_type = type;

        word_list = wp;

        return 1;      /* it worked */

}

int

lookup_word(char *word)

{

        struct word *wp = word_list;

        /* search down the list looking for the word */

        for(; wp; wp = wp->next) {

                if(strcmp(wp->word_name, word) == 0)

                        return wp->word_type;

        }

        return LOOKUP; /* not found */

}

실행 예

verb is am are were be being been do

is

is: verb

noun dog cat horse cow

verb chew eat lick

verb run stand sleep

dog run

dog: noun

run: verb

chew eat sleep cow horse

chew: verb

eat: verb

sleep: verb

cow: verb

horse: noun

verb talk

talk

talk: verb

 

 

문법(Grammars)

정해진 순서대로 토큰이 입력되는가? -- 문법 기술이 필요!

 

간단한 문장의 구성

noun verb.

noun verb noun.

 

문법을 기술하는 방식이 필요함

subject → noun | pronoun

object → noun

sentence → subject verb object

 

다양한 문장을 파싱하기 위하여는 문법을 확장하여야 함.

파서에서 어휘분석기(yylex)를 부르게하기 위하여 어휘분석기를 수정해야함.

 

 

파서(parser)와 렉서(lexer)간의 통신

파서가 상위의 루틴이되어야 함;- 파서(yyparse)는 yylex를 호출, yylex는 토큰(토큰 종류, 토큰 값)을 돌려줌. 따라서, 파서와 렉서는 같은 정의의 토큰을 가져야 함.

 

ex)우리의 품사 구분에서의 예

# define NOUN 257

# define PRONOUN 258

# define VERB 259

# define ADVERB 260

# define ADJECTIVE 261

# define PROPOSITION 263

# define CONJUCTION 263

 

yacc는 토큰정의를 위해 y.tab.h를 만듬.

 

new lexer

%{

/*

 * We now build a lexical analyzer to be used by a higher-level parser.

 */

#include "ch1-05y.h"  /* token codes from the parser */

#define LOOKUP 0 /* default - not a defined word type. */

int state;

%}

%%

\n     { state = LOOKUP; }

\.\n   {      state = LOOKUP;

                return 0; /* end of sentence */

        }

^verb  { state = VERB; }

^adj   { state = ADJECTIVE; }

^adv   { state = ADVERB; }

^noun  { state = NOUN; }

^prep  { state = PREPOSITION; }

^pron  { state = PRONOUN; }

^conj  { state = CONJUNCTION; }

[a-zA-Z]+ {

             if(state != LOOKUP) {

                add_word(state, yytext);

             } else {

                switch(lookup_word(yytext)) {

                case VERB:

                  return(VERB);

                case ADJECTIVE:

                  return(ADJECTIVE);

                case ADVERB:

                  return(ADVERB);

                case NOUN:

                  return(NOUN);

                case PREPOSITION:

                  return(PREPOSITION);

                case PRONOUN:

                  return(PRONOUN);

                case CONJUNCTION:

                  return(CONJUNCTION);

                default:

                  printf("%s:  don't recognize\n", yytext);

                  /* don't return, just ignore it */

                }

            }

          }

.      ;

%%

/* define a linked list of words and types */

struct word {

        char *word_name;

        int word_type;

        struct word *next;

};

struct word *word_list; /* first element in word list */

extern void *malloc();

int

add_word(int type, char *word)

{

        struct word *wp;      

        if(lookup_word(word) != LOOKUP) {

                printf("!!! warning: word %s already defined \n", word);

                return 0;

        }

       

        /* word not there, allocate a new entry and link it on the list */

        wp = (struct word *) malloc(sizeof(struct word));

        wp->next = word_list;

        /* have to copy the word itself as well */

       

        wp->word_name = (char *) malloc(strlen(word)+1);

        strcpy(wp->word_name, word);

        wp->word_type = type;

        word_list = wp;

        return 1;      /* it worked */

}

int

lookup_word(char *word)

{

        struct word *wp = word_list;

        /* search down the list looking for the word */

        for(; wp; wp = wp->next) {

                if(strcmp(wp->word_name, word) == 0)

                        return wp->word_type;

        }

        return LOOKUP; /* not found */

}

 

 

A Yacc Parser

다음은 yacc을 사용하는 첫 번째 예이다. lex의 입력과 유사하다.

%{

/*

 * A lexer for the basic grammar to use for recognizing english sentences.

 */

#include <stdio.h>

%}

%token NOUN PRONOUN VERB ADVERB ADJECTIVE PREPOSITION CONJUNCTION

%%

sentence: subject VERB object { printf("Sentence is valid.\n"); }

        ;

subject:       NOUN

        |      PRONOUN

        ;

object:        NOUN

        ;

%%

extern FILE *yyin;

main()

{

        while(!feof(yyin)) {

                yyparse();

        }

}

yyerror(s)

char *s;

{

    fprintf(stderr, "%s\n", s);

}

 

yacc입력의 구조

definition section

%%

rule section

%%

user subroutine section

 

The Rule Section

rule section은 실제 문법이 생성규칙(production rule)의 형식으로 기술되어야 한다. 각각의 생성규칙은 ":"를 중심으로 왼쪽에는 한 개의 이름이 오고, 오른 쪽에는 문법 심볼들의 나열과 행동코드, 그리고 세미콜론(";")으로 마감한다.

 

   rule section의 기술형식:

        이름 : 문법 심볼  { 행동(C코드) } ;

 

별도로 정하지 않으면, 첫 번째 로 기술된 규칙을 시작 규칙으로 한다.

   ex)

        statement:    NAME '=' expression

                |      expression            { printf("= %d\n", $1); }

                ;

        expression:  expression '+' expression { $$ = $1 + $3; }

                |      expression '-' expression { $$ = $1 - $3; }

                |      expression '*' expression { $$ = $1 * $3; }

                |      expression '/' expression

                                        {      if($3 == 0)

                                                        yyerror("divide by zero");

                                                else

                                                        $$ = $1 / $3;

                                        }

                |      '-' expression %prec UMINUS{ $$ = -$2; }

                |      '(' expression ')'  { $$ = $2; }

                |      NUMBER                { $$ = $1; }

                ;

 

   ex)

         sentence: subject VERB object    { printf("Sentence is valid.\n"); }

                ;

        subject:      NOUN

                |      PRONOUN

                ;

        object:               NOUN

 

 

user subroutine section

파서에 포함될 어떤 C 코드도 쓸수 있다.

   ex)

        extern FILE *yyin;

        main()

        {

                while(!feof(yyin)) {

                        yyparse();

                }

        }

        yyerror(s)

        char *s;

        {

            fprintf(stderr, "%s\n", s);

        }

 

 

좀더 복잡한 문장을 파싱하도록 yacc입력을 수정-ch1-06.y

%{

#include <stdio.h>

/* we found the following required for some yacc implementations. */

/* #define YYSTYPE int */

%}

%token NOUN PRONOUN VERB ADVERB ADJECTIVE PREPOSITION CONJUNCTION

%%

sentence: simple_sentence   { printf("Parsed a simple sentence.\n"); }

        | compound_sentence { printf("Parsed a compound sentence.\n"); }

        ;

simple_sentence: subject verb object

        |      subject verb object prep_phrase

        ;

compound_sentence: simple_sentence CONJUNCTION simple_sentence

        |      compound_sentence CONJUNCTION simple_sentence

        ;

subject:      NOUN

        |      PRONOUN

        |      ADJECTIVE subject

        ;

verb:          VERB

        |      ADVERB VERB

        |      verb VERB

        ;

       

object:               NOUN

        |      ADJECTIVE object

        ;

prep_phrase: PREPOSITION NOUN

        ;

%%

extern FILE *yyin;

main()

{

        while(!feof(yyin)) {

                yyparse();

        }

}

yyerror(s)

char *s;

{

    fprintf(stderr, "%s\n", s);

}

 

Running Lex and Yacc

% lex ch1-n.l

% yacc -d ch1-m.y

% cc -c lex.yy.c y.tab.c

% cc -o example-m.n lex.yy.o y.tab.o -ll -ly

 

 

Lex vs. Handwritten Lexers

(렉스로 스캐너를 자동생성하는 것과 직접 스캐너를 프로그램하는 것과의 비교)

 

hand coded lexical analyzer (Example 1-9. Sample C lexical analyzer)

  #include <stdio.h>

  #include <ctype.h>

  char *progname;

  #define NUMBER 400

  #define COMMENT 401

  #define TEXT 402

  #define COMMAND 403

  main(argc,argv)

  int argc;

  char *argv[];

  {

  int val;

  while(val = lexer()) printf("value is %d\n", val);

  }

 

  lexer()

  {

    int c;

    while ((c=getchar()) == ' ' || c == '\t')    ;

    if (c==EOF)

       return 0;

    if (c=='.' || isdigit(c))  {       /* number */

        while ((c=getchar()) != EOF && isdigit(c));

  if (c=='.') while ((c = gerchar()) != EOF && isdight(c));

        ungetc(c, stdin);

        return NUMBER;

    }

    if (c=='#')  {      /* comment */

       int index = 1;

       while ((c = getchar() != EOF && c != '\n');

       ungetc(c,stdin);

       return COMMENT;

    }

    if (c=='"')  {  /* literal text */

       int index = 1;

       while ((c = getchar()) != EOF &&

      c != '"' && c != '\n');

       if (c == '\n') ungetc(c, stdin);

       return TEXT;

    }

    if (isalpha(c))  {    /* check to see if it is a command */

       int index = 1;

       while((c = getchar()) != EOF && isalnum(c));

       ungetc(c, stdin);

       return COMMAND;

    }

    return c;

  }

 

Sample lex lexical analyzer

%{

  #define NUMBER   400

  #define COMMENT  401

  #define TEXT     402

  #define COMMAND  403

%}

%%

[ \t]+                   ;

[0-9]+                 |

[0-9]+\.[0-9]+         |

\.[0-9]+                 { return NUMBER;   }

#*                       { return COMMENT;   }

\"[^\"\n]*\"             { return TEXT;   }

[a-zA-Z][a-ZA-Z0-9]+     { return COMMAND;   }

\n                       { return '\n';   }

%%

#include <stdio.h>

 

main (argc, argv)

int argc;

char *argv[];

{

int val;

 

while(val = yylex()) printf("value is %d\n",val);

}

 

2. Using Lex(렉스 사용법)

 

Regular Expressions

패턴을 기술하기 위하여 패턴 연산은 메타 캐랙터(metacharacter)를 써서 표기

 

1) .   "\n"을 제외한 모든 글자를 매치시킴

 

2) *   이전의 정규식(regular expression)을 0번 이상 반복하여 매치시킴

 

3) []  character class를 표시: 기술된 글자중 하나를 매치시킴 []안에 기술된 메타캐랙터는 대부분 의미를 상실하나, "^", "-", "\"들은 특별한 의미를 가진다.

       만약 "^"(circumflex)가 첫글자로 나오면 다음의 글자들을 제외하고 매치시킴

        ex) [^\n]

            [^a-zA-Z]

 

       매치시킬 글자의 범위를 표시할 수 있다.

        ex) [0123456789]는 [0-9]로 표시할 수 있다.

 

       이스케이프 문자를 표시할 수 있다.

        ex) [ \t\n] 공백, 탭문자, 뉴라인 문자

            [\40-\176] 아스키 40인 공백부터 176인 "~"까지

 

 

4) ^   입력에서 라인의 첫 자로부터 매치시킨다.

        ex) ^#include       /* C의 전처리 명령 */

 

 

5) $   이전의 정규식을 라인의 끝에서만 매치한다.

        ex) hello$      /* 라인 끝의 hello를 매치한다 */

 

 

6) {}  이전의 정규식을 몇번 매치시킬 것인가를 표시한다.

        ex) ha{1,5}  /* ha, haa, haaa, haaaa, haaaaa를 매치시킨다 */

 

 

7) \   메타캐랙터를 이스케이프 문자로 만든다. C언어의 이스케이프 문자와 같다.

        ex) \n\t

            \*\+\$

 

 

8) +   이전의 정규식을 1번이상 반복하여 매치시킨다.

        ex) [0-9]+

 

 

9) ?   이전의 정규식을 0 또는 1번 매치시킨다.

        ex) -?[0-9]+

 

 

10) |    정규식들을 택일하여 매치시킨다. OR의 의미 이다.

        ex) - | \+

            case | CASE

 

 

11) "..." 리터럴로 표시되어 " "안의 모든 문자가 정규식으로 간주된다. 따라서 모든 메타캐랙터는 의미를 상실한다.

        ex) a"*"b      /* 토큰 a*b를 매치한다 */

 

 

12) /    이전의 정규식은 / 이후의 정규식이 매치될때만 매치된다.

        ex) hello/\n        /* 정규식 hello$와 같다 */

 

 

13) ()   괄호안의 정규식을 한 덩어리로 취급한다. 복잡한 정규식에 *, +, | 등의 연산을 하려할 때 유용하다.

        ex) (ha){1,3}  /* ha, haha, hahaha를 매치한다 */

 

 

정규식 기술의 예

[0-9]

[0-9]+

[0-9]*

-?[0-9]+

[0-9]*\.[0-9]+

([0-9]+)|([0-9]*\.[0-9]+)

-?([0-9]+)|([0-9]*\.[0-9]+)

[eE][-+}?[0-9]+

-?(([0-9]+)|([0-9]*\.[0-9]+)([eE][-+}?[0-9]+)?)

 

수치를 인식하는 렉스 스펙

%%

[\n\t ]       ;

-?(([0-9]+)|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) { printf("number\n"); }

.      ECHO;

%%

main()

{

        yylex();

}

 

라인 첫글자가 "#"이면 코멘트 시작

#.*

 

C의 스트링

\"[^"\n]*["\n]

\".*\"            /* "how" to "do"를 인식할까? */

\"[^"]*\"         /* \n이 나올 때까지 "이 나오지 않으면... */

 

 

단어세기

유닉스의 wc와 같은 기능을 가지는 단어세는 프로그램

 

definition section

%{

unsigned charCount = 0, wordCount = 0, lineCount = 0;

%}

word [^ \t\n]+

eol  \n

 

rule section

%%

{word}{ wordCount++; charCount += yyleng; }

{eol}  { charCount++; lineCount++; }

.      charCount++;

 

user subroutine section

%%

main(argc,argv)

int argc;

char **argv;

{

        if (argc > 1) {

                FILE *file;

                file = fopen(argv[1], "r");

                if (!file) {

                        fprintf(stderr,"could not open %s\n",argv[1]);

                        exit(1);

                }

                yyin = file;

        }

        yylex();

        printf("%d %d %d\n",charCount, wordCount, lineCount);

        return 0;

}

 

% ch2-02 ch2-02.l

467  72  30

 

Using Yacc

 

·         Writing a Yacc Specification

 문법 규칙 및 행동

symbol:     definition

            {action}

            ;

·         Yacc Spec의 형식

declarations

%%

grammar rules

%%

C programs

선언부 

%token         Declare the names of tokens.

%left          Definite left-associative operators.

%right         Define right-associative operators.

%nonassoc      Define operators that may not associate with themselves.

%type          Declare the type of nonterminals.

%union         Declare multiple data types for semantic values.

%start         Declare the start symbol. Default is first in rules section.

%prec          Assign precedence to a rule.

 

%{

C declarations

%}

) 정수 인식

$ cat print_int.y

%token INTEGER

%%

lines:      /* empty */

     | lines line

     { printf("= %d\n", $2);  }

     ;

line:   INTEGER   '\n'

     { $$ = $1; }

     ;

%%

#include "lex.yy.c"

$ lex print-int.l

$ yacc print-int.y

$ cc -o print-int y.tab.c -ly -ll

 

$ print-int

3

= 3

15

= 15

6

= 6

zippy

syntax error

$

A Specification for a Simple Adding Machine

%{

int sum_total = 0;

%}

%token INTEGER

%%

lines:   /* empty */

     | lines line

     ;

line:     '\n'

     |  exp '\n'

     { printf("= %d\n", sum_total); }

     ;

exp:  INTEGER            {sum_total += $1; }

    | '+' INTEGER           {sum_total += $2; }

    | '-' INTEGER           {sum_total -= $2; }

    | '=' INTEGER           {sum_total = $2; }

    | '='                   {sum_total = 0; }

    ;

%%

#include "lex.yy.c"

 

Writing the Lexical Analyzer

 

%%

[0-9]+    {

          sscanf(yytext, %d", &yylval);

          return (INTEGER);

          }

\n        return ('\n');

[-+=]     return yytext[0];

quit      return 0;

.         ;

 

 

Creating the Parser

 

$ lex addup.l

$ yacc addup.y

$ cc -o addup y.tab.c -ly -ll

 

$ addup

3

= 3

5

= 8

+4

= 12

-2

= 10

= 0

= 0

4

= 4

250

= 254

= 100

= 100

-50

= 50

quit

$

 

계산기 만들기(Building a Calculator)

 

계산기를 위한 인터프리터를 구성하여

    실질적인 계산기를 개발

 

   계산기는 인터프리터의 한 종류

     ex) 3 + 4 = 7

  

4칙 연산 기능은 필수

 

   어휘분석기(lexical analyzer): 입력 글자를 토큰 스트림으로

   ex)  36.7 + 43.2 입력

       REAL PLUS REAL 의 토큰 스트림 출력

 

   파서(parser): 적법한 수식으로 인식하기 위한 규칙을 가짐

 

      ex) rexpr REAL | rexpr '+' rexpr

 

   정의된 값을 반환

      ex) $$ = $1 + $3;

 

   expression 뒤의 new line을 인식할 규칙이 필요함

 

     ex) line '\n' | rexpr '\n'

 

 

Writing Regular Expressions for Tokens

 

   2 types of operands : integer & real number

 

   ex) valid real numbers

    3.1415926

       2.718281828

       6.02E28

 

   regular expression으로

     [0-9]+

 

([0-9]*"."[0-9]+) | ([0-9]*"."[0-9]+[eE][+-]?[0-9]+)

[0-9]*"."[0-9]+

[0-9]*"."[0-9]+[eE][+-]?[0-9]+

 

대치 스트링의 정의

형식:

name   translation

 

참조:

{name}

 

오퍼랜드 정의

integer    [0-9]+

dreal    ([0-9]*"."[0-9]+)

ereal    ([0-9]*"."[0-9]+[eE][+-]?[0-9]+)

real         {dreal} | {ereal}

n1           \n

 

 

Defining the Token Types

 

   token valueyylval을 통해 전달

   How to define different token type?

   ex) yylvaldouble

  %{

   #define YYSTYPE double

  %}

 

   ex) token value가 두가지 이상의 type가지기

   %union {

       double  real;       /*  real value  */

       int   integer;      /*  integer value  */

       }

 

   ex) yacc spec 밖에 정의

   typedef union  {

      double  real;       /*  real value  */

           int  integer;       /*  integer value  */

      }  YYSTYPE

 

   union member 알리기 :  < > 안에 표기

   ex)

   %token <real> REAL

     %token <integer> INTEGER

 

   nonterminal symboltype 알리기 : %type

   ex)

   %type <real> rexpr

   %type <integer> iexpr

 

   ex) yylvalmember 표시

    {

sscanf(yytext,"%lf", &yylval.real);

    return REAL;

      }

 

 

완성된 Lex Spec

%{

#include "y.tab.h"

%}

integer  [0-9]+

dreal    ([0-9]*"."[0-9]+)

ereal    ([0-9]*"."[0-9]+[eE][+-]?[0-9]+)

real     {dreal}|{ereal}

n1       \n

%%

[ \t]+    ;

integer  {sscanf(yytext, "%d", &yylval.integer);

            return INTEGER;

           }

real     {sscanf(yytext,"%lf", &yylval.real);

            return REAL;}

n1       {extern int lineno; lineno++;

            return '';

           }

.          {return yytext[0];}

%%

 

   header file을 생성시키기 위하여 yacc option으로 -d 사용하여야 함

 

 

Yacc Spec만들기

 

declaration section

%{

#include <stdio.h>

%}

 

%union  {

    double  real;      /* real value */

    int  integer;      /* integer value */

   }

 

%token <real> REAL

%token <integer> INTEGER

 

%type <real> rexpr

%type <integer> iexpr

 

%left '+' '-'    /* define associativity */

%left '*' '/'    /* define precedence, lowest to highest */

%left UMINUS

 

 

rule section

 

lines:  /* nothing */

| lines  line

;

line:  ‘\n’

| iexpr ‘\n’

   { printf(“%d\n”, $1);}

| rexpr ‘\n’

   { printf(“%15.8lf\n”, $1);}

| error ‘\n’

   {yyerror;}

;

iexpr: INTEGER

     | iexpr '+' iexpr

       { $$ = $1 + $3; }

     | iexpr '-' iexpr

        {$$ = $1 - $3; }

     | iexpr '*' iexpr

        {$$ = $1 * $3; }

     | iexpr '/' iexpr

       { if ($3) $$ = $1 / $3;

         else {

               fprintf(stderr,"divide by zero\n");

               yyerror;

              }

       }

     | '-' iexpr %prec UMINUS  /* tell yacc binds with      */

                                 /*  the specified precedence */

        {$$ = - $2; }

     | '(' iexpr ')'

        {$$ = $2; }

     ;

 

rexpr:  REAL

      | rexpr '+' rexpr

                 {$$ = $1 + $3; }

      | rexpr '-' rexpr

                 {$$ = $1 - $3; }

      | rexpr '*' rexpr

                 {$$ = $1 * $3; }

      | rexpr '/' rexpr

                 {if($3) $$ = $1 / $3;

                  else {

                        fprintf(stderr,"divide by zero\n");

                        yyerror;

                       }

                 }

       | '-' rexpr %prec UMINUS

                 {$$ = - $2; }

       | '(' rexpr ')'

                 {$$ = $2; }

       | iexpr '+' rexpr

                 {$$ = (double)$1 + $3; }

       | iexpr '-' rexpr

                 {$$ = (double)$1 - $3; }

       | iexpr '*' rexpr

                 {$$ = (double)$1 * $3; }

       | iexpr '/' rexpr

                 {if($3) $$ = (double)$1 / $3;

                  else {

                        fprintf(stderr,"divide by zero\n");

                        yyerror;

                       }

                 }

        | rexpr '+' iexpr

                  {$$ = $1 + (double)$3; }

        | rexpr '-' iexpr

                  {$$ = $1 - (double)$3; }

        | rexpr '*' iexpr

                  {$$ = $1 * (double)$3; }

        | rexpr '/' iexpr

                  {if($3) $$ = $1 / (double)$3;

                   else {

                         fprintf(stderr,"divide by zero\n");

                         yyerror;

                        }

                  }

         ;

code section

 

char *progname;

int lineno;

 

main(int argc, char **argv) {

   progname = argv[0];

  

   yyparse();

}

yyerror(char *s) {  /* print warning message  */

   fprintf(stderr, "%s: %s", progname, s);

   fprintf(stderr, "line %d\n", lineno);

}

 

 

Compilation

 

$ lex calc.l

$ yacc -d calc.y

$ cc -o calc y.tab.c lex.yy.c -ly -ll

 

 

Showing the Results

% calc

23 * 34   

782

14 + 5

19

12 * 23.3

   279.60000000

1 / 0

divide by zero

1

1 + 0

1

2.3 * 3.2

    7.36000000

3.14 * 45

  141.30000000

255 * 255

65025

255 * 255 + (3.2 * 4.3)

 65038.76000000

1024 * 1024

1048576

[출처] 6. LEX & YACC|작성자 에러

Lex와 Yacc의 사용법 강좌

 


작성자 : 한국정보통신대학원대학교
           
컴퓨터 및 정보시스템 그룹
           
시스템 소프트웨어 연구실 병렬화 컴파일러팀 김홍숙

작성자 Email : kimkk@icu.ac.kr

문서의 목적:
본 문서는 컴파일러 제작과정중 다른 단계의 기초가 되는 font-end인 어휘 분석기와 구문 분석기를 lex, yacc 툴을 이용하여 작성하는 방법에 대하여 설명한다. 본 문서는 문서의 목적과 작성자에 대한 정보가 포함되어 있다면, 상업적인 목적을 제외한 모든 경우에 대하여 자유로이 사용, 변경, 배포할 수 있다. 작성자는 본 문서의 내용을 있는 그대로만 제공하며, 본 강좌의 내용에 따른 발생 가능한 모든 오류에 대한 책임을 지지 않는다. 다만 내용상의 문제점이나 기술된 내용이 부적절한 경우 작성자에게 전자메일을 통하여 해당 내용을 알려주면, 최선을 다하여 빠른 시간 내에 검토 수정을 할 것이다.

문서 history : 1996.12.24  WfMC WPDL작업 메모 중 lex yacc사용법 정리
                    2000.03.13   ICU 2000년 ICE501 PL과목 강의를 위한 revision
                    2000.03.16  예제보강 및 참고 문헌, 관련 링크 추가  



-목         차 -

1. 컴파일러의 개요 및 Front-end

1.1 Font-end ( tokenizing and parsing)


2. 어휘 분석기 생성 툴 lex (lexical analyzer)

2.1 Definition Section

2.2 Rule Section

2.3 User  Subroutine Section

2.4 컴파일 과정


3. 구문 분석기 생성툴 Yacc (yet another compiler compiler)

3.1문법(Grammar)

3.2 yacc specification 파일 구성

3.3 Definition Section

3.4 Rule Section

3.5 User Subroutine Section

3.6  yacc lex간의 인터페이스


4.  Lex Yacc 컴파일 과정 정리 및 자동화

4.1 Makefile의 이용


5. 참고 문헌 및 관련 링크

[출처] Lex와 Yacc의 사용법 강좌|작성자 제이씨피

+ Recent posts