Qaupot Blog
Software Engineering, Trip

flex & bison with C++ (win32).

http://research.engineering.wustl.edu/~beardj/FlexBisonC++.html ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

2013.06.02์ผ ๊ธฐ์ค€์œผ๋กœ,
win32 ํ™˜๊ฒฝ์—์„œ ์“ธ ์ˆ˜ ์žˆ๋Š” flex๋Š” gnuwin32์˜ 2.5.4 (๋ฌด๋ ค 2004๋…„ ๋ฆด๋ฆฌ์ฆˆ์ž…๋‹ˆ๋‹ค)
http://gnuwin32.sourceforge.net/packages/flex.htm

bison๋„ gnuwin32์˜ 2.4.1 (2009๋…„ ๋ฆด๋ฆฌ์ฆˆ) http://gnuwin32.sourceforge.net/packages/bison.htm

flex์™€ bison์€ ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” C์–ธ์–ด๋กœ ๋œ ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด๋ƒ…๋‹ˆ๋‹ค.
์˜ต์…˜์— ์˜ํ•ด C++๋กœ ๋žฉํ•‘๋œ ๊ฒฐ๊ณผ๋ฌผ์„ ์–ป์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋‹ค์†Œ ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค.
flex 2.5.4์—์„œ ๋งŒ๋“ค์–ด๋‚ด๋Š” C++ ๊ทœ๊ฒฉ์€ <iostream.h>๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
<iostream>๊ณผ ๋ฏธ๋ฌ˜ํ•˜๊ฒŒ ๋‹ค๋ฅผ ๋ฟ๋”๋Ÿฌ, ์ด ํ—ค๋” ์š”์ฆ˜ ์ปดํŒŒ์ผ๋Ÿฌ์—๋Š” ์•ˆ ๋“ค์–ด์žˆ์Šต๋‹ˆ๋‹ค.
bison์˜ ๊ฒฝ์šฐ๋Š” ๋ณ„ ๋ฌธ์ œ์—†์ด C++๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์ง€๋งŒ, flex 2.5.4์— ์žˆ๋Š” yylex์™€ ๊ทœ๊ฒฉ์ด ์•ˆ ๋งž์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์— ๋‹ค๋ฅธ ์ƒ์„ธ ๊ตฌํ˜„์‚ฌํ•ญ๋“ค์„ C++๋กœ ๋งŒ๋“ค์–ด์„œ ์–น๊ณ  ์‹ถ๋‹ค๋ฉด
๊ฐ€์žฅ ๋จผ์ € ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋Š”๊ฒƒ์€ extern "C"๋ฅผ ๋ช…์‹œํ•ด์ฃผ๊ณ  C ํ•จ์ˆ˜๋“ค์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
๋ฌธ์ œ๋Š” C ์ชฝ์—์„œ C++๋กœ ์ž‘์„ฑ๋œ ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค๋ฅผ ์ ‘๊ทผํ•  ๋ฐฉ๋ฒ•์ด ์—†๋‹ค๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

๊ฒฐ๊ตญ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ด๋ฅผ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€, ๊ทธ๋ƒฅ ํŒŒ์ผ ์ด๋ฆ„์„ ๋ฐ”๊ฟ”์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
flex์˜ lํŒŒ์ผ์—๋Š” "%option outfile=ํŒŒ์ผ๋ช….yy.cpp"๊ณผ ๊ฐ™์€ ์˜ต์…˜์œผ๋กœ ํŒŒ์ผ ํ™•์žฅ์ž๋ฅผ cpp๋กœ ๊ณ ์ •ํ•ฉ๋‹ˆ๋‹ค.
bison์—๋Š” "bison ํŒŒ์ผ๋ช….y -d -o ํŒŒ์ผ๋ช….tab.cpp"์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ˜ธ์ถœํ•˜์—ฌ ํ™•์žฅ์ž๋ฅผ cpp๋กœ ๊ณ ์ •ํ•ฉ๋‹ˆ๋‹ค.

  • ํ—ค๋”ํŒŒ์ผ์€ ์ž๋™์œผ๋กœ hppํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค

๊ฐ„๋‹จํ•œ batch ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋นŒ๋“œ ์ „ ์ด๋ฒคํŠธ๋กœ ์˜ฌ๋ ค๋†“๋Š”๋‹ค๋ฉด ๋” ํŽธ๋ฆฌํ•˜๊ฒ ์ง€์š”.

flex -L ํŒŒ์ผ๋ช….l  
bison ํŒŒ์ผ๋ช….y -d -o ํŒŒ์ผ๋ช….tab.cpp
์ด ๋ธ”๋กœ๊ทธ๋Š” ๊ฐœ์ธ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค. ๊ฒŒ์‹œ๊ธ€์€ ์˜ค๋ฅ˜๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์„ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ €์ž๋Š” ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
๊ฒŒ์‹œ๊ธ€์— ๋ณ„๋„์˜ ๊ณ ์ง€๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ, ํฌ๋ฆฌ์—์ดํ‹ฐ๋ธŒ ์ปค๋จผ์ฆˆ ์ €์ž‘์žํ‘œ์‹œ-๋น„์˜๋ฆฌ-๋ณ€๊ฒฝ๊ธˆ์ง€ 4.0 ๋ผ์ด์„ ์Šค๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

This blog is personal blog. published posts may contain some errors, but author doing efforts to clear errors.
If post have not notice of license, it under creative commons Attribution-NonCommercial-NoDerivatives 4.0.