Lex와 Yacc는 개념이 나온지는 오래되었으며, 보통 Compiler 과목 시간에 배우는 개념이겠다.
그리고 Compiler Design 을 위해서 많이 사용되어지지만, 원대한 꿈을 가지고 Compiler 만들기로만 사용하기에는 그 기능이 너무 좋다.
- Compiler Design
- Interpreter Design (Script Language 설계) 다양하게 이용이 가능
- Assembler / Config Design 가능
- Hardware 제어(DTC) 와 HAL
- Parser Design 구현
- Protocol Paser : SIP, RTSP,JSON 등
내가 다시 관심을 크게 가지게 된것은 DTS(Device Tree Source) 이고, DTC(Device Tree Compiler) , libfdt 때문이다.
그리고 다른 HAL (Hardware Abstract Layer) Compiler를 비롯하여 간단한 Compiler들 때문이다.
만약 가능하다면, Device Tree 같은 Compiler를 간단히 구현을 하고싶다.
- Linux의 Lex와 Yacc (Flex, Bison)
Makefile에서 Lex와 Yacc대신 Flex와 Bison을 설정해서 사용하면 될 것이다.
- Lex 와 Yacc 기본역할
- Lex(Flex)의 역할은 입력문자열에 대한 검색역할을 담당 ( Lex scanner)
- Yacc(Bison)는 실제적인 문자열의 분석을 담당 (Yacc parser)
1.1 Lex와 Yacc 한글관련 자료
Lex와 Yacc의 관련된 기능문서 및 관련자료는 이곳에 Link하고 관련 Link를 소개하겠다.
- Lex와 Yacc 한글문서 및 예제
https://wiki.kldp.org/KoreanDoc/html/Lex_Yacc-KLDP/Lex_Yacc-KLDP.html#toc1
https://github.com/swmaestro06-apus/apus/wiki/Lex-&-Yacc-(flex-&-bison)-%EB%82%B4%EC%9A%A9-%EC%A0%95%EB%A6%AC
- Lex와 Yacc 한글문서 및 관련예제
https://www.joinc.co.kr/w/Site/Development/Env/Yacc
1.2 Flex 와 Bison Manual
상위 문서들은 빠른 이해에 도움이 되겠지만 직접 Manual을 읽는 것이 상세한 설정 및 사용법에 더 도움이 더 되는 것 같다.
http://www.epaperpress.com/lexandyacc/index.html
https://www.tutorialspoint.com/compiler_design/index.htm
1.2 Flex 와 Bison Manual
상위 문서들은 빠른 이해에 도움이 되겠지만 직접 Manual을 읽는 것이 상세한 설정 및 사용법에 더 도움이 더 되는 것 같다.
- Lex와 yacc Manual
http://www.epaperpress.com/lexandyacc/index.html
- Compiler의 디자인 방법
https://www.tutorialspoint.com/compiler_design/index.htm
- Lex 와 Yacc (Flex, Bison) 전체 Manual
하지만 나중에 Link가 끊기면 아래로 별도의 사이트로 연결하자.
http://dinosaur.compilertools.net/
http://dinosaur.compilertools.net/flex/index.html
http://dinosaur.compilertools.net/flex/manpage.html
https://www.gnu.org/software/bison/manual/
http://dinosaur.compilertools.net/bison/index.html
2. Flex(Lex)와 Bison(Yacc)의 역할
Lex와 Yacc의 각각의 기본 기능을 알아보고 이를 쉽게 사용을 해보자.
우선 Lex와 Yacc가 Compiler를 만드는 도구이기 때문에 Compiler의 기본 구성을 알아보자.
GCC의 더 깊은 이해하고자 한다면, 예를들면, front-end , back-end 기능 GCC Manual 참조
아래의 사이트에서 컴파일러의 디자인에 대해 자세히 설명을 해주고 있다.
https://www.tutorialspoint.com/compiler_design/compiler_design_overview.htm
https://en.wikipedia.org/wiki/Compiler-compiler
http://dinosaur.compilertools.net/
- Lex와 Yacc의 다양한 예제
- Flex (Lexical Analyzer) Manual
http://dinosaur.compilertools.net/flex/index.html
http://dinosaur.compilertools.net/flex/manpage.html
- Bison ( Parser Generator) Manual
https://www.gnu.org/software/bison/manual/
http://dinosaur.compilertools.net/bison/index.html
2. Flex(Lex)와 Bison(Yacc)의 역할
Lex와 Yacc의 각각의 기본 기능을 알아보고 이를 쉽게 사용을 해보자.
우선 Lex와 Yacc가 Compiler를 만드는 도구이기 때문에 Compiler의 기본 구성을 알아보자.
- Flex (Lexical Analyzer)
입력 문자열을 ‘토큰(Token)’ 단위로 쪼개는 역할을 한다.
즉, “어떤 문자열이 변수인지, 숫자인지, 키워드인지”를 구분한다.
- Bison (Parser Generator)
Flex가 만든 토큰 스트림을 받아, 문법 규칙(Grammar)에 따라 구문을 분석하고
“이 입력이 올바른 문장인가?”, “AST로 어떤 구조를 만들어야 하는가?”를 결정한다.
- Compiler Design 시 Compiler의 구조 (C 언어)
GCC의 더 깊은 이해하고자 한다면, 예를들면, front-end , back-end 기능 GCC Manual 참조
아래의 사이트에서 컴파일러의 디자인에 대해 자세히 설명을 해주고 있다.
https://www.tutorialspoint.com/compiler_design/compiler_design_overview.htm
https://en.wikipedia.org/wiki/Compiler-compiler
[입력 문자열] → [Flex: Tokenizer] → [Bison: Parser] → [C Code]
- 이외 다양한 Parser
2.1 Lexical analysis 의 소개
Lexical analysis or scanner 라고 하며, Flex인 경우는 (The Fast Lexical Analyzer)의 약어이며, C와 C++ 위한 Tool이다.
만약 다른 언어와 조합을 하고자 한다면, 다른 Lexical analysis를 알아보자.
https://en.wikipedia.org/wiki/Lexical_analysis
한글로 번역하고자 하면 어휘분석기 기능이며, 각각의 언어에서 문법요소를 확인하여 어휘최소단위인 Token으로 분리해내는 작업이다.
Lex의 구성은 다음과 같이 구성이 된다고 한다.
Lexical analysis or scanner 라고 하며, Flex인 경우는 (The Fast Lexical Analyzer)의 약어이며, C와 C++ 위한 Tool이다.
만약 다른 언어와 조합을 하고자 한다면, 다른 Lexical analysis를 알아보자.
- Lexical Analysis의 소개
https://en.wikipedia.org/wiki/Lexical_analysis
한글로 번역하고자 하면 어휘분석기 기능이며, 각각의 언어에서 문법요소를 확인하여 어휘최소단위인 Token으로 분리해내는 작업이다.
Lex의 구성은 다음과 같이 구성이 된다고 한다.
- definitions
- %Flex의 기능정의 가능
- 변수 지정도 가능
- %{ }% 안에 C언어의 코드 작성으로 MACRO 및 user code 함수 선언
- rules
- user code section
... definitions ...
# rules에 pattern을 간단하게 하기 위한 선언과 초기 조건
# C코드 삽입시 %{, }% 기호를 표시하고 사이에 쓸 수 있음
%%
... rules ...
# pattern과 action으로 이루어짐
# C코드는 {, }로 감싸서 표시함
%%
... user code section...
# yylex()함수 등 yy 관련과 사용자가 원하는 C 루틴으로 이루어진다.
# 만약 이곳에 생성을 했다면, 상위 %{ }% 안에 선언을 해주자.
- Github의 apus Project (계산기 소스)
어쩔수 없이 이 부분으로 소스 분석을 해야 할 듯하다
https://github.com/swmaestro06-apus/apus/wiki/Lex-&-Yacc-(flex-&-bison)-%EB%82%B4%EC%9A%A9-%EC%A0%95%EB%A6%AC
https://github.com/swmaestro06-apus/apus/wiki/Lex-&-Yacc-(flex-&-bison)-%EB%82%B4%EC%9A%A9-%EC%A0%95%EB%A6%AC