[Compiler] Flex 실습
2023. 3. 31. 22:24ㆍCS/Compiler
Compiler
miro.com
1. 실습 목표
PROGRAM SAMPLE ;
CONST
M = 5 ;
N = 9 ;
VAR
SUM, MUL : INTEGER ;
BEGIN
SUM := M + N ;
MUL := M * N ;
END.
Pascal 코드의 줄 번호를 붙이는 Scanner 만들기.
2. scanner.l
%{
int lineno = 1;
%}
%%
\n {lineno++; ECHO;}
^.*$ printf("%d\t%s", lineno, yytext);
scanner.l
작성.
yytest
: flex, lex에서 토큰(문자 배열 char array)을 저장하는 버퍼- ECHO : printf("%s", yytext)와 동일한 결과. 출력하는 일이 빈번해서 매크로로 정의하여 제공.
flex scanner.l
//scanner.l:7: EOF encountered inside an action -> 마지막에 비어있는 줄을 추가.
flex scanner.l
//lex.yy.c 생성
gcc lex.yy.c -lfl
//a.out 생성
./a.out < test.p
-lfl
: linker flex library 일듯? 실행파일을 생성할 때 링커에게 flex library를 참조하라고 지시하는 옵션.
Link Options - Using the GNU Compiler Collection (GCC)
Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify will be passed to the linker. The compiler may generate calls to memcmp, memset, memcpy and memmove. These entries are usually resol
gcc.gnu.org
window로 작업하다가 너무 힘들어서 ubuntu(WSL)로 작업함.
사랑해요 리눅스
'CS > Compiler' 카테고리의 다른 글
[Compiler] 구문 분석 (0) | 2023.04.15 |
---|---|
[Compiler] Lex 파일 이해하기 (0) | 2023.04.07 |
[Compiler] Scanner 구현 과정 (0) | 2023.03.31 |
[Compiler] 유한 상태 기계 (0) | 2023.03.31 |
[Compiler] 실습 준비(Windows) (0) | 2023.03.24 |