The rest of process source program Lex lexical














![Lex/Yacc 사용법 – Visual Studio. NET 2005 5. Parser generator로 컴파일 - [Project] > Lex/Yacc 사용법 – Visual Studio. NET 2005 5. Parser generator로 컴파일 - [Project] >](https://slidetodoc.com/presentation_image_h/d44b2bf336767e4adc3e2b40bbf19b8d/image-15.jpg)


- Slides: 17
The rest of process source program ‘Lex’를 이용하여 생성 lexical analyzer ‘Yacc’ 을 이용하여 생성 symbol-table manager syntax analyzer semantic analyzer intermediate code generator error handler code optimizer code generator target program Artificial Intelligence Laboratory
Lex를 이용한 lexical analyzer 생성(1/3) Artificial Intelligence Laboratory
Yacc을 이용한 Syntax Analyzer 생성(1/2) Artificial Intelligence Laboratory
lex와 yacc의 결합 Artificial Intelligence Laboratory
Lex와 Yacc을 이용한 Calculator 구현 § Yacc이 생성할 parser에 토근을 제공하기 위한 lex %{ #include “calc. tab. h” extern int yylval; %} 패턴 동작 %% [0 -9]+ { yylval = atoi(yytext); return NUMBER; } [ ₩t]; ₩n return 0; ₩. return yytext[0]; %% 정의 규칙 Artificial Intelligence Laboratory
Lex와 Yacc을 이용한 Calculator 구현 § Calculator 문법을 가진 Yacc input specification %token NAME NUMBER %left '-' '+' %left '*' '/' %nonassoc UMINUS %% statement: NAME '=' expression | expression { printf("= %d₩n", ; expression: expression '+' expression { $$ | expression '-' expression { $$ | expression '*' expression { $$ | expression '/' expression {if($3 정의 $1); } = $1 + $3; } = $1 - $3; } = $1 * $3; } == 0) yyerror("divide by zero"); else $$ = $1 / $3; | | | ; } '-' expression %prec UMINUS { $$ = -$2; } '(' expression ')' { $$ = $2; } NUMBER { $$ = $1; } 규칙 %% Artificial Intelligence Laboratory
Lex/Yacc 사용법 – Visual Studio. NET 2005 1. Parser Generator 설치 - pargen. zip 의 압축을 푼다. [DISK 1]의 설치 파일을 실행한다. 라이브러리 libcd. lib 을 C: ₩Par. Gen₩LIB₩MSDEV 폴더에 저장 한다. Artificial Intelligence Laboratory
Lex/Yacc 사용법 – Visual Studio. NET 2005 2. Visual Studio 2005의 프로젝트 생성 - 응용 프로그램 종류: 콘솔 응용 프로그램 추가옵션: 빈프로젝트 Artificial Intelligence Laboratory
Lex/Yacc 사용법 – Visual Studio. NET 2005 5. Parser generator로 컴파일 - [Project] > [Compile File] 로 calc. y 컴파일. calc. c와 calc. h가 생성됨. calc. c -> calc. tab. c, calc. h -> calc. tab. h로 파일명 변경 § § - lex 컴파일 결과와의 중복을 피하기 위해 변경 calc. tab. h는 lex에서 이용 [Project] > [Compile File] 로 calc. l calc. c와 calc. h가 생성됨. 컴파일. Artificial Intelligence Laboratory
Lex/Yacc 사용법 – Visual Studio. NET 2005 6. Visual Studio로 컴파일 - calc. c, calc. h, calc. tab. c, calc. tab. h를 프로젝트에 추가 빌드 후 실행 Artificial Intelligence Laboratory