flex_1.html 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079
  1. <html><head><title>flex</title></head><body>
  2. <ul>
  3. </ul><H2>NAME </H2><ul>
  4. flex - fast lexical analyzer generator
  5. </ul><H2>SYNOPSIS </H2><ul>
  6. <b>flex</b>
  7. <b>[-bcdfhilnpstvwBFILTV78+?</b> <b>-C[aefFmr]</b> <b>-ooutput</b> <b>-Pprefix</b> <b>-Sskeleton]</b>
  8. <b>[--help</b> <b>--version]</b>
  9. <i>[filename</i> <i>...]</i>
  10. </ul><H2>OVERVIEW </H2><ul>
  11. This manual describes
  12. <i>flex,</i>
  13. a tool for generating programs that perform pattern-matching on text. The
  14. manual includes both tutorial and reference sections:
  15. <pre>
  16. <p><br> Description
  17. <br> a brief overview of the tool
  18. <br>
  19. <p><br> Some Simple Examples
  20. <br>
  21. <p><br> Format Of The Input File
  22. <br>
  23. <p><br> Patterns
  24. <br> the extended regular expressions used by flex
  25. <br>
  26. <p><br> How The Input Is Matched
  27. <br> the rules for determining what has been matched
  28. <br>
  29. <p><br> Actions
  30. <br> how to specify what to do when a pattern is matched
  31. <br>
  32. <p><br> The Generated Scanner
  33. <br> details regarding the scanner that flex produces;
  34. <br> how to control the input source
  35. <br>
  36. <p><br> Start Conditions
  37. <br> introducing context into your scanners, and
  38. <br> managing "mini-scanners"
  39. <br>
  40. <p><br> Multiple Input Buffers
  41. <br> how to manipulate multiple input sources; how to
  42. <br> scan from strings instead of files
  43. <br>
  44. <p><br> End-of-file Rules
  45. <br> special rules for matching the end of the input
  46. <br>
  47. <p><br> Miscellaneous Macros
  48. <br> a summary of macros available to the actions
  49. <br>
  50. <p><br> Values Available To The User
  51. <br> a summary of values available to the actions
  52. <br>
  53. <p><br> Interfacing With Yacc
  54. <br> connecting flex scanners together with yacc parsers
  55. <br>
  56. <p><br> Options
  57. <br> flex command-line options, and the "%option"
  58. <br> directive
  59. <br>
  60. <p><br> Performance Considerations
  61. <br> how to make your scanner go as fast as possible
  62. <br>
  63. <p><br> Generating C++ Scanners
  64. <br> the (experimental) facility for generating C++
  65. <br> scanner classes
  66. <br>
  67. <p><br> Incompatibilities With Lex And POSIX
  68. <br> how flex differs from AT&amp;T lex and the POSIX lex
  69. <br> standard
  70. <br>
  71. <p><br> Diagnostics
  72. <br> those error messages produced by flex (or scanners
  73. <br> it generates) whose meanings might not be apparent
  74. <br>
  75. <p><br> Files
  76. <br> files used by flex
  77. <br>
  78. <p><br> Deficiencies / Bugs
  79. <br> known problems with flex
  80. <br>
  81. <p><br> See Also
  82. <br> other documentation, related tools
  83. <br>
  84. <p><br> Author
  85. <br> includes contact information
  86. <br>
  87. <p><br></pre>
  88. </ul><H2>DESCRIPTION </H2><ul>
  89. <i>flex</i>
  90. is a tool for generating
  91. <i>scanners:</i>
  92. programs which recognized lexical patterns in text.
  93. <i>flex</i>
  94. reads
  95. the given input files, or its standard input if no file names are given,
  96. for a description of a scanner to generate. The description is in
  97. the form of pairs
  98. of regular expressions and C code, called
  99. <i>rules.</i> <i>flex</i>
  100. generates as output a C source file,
  101. <b>lex.yy.c,</b>
  102. which defines a routine
  103. <b>yylex().</b>
  104. This file is compiled and linked with the
  105. <b>-lfl</b>
  106. library to produce an executable. When the executable is run,
  107. it analyzes its input for occurrences
  108. of the regular expressions. Whenever it finds one, it executes
  109. the corresponding C code.
  110. </ul><H2>SOME SIMPLE EXAMPLES </H2><ul>
  111. <p>
  112. First some simple examples to get the flavor of how one uses
  113. <i>flex.</i>
  114. The following
  115. <i>flex</i>
  116. input specifies a scanner which whenever it encounters the string
  117. "username" will replace it with the user's login name:
  118. <pre>
  119. <p><br> %%
  120. <br> username printf( "%s", getlogin() );
  121. <br>
  122. <p><br></pre>
  123. By default, any text not matched by a
  124. <i>flex</i>
  125. scanner
  126. is copied to the output, so the net effect of this scanner is
  127. to copy its input file to its output with each occurrence
  128. of "username" expanded.
  129. In this input, there is just one rule. "username" is the
  130. <i>pattern</i>
  131. and the "printf" is the
  132. <i>action.</i>
  133. The "%%" marks the beginning of the rules.
  134. <p>
  135. Here's another simple example:
  136. <pre>
  137. <p><br> int num_lines = 0, num_chars = 0;
  138. <br>
  139. <p><br> %%
  140. <br> \n ++num_lines; ++num_chars;
  141. <br> . ++num_chars;
  142. <br>
  143. <p><br> %%
  144. <br> main()
  145. <br> {
  146. <br> yylex();
  147. <br> printf( "# of lines = %d, # of chars = %d\n",
  148. <br> num_lines, num_chars );
  149. <br> }
  150. <br>
  151. <p><br></pre>
  152. This scanner counts the number of characters and the number
  153. of lines in its input (it produces no output other than the
  154. final report on the counts). The first line
  155. declares two globals, "num_lines" and "num_chars", which are accessible
  156. both inside
  157. <b>yylex()</b>
  158. and in the
  159. <b>main()</b>
  160. routine declared after the second "%%". There are two rules, one
  161. which matches a newline ("\n") and increments both the line count and
  162. the character count, and one which matches any character other than
  163. a newline (indicated by the "." regular expression).
  164. <p>
  165. A somewhat more complicated example:
  166. <pre>
  167. <p><br> /* scanner for a toy Pascal-like language */
  168. <br>
  169. <p><br> %{
  170. <br> /* need this for the call to atof() below */
  171. <br> #include &lt;math.h&gt;
  172. <br> %}
  173. <br>
  174. <p><br> DIGIT [0-9]
  175. <br> ID [a-z][a-z0-9]*
  176. <br>
  177. <p><br> %%
  178. <br>
  179. <p><br> {DIGIT}+ {
  180. <br> printf( "An integer: %s (%d)\n", yytext,
  181. <br> atoi( yytext ) );
  182. <br> }
  183. <br>
  184. <p><br> {DIGIT}+"."{DIGIT}* {
  185. <br> printf( "A float: %s (%g)\n", yytext,
  186. <br> atof( yytext ) );
  187. <br> }
  188. <br>
  189. <p><br> if|then|begin|end|procedure|function {
  190. <br> printf( "A keyword: %s\n", yytext );
  191. <br> }
  192. <br>
  193. <p><br> {ID} printf( "An identifier: %s\n", yytext );
  194. <br>
  195. <p><br> "+"|"-"|"*"|"/" printf( "An operator: %s\n", yytext );
  196. <br>
  197. <p><br> "{"[^}\n]*"}" /* eat up one-line comments */
  198. <br>
  199. <p><br> [ \t\n]+ /* eat up whitespace */
  200. <br>
  201. <p><br> . printf( "Unrecognized character: %s\n", yytext );
  202. <br>
  203. <p><br> %%
  204. <br>
  205. <p><br> main( argc, argv )
  206. <br> int argc;
  207. <br> char **argv;
  208. <br> {
  209. <br> ++argv, --argc; /* skip over program name */
  210. <br> if ( argc &gt; 0 )
  211. <br> yyin = fopen( argv[0], "r" );
  212. <br> else
  213. <br> yyin = stdin;
  214. <br>
  215. <br> yylex();
  216. <br> }
  217. <br>
  218. <p><br></pre>
  219. This is the beginnings of a simple scanner for a language like
  220. Pascal. It identifies different types of
  221. <i>tokens</i>
  222. and reports on what it has seen.
  223. <p>
  224. The details of this example will be explained in the following
  225. sections.
  226. </ul><H2>FORMAT OF THE INPUT FILE </H2><ul>
  227. The
  228. <i>flex</i>
  229. input file consists of three sections, separated by a line with just
  230. <b>%%</b>
  231. in it:
  232. <pre>
  233. <p><br> definitions
  234. <br> %%
  235. <br> rules
  236. <br> %%
  237. <br> user code
  238. <br>
  239. <p><br></pre>
  240. The
  241. <i>definitions</i>
  242. section contains declarations of simple
  243. <i>name</i>
  244. definitions to simplify the scanner specification, and declarations of
  245. <i>start</i> <i>conditions,</i>
  246. which are explained in a later section.
  247. <p>
  248. Name definitions have the form:
  249. <pre>
  250. <p><br> name definition
  251. <br>
  252. <p><br></pre>
  253. The "name" is a word beginning with a letter or an underscore ('_')
  254. followed by zero or more letters, digits, '_', or '-' (dash).
  255. The definition is taken to begin at the first non-white-space character
  256. following the name and continuing to the end of the line.
  257. The definition can subsequently be referred to using "{name}", which
  258. will expand to "(definition)". For example,
  259. <pre>
  260. <p><br> DIGIT [0-9]
  261. <br> ID [a-z][a-z0-9]*
  262. <br>
  263. <p><br></pre>
  264. defines "DIGIT" to be a regular expression which matches a
  265. single digit, and
  266. "ID" to be a regular expression which matches a letter
  267. followed by zero-or-more letters-or-digits.
  268. A subsequent reference to
  269. <pre>
  270. <p><br> {DIGIT}+"."{DIGIT}*
  271. <br>
  272. <p><br></pre>
  273. is identical to
  274. <pre>
  275. <p><br> ([0-9])+"."([0-9])*
  276. <br>
  277. <p><br></pre>
  278. and matches one-or-more digits followed by a '.' followed
  279. by zero-or-more digits.
  280. <p>
  281. The
  282. <i>rules</i>
  283. section of the
  284. <i>flex</i>
  285. input contains a series of rules of the form:
  286. <pre>
  287. <p><br> pattern action
  288. <br>
  289. <p><br></pre>
  290. where the pattern must be unindented and the action must begin
  291. on the same line.
  292. <p>
  293. See below for a further description of patterns and actions.
  294. <p>
  295. Finally, the user code section is simply copied to
  296. <b>lex.yy.c</b>
  297. verbatim.
  298. It is used for companion routines which call or are called
  299. by the scanner. The presence of this section is optional;
  300. if it is missing, the second
  301. <b>%%</b>
  302. in the input file may be skipped, too.
  303. <p>
  304. In the definitions and rules sections, any
  305. <i>indented</i>
  306. text or text enclosed in
  307. <b>%{</b>
  308. and
  309. <b>%}</b>
  310. is copied verbatim to the output (with the %{}'s removed).
  311. The %{}'s must appear unindented on lines by themselves.
  312. <p>
  313. In the rules section,
  314. any indented or %{} text appearing before the
  315. first rule may be used to declare variables
  316. which are local to the scanning routine and (after the declarations)
  317. code which is to be executed whenever the scanning routine is entered.
  318. Other indented or %{} text in the rule section is still copied to the output,
  319. but its meaning is not well-defined and it may well cause compile-time
  320. errors (this feature is present for
  321. <i>POSIX</i>
  322. compliance; see below for other such features).
  323. <p>
  324. In the definitions section (but not in the rules section),
  325. an unindented comment (i.e., a line
  326. beginning with "/*") is also copied verbatim to the output up
  327. to the next "*/".
  328. </ul><H2>PATTERNS </H2><ul>
  329. The patterns in the input are written using an extended set of regular
  330. expressions. These are:
  331. <pre>
  332. <p><br> x match the character 'x'
  333. <br> . any character (byte) except newline
  334. <br> [xyz] a "character class"; in this case, the pattern
  335. <br> matches either an 'x', a 'y', or a 'z'
  336. <br> [abj-oZ] a "character class" with a range in it; matches
  337. <br> an 'a', a 'b', any letter from 'j' through 'o',
  338. <br> or a 'Z'
  339. <br> [^A-Z] a "negated character class", i.e., any character
  340. <br> but those in the class. In this case, any
  341. <br> character EXCEPT an uppercase letter.
  342. <br> [^A-Z\n] any character EXCEPT an uppercase letter or
  343. <br> a newline
  344. <br> r* zero or more r's, where r is any regular expression
  345. <br> r+ one or more r's
  346. <br> r? zero or one r's (that is, "an optional r")
  347. <br> r{2,5} anywhere from two to five r's
  348. <br> r{2,} two or more r's
  349. <br> r{4} exactly 4 r's
  350. <br> {name} the expansion of the "name" definition
  351. <br> (see above)
  352. <br> "[xyz]\"foo"
  353. <br> the literal string: [xyz]"foo
  354. <br> \X if X is an 'a', 'b', 'f', 'n', 'r', 't', or 'v',
  355. <br> then the ANSI-C interpretation of \x.
  356. <br> Otherwise, a literal 'X' (used to escape
  357. <br> operators such as '*')
  358. <br> \0 a NUL character (ASCII code 0)
  359. <br> \123 the character with octal value 123
  360. <br> \x2a the character with hexadecimal value 2a
  361. <br> (r) match an r; parentheses are used to override
  362. <br> precedence (see below)
  363. <br>
  364. <p><br>
  365. <p><br> rs the regular expression r followed by the
  366. <br> regular expression s; called "concatenation"
  367. <br>
  368. <p><br>
  369. <p><br> r|s either an r or an s
  370. <br>
  371. <p><br>
  372. <p><br> r/s an r but only if it is followed by an s. The
  373. <br> text matched by s is included when determining
  374. <br> whether this rule is the "longest match",
  375. <br> but is then returned to the input before
  376. <br> the action is executed. So the action only
  377. <br> sees the text matched by r. This type
  378. <br> of pattern is called trailing context".
  379. <br> (There are some combinations of r/s that flex
  380. <br> cannot match correctly; see notes in the
  381. <br> Deficiencies / Bugs section below regarding
  382. <br> "dangerous trailing context".)
  383. <br> ^r an r, but only at the beginning of a line (i.e.,
  384. <br> which just starting to scan, or right after a
  385. <br> newline has been scanned).
  386. <br> r$ an r, but only at the end of a line (i.e., just
  387. <br> before a newline). Equivalent to "r/\n".
  388. <br>
  389. <p><br> Note that flex's notion of "newline" is exactly
  390. <br> whatever the C compiler used to compile flex
  391. <br> interprets '\n' as; in particular, on some DOS
  392. <br> systems you must either filter out \r's in the
  393. <br> input yourself, or explicitly use r/\r\n for "r$".
  394. <br>
  395. <p><br>
  396. <p><br> &lt;s&gt;r an r, but only in start condition s (see
  397. <br> below for discussion of start conditions)
  398. <br> &lt;s1,s2,s3&gt;r
  399. <br> same, but in any of start conditions s1,
  400. <br> s2, or s3
  401. <br> &lt;*&gt;r an r in any start condition, even an exclusive one.
  402. <br>
  403. <p><br>
  404. <p><br> &lt;&lt;EOF&gt;&gt; an end-of-file
  405. <br> &lt;s1,s2&gt;&lt;&lt;EOF&gt;&gt;
  406. <br> an end-of-file when in start condition s1 or s2
  407. <br>
  408. <p><br></pre>
  409. Note that inside of a character class, all regular expression operators
  410. lose their special meaning except escape ('\') and the character class
  411. operators, '-', ']', and, at the beginning of the class, '^'.
  412. <p>
  413. The regular expressions listed above are grouped according to
  414. precedence, from highest precedence at the top to lowest at the bottom.
  415. Those grouped together have equal precedence. For example,
  416. <pre>
  417. <p><br> foo|bar*
  418. <br>
  419. <p><br></pre>
  420. is the same as
  421. <pre>
  422. <p><br> (foo)|(ba(r*))
  423. <br>
  424. <p><br></pre>
  425. since the '*' operator has higher precedence than concatenation,
  426. and concatenation higher than alternation ('|'). This pattern
  427. therefore matches
  428. <i>either</i>
  429. the string "foo"
  430. <i>or</i>
  431. the string "ba" followed by zero-or-more r's.
  432. To match "foo" or zero-or-more "bar"'s, use:
  433. <pre>
  434. <p><br> foo|(bar)*
  435. <br>
  436. <p><br></pre>
  437. and to match zero-or-more "foo"'s-or-"bar"'s:
  438. <pre>
  439. <p><br> (foo|bar)*
  440. <br>
  441. <p><br></pre>
  442. <p>
  443. In addition to characters and ranges of characters, character classes
  444. can also contain character class
  445. <i>expressions.</i>
  446. These are expressions enclosed inside
  447. <b>[:</b>
  448. and
  449. <b>:]</b>
  450. delimiters (which themselves must appear between the '[' and ']' of the
  451. character class; other elements may occur inside the character class, too).
  452. The valid expressions are:
  453. <pre>
  454. <p><br> [:alnum:] [:alpha:] [:blank:]
  455. <br> [:cntrl:] [:digit:] [:graph:]
  456. <br> [:lower:] [:print:] [:punct:]
  457. <br> [:space:] [:upper:] [:xdigit:]
  458. <br>
  459. <p><br></pre>
  460. These expressions all designate a set of characters equivalent to
  461. the corresponding standard C
  462. <b>isXXX</b>
  463. function. For example,
  464. <b>[:alnum:]</b>
  465. designates those characters for which
  466. <b>isalnum()</b>
  467. returns true - i.e., any alphabetic or numeric.
  468. Some systems don't provide
  469. <b>isblank(),</b>
  470. so flex defines
  471. <b>[:blank:]</b>
  472. as a blank or a tab.
  473. <p>
  474. For example, the following character classes are all equivalent:
  475. <pre>
  476. <p><br> [[:alnum:]]
  477. <br> [[:alpha:][:digit:]
  478. <br> [[:alpha:]0-9]
  479. <br> [a-zA-Z0-9]
  480. <br>
  481. <p><br></pre>
  482. If your scanner is case-insensitive (the
  483. <b>-i</b>
  484. flag), then
  485. <b>[:upper:]</b>
  486. and
  487. <b>[:lower:]</b>
  488. are equivalent to
  489. <b>[:alpha:].</b>
  490. <p>
  491. Some notes on patterns:
  492. <p><dl compact><dt>-<dd>A negated character class such as the example "[^A-Z]"
  493. above
  494. <i>will</i> <i>match</i> <i>a</i> <i>newline</i>
  495. unless "\n" (or an equivalent escape sequence) is one of the
  496. characters explicitly present in the negated character class
  497. (e.g., "[^A-Z\n]"). This is unlike how many other regular
  498. expression tools treat negated character classes, but unfortunately
  499. the inconsistency is historically entrenched.
  500. Matching newlines means that a pattern like [^"]* can match the entire
  501. input unless there's another quote in the input.
  502. <dt>-<dd>A rule can have at most one instance of trailing context (the '/' operator
  503. or the '$' operator). The start condition, '^', and "&lt;&lt;EOF&gt;&gt;" patterns
  504. can only occur at the beginning of a pattern, and, as well as with '/' and '$',
  505. cannot be grouped inside parentheses. A '^' which does not occur at
  506. the beginning of a rule or a '$' which does not occur at the end of
  507. a rule loses its special properties and is treated as a normal character.
  508. <dt><dd>The following are illegal:
  509. <pre>
  510. <p><br> foo/bar$
  511. <br> &lt;sc1&gt;foo&lt;sc2&gt;bar
  512. <br>
  513. <p><br></pre>
  514. Note that the first of these, can be written "foo/bar\n".
  515. <dt><dd>The following will result in '$' or '^' being treated as a normal character:
  516. <pre>
  517. <p><br> foo|(bar$)
  518. <br> foo|^bar
  519. <br>
  520. <p><br></pre>
  521. If what's wanted is a "foo" or a bar-followed-by-a-newline, the following
  522. could be used (the special '|' action is explained below):
  523. <pre>
  524. <p><br> foo |
  525. <br> bar$ /* action goes here */
  526. <br>
  527. <p><br></pre>
  528. A similar trick will work for matching a foo or a
  529. bar-at-the-beginning-of-a-line.
  530. </dl>
  531. </ul><H2>HOW THE INPUT IS MATCHED </H2><ul>
  532. When the generated scanner is run, it analyzes its input looking
  533. for strings which match any of its patterns. If it finds more than
  534. one match, it takes the one matching the most text (for trailing
  535. context rules, this includes the length of the trailing part, even
  536. though it will then be returned to the input). If it finds two
  537. or more matches of the same length, the
  538. rule listed first in the
  539. <i>flex</i>
  540. input file is chosen.
  541. <p>
  542. Once the match is determined, the text corresponding to the match
  543. (called the
  544. <i>token)</i>
  545. is made available in the global character pointer
  546. <b>yytext,</b>
  547. and its length in the global integer
  548. <b>yyleng.</b>
  549. The
  550. <i>action</i>
  551. corresponding to the matched pattern is then executed (a more
  552. detailed description of actions follows), and then the remaining
  553. input is scanned for another match.
  554. <p>
  555. If no match is found, then the
  556. <i>default</i> <i>rule</i>
  557. is executed: the next character in the input is considered matched and
  558. copied to the standard output. Thus, the simplest legal
  559. <i>flex</i>
  560. input is:
  561. <pre>
  562. <p><br> %%
  563. <br>
  564. <p><br></pre>
  565. which generates a scanner that simply copies its input (one character
  566. at a time) to its output.
  567. <p>
  568. Note that
  569. <b>yytext</b>
  570. can be defined in two different ways: either as a character
  571. <i>pointer</i>
  572. or as a character
  573. <i>array.</i>
  574. You can control which definition
  575. <i>flex</i>
  576. uses by including one of the special directives
  577. <b>%pointer</b>
  578. or
  579. <b>%array</b>
  580. in the first (definitions) section of your flex input. The default is
  581. <b>%pointer,</b>
  582. unless you use the
  583. <b>-l</b>
  584. lex compatibility option, in which case
  585. <b>yytext</b>
  586. will be an array.
  587. The advantage of using
  588. <b>%pointer</b>
  589. is substantially faster scanning and no buffer overflow when matching
  590. very large tokens (unless you run out of dynamic memory). The disadvantage
  591. is that you are restricted in how your actions can modify
  592. <b>yytext</b>
  593. (see the next section), and calls to the
  594. <b>unput()</b>
  595. function destroys the present contents of
  596. <b>yytext,</b>
  597. which can be a considerable porting headache when moving between different
  598. <i>lex</i>
  599. versions.
  600. <p>
  601. The advantage of
  602. <b>%array</b>
  603. is that you can then modify
  604. <b>yytext</b>
  605. to your heart's content, and calls to
  606. <b>unput()</b>
  607. do not destroy
  608. <b>yytext</b>
  609. (see below). Furthermore, existing
  610. <i>lex</i>
  611. programs sometimes access
  612. <b>yytext</b>
  613. externally using declarations of the form:
  614. <pre>
  615. extern char yytext[];
  616. <br></pre>
  617. This definition is erroneous when used with
  618. <b>%pointer,</b>
  619. but correct for
  620. <b>%array.</b>
  621. <p>
  622. <b>%array</b>
  623. defines
  624. <b>yytext</b>
  625. to be an array of
  626. <b>YYLMAX</b>
  627. characters, which defaults to a fairly large value. You can change
  628. the size by simply #define'ing
  629. <b>YYLMAX</b>
  630. to a different value in the first section of your
  631. <i>flex</i>
  632. input. As mentioned above, with
  633. <b>%pointer</b>
  634. yytext grows dynamically to accommodate large tokens. While this means your
  635. <b>%pointer</b>
  636. scanner can accommodate very large tokens (such as matching entire blocks
  637. of comments), bear in mind that each time the scanner must resize
  638. <b>yytext</b>
  639. it also must rescan the entire token from the beginning, so matching such
  640. tokens can prove slow.
  641. <b>yytext</b>
  642. presently does
  643. <i>not</i>
  644. dynamically grow if a call to
  645. <b>unput()</b>
  646. results in too much text being pushed back; instead, a run-time error results.
  647. <p>
  648. Also note that you cannot use
  649. <b>%array</b>
  650. with C++ scanner classes
  651. (the
  652. <b>c++</b>
  653. option; see below).
  654. </ul><H2>ACTIONS </H2><ul>
  655. Each pattern in a rule has a corresponding action, which can be any
  656. arbitrary C statement. The pattern ends at the first non-escaped
  657. whitespace character; the remainder of the line is its action. If the
  658. action is empty, then when the pattern is matched the input token
  659. is simply discarded. For example, here is the specification for a program
  660. which deletes all occurrences of "zap me" from its input:
  661. <pre>
  662. <p><br> %%
  663. <br> "zap me"
  664. <br>
  665. <p><br></pre>
  666. (It will copy all other characters in the input to the output since
  667. they will be matched by the default rule.)
  668. <p>
  669. Here is a program which compresses multiple blanks and tabs down to
  670. a single blank, and throws away whitespace found at the end of a line:
  671. <pre>
  672. <p><br> %%
  673. <br> [ \t]+ putchar( ' ' );
  674. <br> [ \t]+$ /* ignore this token */
  675. <br>
  676. <p><br></pre>
  677. <p>
  678. If the action contains a '{', then the action spans till the balancing '}'
  679. is found, and the action may cross multiple lines.
  680. <i>flex</i>
  681. knows about C strings and comments and won't be fooled by braces found
  682. within them, but also allows actions to begin with
  683. <b>%{</b>
  684. and will consider the action to be all the text up to the next
  685. <b>%}</b>
  686. (regardless of ordinary braces inside the action).
  687. <p>
  688. An action consisting solely of a vertical bar ('|') means "same as
  689. the action for the next rule." See below for an illustration.
  690. <p>
  691. Actions can include arbitrary C code, including
  692. <b>return</b>
  693. statements to return a value to whatever routine called
  694. <b>yylex().</b>
  695. Each time
  696. <b>yylex()</b>
  697. is called it continues processing tokens from where it last left
  698. off until it either reaches
  699. the end of the file or executes a return.
  700. <p>
  701. Actions are free to modify
  702. <b>yytext</b>
  703. except for lengthening it (adding
  704. characters to its end--these will overwrite later characters in the
  705. input stream). This however does not apply when using
  706. <b>%array</b>
  707. (see above); in that case,
  708. <b>yytext</b>
  709. may be freely modified in any way.
  710. <p>
  711. Actions are free to modify
  712. <b>yyleng</b>
  713. except they should not do so if the action also includes use of
  714. <b>yymore()</b>
  715. (see below).
  716. <p>
  717. There are a number of special directives which can be included within
  718. an action:
  719. <p><dl compact><dt>-<dd><b>ECHO</b>
  720. copies yytext to the scanner's output.
  721. <dt>-<dd><b>BEGIN</b>
  722. followed by the name of a start condition places the scanner in the
  723. corresponding start condition (see below).
  724. <dt>-<dd><b>REJECT</b>
  725. directs the scanner to proceed on to the "second best" rule which matched the
  726. input (or a prefix of the input). The rule is chosen as described
  727. above in "How the Input is Matched", and
  728. <b>yytext</b>
  729. and
  730. <b>yyleng</b>
  731. set up appropriately.
  732. It may either be one which matched as much text
  733. as the originally chosen rule but came later in the
  734. <i>flex</i>
  735. input file, or one which matched less text.
  736. For example, the following will both count the
  737. words in the input and call the routine special() whenever "frob" is seen:
  738. <pre>
  739. <p><br> int word_count = 0;
  740. <br> %%
  741. <br>
  742. <p><br> frob special(); REJECT;
  743. <br> [^ \t\n]+ ++word_count;
  744. <br>
  745. <p><br></pre>
  746. Without the
  747. <b>REJECT,</b>
  748. any "frob"'s in the input would not be counted as words, since the
  749. scanner normally executes only one action per token.
  750. Multiple
  751. <b>REJECT's</b>
  752. are allowed, each one finding the next best choice to the currently
  753. active rule. For example, when the following scanner scans the token
  754. "abcd", it will write "abcdabcaba" to the output:
  755. <pre>
  756. <p><br> %%
  757. <br> a |
  758. <br> ab |
  759. <br> abc |
  760. <br> abcd ECHO; REJECT;
  761. <br> .|\n /* eat up any unmatched character */
  762. <br>
  763. <p><br></pre>
  764. (The first three rules share the fourth's action since they use
  765. the special '|' action.)
  766. <b>REJECT</b>
  767. is a particularly expensive feature in terms of scanner performance;
  768. if it is used in
  769. <i>any</i>
  770. of the scanner's actions it will slow down
  771. <i>all</i>
  772. of the scanner's matching. Furthermore,
  773. <b>REJECT</b>
  774. cannot be used with the
  775. <i>-Cf</i>
  776. or
  777. <i>-CF</i>
  778. options (see below).
  779. <dt><dd>Note also that unlike the other special actions,
  780. <b>REJECT</b>
  781. is a
  782. <i>branch;</i>
  783. code immediately following it in the action will
  784. <i>not</i>
  785. be executed.
  786. <dt>-<dd><b>yymore()</b>
  787. tells the scanner that the next time it matches a rule, the corresponding
  788. token should be
  789. <i>appended</i>
  790. onto the current value of
  791. <b>yytext</b>
  792. rather than replacing it. For example, given the input "mega-kludge"
  793. the following will write "mega-mega-kludge" to the output:
  794. <pre>
  795. <p><br> %%
  796. <br> mega- ECHO; yymore();
  797. <br> kludge ECHO;
  798. <br>
  799. <p><br></pre>
  800. First "mega-" is matched and echoed to the output. Then "kludge"
  801. is matched, but the previous "mega-" is still hanging around at the
  802. beginning of
  803. <b>yytext</b>
  804. so the
  805. <b>ECHO</b>
  806. for the "kludge" rule will actually write "mega-kludge".
  807. </dl>
  808. <p>
  809. Two notes regarding use of
  810. <b>yymore().</b>
  811. First,
  812. <b>yymore()</b>
  813. depends on the value of
  814. <i>yyleng</i>
  815. correctly reflecting the size of the current token, so you must not
  816. modify
  817. <i>yyleng</i>
  818. if you are using
  819. <b>yymore().</b>
  820. Second, the presence of
  821. <b>yymore()</b>
  822. in the scanner's action entails a minor performance penalty in the
  823. scanner's matching speed.
  824. <p><dl compact><dt>-<dd><b>yyless(n)</b>
  825. returns all but the first
  826. <i>n</i>
  827. characters of the current token back to the input stream, where they
  828. will be rescanned when the scanner looks for the next match.
  829. <b>yytext</b>
  830. and
  831. <b>yyleng</b>
  832. are adjusted appropriately (e.g.,
  833. <b>yyleng</b>
  834. will now be equal to
  835. <i>n</i>
  836. ). For example, on the input "foobar" the following will write out
  837. "foobarbar":
  838. <pre>
  839. <p><br> %%
  840. <br> foobar ECHO; yyless(3);
  841. <br> [a-z]+ ECHO;
  842. <br>
  843. <p><br></pre>
  844. An argument of 0 to
  845. <b>yyless</b>
  846. will cause the entire current input string to be scanned again. Unless you've
  847. changed how the scanner will subsequently process its input (using
  848. <b>BEGIN,</b>
  849. for example), this will result in an endless loop.
  850. </dl>
  851. <p>
  852. Note that
  853. <b>yyless</b>
  854. is a macro and can only be used in the flex input file, not from
  855. other source files.
  856. <p><dl compact><dt>-<dd><b>unput(c)</b>
  857. puts the character
  858. <i>c</i>
  859. back onto the input stream. It will be the next character scanned.
  860. The following action will take the current token and cause it
  861. to be rescanned enclosed in parentheses.
  862. <pre>
  863. <p><br> {
  864. <br> int i;
  865. <br> /* Copy yytext because unput() trashes yytext */
  866. <br> char *yycopy = strdup( yytext );
  867. <br> unput( ')' );
  868. <br> for ( i = yyleng - 1; i &gt;= 0; --i )
  869. <br> unput( yycopy[i] );
  870. <br> unput( '(' );
  871. <br> free( yycopy );
  872. <br> }
  873. <br>
  874. <p><br></pre>
  875. Note that since each
  876. <b>unput()</b>
  877. puts the given character back at the
  878. <i>beginning</i>
  879. of the input stream, pushing back strings must be done back-to-front.
  880. </dl>
  881. <p>
  882. An important potential problem when using
  883. <b>unput()</b>
  884. is that if you are using
  885. <b>%pointer</b>
  886. (the default), a call to
  887. <b>unput()</b>
  888. <i>destroys</i>
  889. the contents of
  890. <i>yytext,</i>
  891. starting with its rightmost character and devouring one character to
  892. the left with each call. If you need the value of yytext preserved
  893. after a call to
  894. <b>unput()</b>
  895. (as in the above example),
  896. you must either first copy it elsewhere, or build your scanner using
  897. <b>%array</b>
  898. instead (see How The Input Is Matched).
  899. <p>
  900. Finally, note that you cannot put back
  901. <b>EOF</b>
  902. to attempt to mark the input stream with an end-of-file.
  903. <p><dl compact><dt>-<dd><b>input()</b>
  904. reads the next character from the input stream. For example,
  905. the following is one way to eat up C comments:
  906. <pre>
  907. <p><br> %%
  908. <br> "/*" {
  909. <br> register int c;
  910. <br>
  911. <p><br> for ( ; ; )
  912. <br> {
  913. <br> while ( (c = input()) != '*' &amp;&amp;
  914. <br> c != EOF )
  915. <br> ; /* eat up text of comment */
  916. <br>
  917. <p><br> if ( c == '*' )
  918. <br> {
  919. <br> while ( (c = input()) == '*' )
  920. <br> ;
  921. <br> if ( c == '/' )
  922. <br> break; /* found the end */
  923. <br> }
  924. <br>
  925. <p><br> if ( c == EOF )
  926. <br> {
  927. <br> error( "EOF in comment" );
  928. <br> break;
  929. <br> }
  930. <br> }
  931. <br> }
  932. <br>
  933. <p><br></pre>
  934. (Note that if the scanner is compiled using
  935. <b>C++,</b>
  936. then
  937. <b>input()</b>
  938. is instead referred to as
  939. <b>yyinput(),</b>
  940. in order to avoid a name clash with the
  941. <b>C++</b>
  942. stream by the name of
  943. <i>input.)</i>
  944. <dt>-<dd><b>YY_FLUSH_BUFFER</b>
  945. flushes the scanner's internal buffer
  946. so that the next time the scanner attempts to match a token, it will
  947. first refill the buffer using
  948. <b>YY_INPUT</b>
  949. (see The Generated Scanner, below). This action is a special case
  950. of the more general
  951. <b>yy_flush_buffer()</b>
  952. function, described below in the section Multiple Input Buffers.
  953. <dt>-<dd><b>yyterminate()</b>
  954. can be used in lieu of a return statement in an action. It terminates
  955. the scanner and returns a 0 to the scanner's caller, indicating "all done".
  956. By default,
  957. <b>yyterminate()</b>
  958. is also called when an end-of-file is encountered. It is a macro and
  959. may be redefined.
  960. </dl>
  961. </ul><H2>THE GENERATED SCANNER </H2><ul>
  962. The output of
  963. <i>flex</i>
  964. is the file
  965. <b>lex.yy.c,</b>
  966. which contains the scanning routine
  967. <b>yylex(),</b>
  968. a number of tables used by it for matching tokens, and a number
  969. of auxiliary routines and macros. By default,
  970. <b>yylex()</b>
  971. is declared as follows:
  972. <pre>
  973. <p><br> int yylex()
  974. <br> {
  975. <br> ... various definitions and the actions in here ...
  976. <br> }
  977. <br>
  978. <p><br></pre>
  979. (If your environment supports function prototypes, then it will
  980. be "int yylex( void )".) This definition may be changed by defining
  981. the "YY_DECL" macro. For example, you could use:
  982. <pre>
  983. <p><br> #define YY_DECL float lexscan( a, b ) float a, b;
  984. <br>
  985. <p><br></pre>
  986. to give the scanning routine the name
  987. <i>lexscan,</i>
  988. returning a float, and taking two floats as arguments. Note that
  989. if you give arguments to the scanning routine using a
  990. K&amp;R-style/non-prototyped function declaration, you must terminate
  991. the definition with a semi-colon (;).
  992. <p>
  993. Whenever
  994. <b>yylex()</b>
  995. is called, it scans tokens from the global input file
  996. <i>yyin</i>
  997. (which defaults to stdin). It continues until it either reaches
  998. an end-of-file (at which point it returns the value 0) or
  999. one of its actions executes a
  1000. <i>return</i>
  1001. statement.
  1002. <p>
  1003. If the scanner reaches an end-of-file, subsequent calls are undefined
  1004. unless either
  1005. <i>yyin</i>
  1006. is pointed at a new input file (in which case scanning continues from
  1007. that file), or
  1008. <b>yyrestart()</b>
  1009. is called.
  1010. <b>yyrestart()</b>
  1011. takes one argument, a
  1012. <b>FILE</b> <b>*</b>
  1013. pointer (which can be nil, if you've set up
  1014. <b>YY_INPUT</b>
  1015. to scan from a source other than
  1016. <i>yyin),</i>
  1017. and initializes
  1018. <i>yyin</i>
  1019. for scanning from that file. Essentially there is no difference between
  1020. just assigning
  1021. <i>yyin</i>
  1022. to a new input file or using
  1023. <b>yyrestart()</b>
  1024. to do so; the latter is available for compatibility with previous versions
  1025. of
  1026. <i>flex,</i>
  1027. and because it can be used to switch input files in the middle of scanning.
  1028. It can also be used to throw away the current input buffer, by calling
  1029. it with an argument of
  1030. <i>yyin;</i>
  1031. but better is to use
  1032. <b>YY_FLUSH_BUFFER</b>
  1033. (see above).
  1034. Note that
  1035. <b>yyrestart()</b>
  1036. does
  1037. <i>not</i>
  1038. reset the start condition to
  1039. <b>INITIAL</b>
  1040. (see Start Conditions, below).
  1041. <p>
  1042. If
  1043. <b>yylex()</b>
  1044. stops scanning due to executing a
  1045. <i>return</i>
  1046. statement in one of the actions, the scanner may then be called again and it
  1047. will resume scanning where it left off.
  1048. <p>
  1049. By default (and for purposes of efficiency), the scanner uses
  1050. block-reads rather than simple
  1051. <i>getc()</i>
  1052. calls to read characters from
  1053. <i>yyin.</i>
  1054. The nature of how it gets its input can be controlled by defining the
  1055. <b>YY_INPUT</b>
  1056. macro.
  1057. YY_INPUT's calling sequence is "YY_INPUT(buf,result,max_size)". Its
  1058. action is to place up to
  1059. <i>max_size</i>
  1060. characters in the character array
  1061. <i>buf</i>
  1062. and return in the integer variable
  1063. <i>result</i>
  1064. either the
  1065. number of characters read or the constant YY_NULL (0 on Unix systems)
  1066. to indicate EOF. The default YY_INPUT reads from the
  1067. global file-pointer "yyin".
  1068. <p>
  1069. A sample definition of YY_INPUT (in the definitions
  1070. section of the input file):
  1071. <pre>
  1072. <p><br> %{
  1073. <br> #define YY_INPUT(buf,result,max_size) \
  1074. <br> { \
  1075. <br> int c = getchar(); \
  1076. <br> result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \
  1077. <br> }
  1078. <br> %}
  1079. <br>
  1080. <p><br></pre>
  1081. This definition will change the input processing to occur
  1082. one character at a time.
  1083. <p>
  1084. When the scanner receives an end-of-file indication from YY_INPUT,
  1085. it then checks the
  1086. <b>yywrap()</b>
  1087. function. If
  1088. <b>yywrap()</b>
  1089. returns false (zero), then it is assumed that the
  1090. function has gone ahead and set up
  1091. <i>yyin</i>
  1092. to point to another input file, and scanning continues. If it returns
  1093. true (non-zero), then the scanner terminates, returning 0 to its
  1094. caller. Note that in either case, the start condition remains unchanged;
  1095. it does
  1096. <i>not</i>
  1097. revert to
  1098. <b>INITIAL.</b>
  1099. <p>
  1100. If you do not supply your own version of
  1101. <b>yywrap(),</b>
  1102. then you must either use
  1103. <b>%option</b> <b>noyywrap</b>
  1104. (in which case the scanner behaves as though
  1105. <b>yywrap()</b>
  1106. returned 1), or you must link with
  1107. <b>-lfl</b>
  1108. to obtain the default version of the routine, which always returns 1.
  1109. <p>
  1110. Three routines are available for scanning from in-memory buffers rather
  1111. than files:
  1112. <b>yy_scan_string(),</b> <b>yy_scan_bytes(),</b>
  1113. and
  1114. <b>yy_scan_buffer().</b>
  1115. See the discussion of them below in the section Multiple Input Buffers.
  1116. <p>
  1117. The scanner writes its
  1118. <b>ECHO</b>
  1119. output to the
  1120. <i>yyout</i>
  1121. global (default, stdout), which may be redefined by the user simply
  1122. by assigning it to some other
  1123. <b>FILE</b>
  1124. pointer.
  1125. </ul><H2>START CONDITIONS </H2><ul>
  1126. <i>flex</i>
  1127. provides a mechanism for conditionally activating rules. Any rule
  1128. whose pattern is prefixed with "&lt;sc&gt;" will only be active when
  1129. the scanner is in the start condition named "sc". For example,
  1130. <pre>
  1131. <p><br> &lt;STRING&gt;[^"]* { /* eat up the string body ... */
  1132. <br> ...
  1133. <br> }
  1134. <br>
  1135. <p><br></pre>
  1136. will be active only when the scanner is in the "STRING" start
  1137. condition, and
  1138. <pre>
  1139. <p><br> &lt;INITIAL,STRING,QUOTE&gt;\. { /* handle an escape ... */
  1140. <br> ...
  1141. <br> }
  1142. <br>
  1143. <p><br></pre>
  1144. will be active only when the current start condition is
  1145. either "INITIAL", "STRING", or "QUOTE".
  1146. <p>
  1147. Start conditions
  1148. are declared in the definitions (first) section of the input
  1149. using unindented lines beginning with either
  1150. <b>%s</b>
  1151. or
  1152. <b>%x</b>
  1153. followed by a list of names.
  1154. The former declares
  1155. <i>inclusive</i>
  1156. start conditions, the latter
  1157. <i>exclusive</i>
  1158. start conditions. A start condition is activated using the
  1159. <b>BEGIN</b>
  1160. action. Until the next
  1161. <b>BEGIN</b>
  1162. action is executed, rules with the given start
  1163. condition will be active and
  1164. rules with other start conditions will be inactive.
  1165. If the start condition is
  1166. <i>inclusive,</i>
  1167. then rules with no start conditions at all will also be active.
  1168. If it is
  1169. <i>exclusive,</i>
  1170. then
  1171. <i>only</i>
  1172. rules qualified with the start condition will be active.
  1173. A set of rules contingent on the same exclusive start condition
  1174. describe a scanner which is independent of any of the other rules in the
  1175. <i>flex</i>
  1176. input. Because of this,
  1177. exclusive start conditions make it easy to specify "mini-scanners"
  1178. which scan portions of the input that are syntactically different
  1179. from the rest (e.g., comments).
  1180. <p>
  1181. If the distinction between inclusive and exclusive start conditions
  1182. is still a little vague, here's a simple example illustrating the
  1183. connection between the two. The set of rules:
  1184. <pre>
  1185. <p><br> %s example
  1186. <br> %%
  1187. <br>
  1188. <p><br> &lt;example&gt;foo do_something();
  1189. <br>
  1190. <p><br> bar something_else();
  1191. <br>
  1192. <p><br></pre>
  1193. is equivalent to
  1194. <pre>
  1195. <p><br> %x example
  1196. <br> %%
  1197. <br>
  1198. <p><br> &lt;example&gt;foo do_something();
  1199. <br>
  1200. <p><br> &lt;INITIAL,example&gt;bar something_else();
  1201. <br>
  1202. <p><br></pre>
  1203. Without the
  1204. <b>&lt;INITIAL,example&gt;</b>
  1205. qualifier, the
  1206. <i>bar</i>
  1207. pattern in the second example wouldn't be active (i.e., couldn't match)
  1208. when in start condition
  1209. <b>example.</b>
  1210. If we just used
  1211. <b>&lt;example&gt;</b>
  1212. to qualify
  1213. <i>bar,</i>
  1214. though, then it would only be active in
  1215. <b>example</b>
  1216. and not in
  1217. <b>INITIAL,</b>
  1218. while in the first example it's active in both, because in the first
  1219. example the
  1220. <b>example</b>
  1221. startion condition is an
  1222. <i>inclusive</i>
  1223. <b>(%s)</b>
  1224. start condition.
  1225. <p>
  1226. Also note that the special start-condition specifier
  1227. <b>&lt;*&gt;</b>
  1228. matches every start condition. Thus, the above example could also
  1229. have been written;
  1230. <pre>
  1231. <p><br> %x example
  1232. <br> %%
  1233. <br>
  1234. <p><br> &lt;example&gt;foo do_something();
  1235. <br>
  1236. <p><br> &lt;*&gt;bar something_else();
  1237. <br>
  1238. <p><br></pre>
  1239. <p>
  1240. The default rule (to
  1241. <b>ECHO</b>
  1242. any unmatched character) remains active in start conditions. It
  1243. is equivalent to:
  1244. <pre>
  1245. <p><br> &lt;*&gt;.|\n ECHO;
  1246. <br>
  1247. <p><br></pre>
  1248. <p>
  1249. <b>BEGIN(0)</b>
  1250. returns to the original state where only the rules with
  1251. no start conditions are active. This state can also be
  1252. referred to as the start-condition "INITIAL", so
  1253. <b>BEGIN(INITIAL)</b>
  1254. is equivalent to
  1255. <b>BEGIN(0).</b>
  1256. (The parentheses around the start condition name are not required but
  1257. are considered good style.)
  1258. <p>
  1259. <b>BEGIN</b>
  1260. actions can also be given as indented code at the beginning
  1261. of the rules section. For example, the following will cause
  1262. the scanner to enter the "SPECIAL" start condition whenever
  1263. <b>yylex()</b>
  1264. is called and the global variable
  1265. <i>enter_special</i>
  1266. is true:
  1267. <pre>
  1268. <p><br> int enter_special;
  1269. <br>
  1270. <p><br> %x SPECIAL
  1271. <br> %%
  1272. <br> if ( enter_special )
  1273. <br> BEGIN(SPECIAL);
  1274. <br>
  1275. <p><br> &lt;SPECIAL&gt;blahblahblah
  1276. <br> ...more rules follow...
  1277. <br>
  1278. <p><br></pre>
  1279. <p>
  1280. To illustrate the uses of start conditions,
  1281. here is a scanner which provides two different interpretations
  1282. of a string like "123.456". By default it will treat it as
  1283. as three tokens, the integer "123", a dot ('.'), and the integer "456".
  1284. But if the string is preceded earlier in the line by the string
  1285. "expect-floats"
  1286. it will treat it as a single token, the floating-point number
  1287. 123.456:
  1288. <pre>
  1289. <p><br> %{
  1290. <br> #include &lt;math.h&gt;
  1291. <br> %}
  1292. <br> %s expect
  1293. <br>
  1294. <p><br> %%
  1295. <br> expect-floats BEGIN(expect);
  1296. <br>
  1297. <p><br> &lt;expect&gt;[0-9]+"."[0-9]+ {
  1298. <br> printf( "found a float, = %f\n",
  1299. <br> atof( yytext ) );
  1300. <br> }
  1301. <br> &lt;expect&gt;\n {
  1302. <br> /* that's the end of the line, so
  1303. <br> * we need another "expect-number"
  1304. <br> * before we'll recognize any more
  1305. <br> * numbers
  1306. <br> */
  1307. <br> BEGIN(INITIAL);
  1308. <br> }
  1309. <br>
  1310. <p><br> [0-9]+ {
  1311. <br> printf( "found an integer, = %d\n",
  1312. <br> atoi( yytext ) );
  1313. <br> }
  1314. <br>
  1315. <p><br> "." printf( "found a dot\n" );
  1316. <br>
  1317. <p><br></pre>
  1318. Here is a scanner which recognizes (and discards) C comments while
  1319. maintaining a count of the current input line.
  1320. <pre>
  1321. <p><br> %x comment
  1322. <br> %%
  1323. <br> int line_num = 1;
  1324. <br>
  1325. <p><br> "/*" BEGIN(comment);
  1326. <br>
  1327. <p><br> &lt;comment&gt;[^*\n]* /* eat anything that's not a '*' */
  1328. <br> &lt;comment&gt;"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */
  1329. <br> &lt;comment&gt;\n ++line_num;
  1330. <br> &lt;comment&gt;"*"+"/" BEGIN(INITIAL);
  1331. <br>
  1332. <p><br></pre>
  1333. This scanner goes to a bit of trouble to match as much
  1334. text as possible with each rule. In general, when attempting to write
  1335. a high-speed scanner try to match as much possible in each rule, as
  1336. it's a big win.
  1337. <p>
  1338. Note that start-conditions names are really integer values and
  1339. can be stored as such. Thus, the above could be extended in the
  1340. following fashion:
  1341. <pre>
  1342. <p><br> %x comment foo
  1343. <br> %%
  1344. <br> int line_num = 1;
  1345. <br> int comment_caller;
  1346. <br>
  1347. <p><br> "/*" {
  1348. <br> comment_caller = INITIAL;
  1349. <br> BEGIN(comment);
  1350. <br> }
  1351. <br>
  1352. <p><br> ...
  1353. <br>
  1354. <p><br> &lt;foo&gt;"/*" {
  1355. <br> comment_caller = foo;
  1356. <br> BEGIN(comment);
  1357. <br> }
  1358. <br>
  1359. <p><br> &lt;comment&gt;[^*\n]* /* eat anything that's not a '*' */
  1360. <br> &lt;comment&gt;"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */
  1361. <br> &lt;comment&gt;\n ++line_num;
  1362. <br> &lt;comment&gt;"*"+"/" BEGIN(comment_caller);
  1363. <br>
  1364. <p><br></pre>
  1365. Furthermore, you can access the current start condition using
  1366. the integer-valued
  1367. <b>YY_START</b>
  1368. macro. For example, the above assignments to
  1369. <i>comment_caller</i>
  1370. could instead be written
  1371. <pre>
  1372. <p><br> comment_caller = YY_START;
  1373. <br>
  1374. <p><br></pre>
  1375. Flex provides
  1376. <b>YYSTATE</b>
  1377. as an alias for
  1378. <b>YY_START</b>
  1379. (since that is what's used by AT&amp;T
  1380. <i>lex).</i>
  1381. <p>
  1382. Note that start conditions do not have their own name-space; %s's and %x's
  1383. declare names in the same fashion as #define's.
  1384. <p>
  1385. Finally, here's an example of how to match C-style quoted strings using
  1386. exclusive start conditions, including expanded escape sequences (but
  1387. not including checking for a string that's too long):
  1388. <pre>
  1389. <p><br> %x str
  1390. <br>
  1391. <p><br> %%
  1392. <br> char string_buf[MAX_STR_CONST];
  1393. <br> char *string_buf_ptr;
  1394. <br>
  1395. <p><br>
  1396. <p><br> \" string_buf_ptr = string_buf; BEGIN(str);
  1397. <br>
  1398. <p><br> &lt;str&gt;\" { /* saw closing quote - all done */
  1399. <br> BEGIN(INITIAL);
  1400. <br> *string_buf_ptr = '\0';
  1401. <br> /* return string constant token type and
  1402. <br> * value to parser
  1403. <br> */
  1404. <br> }
  1405. <br>
  1406. <p><br> &lt;str&gt;\n {
  1407. <br> /* error - unterminated string constant */
  1408. <br> /* generate error message */
  1409. <br> }
  1410. <br>
  1411. <p><br> &lt;str&gt;\\[0-7]{1,3} {
  1412. <br> /* octal escape sequence */
  1413. <br> int result;
  1414. <br>
  1415. <p><br> (void) sscanf( yytext + 1, "%o", &amp;result );
  1416. <br>
  1417. <p><br> if ( result &gt; 0xff )
  1418. <br> /* error, constant is out-of-bounds */
  1419. <br>
  1420. <p><br> *string_buf_ptr++ = result;
  1421. <br> }
  1422. <br>
  1423. <p><br> &lt;str&gt;\\[0-9]+ {
  1424. <br> /* generate error - bad escape sequence; something
  1425. <br> * like '\48' or '\0777777'
  1426. <br> */
  1427. <br> }
  1428. <br>
  1429. <p><br> &lt;str&gt;\\n *string_buf_ptr++ = '\n';
  1430. <br> &lt;str&gt;\\t *string_buf_ptr++ = '\t';
  1431. <br> &lt;str&gt;\\r *string_buf_ptr++ = '\r';
  1432. <br> &lt;str&gt;\\b *string_buf_ptr++ = '\b';
  1433. <br> &lt;str&gt;\\f *string_buf_ptr++ = '\f';
  1434. <br>
  1435. <p><br> &lt;str&gt;\\(.|\n) *string_buf_ptr++ = yytext[1];
  1436. <br>
  1437. <p><br> &lt;str&gt;[^\\\n\"]+ {
  1438. <br> char *yptr = yytext;
  1439. <br>
  1440. <p><br> while ( *yptr )
  1441. <br> *string_buf_ptr++ = *yptr++;
  1442. <br> }
  1443. <br>
  1444. <p><br></pre>
  1445. <p>
  1446. Often, such as in some of the examples above, you wind up writing a
  1447. whole bunch of rules all preceded by the same start condition(s). Flex
  1448. makes this a little easier and cleaner by introducing a notion of
  1449. start condition
  1450. <i>scope.</i>
  1451. A start condition scope is begun with:
  1452. <pre>
  1453. <p><br> &lt;SCs&gt;{
  1454. <br>
  1455. <p><br></pre>
  1456. where
  1457. <i>SCs</i>
  1458. is a list of one or more start conditions. Inside the start condition
  1459. scope, every rule automatically has the prefix
  1460. <i>&lt;SCs&gt;</i>
  1461. applied to it, until a
  1462. <i>'}'</i>
  1463. which matches the initial
  1464. <i>'{'.</i>
  1465. So, for example,
  1466. <pre>
  1467. <p><br> &lt;ESC&gt;{
  1468. <br> "\\n" return '\n';
  1469. <br> "\\r" return '\r';
  1470. <br> "\\f" return '\f';
  1471. <br> "\\0" return '\0';
  1472. <br> }
  1473. <br>
  1474. <p><br></pre>
  1475. is equivalent to:
  1476. <pre>
  1477. <p><br> &lt;ESC&gt;"\\n" return '\n';
  1478. <br> &lt;ESC&gt;"\\r" return '\r';
  1479. <br> &lt;ESC&gt;"\\f" return '\f';
  1480. <br> &lt;ESC&gt;"\\0" return '\0';
  1481. <br>
  1482. <p><br></pre>
  1483. Start condition scopes may be nested.
  1484. <p>
  1485. Three routines are available for manipulating stacks of start conditions:
  1486. <p><dl compact><dt><b>void</b> <b>yy_push_state(int</b> <b>new_state)</b>
  1487. <dd>pushes the current start condition onto the top of the start condition
  1488. stack and switches to
  1489. <i>new_state</i>
  1490. as though you had used
  1491. <b>BEGIN</b> <b>new_state</b>
  1492. (recall that start condition names are also integers).
  1493. <dt><b>void</b> <b>yy_pop_state()</b>
  1494. <dd>pops the top of the stack and switches to it via
  1495. <b>BEGIN.</b>
  1496. <dt><b>int</b> <b>yy_top_state()</b>
  1497. <dd>returns the top of the stack without altering the stack's contents.
  1498. </dl>
  1499. <p>
  1500. The start condition stack grows dynamically and so has no built-in
  1501. size limitation. If memory is exhausted, program execution aborts.
  1502. <p>
  1503. To use start condition stacks, your scanner must include a
  1504. <b>%option</b> <b>stack</b>
  1505. directive (see Options below).
  1506. </ul><H2>MULTIPLE INPUT BUFFERS </H2><ul>
  1507. Some scanners (such as those which support "include" files)
  1508. require reading from several input streams. As
  1509. <i>flex</i>
  1510. scanners do a large amount of buffering, one cannot control
  1511. where the next input will be read from by simply writing a
  1512. <b>YY_INPUT</b>
  1513. which is sensitive to the scanning context.
  1514. <b>YY_INPUT</b>
  1515. is only called when the scanner reaches the end of its buffer, which
  1516. may be a long time after scanning a statement such as an "include"
  1517. which requires switching the input source.
  1518. <p>
  1519. To negotiate these sorts of problems,
  1520. <i>flex</i>
  1521. provides a mechanism for creating and switching between multiple
  1522. input buffers. An input buffer is created by using:
  1523. <pre>
  1524. <p><br> YY_BUFFER_STATE yy_create_buffer( FILE *file, int size )
  1525. <br>
  1526. <p><br></pre>
  1527. which takes a
  1528. <i>FILE</i>
  1529. pointer and a size and creates a buffer associated with the given
  1530. file and large enough to hold
  1531. <i>size</i>
  1532. characters (when in doubt, use
  1533. <b>YY_BUF_SIZE</b>
  1534. for the size). It returns a
  1535. <b>YY_BUFFER_STATE</b>
  1536. handle, which may then be passed to other routines (see below). The
  1537. <b>YY_BUFFER_STATE</b>
  1538. type is a pointer to an opaque
  1539. <b>struct</b> <b>yy_buffer_state</b>
  1540. structure, so you may safely initialize YY_BUFFER_STATE variables to
  1541. <b>((YY_BUFFER_STATE)</b> <b>0)</b>
  1542. if you wish, and also refer to the opaque structure in order to
  1543. correctly declare input buffers in source files other than that
  1544. of your scanner. Note that the
  1545. <i>FILE</i>
  1546. pointer in the call to
  1547. <b>yy_create_buffer</b>
  1548. is only used as the value of
  1549. <i>yyin</i>
  1550. seen by
  1551. <b>YY_INPUT;</b>
  1552. if you redefine
  1553. <b>YY_INPUT</b>
  1554. so it no longer uses
  1555. <i>yyin,</i>
  1556. then you can safely pass a nil
  1557. <i>FILE</i>
  1558. pointer to
  1559. <b>yy_create_buffer.</b>
  1560. You select a particular buffer to scan from using:
  1561. <pre>
  1562. <p><br> void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer )
  1563. <br>
  1564. <p><br></pre>
  1565. switches the scanner's input buffer so subsequent tokens will
  1566. come from
  1567. <i>new_buffer.</i>
  1568. Note that
  1569. <b>yy_switch_to_buffer()</b>
  1570. may be used by yywrap() to set things up for continued scanning, instead
  1571. of opening a new file and pointing
  1572. <i>yyin</i>
  1573. at it. Note also that switching input sources via either
  1574. <b>yy_switch_to_buffer()</b>
  1575. or
  1576. <b>yywrap()</b>
  1577. does
  1578. <i>not</i>
  1579. change the start condition.
  1580. <pre>
  1581. <p><br> void yy_delete_buffer( YY_BUFFER_STATE buffer )
  1582. <br>
  1583. <p><br></pre>
  1584. is used to reclaim the storage associated with a buffer.
  1585. You can also clear the current contents of a buffer using:
  1586. <pre>
  1587. <p><br> void yy_flush_buffer( YY_BUFFER_STATE buffer )
  1588. <br>
  1589. <p><br></pre>
  1590. This function discards the buffer's contents,
  1591. so the next time the scanner attempts to match a token from the
  1592. buffer, it will first fill the buffer anew using
  1593. <b>YY_INPUT.</b>
  1594. <p>
  1595. <b>yy_new_buffer()</b>
  1596. is an alias for
  1597. <b>yy_create_buffer(),</b>
  1598. provided for compatibility with the C++ use of
  1599. <i>new</i>
  1600. and
  1601. <i>delete</i>
  1602. for creating and destroying dynamic objects.
  1603. <p>
  1604. Finally, the
  1605. <b>YY_CURRENT_BUFFER</b>
  1606. macro returns a
  1607. <b>YY_BUFFER_STATE</b>
  1608. handle to the current buffer.
  1609. <p>
  1610. Here is an example of using these features for writing a scanner
  1611. which expands include files (the
  1612. <b>&lt;&lt;EOF&gt;&gt;</b>
  1613. feature is discussed below):
  1614. <pre>
  1615. <p><br> /* the "incl" state is used for picking up the name
  1616. <br> * of an include file
  1617. <br> */
  1618. <br> %x incl
  1619. <br>
  1620. <p><br> %{
  1621. <br> #define MAX_INCLUDE_DEPTH 10
  1622. <br> YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
  1623. <br> int include_stack_ptr = 0;
  1624. <br> %}
  1625. <br>
  1626. <p><br> %%
  1627. <br> include BEGIN(incl);
  1628. <br>
  1629. <p><br> [a-z]+ ECHO;
  1630. <br> [^a-z\n]*\n? ECHO;
  1631. <br>
  1632. <p><br> &lt;incl&gt;[ \t]* /* eat the whitespace */
  1633. <br> &lt;incl&gt;[^ \t\n]+ { /* got the include file name */
  1634. <br> if ( include_stack_ptr &gt;= MAX_INCLUDE_DEPTH )
  1635. <br> {
  1636. <br> fprintf( stderr, "Includes nested too deeply" );
  1637. <br> exit( 1 );
  1638. <br> }
  1639. <br>
  1640. <p><br> include_stack[include_stack_ptr++] =
  1641. <br> YY_CURRENT_BUFFER;
  1642. <br>
  1643. <p><br> yyin = fopen( yytext, "r" );
  1644. <br>
  1645. <p><br> if ( ! yyin )
  1646. <br> error( ... );
  1647. <br>
  1648. <p><br> yy_switch_to_buffer(
  1649. <br> yy_create_buffer( yyin, YY_BUF_SIZE ) );
  1650. <br>
  1651. <p><br> BEGIN(INITIAL);
  1652. <br> }
  1653. <br>
  1654. <p><br> &lt;&lt;EOF&gt;&gt; {
  1655. <br> if ( --include_stack_ptr &lt; 0 )
  1656. <br> {
  1657. <br> yyterminate();
  1658. <br> }
  1659. <br>
  1660. <p><br> else
  1661. <br> {
  1662. <br> yy_delete_buffer( YY_CURRENT_BUFFER );
  1663. <br> yy_switch_to_buffer(
  1664. <br> include_stack[include_stack_ptr] );
  1665. <br> }
  1666. <br> }
  1667. <br>
  1668. <p><br></pre>
  1669. Three routines are available for setting up input buffers for
  1670. scanning in-memory strings instead of files. All of them create
  1671. a new input buffer for scanning the string, and return a corresponding
  1672. <b>YY_BUFFER_STATE</b>
  1673. handle (which you should delete with
  1674. <b>yy_delete_buffer()</b>
  1675. when done with it). They also switch to the new buffer using
  1676. <b>yy_switch_to_buffer(),</b>
  1677. so the next call to
  1678. <b>yylex()</b>
  1679. will start scanning the string.
  1680. <p><dl compact><dt><b>yy_scan_string(const</b> <b>char</b> <b>*str)</b>
  1681. <dd>scans a NUL-terminated string.
  1682. <dt><b>yy_scan_bytes(const</b> <b>char</b> <b>*bytes,</b> <b>int</b> <b>len)</b>
  1683. <dd>scans
  1684. <i>len</i>
  1685. bytes (including possibly NUL's)
  1686. starting at location
  1687. <i>bytes.</i>
  1688. </dl>
  1689. <p>
  1690. Note that both of these functions create and scan a
  1691. <i>copy</i>
  1692. of the string or bytes. (This may be desirable, since
  1693. <b>yylex()</b>
  1694. modifies the contents of the buffer it is scanning.) You can avoid the
  1695. copy by using:
  1696. <p><dl compact><dt><b>yy_scan_buffer(char</b> <b>*base,</b> <b>yy_size_t</b> <b>size)</b>
  1697. <dd>which scans in place the buffer starting at
  1698. <i>base,</i>
  1699. consisting of
  1700. <i>size</i>
  1701. bytes, the last two bytes of which
  1702. <i>must</i>
  1703. be
  1704. <b>YY_END_OF_BUFFER_CHAR</b>
  1705. (ASCII NUL).
  1706. These last two bytes are not scanned; thus, scanning
  1707. consists of
  1708. <b>base[0]</b>
  1709. through
  1710. <b>base[size-2],</b>
  1711. inclusive.
  1712. <dt><dd>If you fail to set up
  1713. <i>base</i>
  1714. in this manner (i.e., forget the final two
  1715. <b>YY_END_OF_BUFFER_CHAR</b>
  1716. bytes), then
  1717. <b>yy_scan_buffer()</b>
  1718. returns a nil pointer instead of creating a new input buffer.
  1719. <dt><dd>The type
  1720. <b>yy_size_t</b>
  1721. is an integral type to which you can cast an integer expression
  1722. reflecting the size of the buffer.
  1723. </dl>
  1724. </ul><H2>END-OF-FILE RULES </H2><ul>
  1725. The special rule "&lt;&lt;EOF&gt;&gt;" indicates
  1726. actions which are to be taken when an end-of-file is
  1727. encountered and yywrap() returns non-zero (i.e., indicates
  1728. no further files to process). The action must finish
  1729. by doing one of four things:
  1730. <p><dl compact><dt>-<dd>assigning
  1731. <i>yyin</i>
  1732. to a new input file (in previous versions of flex, after doing the
  1733. assignment you had to call the special action
  1734. <b>YY_NEW_FILE;</b>
  1735. this is no longer necessary);
  1736. <dt>-<dd>executing a
  1737. <i>return</i>
  1738. statement;
  1739. <dt>-<dd>executing the special
  1740. <b>yyterminate()</b>
  1741. action;
  1742. <dt>-<dd>or, switching to a new buffer using
  1743. <b>yy_switch_to_buffer()</b>
  1744. as shown in the example above.
  1745. </dl>
  1746. <p>
  1747. &lt;&lt;EOF&gt;&gt; rules may not be used with other
  1748. patterns; they may only be qualified with a list of start
  1749. conditions. If an unqualified &lt;&lt;EOF&gt;&gt; rule is given, it
  1750. applies to
  1751. <i>all</i>
  1752. start conditions which do not already have &lt;&lt;EOF&gt;&gt; actions. To
  1753. specify an &lt;&lt;EOF&gt;&gt; rule for only the initial start condition, use
  1754. <pre>
  1755. <p><br> &lt;INITIAL&gt;&lt;&lt;EOF&gt;&gt;
  1756. <br>
  1757. <p><br></pre>
  1758. <p>
  1759. These rules are useful for catching things like unclosed comments.
  1760. An example:
  1761. <pre>
  1762. <p><br> %x quote
  1763. <br> %%
  1764. <br>
  1765. <p><br> ...other rules for dealing with quotes...
  1766. <br>
  1767. <p><br> &lt;quote&gt;&lt;&lt;EOF&gt;&gt; {
  1768. <br> error( "unterminated quote" );
  1769. <br> yyterminate();
  1770. <br> }
  1771. <br> &lt;&lt;EOF&gt;&gt; {
  1772. <br> if ( *++filelist )
  1773. <br> yyin = fopen( *filelist, "r" );
  1774. <br> else
  1775. <br> yyterminate();
  1776. <br> }
  1777. <br>
  1778. <p><br></pre>
  1779. </ul><H2>MISCELLANEOUS MACROS </H2><ul>
  1780. The macro
  1781. <b>YY_USER_ACTION</b>
  1782. can be defined to provide an action
  1783. which is always executed prior to the matched rule's action. For example,
  1784. it could be #define'd to call a routine to convert yytext to lower-case.
  1785. When
  1786. <b>YY_USER_ACTION</b>
  1787. is invoked, the variable
  1788. <i>yy_act</i>
  1789. gives the number of the matched rule (rules are numbered starting with 1).
  1790. Suppose you want to profile how often each of your rules is matched. The
  1791. following would do the trick:
  1792. <pre>
  1793. <p><br> #define YY_USER_ACTION ++ctr[yy_act]
  1794. <br>
  1795. <p><br></pre>
  1796. where
  1797. <i>ctr</i>
  1798. is an array to hold the counts for the different rules. Note that
  1799. the macro
  1800. <b>YY_NUM_RULES</b>
  1801. gives the total number of rules (including the default rule, even if
  1802. you use
  1803. <b>-s),</b>
  1804. so a correct declaration for
  1805. <i>ctr</i>
  1806. is:
  1807. <pre>
  1808. <p><br> int ctr[YY_NUM_RULES];
  1809. <br>
  1810. <p><br></pre>
  1811. <p>
  1812. The macro
  1813. <b>YY_USER_INIT</b>
  1814. may be defined to provide an action which is always executed before
  1815. the first scan (and before the scanner's internal initializations are done).
  1816. For example, it could be used to call a routine to read
  1817. in a data table or open a logging file.
  1818. <p>
  1819. The macro
  1820. <b>yy_set_interactive(is_interactive)</b>
  1821. can be used to control whether the current buffer is considered
  1822. <i>interactive.</i>
  1823. An interactive buffer is processed more slowly,
  1824. but must be used when the scanner's input source is indeed
  1825. interactive to avoid problems due to waiting to fill buffers
  1826. (see the discussion of the
  1827. <b>-I</b>
  1828. flag below). A non-zero value
  1829. in the macro invocation marks the buffer as interactive, a zero
  1830. value as non-interactive. Note that use of this macro overrides
  1831. <b>%option</b> <b>always-interactive</b>
  1832. or
  1833. <b>%option</b> <b>never-interactive</b>
  1834. (see Options below).
  1835. <b>yy_set_interactive()</b>
  1836. must be invoked prior to beginning to scan the buffer that is
  1837. (or is not) to be considered interactive.
  1838. <p>
  1839. The macro
  1840. <b>yy_set_bol(at_bol)</b>
  1841. can be used to control whether the current buffer's scanning
  1842. context for the next token match is done as though at the
  1843. beginning of a line. A non-zero macro argument makes rules anchored with
  1844. '^' active, while a zero argument makes '^' rules inactive.
  1845. <p>
  1846. The macro
  1847. <b>YY_AT_BOL()</b>
  1848. returns true if the next token scanned from the current buffer
  1849. will have '^' rules active, false otherwise.
  1850. <p>
  1851. In the generated scanner, the actions are all gathered in one large
  1852. switch statement and separated using
  1853. <b>YY_BREAK,</b>
  1854. which may be redefined. By default, it is simply a "break", to separate
  1855. each rule's action from the following rule's.
  1856. Redefining
  1857. <b>YY_BREAK</b>
  1858. allows, for example, C++ users to
  1859. #define YY_BREAK to do nothing (while being very careful that every
  1860. rule ends with a "break" or a "return"!) to avoid suffering from
  1861. unreachable statement warnings where because a rule's action ends with
  1862. "return", the
  1863. <b>YY_BREAK</b>
  1864. is inaccessible.
  1865. </ul><H2>VALUES AVAILABLE TO THE USER </H2><ul>
  1866. This section summarizes the various values available to the user
  1867. in the rule actions.
  1868. <p><dl compact><dt>-<dd><b>char</b> <b>*yytext</b>
  1869. holds the text of the current token. It may be modified but not lengthened
  1870. (you cannot append characters to the end).
  1871. <dt><dd>If the special directive
  1872. <b>%array</b>
  1873. appears in the first section of the scanner description, then
  1874. <b>yytext</b>
  1875. is instead declared
  1876. <b>char</b> <b>yytext[YYLMAX],</b>
  1877. where
  1878. <b>YYLMAX</b>
  1879. is a macro definition that you can redefine in the first section
  1880. if you don't like the default value (generally 8KB). Using
  1881. <b>%array</b>
  1882. results in somewhat slower scanners, but the value of
  1883. <b>yytext</b>
  1884. becomes immune to calls to
  1885. <i>input()</i>
  1886. and
  1887. <i>unput(),</i>
  1888. which potentially destroy its value when
  1889. <b>yytext</b>
  1890. is a character pointer. The opposite of
  1891. <b>%array</b>
  1892. is
  1893. <b>%pointer,</b>
  1894. which is the default.
  1895. <dt><dd>You cannot use
  1896. <b>%array</b>
  1897. when generating C++ scanner classes
  1898. (the
  1899. <b>-+</b>
  1900. flag).
  1901. <dt>-<dd><b>int</b> <b>yyleng</b>
  1902. holds the length of the current token.
  1903. <dt>-<dd><b>FILE</b> <b>*yyin</b>
  1904. is the file which by default
  1905. <i>flex</i>
  1906. reads from. It may be redefined but doing so only makes sense before
  1907. scanning begins or after an EOF has been encountered. Changing it in
  1908. the midst of scanning will have unexpected results since
  1909. <i>flex</i>
  1910. buffers its input; use
  1911. <b>yyrestart()</b>
  1912. instead.
  1913. Once scanning terminates because an end-of-file
  1914. has been seen, you can assign
  1915. <i>yyin</i>
  1916. at the new input file and then call the scanner again to continue scanning.
  1917. <dt>-<dd><b>void</b> <b>yyrestart(</b> <b>FILE</b> <b>*new_file</b> <b>)</b>
  1918. may be called to point
  1919. <i>yyin</i>
  1920. at the new input file. The switch-over to the new file is immediate
  1921. (any previously buffered-up input is lost). Note that calling
  1922. <b>yyrestart()</b>
  1923. with
  1924. <i>yyin</i>
  1925. as an argument thus throws away the current input buffer and continues
  1926. scanning the same input file.
  1927. <dt>-<dd><b>FILE</b> <b>*yyout</b>
  1928. is the file to which
  1929. <b>ECHO</b>
  1930. actions are done. It can be reassigned by the user.
  1931. <dt>-<dd><b>YY_CURRENT_BUFFER</b>
  1932. returns a
  1933. <b>YY_BUFFER_STATE</b>
  1934. handle to the current buffer.
  1935. <dt>-<dd><b>YY_START</b>
  1936. returns an integer value corresponding to the current start
  1937. condition. You can subsequently use this value with
  1938. <b>BEGIN</b>
  1939. to return to that start condition.
  1940. </dl>
  1941. </ul><H2>INTERFACING WITH YACC </H2><ul>
  1942. One of the main uses of
  1943. <i>flex</i>
  1944. is as a companion to the
  1945. <i>yacc</i>
  1946. parser-generator.
  1947. <i>yacc</i>
  1948. parsers expect to call a routine named
  1949. <b>yylex()</b>
  1950. to find the next input token. The routine is supposed to
  1951. return the type of the next token as well as putting any associated
  1952. value in the global
  1953. <b>yylval.</b>
  1954. To use
  1955. <i>flex</i>
  1956. with
  1957. <i>yacc,</i>
  1958. one specifies the
  1959. <b>-d</b>
  1960. option to
  1961. <i>yacc</i>
  1962. to instruct it to generate the file
  1963. <b>y.tab.h</b>
  1964. containing definitions of all the
  1965. <b>%tokens</b>
  1966. appearing in the
  1967. <i>yacc</i>
  1968. input. This file is then included in the
  1969. <i>flex</i>
  1970. scanner. For example, if one of the tokens is "TOK_NUMBER",
  1971. part of the scanner might look like:
  1972. <pre>
  1973. <p><br> %{
  1974. <br> #include "y.tab.h"
  1975. <br> %}
  1976. <br>
  1977. <p><br> %%
  1978. <br>
  1979. <p><br> [0-9]+ yylval = atoi( yytext ); return TOK_NUMBER;
  1980. <br>
  1981. <p><br></pre>
  1982. </ul><H2>OPTIONS </H2><ul>
  1983. <i>flex</i>
  1984. has the following options:
  1985. <p><dl compact><dt><b>-b</b>
  1986. <dd>Generate backing-up information to
  1987. <i>lex.backup.</i>
  1988. This is a list of scanner states which require backing up
  1989. and the input characters on which they do so. By adding rules one
  1990. can remove backing-up states. If
  1991. <i>all</i>
  1992. backing-up states are eliminated and
  1993. <b>-Cf</b>
  1994. or
  1995. <b>-CF</b>
  1996. is used, the generated scanner will run faster (see the
  1997. <b>-p</b>
  1998. flag). Only users who wish to squeeze every last cycle out of their
  1999. scanners need worry about this option. (See the section on Performance
  2000. Considerations below.)
  2001. <dt><b>-c</b>
  2002. <dd>is a do-nothing, deprecated option included for POSIX compliance.
  2003. <dt><b>-d</b>
  2004. <dd>makes the generated scanner run in
  2005. <i>debug</i>
  2006. mode. Whenever a pattern is recognized and the global
  2007. <b>yy_flex_debug</b>
  2008. is non-zero (which is the default),
  2009. the scanner will write to
  2010. <i>stderr</i>
  2011. a line of the form:
  2012. <pre>
  2013. <p><br> --accepting rule at line 53 ("the matched text")
  2014. <br>
  2015. <p><br></pre>
  2016. The line number refers to the location of the rule in the file
  2017. defining the scanner (i.e., the file that was fed to flex). Messages
  2018. are also generated when the scanner backs up, accepts the
  2019. default rule, reaches the end of its input buffer (or encounters
  2020. a NUL; at this point, the two look the same as far as the scanner's concerned),
  2021. or reaches an end-of-file.
  2022. <dt><b>-f</b>
  2023. <dd>specifies
  2024. <i>fast</i> <i>scanner.</i>
  2025. No table compression is done and stdio is bypassed.
  2026. The result is large but fast. This option is equivalent to
  2027. <b>-Cfr</b>
  2028. (see below).
  2029. <dt><b>-h</b>
  2030. <dd>generates a "help" summary of
  2031. <i>flex's</i>
  2032. options to
  2033. <i>stdout</i>
  2034. and then exits.
  2035. <b>-?</b>
  2036. and
  2037. <b>--help</b>
  2038. are synonyms for
  2039. <b>-h.</b>
  2040. <dt><b>-i</b>
  2041. <dd>instructs
  2042. <i>flex</i>
  2043. to generate a
  2044. <i>case-insensitive</i>
  2045. scanner. The case of letters given in the
  2046. <i>flex</i>
  2047. input patterns will
  2048. be ignored, and tokens in the input will be matched regardless of case. The
  2049. matched text given in
  2050. <i>yytext</i>
  2051. will have the preserved case (i.e., it will not be folded).
  2052. <dt><b>-l</b>
  2053. <dd>turns on maximum compatibility with the original AT&amp;T
  2054. <i>lex</i>
  2055. implementation. Note that this does not mean
  2056. <i>full</i>
  2057. compatibility. Use of this option costs a considerable amount of
  2058. performance, and it cannot be used with the
  2059. <b>-+,</b> <b>-f,</b> <b>-F,</b> <b>-Cf,</b>
  2060. or
  2061. <b>-CF</b>
  2062. options. For details on the compatibilities it provides, see the section
  2063. "Incompatibilities With Lex And POSIX" below. This option also results
  2064. in the name
  2065. <b>YY_FLEX_LEX_COMPAT</b>
  2066. being #define'd in the generated scanner.
  2067. <dt><b>-n</b>
  2068. <dd>is another do-nothing, deprecated option included only for
  2069. POSIX compliance.
  2070. <dt><b>-p</b>
  2071. <dd>generates a performance report to stderr. The report
  2072. consists of comments regarding features of the
  2073. <i>flex</i>
  2074. input file which will cause a serious loss of performance in the resulting
  2075. scanner. If you give the flag twice, you will also get comments regarding
  2076. features that lead to minor performance losses.
  2077. <dt><dd>Note that the use of
  2078. <b>REJECT,</b>
  2079. <b>%option</b> <b>yylineno,</b>
  2080. and variable trailing context (see the Deficiencies / Bugs section below)
  2081. entails a substantial performance penalty; use of
  2082. <i>yymore(),</i>
  2083. the
  2084. <b>^</b>
  2085. operator,
  2086. and the
  2087. <b>-I</b>
  2088. flag entail minor performance penalties.
  2089. <dt><b>-s</b>
  2090. <dd>causes the
  2091. <i>default</i> <i>rule</i>
  2092. (that unmatched scanner input is echoed to
  2093. <i>stdout)</i>
  2094. to be suppressed. If the scanner encounters input that does not
  2095. match any of its rules, it aborts with an error. This option is
  2096. useful for finding holes in a scanner's rule set.
  2097. <dt><b>-t</b>
  2098. <dd>instructs
  2099. <i>flex</i>
  2100. to write the scanner it generates to standard output instead
  2101. of
  2102. <b>lex.yy.c.</b>
  2103. <dt><b>-v</b>
  2104. <dd>specifies that
  2105. <i>flex</i>
  2106. should write to
  2107. <i>stderr</i>
  2108. a summary of statistics regarding the scanner it generates.
  2109. Most of the statistics are meaningless to the casual
  2110. <i>flex</i>
  2111. user, but the first line identifies the version of
  2112. <i>flex</i>
  2113. (same as reported by
  2114. <b>-V),</b>
  2115. and the next line the flags used when generating the scanner, including
  2116. those that are on by default.
  2117. <dt><b>-w</b>
  2118. <dd>suppresses warning messages.
  2119. <dt><b>-B</b>
  2120. <dd>instructs
  2121. <i>flex</i>
  2122. to generate a
  2123. <i>batch</i>
  2124. scanner, the opposite of
  2125. <i>interactive</i>
  2126. scanners generated by
  2127. <b>-I</b>
  2128. (see below). In general, you use
  2129. <b>-B</b>
  2130. when you are
  2131. <i>certain</i>
  2132. that your scanner will never be used interactively, and you want to
  2133. squeeze a
  2134. <i>little</i>
  2135. more performance out of it. If your goal is instead to squeeze out a
  2136. <i>lot</i>
  2137. more performance, you should be using the
  2138. <b>-Cf</b>
  2139. or
  2140. <b>-CF</b>
  2141. options (discussed below), which turn on
  2142. <b>-B</b>
  2143. automatically anyway.
  2144. <dt><b>-F</b>
  2145. <dd>specifies that the
  2146. fast
  2147. scanner table representation should be used (and stdio
  2148. bypassed). This representation is
  2149. about as fast as the full table representation
  2150. <b>(-f),</b>
  2151. and for some sets of patterns will be considerably smaller (and for
  2152. others, larger). In general, if the pattern set contains both "keywords"
  2153. and a catch-all, "identifier" rule, such as in the set:
  2154. <pre>
  2155. <p><br> "case" return TOK_CASE;
  2156. <br> "switch" return TOK_SWITCH;
  2157. <br> ...
  2158. <br> "default" return TOK_DEFAULT;
  2159. <br> [a-z]+ return TOK_ID;
  2160. <br>
  2161. <p><br></pre>
  2162. then you're better off using the full table representation. If only
  2163. the "identifier" rule is present and you then use a hash table or some such
  2164. to detect the keywords, you're better off using
  2165. <b>-F.</b>
  2166. <dt><dd>This option is equivalent to
  2167. <b>-CFr</b>
  2168. (see below). It cannot be used with
  2169. <b>-+.</b>
  2170. <dt><b>-I</b>
  2171. <dd>instructs
  2172. <i>flex</i>
  2173. to generate an
  2174. <i>interactive</i>
  2175. scanner. An interactive scanner is one that only looks ahead to decide
  2176. what token has been matched if it absolutely must. It turns out that
  2177. always looking one extra character ahead, even if the scanner has already
  2178. seen enough text to disambiguate the current token, is a bit faster than
  2179. only looking ahead when necessary. But scanners that always look ahead
  2180. give dreadful interactive performance; for example, when a user types
  2181. a newline, it is not recognized as a newline token until they enter
  2182. <i>another</i>
  2183. token, which often means typing in another whole line.
  2184. <dt><dd><i>Flex</i>
  2185. scanners default to
  2186. <i>interactive</i>
  2187. unless you use the
  2188. <b>-Cf</b>
  2189. or
  2190. <b>-CF</b>
  2191. table-compression options (see below). That's because if you're looking
  2192. for high-performance you should be using one of these options, so if you
  2193. didn't,
  2194. <i>flex</i>
  2195. assumes you'd rather trade off a bit of run-time performance for intuitive
  2196. interactive behavior. Note also that you
  2197. <i>cannot</i>
  2198. use
  2199. <b>-I</b>
  2200. in conjunction with
  2201. <b>-Cf</b>
  2202. or
  2203. <b>-CF.</b>
  2204. Thus, this option is not really needed; it is on by default for all those
  2205. cases in which it is allowed.
  2206. <dt><dd>You can force a scanner to
  2207. <i>not</i>
  2208. be interactive by using
  2209. <b>-B</b>
  2210. (see above).
  2211. <dt><b>-L</b>
  2212. <dd>instructs
  2213. <i>flex</i>
  2214. not to generate
  2215. <b>#line</b>
  2216. directives. Without this option,
  2217. <i>flex</i>
  2218. peppers the generated scanner
  2219. with #line directives so error messages in the actions will be correctly
  2220. located with respect to either the original
  2221. <i>flex</i>
  2222. input file (if the errors are due to code in the input file), or
  2223. <b>lex.yy.c</b>
  2224. (if the errors are
  2225. <i>flex's</i>
  2226. fault -- you should report these sorts of errors to the email address
  2227. given below).
  2228. <dt><b>-T</b>
  2229. <dd>makes
  2230. <i>flex</i>
  2231. run in
  2232. <i>trace</i>
  2233. mode. It will generate a lot of messages to
  2234. <i>stderr</i>
  2235. concerning
  2236. the form of the input and the resultant non-deterministic and deterministic
  2237. finite automata. This option is mostly for use in maintaining
  2238. <i>flex.</i>
  2239. <dt><b>-V</b>
  2240. <dd>prints the version number to
  2241. <i>stdout</i>
  2242. and exits.
  2243. <b>--version</b>
  2244. is a synonym for
  2245. <b>-V.</b>
  2246. <dt><b>-7</b>
  2247. <dd>instructs
  2248. <i>flex</i>
  2249. to generate a 7-bit scanner, i.e., one which can only recognized 7-bit
  2250. characters in its input. The advantage of using
  2251. <b>-7</b>
  2252. is that the scanner's tables can be up to half the size of those generated
  2253. using the
  2254. <b>-8</b>
  2255. option (see below). The disadvantage is that such scanners often hang
  2256. or crash if their input contains an 8-bit character.
  2257. <dt><dd>Note, however, that unless you generate your scanner using the
  2258. <b>-Cf</b>
  2259. or
  2260. <b>-CF</b>
  2261. table compression options, use of
  2262. <b>-7</b>
  2263. will save only a small amount of table space, and make your scanner
  2264. considerably less portable.
  2265. <i>Flex's</i>
  2266. default behavior is to generate an 8-bit scanner unless you use the
  2267. <b>-Cf</b>
  2268. or
  2269. <b>-CF,</b>
  2270. in which case
  2271. <i>flex</i>
  2272. defaults to generating 7-bit scanners unless your site was always
  2273. configured to generate 8-bit scanners (as will often be the case
  2274. with non-USA sites). You can tell whether flex generated a 7-bit
  2275. or an 8-bit scanner by inspecting the flag summary in the
  2276. <b>-v</b>
  2277. output as described above.
  2278. <dt><dd>Note that if you use
  2279. <b>-Cfe</b>
  2280. or
  2281. <b>-CFe</b>
  2282. (those table compression options, but also using equivalence classes as
  2283. discussed see below), flex still defaults to generating an 8-bit
  2284. scanner, since usually with these compression options full 8-bit tables
  2285. are not much more expensive than 7-bit tables.
  2286. <dt><b>-8</b>
  2287. <dd>instructs
  2288. <i>flex</i>
  2289. to generate an 8-bit scanner, i.e., one which can recognize 8-bit
  2290. characters. This flag is only needed for scanners generated using
  2291. <b>-Cf</b>
  2292. or
  2293. <b>-CF,</b>
  2294. as otherwise flex defaults to generating an 8-bit scanner anyway.
  2295. <dt><dd>See the discussion of
  2296. <b>-7</b>
  2297. above for flex's default behavior and the tradeoffs between 7-bit
  2298. and 8-bit scanners.
  2299. <dt><b>-+</b>
  2300. <dd>specifies that you want flex to generate a C++
  2301. scanner class. See the section on Generating C++ Scanners below for
  2302. details.
  2303. <dt><b>-C[aefFmr]</b>
  2304. <dd>controls the degree of table compression and, more generally, trade-offs
  2305. between small scanners and fast scanners.
  2306. <dt><dd><b>-Ca</b>
  2307. ("align") instructs flex to trade off larger tables in the
  2308. generated scanner for faster performance because the elements of
  2309. the tables are better aligned for memory access and computation. On some
  2310. RISC architectures, fetching and manipulating longwords is more efficient
  2311. than with smaller-sized units such as shortwords. This option can
  2312. double the size of the tables used by your scanner.
  2313. <dt><dd><b>-Ce</b>
  2314. directs
  2315. <i>flex</i>
  2316. to construct
  2317. <i>equivalence</i> <i>classes,</i>
  2318. i.e., sets of characters
  2319. which have identical lexical properties (for example, if the only
  2320. appearance of digits in the
  2321. <i>flex</i>
  2322. input is in the character class
  2323. "[0-9]" then the digits '0', '1', ..., '9' will all be put
  2324. in the same equivalence class). Equivalence classes usually give
  2325. dramatic reductions in the final table/object file sizes (typically
  2326. a factor of 2-5) and are pretty cheap performance-wise (one array
  2327. look-up per character scanned).
  2328. <dt><dd><b>-Cf</b>
  2329. specifies that the
  2330. <i>full</i>
  2331. scanner tables should be generated -
  2332. <i>flex</i>
  2333. should not compress the
  2334. tables by taking advantages of similar transition functions for
  2335. different states.
  2336. <dt><dd><b>-CF</b>
  2337. specifies that the alternate fast scanner representation (described
  2338. above under the
  2339. <b>-F</b>
  2340. flag)
  2341. should be used. This option cannot be used with
  2342. <b>-+.</b>
  2343. <dt><dd><b>-Cm</b>
  2344. directs
  2345. <i>flex</i>
  2346. to construct
  2347. <i>meta-equivalence</i> <i>classes,</i>
  2348. which are sets of equivalence classes (or characters, if equivalence
  2349. classes are not being used) that are commonly used together. Meta-equivalence
  2350. classes are often a big win when using compressed tables, but they
  2351. have a moderate performance impact (one or two "if" tests and one
  2352. array look-up per character scanned).
  2353. <dt><dd><b>-Cr</b>
  2354. causes the generated scanner to
  2355. <i>bypass</i>
  2356. use of the standard I/O library (stdio) for input. Instead of calling
  2357. <b>fread()</b>
  2358. or
  2359. <b>getc(),</b>
  2360. the scanner will use the
  2361. <b>read()</b>
  2362. system call, resulting in a performance gain which varies from system
  2363. to system, but in general is probably negligible unless you are also using
  2364. <b>-Cf</b>
  2365. or
  2366. <b>-CF.</b>
  2367. Using
  2368. <b>-Cr</b>
  2369. can cause strange behavior if, for example, you read from
  2370. <i>yyin</i>
  2371. using stdio prior to calling the scanner (because the scanner will miss
  2372. whatever text your previous reads left in the stdio input buffer).
  2373. <dt><dd><b>-Cr</b>
  2374. has no effect if you define
  2375. <b>YY_INPUT</b>
  2376. (see The Generated Scanner above).
  2377. <dt><dd>A lone
  2378. <b>-C</b>
  2379. specifies that the scanner tables should be compressed but neither
  2380. equivalence classes nor meta-equivalence classes should be used.
  2381. <dt><dd>The options
  2382. <b>-Cf</b>
  2383. or
  2384. <b>-CF</b>
  2385. and
  2386. <b>-Cm</b>
  2387. do not make sense together - there is no opportunity for meta-equivalence
  2388. classes if the table is not being compressed. Otherwise the options
  2389. may be freely mixed, and are cumulative.
  2390. <dt><dd>The default setting is
  2391. <b>-Cem,</b>
  2392. which specifies that
  2393. <i>flex</i>
  2394. should generate equivalence classes
  2395. and meta-equivalence classes. This setting provides the highest
  2396. degree of table compression. You can trade off
  2397. faster-executing scanners at the cost of larger tables with
  2398. the following generally being true:
  2399. <pre>
  2400. <p><br> slowest &amp; smallest
  2401. <br> -Cem
  2402. <br> -Cm
  2403. <br> -Ce
  2404. <br> -C
  2405. <br> -C{f,F}e
  2406. <br> -C{f,F}
  2407. <br> -C{f,F}a
  2408. <br> fastest &amp; largest
  2409. <br>
  2410. <p><br></pre>
  2411. Note that scanners with the smallest tables are usually generated and
  2412. compiled the quickest, so
  2413. during development you will usually want to use the default, maximal
  2414. compression.
  2415. <dt><dd><b>-Cfe</b>
  2416. is often a good compromise between speed and size for production
  2417. scanners.
  2418. <dt><b>-ooutput</b>
  2419. <dd>directs flex to write the scanner to the file
  2420. <b>output</b>
  2421. instead of
  2422. <b>lex.yy.c.</b>
  2423. If you combine
  2424. <b>-o</b>
  2425. with the
  2426. <b>-t</b>
  2427. option, then the scanner is written to
  2428. <i>stdout</i>
  2429. but its
  2430. <b>#line</b>
  2431. directives (see the
  2432. <b>\-L</b>
  2433. option above) refer to the file
  2434. <b>output.</b>
  2435. <dt><b>-Pprefix</b>
  2436. <dd>changes the default
  2437. <i>yy</i>
  2438. prefix used by
  2439. <i>flex</i>
  2440. for all globally-visible variable and function names to instead be
  2441. <i>prefix.</i>
  2442. For example,
  2443. <b>-Pfoo</b>
  2444. changes the name of
  2445. <b>yytext</b>
  2446. to
  2447. <b>footext.</b>
  2448. It also changes the name of the default output file from
  2449. <b>lex.yy.c</b>
  2450. to
  2451. <b>lex.foo.c.</b>
  2452. Here are all of the names affected:
  2453. <pre>
  2454. <p><br> yy_create_buffer
  2455. <br> yy_delete_buffer
  2456. <br> yy_flex_debug
  2457. <br> yy_init_buffer
  2458. <br> yy_flush_buffer
  2459. <br> yy_load_buffer_state
  2460. <br> yy_switch_to_buffer
  2461. <br> yyin
  2462. <br> yyleng
  2463. <br> yylex
  2464. <br> yylineno
  2465. <br> yyout
  2466. <br> yyrestart
  2467. <br> yytext
  2468. <br> yywrap
  2469. <br>
  2470. <p><br></pre>
  2471. (If you are using a C++ scanner, then only
  2472. <b>yywrap</b>
  2473. and
  2474. <b>yyFlexLexer</b>
  2475. are affected.)
  2476. Within your scanner itself, you can still refer to the global variables
  2477. and functions using either version of their name; but externally, they
  2478. have the modified name.
  2479. <dt><dd>This option lets you easily link together multiple
  2480. <i>flex</i>
  2481. programs into the same executable. Note, though, that using this
  2482. option also renames
  2483. <b>yywrap(),</b>
  2484. so you now
  2485. <i>must</i>
  2486. either
  2487. provide your own (appropriately-named) version of the routine for your
  2488. scanner, or use
  2489. <b>%option</b> <b>noyywrap,</b>
  2490. as linking with
  2491. <b>-lfl</b>
  2492. no longer provides one for you by default.
  2493. <dt><b>-Sskeleton_file</b>
  2494. <dd>overrides the default skeleton file from which
  2495. <i>flex</i>
  2496. constructs its scanners. You'll never need this option unless you are doing
  2497. <i>flex</i>
  2498. maintenance or development.
  2499. </dl>
  2500. <p>
  2501. <i>flex</i>
  2502. also provides a mechanism for controlling options within the
  2503. scanner specification itself, rather than from the flex command-line.
  2504. This is done by including
  2505. <b>%option</b>
  2506. directives in the first section of the scanner specification.
  2507. You can specify multiple options with a single
  2508. <b>%option</b>
  2509. directive, and multiple directives in the first section of your flex input
  2510. file. Most
  2511. options are given simply as names, optionally preceded by the
  2512. word "no" (with no intervening whitespace) to negate their meaning.
  2513. A number are equivalent to flex flags or their negation:
  2514. <pre>
  2515. <p><br> 7bit -7 option
  2516. <br> 8bit -8 option
  2517. <br> align -Ca option
  2518. <br> backup -b option
  2519. <br> batch -B option
  2520. <br> c++ -+ option
  2521. <br>
  2522. <p><br> caseful or
  2523. <br> case-sensitive opposite of -i (default)
  2524. <br>
  2525. <p><br> case-insensitive or
  2526. <br> caseless -i option
  2527. <br>
  2528. <p><br> debug -d option
  2529. <br> default opposite of -s option
  2530. <br> ecs -Ce option
  2531. <br> fast -F option
  2532. <br> full -f option
  2533. <br> interactive -I option
  2534. <br> lex-compat -l option
  2535. <br> meta-ecs -Cm option
  2536. <br> perf-report -p option
  2537. <br> read -Cr option
  2538. <br> stdout -t option
  2539. <br> verbose -v option
  2540. <br> warn opposite of -w option
  2541. <br> (use "%option nowarn" for -w)
  2542. <br>
  2543. <p><br> array equivalent to "%array"
  2544. <br> pointer equivalent to "%pointer" (default)
  2545. <br>
  2546. <p><br></pre>
  2547. Some
  2548. <b>%option's</b>
  2549. provide features otherwise not available:
  2550. <p><dl compact><dt><b>always-interactive</b>
  2551. <dd>instructs flex to generate a scanner which always considers its input
  2552. "interactive". Normally, on each new input file the scanner calls
  2553. <b>isatty()</b>
  2554. in an attempt to determine whether
  2555. the scanner's input source is interactive and thus should be read a
  2556. character at a time. When this option is used, however, then no
  2557. such call is made.
  2558. <dt><b>main</b>
  2559. <dd>directs flex to provide a default
  2560. <b>main()</b>
  2561. program for the scanner, which simply calls
  2562. <b>yylex().</b>
  2563. This option implies
  2564. <b>noyywrap</b>
  2565. (see below).
  2566. <dt><b>never-interactive</b>
  2567. <dd>instructs flex to generate a scanner which never considers its input
  2568. "interactive" (again, no call made to
  2569. <b>isatty()).</b>
  2570. This is the opposite of
  2571. <b>always-interactive.</b>
  2572. <dt><b>stack</b>
  2573. <dd>enables the use of start condition stacks (see Start Conditions above).
  2574. <dt><b>stdinit</b>
  2575. <dd>if unset (i.e.,
  2576. <b>%option</b> <b>nostdinit)</b>
  2577. initializes
  2578. <i>yyin</i>
  2579. and
  2580. <i>yyout</i>
  2581. to nil
  2582. <i>FILE</i>
  2583. pointers, instead of
  2584. <i>stdin</i>
  2585. and
  2586. <i>stdout.</i>
  2587. <dt><b>yylineno</b>
  2588. <dd>directs
  2589. <i>flex</i>
  2590. to generate a scanner that maintains the number of the current line
  2591. read from its input in the global variable
  2592. <b>yylineno.</b>
  2593. This option is implied by
  2594. <b>%option</b> <b>lex-compat.</b>
  2595. <dt><b>yywrap</b>
  2596. <dd>if unset (i.e.,
  2597. <b>%option</b> <b>noyywrap),</b>
  2598. makes the scanner not call
  2599. <b>yywrap()</b>
  2600. upon an end-of-file, but simply assume that there are no more
  2601. files to scan (until the user points
  2602. <i>yyin</i>
  2603. at a new file and calls
  2604. <b>yylex()</b>
  2605. again).
  2606. </dl>
  2607. <p>
  2608. <i>flex</i>
  2609. scans your rule actions to determine whether you use the
  2610. <b>REJECT</b>
  2611. or
  2612. <b>yymore()</b>
  2613. features. The
  2614. <b>reject</b>
  2615. and
  2616. <b>yymore</b>
  2617. options are available to override its decision as to whether you use the
  2618. options, either by setting them (e.g.,
  2619. <b>%option</b> <b>reject)</b>
  2620. to indicate the feature is indeed used, or
  2621. unsetting them to indicate it actually is not used
  2622. (e.g.,
  2623. <b>%option</b> <b>noyymore).</b>
  2624. <p>
  2625. Three options take string-delimited values, offset with '=':
  2626. <pre>
  2627. <p><br> %option outfile="ABC"
  2628. <br>
  2629. <p><br></pre>
  2630. is equivalent to
  2631. <b>-oABC,</b>
  2632. and
  2633. <pre>
  2634. <p><br> %option prefix="XYZ"
  2635. <br>
  2636. <p><br></pre>
  2637. is equivalent to
  2638. <b>-PXYZ.</b>
  2639. Finally,
  2640. <pre>
  2641. <p><br> %option yyclass="foo"
  2642. <br>
  2643. <p><br></pre>
  2644. only applies when generating a C++ scanner (
  2645. <b>-+</b>
  2646. option). It informs
  2647. <i>flex</i>
  2648. that you have derived
  2649. <b>foo</b>
  2650. as a subclass of
  2651. <b>yyFlexLexer,</b>
  2652. so
  2653. <i>flex</i>
  2654. will place your actions in the member function
  2655. <b>foo::yylex()</b>
  2656. instead of
  2657. <b>yyFlexLexer::yylex().</b>
  2658. It also generates a
  2659. <b>yyFlexLexer::yylex()</b>
  2660. member function that emits a run-time error (by invoking
  2661. <b>yyFlexLexer::LexerError())</b>
  2662. if called.
  2663. See Generating C++ Scanners, below, for additional information.
  2664. <p>
  2665. A number of options are available for lint purists who want to suppress
  2666. the appearance of unneeded routines in the generated scanner. Each of the
  2667. following, if unset, results in the corresponding routine not appearing in
  2668. the generated scanner:
  2669. <pre>
  2670. <p><br> input, unput
  2671. <br> yy_push_state, yy_pop_state, yy_top_state
  2672. <br> yy_scan_buffer, yy_scan_bytes, yy_scan_string
  2673. <br>
  2674. <p><br></pre>
  2675. (though
  2676. <b>yy_push_state()</b>
  2677. and friends won't appear anyway unless you use
  2678. <b>%option</b> <b>stack).</b>
  2679. </ul><H2>PERFORMANCE CONSIDERATIONS </H2><ul>
  2680. The main design goal of
  2681. <i>flex</i>
  2682. is that it generate high-performance scanners. It has been optimized
  2683. for dealing well with large sets of rules. Aside from the effects on
  2684. scanner speed of the table compression
  2685. <b>-C</b>
  2686. options outlined above,
  2687. there are a number of options/actions which degrade performance. These
  2688. are, from most expensive to least:
  2689. <pre>
  2690. <p><br> REJECT
  2691. <br> %option yylineno
  2692. <br> arbitrary trailing context
  2693. <br>
  2694. <p><br> pattern sets that require backing up
  2695. <br> %array
  2696. <br> %option interactive
  2697. <br> %option always-interactive
  2698. <br>
  2699. <p><br> '^' beginning-of-line operator
  2700. <br> yymore()
  2701. <br>
  2702. <p><br></pre>
  2703. with the first three all being quite expensive and the last two
  2704. being quite cheap. Note also that
  2705. <b>unput()</b>
  2706. is implemented as a routine call that potentially does quite a bit of
  2707. work, while
  2708. <b>yyless()</b>
  2709. is a quite-cheap macro; so if just putting back some excess text you
  2710. scanned, use
  2711. <b>yyless().</b>
  2712. <p>
  2713. <b>REJECT</b>
  2714. should be avoided at all costs when performance is important.
  2715. It is a particularly expensive option.
  2716. <p>
  2717. Getting rid of backing up is messy and often may be an enormous
  2718. amount of work for a complicated scanner. In principal, one begins
  2719. by using the
  2720. <b>-b</b>
  2721. flag to generate a
  2722. <i>lex.backup</i>
  2723. file. For example, on the input
  2724. <pre>
  2725. <p><br> %%
  2726. <br> foo return TOK_KEYWORD;
  2727. <br> foobar return TOK_KEYWORD;
  2728. <br>
  2729. <p><br></pre>
  2730. the file looks like:
  2731. <pre>
  2732. <p><br> State #6 is non-accepting -
  2733. <br> associated rule line numbers:
  2734. <br> 2 3
  2735. <br> out-transitions: [ o ]
  2736. <br> jam-transitions: EOF [ \001-n p-\177 ]
  2737. <br>
  2738. <p><br> State #8 is non-accepting -
  2739. <br> associated rule line numbers:
  2740. <br> 3
  2741. <br> out-transitions: [ a ]
  2742. <br> jam-transitions: EOF [ \001-` b-\177 ]
  2743. <br>
  2744. <p><br> State #9 is non-accepting -
  2745. <br> associated rule line numbers:
  2746. <br> 3
  2747. <br> out-transitions: [ r ]
  2748. <br> jam-transitions: EOF [ \001-q s-\177 ]
  2749. <br>
  2750. <p><br> Compressed tables always back up.
  2751. <br>
  2752. <p><br></pre>
  2753. The first few lines tell us that there's a scanner state in
  2754. which it can make a transition on an 'o' but not on any other
  2755. character, and that in that state the currently scanned text does not match
  2756. any rule. The state occurs when trying to match the rules found
  2757. at lines 2 and 3 in the input file.
  2758. If the scanner is in that state and then reads
  2759. something other than an 'o', it will have to back up to find
  2760. a rule which is matched. With
  2761. a bit of headscratching one can see that this must be the
  2762. state it's in when it has seen "fo". When this has happened,
  2763. if anything other than another 'o' is seen, the scanner will
  2764. have to back up to simply match the 'f' (by the default rule).
  2765. <p>
  2766. The comment regarding State #8 indicates there's a problem
  2767. when "foob" has been scanned. Indeed, on any character other
  2768. than an 'a', the scanner will have to back up to accept "foo".
  2769. Similarly, the comment for State #9 concerns when "fooba" has
  2770. been scanned and an 'r' does not follow.
  2771. <p>
  2772. The final comment reminds us that there's no point going to
  2773. all the trouble of removing backing up from the rules unless
  2774. we're using
  2775. <b>-Cf</b>
  2776. or
  2777. <b>-CF,</b>
  2778. since there's no performance gain doing so with compressed scanners.
  2779. <p>
  2780. The way to remove the backing up is to add "error" rules:
  2781. <pre>
  2782. <p><br> %%
  2783. <br> foo return TOK_KEYWORD;
  2784. <br> foobar return TOK_KEYWORD;
  2785. <br>
  2786. <p><br> fooba |
  2787. <br> foob |
  2788. <br> fo {
  2789. <br> /* false alarm, not really a keyword */
  2790. <br> return TOK_ID;
  2791. <br> }
  2792. <br>
  2793. <p><br></pre>
  2794. <p>
  2795. Eliminating backing up among a list of keywords can also be
  2796. done using a "catch-all" rule:
  2797. <pre>
  2798. <p><br> %%
  2799. <br> foo return TOK_KEYWORD;
  2800. <br> foobar return TOK_KEYWORD;
  2801. <br>
  2802. <p><br> [a-z]+ return TOK_ID;
  2803. <br>
  2804. <p><br></pre>
  2805. This is usually the best solution when appropriate.
  2806. <p>
  2807. Backing up messages tend to cascade.
  2808. With a complicated set of rules it's not uncommon to get hundreds
  2809. of messages. If one can decipher them, though, it often
  2810. only takes a dozen or so rules to eliminate the backing up (though
  2811. it's easy to make a mistake and have an error rule accidentally match
  2812. a valid token. A possible future
  2813. <i>flex</i>
  2814. feature will be to automatically add rules to eliminate backing up).
  2815. <p>
  2816. It's important to keep in mind that you gain the benefits of eliminating
  2817. backing up only if you eliminate
  2818. <i>every</i>
  2819. instance of backing up. Leaving just one means you gain nothing.
  2820. <p>
  2821. <i>Variable</i>
  2822. trailing context (where both the leading and trailing parts do not have
  2823. a fixed length) entails almost the same performance loss as
  2824. <b>REJECT</b>
  2825. (i.e., substantial). So when possible a rule like:
  2826. <pre>
  2827. <p><br> %%
  2828. <br> mouse|rat/(cat|dog) run();
  2829. <br>
  2830. <p><br></pre>
  2831. is better written:
  2832. <pre>
  2833. <p><br> %%
  2834. <br> mouse/cat|dog run();
  2835. <br> rat/cat|dog run();
  2836. <br>
  2837. <p><br></pre>
  2838. or as
  2839. <pre>
  2840. <p><br> %%
  2841. <br> mouse|rat/cat run();
  2842. <br> mouse|rat/dog run();
  2843. <br>
  2844. <p><br></pre>
  2845. Note that here the special '|' action does
  2846. <i>not</i>
  2847. provide any savings, and can even make things worse (see
  2848. Deficiencies / Bugs below).
  2849. <p>
  2850. Another area where the user can increase a scanner's performance
  2851. (and one that's easier to implement) arises from the fact that
  2852. the longer the tokens matched, the faster the scanner will run.
  2853. This is because with long tokens the processing of most input
  2854. characters takes place in the (short) inner scanning loop, and
  2855. does not often have to go through the additional work of setting up
  2856. the scanning environment (e.g.,
  2857. <b>yytext)</b>
  2858. for the action. Recall the scanner for C comments:
  2859. <pre>
  2860. <p><br> %x comment
  2861. <br> %%
  2862. <br> int line_num = 1;
  2863. <br>
  2864. <p><br> "/*" BEGIN(comment);
  2865. <br>
  2866. <p><br> &lt;comment&gt;[^*\n]*
  2867. <br> &lt;comment&gt;"*"+[^*/\n]*
  2868. <br> &lt;comment&gt;\n ++line_num;
  2869. <br> &lt;comment&gt;"*"+"/" BEGIN(INITIAL);
  2870. <br>
  2871. <p><br></pre>
  2872. This could be sped up by writing it as:
  2873. <pre>
  2874. <p><br> %x comment
  2875. <br> %%
  2876. <br> int line_num = 1;
  2877. <br>
  2878. <p><br> "/*" BEGIN(comment);
  2879. <br>
  2880. <p><br> &lt;comment&gt;[^*\n]*
  2881. <br> &lt;comment&gt;[^*\n]*\n ++line_num;
  2882. <br> &lt;comment&gt;"*"+[^*/\n]*
  2883. <br> &lt;comment&gt;"*"+[^*/\n]*\n ++line_num;
  2884. <br> &lt;comment&gt;"*"+"/" BEGIN(INITIAL);
  2885. <br>
  2886. <p><br></pre>
  2887. Now instead of each newline requiring the processing of another
  2888. action, recognizing the newlines is "distributed" over the other rules
  2889. to keep the matched text as long as possible. Note that
  2890. <i>adding</i>
  2891. rules does
  2892. <i>not</i>
  2893. slow down the scanner! The speed of the scanner is independent
  2894. of the number of rules or (modulo the considerations given at the
  2895. beginning of this section) how complicated the rules are with
  2896. regard to operators such as '*' and '|'.
  2897. <p>
  2898. A final example in speeding up a scanner: suppose you want to scan
  2899. through a file containing identifiers and keywords, one per line
  2900. and with no other extraneous characters, and recognize all the
  2901. keywords. A natural first approach is:
  2902. <pre>
  2903. <p><br> %%
  2904. <br> asm |
  2905. <br> auto |
  2906. <br> break |
  2907. <br> ... etc ...
  2908. <br> volatile |
  2909. <br> while /* it's a keyword */
  2910. <br>
  2911. <p><br> .|\n /* it's not a keyword */
  2912. <br>
  2913. <p><br></pre>
  2914. To eliminate the back-tracking, introduce a catch-all rule:
  2915. <pre>
  2916. <p><br> %%
  2917. <br> asm |
  2918. <br> auto |
  2919. <br> break |
  2920. <br> ... etc ...
  2921. <br> volatile |
  2922. <br> while /* it's a keyword */
  2923. <br>
  2924. <p><br> [a-z]+ |
  2925. <br> .|\n /* it's not a keyword */
  2926. <br>
  2927. <p><br></pre>
  2928. Now, if it's guaranteed that there's exactly one word per line,
  2929. then we can reduce the total number of matches by a half by
  2930. merging in the recognition of newlines with that of the other
  2931. tokens:
  2932. <pre>
  2933. <p><br> %%
  2934. <br> asm\n |
  2935. <br> auto\n |
  2936. <br> break\n |
  2937. <br> ... etc ...
  2938. <br> volatile\n |
  2939. <br> while\n /* it's a keyword */
  2940. <br>
  2941. <p><br> [a-z]+\n |
  2942. <br> .|\n /* it's not a keyword */
  2943. <br>
  2944. <p><br></pre>
  2945. One has to be careful here, as we have now reintroduced backing up
  2946. into the scanner. In particular, while
  2947. <i>we</i>
  2948. know that there will never be any characters in the input stream
  2949. other than letters or newlines,
  2950. <i>flex</i>
  2951. can't figure this out, and it will plan for possibly needing to back up
  2952. when it has scanned a token like "auto" and then the next character
  2953. is something other than a newline or a letter. Previously it would
  2954. then just match the "auto" rule and be done, but now it has no "auto"
  2955. rule, only a "auto\n" rule. To eliminate the possibility of backing up,
  2956. we could either duplicate all rules but without final newlines, or,
  2957. since we never expect to encounter such an input and therefore don't
  2958. how it's classified, we can introduce one more catch-all rule, this
  2959. one which doesn't include a newline:
  2960. <pre>
  2961. <p><br> %%
  2962. <br> asm\n |
  2963. <br> auto\n |
  2964. <br> break\n |
  2965. <br> ... etc ...
  2966. <br> volatile\n |
  2967. <br> while\n /* it's a keyword */
  2968. <br>
  2969. <p><br> [a-z]+\n |
  2970. <br> [a-z]+ |
  2971. <br> .|\n /* it's not a keyword */
  2972. <br>
  2973. <p><br></pre>
  2974. Compiled with
  2975. <b>-Cf,</b>
  2976. this is about as fast as one can get a
  2977. <i>flex</i>
  2978. scanner to go for this particular problem.
  2979. <p>
  2980. A final note:
  2981. <i>flex</i>
  2982. is slow when matching NUL's, particularly when a token contains
  2983. multiple NUL's.
  2984. It's best to write rules which match
  2985. <i>short</i>
  2986. amounts of text if it's anticipated that the text will often include NUL's.
  2987. <p>
  2988. Another final note regarding performance: as mentioned above in the section
  2989. How the Input is Matched, dynamically resizing
  2990. <b>yytext</b>
  2991. to accommodate huge tokens is a slow process because it presently requires that
  2992. the (huge) token be rescanned from the beginning. Thus if performance is
  2993. vital, you should attempt to match "large" quantities of text but not
  2994. "huge" quantities, where the cutoff between the two is at about 8K
  2995. characters/token.
  2996. </ul><H2>GENERATING C++ SCANNERS </H2><ul>
  2997. <i>flex</i>
  2998. provides two different ways to generate scanners for use with C++. The
  2999. first way is to simply compile a scanner generated by
  3000. <i>flex</i>
  3001. using a C++ compiler instead of a C compiler. You should not encounter
  3002. any compilations errors (please report any you find to the email address
  3003. given in the Author section below). You can then use C++ code in your
  3004. rule actions instead of C code. Note that the default input source for
  3005. your scanner remains
  3006. <i>yyin,</i>
  3007. and default echoing is still done to
  3008. <i>yyout.</i>
  3009. Both of these remain
  3010. <i>FILE</i> <i>*</i>
  3011. variables and not C++
  3012. <i>streams.</i>
  3013. <p>
  3014. You can also use
  3015. <i>flex</i>
  3016. to generate a C++ scanner class, using the
  3017. <b>-+</b>
  3018. option (or, equivalently,
  3019. <b>%option</b> <b>c++),</b>
  3020. which is automatically specified if the name of the flex
  3021. executable ends in a '+', such as
  3022. <i>flex++.</i>
  3023. When using this option, flex defaults to generating the scanner to the file
  3024. <b>lex.yy.cc</b>
  3025. instead of
  3026. <b>lex.yy.c.</b>
  3027. The generated scanner includes the header file
  3028. <i>FlexLexer.h,</i>
  3029. which defines the interface to two C++ classes.
  3030. <p>
  3031. The first class,
  3032. <b>FlexLexer,</b>
  3033. provides an abstract base class defining the general scanner class
  3034. interface. It provides the following member functions:
  3035. <p><dl compact><dt><b>const</b> <b>char*</b> <b>YYText()</b>
  3036. <dd>returns the text of the most recently matched token, the equivalent of
  3037. <b>yytext.</b>
  3038. <dt><b>int</b> <b>YYLeng()</b>
  3039. <dd>returns the length of the most recently matched token, the equivalent of
  3040. <b>yyleng.</b>
  3041. <dt><b>int</b> <b>lineno()</b> <b>const</b>
  3042. <dd>returns the current input line number
  3043. (see
  3044. <b>%option</b> <b>yylineno),</b>
  3045. or
  3046. <b>1</b>
  3047. if
  3048. <b>%option</b> <b>yylineno</b>
  3049. was not used.
  3050. <dt><b>void</b> <b>set_debug(</b> <b>int</b> <b>flag</b> <b>)</b>
  3051. <dd>sets the debugging flag for the scanner, equivalent to assigning to
  3052. <b>yy_flex_debug</b>
  3053. (see the Options section above). Note that you must build the scanner
  3054. using
  3055. <b>%option</b> <b>debug</b>
  3056. to include debugging information in it.
  3057. <dt><b>int</b> <b>debug()</b> <b>const</b>
  3058. <dd>returns the current setting of the debugging flag.
  3059. </dl>
  3060. <p>
  3061. Also provided are member functions equivalent to
  3062. <b>yy_switch_to_buffer(),</b>
  3063. <b>yy_create_buffer()</b>
  3064. (though the first argument is an
  3065. <b>istream*</b>
  3066. object pointer and not a
  3067. <b>FILE*),</b>
  3068. <b>yy_flush_buffer(),</b>
  3069. <b>yy_delete_buffer(),</b>
  3070. and
  3071. <b>yyrestart()</b>
  3072. (again, the first argument is a
  3073. <b>istream*</b>
  3074. object pointer).
  3075. <p>
  3076. The second class defined in
  3077. <i>FlexLexer.h</i>
  3078. is
  3079. <b>yyFlexLexer,</b>
  3080. which is derived from
  3081. <b>FlexLexer.</b>
  3082. It defines the following additional member functions:
  3083. <p><dl compact><dt><b>yyFlexLexer( istream* arg_yyin = 0, ostream* arg_yyout = 0 )
  3084. </b><dd>constructs a
  3085. <b>yyFlexLexer</b>
  3086. object using the given streams for input and output. If not specified,
  3087. the streams default to
  3088. <b>cin</b>
  3089. and
  3090. <b>cout,</b>
  3091. respectively.
  3092. <dt><b>virtual</b> <b>int</b> <b>yylex()</b>
  3093. <dd>performs the same role is
  3094. <b>yylex()</b>
  3095. does for ordinary flex scanners: it scans the input stream, consuming
  3096. tokens, until a rule's action returns a value. If you derive a subclass
  3097. <b>S</b>
  3098. from
  3099. <b>yyFlexLexer</b>
  3100. and want to access the member functions and variables of
  3101. <b>S</b>
  3102. inside
  3103. <b>yylex(),</b>
  3104. then you need to use
  3105. <b>%option</b> <b>yyclass=S</b>
  3106. to inform
  3107. <i>flex</i>
  3108. that you will be using that subclass instead of
  3109. <b>yyFlexLexer.</b>
  3110. In this case, rather than generating
  3111. <b>yyFlexLexer::yylex(),</b>
  3112. <i>flex</i>
  3113. generates
  3114. <b>S::yylex()</b>
  3115. (and also generates a dummy
  3116. <b>yyFlexLexer::yylex()</b>
  3117. that calls
  3118. <b>yyFlexLexer::LexerError()</b>
  3119. if called).
  3120. <dt><b>virtual void switch_streams(istream* new_in = 0,
  3121. </b><b><dd>ostream* new_out = 0)
  3122. </b>reassigns
  3123. <b>yyin</b>
  3124. to
  3125. <b>new_in</b>
  3126. (if non-nil)
  3127. and
  3128. <b>yyout</b>
  3129. to
  3130. <b>new_out</b>
  3131. (ditto), deleting the previous input buffer if
  3132. <b>yyin</b>
  3133. is reassigned.
  3134. <dt><b>int yylex( istream* new_in = 0, ostream* new_out = 0 )
  3135. </b><dd>first switches the input streams via
  3136. <b>switch_streams(</b> <b>new_in,</b> <b>new_out</b> <b>)</b>
  3137. and then returns the value of
  3138. <b>yylex().</b>
  3139. </dl>
  3140. <p>
  3141. In addition,
  3142. <b>yyFlexLexer</b>
  3143. defines the following protected virtual functions which you can redefine
  3144. in derived classes to tailor the scanner:
  3145. <p><dl compact><dt><b>virtual int LexerInput( char* buf, int max_size )
  3146. </b><dd>reads up to
  3147. <b>max_size</b>
  3148. characters into
  3149. <b>buf</b>
  3150. and returns the number of characters read. To indicate end-of-input,
  3151. return 0 characters. Note that "interactive" scanners (see the
  3152. <b>-B</b>
  3153. and
  3154. <b>-I</b>
  3155. flags) define the macro
  3156. <b>YY_INTERACTIVE.</b>
  3157. If you redefine
  3158. <b>LexerInput()</b>
  3159. and need to take different actions depending on whether or not
  3160. the scanner might be scanning an interactive input source, you can
  3161. test for the presence of this name via
  3162. <b>#ifdef.</b>
  3163. <dt><b>virtual void LexerOutput( const char* buf, int size )
  3164. </b><dd>writes out
  3165. <b>size</b>
  3166. characters from the buffer
  3167. <b>buf,</b>
  3168. which, while NUL-terminated, may also contain "internal" NUL's if
  3169. the scanner's rules can match text with NUL's in them.
  3170. <dt><b>virtual void LexerError( const char* msg )
  3171. </b><dd>reports a fatal error message. The default version of this function
  3172. writes the message to the stream
  3173. <b>cerr</b>
  3174. and exits.
  3175. </dl>
  3176. <p>
  3177. Note that a
  3178. <b>yyFlexLexer</b>
  3179. object contains its
  3180. <i>entire</i>
  3181. scanning state. Thus you can use such objects to create reentrant
  3182. scanners. You can instantiate multiple instances of the same
  3183. <b>yyFlexLexer</b>
  3184. class, and you can also combine multiple C++ scanner classes together
  3185. in the same program using the
  3186. <b>-P</b>
  3187. option discussed above.
  3188. <p>
  3189. Finally, note that the
  3190. <b>%array</b>
  3191. feature is not available to C++ scanner classes; you must use
  3192. <b>%pointer</b>
  3193. (the default).
  3194. <p>
  3195. Here is an example of a simple C++ scanner:
  3196. <pre>
  3197. <p><br> // An example of using the flex C++ scanner class.
  3198. <br>
  3199. <p><br> %{
  3200. <br> int mylineno = 0;
  3201. <br> %}
  3202. <br>
  3203. <p><br> string \"[^\n"]+\"
  3204. <br>
  3205. <p><br> ws [ \t]+
  3206. <br>
  3207. <p><br> alpha [A-Za-z]
  3208. <br> dig [0-9]
  3209. <br> name ({alpha}|{dig}|\$)({alpha}|{dig}|[_.\-/$])*
  3210. <br> num1 [-+]?{dig}+\.?([eE][-+]?{dig}+)?
  3211. <br> num2 [-+]?{dig}*\.{dig}+([eE][-+]?{dig}+)?
  3212. <br> number {num1}|{num2}
  3213. <br>
  3214. <p><br> %%
  3215. <br>
  3216. <p><br> {ws} /* skip blanks and tabs */
  3217. <br>
  3218. <p><br> "/*" {
  3219. <br> int c;
  3220. <br>
  3221. <p><br> while((c = yyinput()) != 0)
  3222. <br> {
  3223. <br> if(c == '\n')
  3224. <br> ++mylineno;
  3225. <br>
  3226. <p><br> else if(c == '*')
  3227. <br> {
  3228. <br> if((c = yyinput()) == '/')
  3229. <br> break;
  3230. <br> else
  3231. <br> unput(c);
  3232. <br> }
  3233. <br> }
  3234. <br> }
  3235. <br>
  3236. <p><br> {number} cout &lt;&lt; "number " &lt;&lt; YYText() &lt;&lt; '\n';
  3237. <br>
  3238. <p><br> \n mylineno++;
  3239. <br>
  3240. <p><br> {name} cout &lt;&lt; "name " &lt;&lt; YYText() &lt;&lt; '\n';
  3241. <br>
  3242. <p><br> {string} cout &lt;&lt; "string " &lt;&lt; YYText() &lt;&lt; '\n';
  3243. <br>
  3244. <p><br> %%
  3245. <br>
  3246. <p><br> int main( int /* argc */, char** /* argv */ )
  3247. <br> {
  3248. <br> FlexLexer* lexer = new yyFlexLexer;
  3249. <br> while(lexer-&gt;yylex() != 0)
  3250. <br> ;
  3251. <br> return 0;
  3252. <br> }
  3253. <br></pre>
  3254. If you want to create multiple (different) lexer classes, you use the
  3255. <b>-P</b>
  3256. flag (or the
  3257. <b>prefix=</b>
  3258. option) to rename each
  3259. <b>yyFlexLexer</b>
  3260. to some other
  3261. <b>xxFlexLexer.</b>
  3262. You then can include
  3263. <b>&lt;FlexLexer.h&gt;</b>
  3264. in your other sources once per lexer class, first renaming
  3265. <b>yyFlexLexer</b>
  3266. as follows:
  3267. <pre>
  3268. <p><br> #undef yyFlexLexer
  3269. <br> #define yyFlexLexer xxFlexLexer
  3270. <br> #include &lt;FlexLexer.h&gt;
  3271. <br>
  3272. <p><br> #undef yyFlexLexer
  3273. <br> #define yyFlexLexer zzFlexLexer
  3274. <br> #include &lt;FlexLexer.h&gt;
  3275. <br>
  3276. <p><br></pre>
  3277. if, for example, you used
  3278. <b>%option</b> <b>prefix=xx</b>
  3279. for one of your scanners and
  3280. <b>%option</b> <b>prefix=zz</b>
  3281. for the other.
  3282. <p>
  3283. IMPORTANT: the present form of the scanning class is
  3284. <i>experimental</i>
  3285. and may change considerably between major releases.
  3286. </ul><H2>INCOMPATIBILITIES WITH LEX AND POSIX </H2><ul>
  3287. <i>flex</i>
  3288. is a rewrite of the AT&amp;T Unix
  3289. <i>lex</i>
  3290. tool (the two implementations do not share any code, though),
  3291. with some extensions and incompatibilities, both of which
  3292. are of concern to those who wish to write scanners acceptable
  3293. to either implementation. Flex is fully compliant with the POSIX
  3294. <i>lex</i>
  3295. specification, except that when using
  3296. <b>%pointer</b>
  3297. (the default), a call to
  3298. <b>unput()</b>
  3299. destroys the contents of
  3300. <b>yytext,</b>
  3301. which is counter to the POSIX specification.
  3302. <p>
  3303. In this section we discuss all of the known areas of incompatibility
  3304. between flex, AT&amp;T lex, and the POSIX specification.
  3305. <p>
  3306. <i>flex's</i>
  3307. <b>-l</b>
  3308. option turns on maximum compatibility with the original AT&amp;T
  3309. <i>lex</i>
  3310. implementation, at the cost of a major loss in the generated scanner's
  3311. performance. We note below which incompatibilities can be overcome
  3312. using the
  3313. <b>-l</b>
  3314. option.
  3315. <p>
  3316. <i>flex</i>
  3317. is fully compatible with
  3318. <i>lex</i>
  3319. with the following exceptions:
  3320. <p><dl compact><dt>-<dd>The undocumented
  3321. <i>lex</i>
  3322. scanner internal variable
  3323. <b>yylineno</b>
  3324. is not supported unless
  3325. <b>-l</b>
  3326. or
  3327. <b>%option</b> <b>yylineno</b>
  3328. is used.
  3329. <dt><dd><b>yylineno</b>
  3330. should be maintained on a per-buffer basis, rather than a per-scanner
  3331. (single global variable) basis.
  3332. <dt><dd><b>yylineno</b>
  3333. is not part of the POSIX specification.
  3334. <dt>-<dd>The
  3335. <b>input()</b>
  3336. routine is not redefinable, though it may be called to read characters
  3337. following whatever has been matched by a rule. If
  3338. <b>input()</b>
  3339. encounters an end-of-file the normal
  3340. <b>yywrap()</b>
  3341. processing is done. A ``real'' end-of-file is returned by
  3342. <b>input()</b>
  3343. as
  3344. <i>EOF.</i>
  3345. <dt><dd>Input is instead controlled by defining the
  3346. <b>YY_INPUT</b>
  3347. macro.
  3348. <dt><dd>The
  3349. <i>flex</i>
  3350. restriction that
  3351. <b>input()</b>
  3352. cannot be redefined is in accordance with the POSIX specification,
  3353. which simply does not specify any way of controlling the
  3354. scanner's input other than by making an initial assignment to
  3355. <i>yyin.</i>
  3356. <dt>-<dd>The
  3357. <b>unput()</b>
  3358. routine is not redefinable. This restriction is in accordance with POSIX.
  3359. <dt>-<dd><i>flex</i>
  3360. scanners are not as reentrant as
  3361. <i>lex</i>
  3362. scanners. In particular, if you have an interactive scanner and
  3363. an interrupt handler which long-jumps out of the scanner, and
  3364. the scanner is subsequently called again, you may get the following
  3365. message:
  3366. <pre>
  3367. <p><br> fatal flex scanner internal error--end of buffer missed
  3368. <br>
  3369. <p><br></pre>
  3370. To reenter the scanner, first use
  3371. <pre>
  3372. <p><br> yyrestart( yyin );
  3373. <br>
  3374. <p><br></pre>
  3375. Note that this call will throw away any buffered input; usually this
  3376. isn't a problem with an interactive scanner.
  3377. <dt><dd>Also note that flex C++ scanner classes
  3378. <i>are</i>
  3379. reentrant, so if using C++ is an option for you, you should use
  3380. them instead. See "Generating C++ Scanners" above for details.
  3381. <dt>-<dd><b>output()</b>
  3382. is not supported.
  3383. Output from the
  3384. <b>ECHO</b>
  3385. macro is done to the file-pointer
  3386. <i>yyout</i>
  3387. (default
  3388. <i>stdout).</i>
  3389. <dt><dd><b>output()</b>
  3390. is not part of the POSIX specification.
  3391. <dt>-<dd><i>lex</i>
  3392. does not support exclusive start conditions (%x), though they
  3393. are in the POSIX specification.
  3394. <dt>-<dd>When definitions are expanded,
  3395. <i>flex</i>
  3396. encloses them in parentheses.
  3397. With lex, the following:
  3398. <pre>
  3399. <p><br> NAME [A-Z][A-Z0-9]*
  3400. <br> %%
  3401. <br> foo{NAME}? printf( "Found it\n" );
  3402. <br> %%
  3403. <br>
  3404. <p><br></pre>
  3405. will not match the string "foo" because when the macro
  3406. is expanded the rule is equivalent to "foo[A-Z][A-Z0-9]*?"
  3407. and the precedence is such that the '?' is associated with
  3408. "[A-Z0-9]*". With
  3409. <i>flex,</i>
  3410. the rule will be expanded to
  3411. "foo([A-Z][A-Z0-9]*)?" and so the string "foo" will match.
  3412. <dt><dd>Note that if the definition begins with
  3413. <b>^</b>
  3414. or ends with
  3415. <b>$</b>
  3416. then it is
  3417. <i>not</i>
  3418. expanded with parentheses, to allow these operators to appear in
  3419. definitions without losing their special meanings. But the
  3420. <b>&lt;s&gt;,</b> <b>/,</b>
  3421. and
  3422. <b>&lt;&lt;EOF&gt;&gt;</b>
  3423. operators cannot be used in a
  3424. <i>flex</i>
  3425. definition.
  3426. <dt><dd>Using
  3427. <b>-l</b>
  3428. results in the
  3429. <i>lex</i>
  3430. behavior of no parentheses around the definition.
  3431. <dt><dd>The POSIX specification is that the definition be enclosed in parentheses.
  3432. <dt>-<dd>Some implementations of
  3433. <i>lex</i>
  3434. allow a rule's action to begin on a separate line, if the rule's pattern
  3435. has trailing whitespace:
  3436. <pre>
  3437. <p><br> %%
  3438. <br> foo|bar&lt;space here&gt;
  3439. <br> { foobar_action(); }
  3440. <br>
  3441. <p><br></pre>
  3442. <i>flex</i>
  3443. does not support this feature.
  3444. <dt>-<dd>The
  3445. <i>lex</i>
  3446. <b>%r</b>
  3447. (generate a Ratfor scanner) option is not supported. It is not part
  3448. of the POSIX specification.
  3449. <dt>-<dd>After a call to
  3450. <b>unput(),</b>
  3451. <i>yytext</i>
  3452. is undefined until the next token is matched, unless the scanner
  3453. was built using
  3454. <b>%array.</b>
  3455. This is not the case with
  3456. <i>lex</i>
  3457. or the POSIX specification. The
  3458. <b>-l</b>
  3459. option does away with this incompatibility.
  3460. <dt>-<dd>The precedence of the
  3461. <b>{}</b>
  3462. (numeric range) operator is different.
  3463. <i>lex</i>
  3464. interprets "abc{1,3}" as "match one, two, or
  3465. three occurrences of 'abc'", whereas
  3466. <i>flex</i>
  3467. interprets it as "match 'ab'
  3468. followed by one, two, or three occurrences of 'c'". The latter is
  3469. in agreement with the POSIX specification.
  3470. <dt>-<dd>The precedence of the
  3471. <b>^</b>
  3472. operator is different.
  3473. <i>lex</i>
  3474. interprets "^foo|bar" as "match either 'foo' at the beginning of a line,
  3475. or 'bar' anywhere", whereas
  3476. <i>flex</i>
  3477. interprets it as "match either 'foo' or 'bar' if they come at the beginning
  3478. of a line". The latter is in agreement with the POSIX specification.
  3479. <dt>-<dd>The special table-size declarations such as
  3480. <b>%a</b>
  3481. supported by
  3482. <i>lex</i>
  3483. are not required by
  3484. <i>flex</i>
  3485. scanners;
  3486. <i>flex</i>
  3487. ignores them.
  3488. <dt>-<dd>The name
  3489. FLEX_SCANNER
  3490. is #define'd so scanners may be written for use with either
  3491. <i>flex</i>
  3492. or
  3493. <i>lex.</i>
  3494. Scanners also include
  3495. <b>YY_FLEX_MAJOR_VERSION</b>
  3496. and
  3497. <b>YY_FLEX_MINOR_VERSION</b>
  3498. indicating which version of
  3499. <i>flex</i>
  3500. generated the scanner
  3501. (for example, for the 2.5 release, these defines would be 2 and 5
  3502. respectively).
  3503. </dl>
  3504. <p>
  3505. The following
  3506. <i>flex</i>
  3507. features are not included in
  3508. <i>lex</i>
  3509. or the POSIX specification:
  3510. <pre>
  3511. <p><br> C++ scanners
  3512. <br> %option
  3513. <br> start condition scopes
  3514. <br> start condition stacks
  3515. <br> interactive/non-interactive scanners
  3516. <br> yy_scan_string() and friends
  3517. <br> yyterminate()
  3518. <br> yy_set_interactive()
  3519. <br> yy_set_bol()
  3520. <br> YY_AT_BOL()
  3521. <br> &lt;&lt;EOF&gt;&gt;
  3522. <br> &lt;*&gt;
  3523. <br> YY_DECL
  3524. <br> YY_START
  3525. <br> YY_USER_ACTION
  3526. <br> YY_USER_INIT
  3527. <br> #line directives
  3528. <br> %{}'s around actions
  3529. <br> multiple actions on a line
  3530. <br>
  3531. <p><br></pre>
  3532. plus almost all of the flex flags.
  3533. The last feature in the list refers to the fact that with
  3534. <i>flex</i>
  3535. you can put multiple actions on the same line, separated with
  3536. semi-colons, while with
  3537. <i>lex,</i>
  3538. the following
  3539. <pre>
  3540. <p><br> foo handle_foo(); ++num_foos_seen;
  3541. <br>
  3542. <p><br></pre>
  3543. is (rather surprisingly) truncated to
  3544. <pre>
  3545. <p><br> foo handle_foo();
  3546. <br>
  3547. <p><br></pre>
  3548. <i>flex</i>
  3549. does not truncate the action. Actions that are not enclosed in
  3550. braces are simply terminated at the end of the line.
  3551. </ul><H2>DIAGNOSTICS </H2><ul>
  3552. <p>
  3553. <i>warning,</i> <i>rule</i> <i>cannot</i> <i>be</i> <i>matched</i>
  3554. indicates that the given rule
  3555. cannot be matched because it follows other rules that will
  3556. always match the same text as it. For
  3557. example, in the following "foo" cannot be matched because it comes after
  3558. an identifier "catch-all" rule:
  3559. <pre>
  3560. <p><br> [a-z]+ got_identifier();
  3561. <br> foo got_foo();
  3562. <br>
  3563. <p><br></pre>
  3564. Using
  3565. <b>REJECT</b>
  3566. in a scanner suppresses this warning.
  3567. <p>
  3568. <i>warning,</i>
  3569. <b>-s</b>
  3570. <i>option given but default rule can be matched
  3571. </i>means that it is possible (perhaps only in a particular start condition)
  3572. that the default rule (match any single character) is the only one
  3573. that will match a particular input. Since
  3574. <b>-s</b>
  3575. was given, presumably this is not intended.
  3576. <p>
  3577. <i>reject_used_but_not_detected</i> <i>undefined</i>
  3578. or
  3579. <i>yymore_used_but_not_detected</i> <i>undefined</i> <i>-</i>
  3580. These errors can occur at compile time. They indicate that the
  3581. scanner uses
  3582. <b>REJECT</b>
  3583. or
  3584. <b>yymore()</b>
  3585. but that
  3586. <i>flex</i>
  3587. failed to notice the fact, meaning that
  3588. <i>flex</i>
  3589. scanned the first two sections looking for occurrences of these actions
  3590. and failed to find any, but somehow you snuck some in (via a #include
  3591. file, for example). Use
  3592. <b>%option</b> <b>reject</b>
  3593. or
  3594. <b>%option</b> <b>yymore</b>
  3595. to indicate to flex that you really do use these features.
  3596. <p>
  3597. <i>flex</i> <i>scanner</i> <i>jammed</i> <i>-</i>
  3598. a scanner compiled with
  3599. <b>-s</b>
  3600. has encountered an input string which wasn't matched by
  3601. any of its rules. This error can also occur due to internal problems.
  3602. <p>
  3603. <i>token</i> <i>too</i> <i>large,</i> <i>exceeds</i> <i>YYLMAX</i> <i>-</i>
  3604. your scanner uses
  3605. <b>%array</b>
  3606. and one of its rules matched a string longer than the
  3607. <b>YYLMAX</b>
  3608. constant (8K bytes by default). You can increase the value by
  3609. #define'ing
  3610. <b>YYLMAX</b>
  3611. in the definitions section of your
  3612. <i>flex</i>
  3613. input.
  3614. <p>
  3615. <i>scanner</i> <i>requires</i> <i>-8</i> <i>flag</i> <i>to</i>
  3616. <i>use</i> <i>the</i> <i>character</i> <i>'x'</i> <i>-</i>
  3617. Your scanner specification includes recognizing the 8-bit character
  3618. <i>'x'</i>
  3619. and you did not specify the -8 flag, and your scanner defaulted to 7-bit
  3620. because you used the
  3621. <b>-Cf</b>
  3622. or
  3623. <b>-CF</b>
  3624. table compression options. See the discussion of the
  3625. <b>-7</b>
  3626. flag for details.
  3627. <p>
  3628. <i>flex</i> <i>scanner</i> <i>push-back</i> <i>overflow</i> <i>-</i>
  3629. you used
  3630. <b>unput()</b>
  3631. to push back so much text that the scanner's buffer could not hold
  3632. both the pushed-back text and the current token in
  3633. <b>yytext.</b>
  3634. Ideally the scanner should dynamically resize the buffer in this case, but at
  3635. present it does not.
  3636. <p>
  3637. <i>input buffer overflow, can't enlarge buffer because scanner uses REJECT -
  3638. </i>the scanner was working on matching an extremely large token and needed
  3639. to expand the input buffer. This doesn't work with scanners that use
  3640. <b>REJECT.
  3641. </b>
  3642. <p>
  3643. <i>fatal flex scanner internal error--end of buffer missed -
  3644. </i>This can occur in an scanner which is reentered after a long-jump
  3645. has jumped out (or over) the scanner's activation frame. Before
  3646. reentering the scanner, use:
  3647. <pre>
  3648. <p><br> yyrestart( yyin );
  3649. <br>
  3650. <p><br></pre>
  3651. or, as noted above, switch to using the C++ scanner class.
  3652. <p>
  3653. <i>too</i> <i>many</i> <i>start</i> <i>conditions</i> <i>in</i> <i>&lt;&gt;</i> <i>construct!</i> <i>-</i>
  3654. you listed more start conditions in a &lt;&gt; construct than exist (so
  3655. you must have listed at least one of them twice).
  3656. </ul><H2>FILES </H2><ul>
  3657. <p><dl compact><dt><b>-lfl</b>
  3658. <dd>library with which scanners must be linked.
  3659. <dt><i>lex.yy.c</i>
  3660. <dd>generated scanner (called
  3661. <i>lexyy.c</i>
  3662. on some systems).
  3663. <dt><i>lex.yy.cc</i>
  3664. <dd>generated C++ scanner class, when using
  3665. <b>-+.</b>
  3666. <dt><i>&lt;FlexLexer.h&gt;</i>
  3667. <dd>header file defining the C++ scanner base class,
  3668. <b>FlexLexer,</b>
  3669. and its derived class,
  3670. <b>yyFlexLexer.</b>
  3671. <dt><i>flex.skl</i>
  3672. <dd>skeleton scanner. This file is only used when building flex, not when
  3673. flex executes.
  3674. <dt><i>lex.backup</i>
  3675. <dd>backing-up information for
  3676. <b>-b</b>
  3677. flag (called
  3678. <i>lex.bck</i>
  3679. on some systems).
  3680. </dl>
  3681. </ul><H2>DEFICIENCIES / BUGS </H2><ul>
  3682. <p>
  3683. Some trailing context
  3684. patterns cannot be properly matched and generate
  3685. warning messages ("dangerous trailing context"). These are
  3686. patterns where the ending of the
  3687. first part of the rule matches the beginning of the second
  3688. part, such as "zx*/xy*", where the 'x*' matches the 'x' at
  3689. the beginning of the trailing context. (Note that the POSIX draft
  3690. states that the text matched by such patterns is undefined.)
  3691. <p>
  3692. For some trailing context rules, parts which are actually fixed-length are
  3693. not recognized as such, leading to the abovementioned performance loss.
  3694. In particular, parts using '|' or {n} (such as "foo{3}") are always
  3695. considered variable-length.
  3696. <p>
  3697. Combining trailing context with the special '|' action can result in
  3698. <i>fixed</i>
  3699. trailing context being turned into the more expensive
  3700. <i>variable</i>
  3701. trailing context. For example, in the following:
  3702. <pre>
  3703. <p><br> %%
  3704. <br> abc |
  3705. <br> xyz/def
  3706. <br>
  3707. <p><br></pre>
  3708. <p>
  3709. Use of
  3710. <b>unput()</b>
  3711. invalidates yytext and yyleng, unless the
  3712. <b>%array</b>
  3713. directive
  3714. or the
  3715. <b>-l</b>
  3716. option has been used.
  3717. <p>
  3718. Pattern-matching of NUL's is substantially slower than matching other
  3719. characters.
  3720. <p>
  3721. Dynamic resizing of the input buffer is slow, as it entails rescanning
  3722. all the text matched so far by the current (generally huge) token.
  3723. <p>
  3724. Due to both buffering of input and read-ahead, you cannot intermix
  3725. calls to &lt;stdio.h&gt; routines, such as, for example,
  3726. <b>getchar(),</b>
  3727. with
  3728. <i>flex</i>
  3729. rules and expect it to work. Call
  3730. <b>input()</b>
  3731. instead.
  3732. <p>
  3733. The total table entries listed by the
  3734. <b>-v</b>
  3735. flag excludes the number of table entries needed to determine
  3736. what rule has been matched. The number of entries is equal
  3737. to the number of DFA states if the scanner does not use
  3738. <b>REJECT,</b>
  3739. and somewhat greater than the number of states if it does.
  3740. <p>
  3741. <b>REJECT</b>
  3742. cannot be used with the
  3743. <b>-f</b>
  3744. or
  3745. <b>-F</b>
  3746. options.
  3747. <p>
  3748. The
  3749. <i>flex</i>
  3750. internal algorithms need documentation.
  3751. </ul><H2>SEE ALSO </H2><ul>
  3752. <p>
  3753. lex(1), yacc(1), sed(1), awk(1).
  3754. <p>
  3755. John Levine, Tony Mason, and Doug Brown,
  3756. <i>Lex</i> <i>&amp;</i> <i>Yacc,</i>
  3757. O'Reilly and Associates. Be sure to get the 2nd edition.
  3758. <p>
  3759. M. E. Lesk and E. Schmidt,
  3760. <i>LEX</i> <i>-</i> <i>Lexical</i> <i>Analyzer</i> <i>Generator</i>
  3761. <p>
  3762. Alfred Aho, Ravi Sethi and Jeffrey Ullman,
  3763. <i>Compilers:</i> <i>Principles,</i> <i>Techniques</i> <i>and</i> <i>Tools,</i>
  3764. Addison-Wesley (1986). Describes the pattern-matching techniques used by
  3765. <i>flex</i>
  3766. (deterministic finite automata).
  3767. </ul><H2>AUTHOR </H2><ul>
  3768. Vern Paxson, with the help of many ideas and much inspiration from
  3769. Van Jacobson. Original version by Jef Poskanzer. The fast table
  3770. representation is a partial implementation of a design done by Van
  3771. Jacobson. The implementation was done by Kevin Gong and Vern Paxson.
  3772. <p>
  3773. Thanks to the many
  3774. <i>flex</i>
  3775. beta-testers, feedbackers, and contributors, especially Francois Pinard,
  3776. Casey Leedom,
  3777. Stan Adermann, Terry Allen, David Barker-Plummer, John Basrai,
  3778. Nelson H.F. Beebe, [email protected],
  3779. Karl Berry, Peter A. Bigot, Simon Blanchard,
  3780. Keith Bostic, Frederic Brehm, Ian Brockbank, Kin Cho, Nick Christopher,
  3781. Brian Clapper, J.T. Conklin,
  3782. Jason Coughlin, Bill Cox, Nick Cropper, Dave Curtis, Scott David
  3783. Daniels, Chris G. Demetriou, Theo Deraadt,
  3784. Mike Donahue, Chuck Doucette, Tom Epperly, Leo Eskin,
  3785. Chris Faylor, Chris Flatters, Jon Forrest, Joe Gayda, Kaveh R. Ghazi,
  3786. Eric Goldman, Christopher M. Gould, Ulrich Grepel, Peer Griebel,
  3787. Jan Hajic, Charles Hemphill, NORO Hideo,
  3788. Jarkko Hietaniemi, Scott Hofmann,
  3789. Jeff Honig, Dana Hudes, Eric Hughes, John Interrante,
  3790. Ceriel Jacobs, Michal Jaegermann, Sakari Jalovaara, Jeffrey R. Jones,
  3791. Henry Juengst, Klaus Kaempf, Jonathan I. Kamens, Terrence O Kane,
  3792. Amir Katz, [email protected], Kevin B. Kenny,
  3793. Steve Kirsch, Winfried Koenig, Marq Kole, Ronald Lamprecht,
  3794. Greg Lee, Rohan Lenard, Craig Leres, John Levine, Steve Liddle, Mike Long,
  3795. Mohamed el Lozy, Brian Madsen, Malte, Joe Marshall,
  3796. Bengt Martensson, Chris Metcalf,
  3797. Luke Mewburn, Jim Meyering, R. Alexander Milowski, Erik Naggum,
  3798. G.T. Nicol, Landon Noll, James Nordby, Marc Nozell,
  3799. Richard Ohnemus, Karsten Pahnke,
  3800. Sven Panne, Roland Pesch, Walter Pelissero, Gaumond
  3801. Pierre, Esmond Pitt, Jef Poskanzer, Joe Rahmeh, Jarmo Raiha,
  3802. Frederic Raimbault, Pat Rankin, Rick Richardson,
  3803. Kevin Rodgers, Kai Uwe Rommel, Jim Roskind, Alberto Santini,
  3804. Andreas Scherer, Darrell Schiebel, Raf Schietekat,
  3805. Doug Schmidt, Philippe Schnoebelen, Andreas Schwab,
  3806. Alex Siegel, Eckehard Stolz, Jan-Erik Strvmquist,
  3807. Mike Stump, Paul Stuart, Dave Tallman, Ian Lance Taylor,
  3808. Chris Thewalt, Richard M. Timoney, Jodi Tsai,
  3809. Paul Tuinenga, Gary Weik, Frank Whaley, Gerhard Wilhelms, Kent Williams, Ken
  3810. Yap, Ron Zellar, Nathan Zelle, David Zuhn,
  3811. and those whose names have slipped my marginal
  3812. mail-archiving skills but whose contributions are appreciated all the
  3813. same.
  3814. <p>
  3815. Thanks to Keith Bostic, Jon Forrest, Noah Friedman,
  3816. John Gilmore, Craig Leres, John Levine, Bob Mulcahy, G.T.
  3817. Nicol, Francois Pinard, Rich Salz, and Richard Stallman for help with various
  3818. distribution headaches.
  3819. <p>
  3820. Thanks to Esmond Pitt and Earle Horton for 8-bit character support; to
  3821. Benson Margulies and Fred Burke for C++ support; to Kent Williams and Tom
  3822. Epperly for C++ class support; to Ove Ewerlid for support of NUL's; and to
  3823. Eric Hughes for support of multiple buffers.
  3824. <p>
  3825. This work was primarily done when I was with the Real Time Systems Group
  3826. at the Lawrence Berkeley Laboratory in Berkeley, CA. Many thanks to all there
  3827. for the support I received.
  3828. <p>
  3829. Send comments to [email protected].
  3830. </ul></body></html>