Flex (어휘분석기): 두 판 사이의 차이

위키백과, 우리 모두의 백과사전.
내용 삭제됨 내용 추가됨
Choboty (토론 | 기여)
TedBot (토론 | 기여)
잔글 봇: 틀 이름 및 스타일 정리
18번째 줄: 18번째 줄:
숫자: <code>0-9 {0-9}</code>; 식별자: <code>a-zA-Z {a-zA-Z0-9}</code> 및 키워드: <code>begin</code>, <code>call</code>, <code>const</code>, <code>do</code>, <code>end</code>, <code>if</code>, <code>odd</code>, <code>procedure</code>, <code>then</code>, <code>var</code>, <code>while</code>.
숫자: <code>0-9 {0-9}</code>; 식별자: <code>a-zA-Z {a-zA-Z0-9}</code> 및 키워드: <code>begin</code>, <code>call</code>, <code>const</code>, <code>do</code>, <code>end</code>, <code>if</code>, <code>odd</code>, <code>procedure</code>, <code>then</code>, <code>var</code>, <code>while</code>.


<source lang="c">
<syntaxhighlight lang="c">
%{
%{
#include "y.tab.h"
#include "y.tab.h"
65번째 줄: 65번째 줄:


int yywrap(void){return 1;}
int yywrap(void){return 1;}
</syntaxhighlight>
</source>


== 관련 항목 ==
== 관련 항목 ==

2020년 4월 17일 (금) 23:16 판

flex
개발자Vern Paxson
안정화 버전
2.6.4 / 2017년 5월 6일(6년 전)(2017-05-06)
저장소
운영 체제유닉스 계열
종류어휘 분석기 발생기
라이선스BSD 라이선스
웹사이트flex.sourceforge.net

flex는 《fast lexical analyzer generator》의 줄임말로 lex의 기능을 개선한 자유 소프트웨어이다. 주로 bison과 쌍을 이루어 구문 분석기를 만드는 데 사용된다. flex를 이용하면 C로 구문 문석 코드를 만들 수 있다. 한편 C++ 코드를 만들어 주는 비슷한 기능을 하는 프로그램으로 flex++가 있으며 flex와 함께 배포된다. 작성자는 "Vern Paxson"씨로 1987년도에 처음 만들어졌다.

예제

아래는 PL/0 프로그래밍 언어를 위한 Flex 스캐너의 예이다.

인식되는 토큰은 다음과 같다: '+', '-', '*', '/', '=', '(', ')', ',', ';', '.', ':=', '<', '<=', '<>', '>', '>='; 숫자: 0-9 {0-9}; 식별자: a-zA-Z {a-zA-Z0-9} 및 키워드: begin, call, const, do, end, if, odd, procedure, then, var, while.

%{
#include "y.tab.h"
%}

digit [0-9]
letter [a-zA-Z]

%%
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return TIMES; }
"/" { return SLASH; }
"(" { return LPAREN; }
")" { return RPAREN; }
";" { return SEMICOLON; }
"," { return COMMA; }
"." { return PERIOD; }
":=" { return BECOMES; }
"=" { return EQL; }
"<>" { return NEQ; }
"<" { return LSS; }
">" { return GTR; }
"<=" { return LEQ; }
">=" { return GEQ; }
"begin" { return BEGINSYM; }
"call" { return CALLSYM; }
"const" { return CONSTSYM; }
"do" { return DOSYM; }
"end" { return ENDSYM; }
"if" { return IFSYM; }
"odd" { return ODDSYM; }
"procedure" { return PROCSYM; }
"then" { return THENSYM; }
"var" { return VARSYM; }
"while" { return WHILESYM; }
{letter}({letter}|{digit})* {
                       yylval.id = strdup(yytext);
                       return IDENT;      }
{digit}+ { yylval.num = atoi(yytext);
                       return NUMBER;     }
[ \t\n\r] /* skip whitespace */
. { printf("Unknown character [%c]\n",yytext[0]);
                       return UNKNOWN;    }
%%

int yywrap(void){return 1;}

관련 항목

외부 링크