Flex (어휘분석기)
보이기
개발자 | Vern Paxson |
---|---|
발표일 | around 1987년[1] |
안정화 버전 | 2.6.4
/ 2017년 5월 6일 |
저장소 | |
운영 체제 | 유닉스 계열 |
종류 | 어휘 분석기 발생기 |
라이선스 | BSD 라이선스 |
웹사이트 | flex |
flex는 《fast lexical analyzer generator》의 줄임말로 lex의 기능을 개선한 자유 소프트웨어이다.[2] 주로 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;}
같이 보기
[편집]각주
[편집]- ↑ Levine, John (August 2009). 《flex & bison》. O'Reilly Media. 9쪽. ISBN 978-0-596-15597-1.
In about 1987, Vern Paxson of the Lawrence Berkeley Lab took a version of lex written in ratfor (an extended Fortran popular at the time) and translated it into C, calling it flex, for 'Fast Lexical Analyzer Generator.'
- ↑ Levine, John R.; Mason, Tony; Brown, Doug (1992). 《lex & yacc》 2판. O'Reilly. 279쪽. ISBN 1-56592-000-7.
A freely available version of lex is flex.
외부 링크
[편집]- Flex
- 공식 웹사이트
- (영어) ANSI C lex 정의