[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"detail-sidebar-cat-1-en-105":3,"doc-seo-573465-105":53,"doc-detail-573465-en":120},{"code":4,"msg":5,"data":6},0,"success",[7,14,19,24,29,34,39,44,49],{"id":8,"doc_module":9,"doc_module_name":10,"category_name":11,"show_sort_weight":12,"slug":13},11,1,"Template","Presentations",90,"presentations",{"id":15,"doc_module":9,"doc_module_name":10,"category_name":16,"show_sort_weight":17,"slug":18},12,"Resumes",80,"resumes",{"id":20,"doc_module":9,"doc_module_name":10,"category_name":21,"show_sort_weight":22,"slug":23},14,"Invoices",70,"invoices",{"id":25,"doc_module":9,"doc_module_name":10,"category_name":26,"show_sort_weight":27,"slug":28},15,"Posters",60,"posters",{"id":30,"doc_module":9,"doc_module_name":10,"category_name":31,"show_sort_weight":32,"slug":33},16,"Social Media",50,"social-media",{"id":35,"doc_module":9,"doc_module_name":10,"category_name":36,"show_sort_weight":37,"slug":38},17,"Forms",40,"forms",{"id":40,"doc_module":9,"doc_module_name":10,"category_name":41,"show_sort_weight":42,"slug":43},18,"Letters",30,"letters",{"id":45,"doc_module":9,"doc_module_name":10,"category_name":46,"show_sort_weight":47,"slug":48},21,"Paper Templates",5,"papers-templates",{"id":50,"doc_module":9,"doc_module_name":10,"category_name":51,"show_sort_weight":4,"slug":52},158,"General","general-158",{"code":4,"msg":54,"data":55},"ok",{"site_id":56,"language":57,"slug":58,"title":59,"keywords":60,"description":61,"schema_data":62,"social_meta":113,"head_meta":115,"extra_data":117,"updated_unix":119},105,"en","flex-in-a-nutshell-handout-5-quick-introduction","flex In A Nutshell - Handout 5 - Quick Introduction","","CS143 Summer 2012 handout 05 presents flex, a fast lexical analyzer generator, and explains how to define scanner rules that map input patterns to C code actions. It describes the flex workflow: generating a combined NFA, converting to a minimized DFA, producing lex.yy.c, and using yylex with the lex library or a custom main. The handout outlines flex input structure, Declarations/Definitions/Rules/User subroutines, and key rule pattern operators such as character classes, repetition, anchoring, lookahead, and named definitions.",{"@graph":63,"@context":112},[64,80,95],{"@type":65,"itemListElement":66},"BreadcrumbList",[67,71,74,77],{"item":68,"name":69,"@type":70,"position":9},"https://docshare.wps.com","Home","ListItem",{"item":72,"name":10,"@type":70,"position":73},"https://docshare.wps.com/template/",2,{"item":75,"name":11,"@type":70,"position":76},"https://docshare.wps.com/template/presentations/",3,{"item":78,"name":59,"@type":70,"position":79},"https://docshare.wps.com/template/flex-in-a-nutshell-handout-5-quick-introduction/573465/",4,{"url":78,"name":59,"@type":81,"author":82,"headline":59,"publisher":85,"fileFormat":88,"inLanguage":57,"description":61,"dateModified":89,"datePublished":89,"encodingFormat":88,"isAccessibleForFree":90,"interactionStatistic":91},"DigitalDocument",{"name":83,"@type":84},"Muffet","Person",{"url":68,"name":86,"@type":87},"DocShare","Organization","application/pdf","2026-10-14",true,{"@type":92,"interactionType":93,"userInteractionCount":4},"InteractionCounter",{"@type":94},"ViewAction",{"@type":96,"mainEntity":97},"FAQPage",[98,104,108],{"name":99,"@type":100,"acceptedAnswer":101},"What is flex and how does it generate a scanner?","Question",{"text":102,"@type":103},"flex generates a scanner by taking a specification of regular-expression patterns and C actions, then building and minimizing automata to recognize the specified tokens. It outputs C code implementing the scanner and provides yylex() to scan tokens.","Answer",{"name":105,"@type":100,"acceptedAnswer":106},"How is a flex input (.l) file structured?",{"text":107,"@type":103},"A flex input file has optional Declarations and User subroutines, an optional Definitions section, and a required Rules section. The Declarations and subroutines are copied verbatim into the generated C file, while Definitions supports named pattern substitution.",{"name":109,"@type":100,"acceptedAnswer":110},"How does flex choose among matching patterns when scanning input?",{"text":111,"@type":103},"flex finds the longest possible match among all available patterns. If multiple patterns match an equally long sequence, flex uses the pattern that appears first in the file.","https://schema.org",{"og:url":78,"og:type":114,"og:title":59,"og:site_name":86,"og:description":61},"article",{"robots":116,"canonical":78},"index,follow",{"doc_id":118,"site_id":56},573465,1791940380,{"code":4,"msg":5,"data":121},{"doc_id":118,"user_id":122,"nickname":83,"user_avatar":123,"doc_module":9,"category_id":8,"category_name":11,"doc_title":59,"doc_description":61,"doc_content":124,"file_id":125,"file_url":126,"file_type":127,"file_size":128,"view_count":4,"is_deleted":4,"is_public":9,"is_downloadable":9,"audit_status":9,"page_count":47,"language":129,"language_code":57,"site_id":56,"html_lang":57,"table_of_contents":130,"faqs":131,"seo_title":132,"seo_description":61,"update_tm":119,"read_time":73},962090768927,"https://ap-avatar.wpscdn.com/davatar_085a072bc5b1113ac321206ff7593b45","CS143  \nSummer 2012  \nHandout 05 June 27, 2012  \nflex In A Nutshell  \nHandout written by Julie Zelenski with minor edits by Keith Schwarz.  \nflex is a fast lexical analyzer generator. You specify the scanner you want in the form of patterns to match and actions to apply for each token. flex takes your specification and generates a combined NFA to recognize all your patterns, converts it to an equivalent DFA, minimizes the automaton as much as possible, and generates C code that will implement it. flex is similar to another tool, lex, designed by Lesk and Schmidt, and bears many similarities to it. While we will use flex in the course, almost all features we use are also present in the original lex.  \nThis handout is designed to give you a quick introduction to the flex tool. This should serve as a useful reference for the first programming assignment. However, you should be aware that in order to complete the first assignment, you may need to use some more advanced features of flex that aren't covered here. To learn more about flex, run info flex, or read the documentation at [http://flex.sourceforge.net/manual/](http://flex.sourceforge.net/manual/) .  \nHow It Works  \nflex is designed for use with C code and generates a scanner written in C. The scanner is specified using regular expressions for patterns and C code for the actions. The specification files are traditionally identified by their .l extension. You invoke flex on a .l file and it creates lex.yy .c , a source file containing a wad of unrecognizable C code that implements a FA encoding all your rules and including the code for the actions you specified. The file provides an extern function yylex() that will scan one token. You compile that C file normally, link with the lex library, and you have built a scanner! The scanner reads from stdin and writes to stdout by default.  \n% lex myFile .l  \n% gcc -o scan lex.yy .c -ll % ./scan  \ncreates lex.yy .c containing C code for scanner compiles scanner, links with lex lib  \nexecutes scanner, will read from stdin  \nLinking with the lex library provides a simple main that will repeatedly calls yylex until it reaches EOF. You can also compile and link the scanner into your project and use your own main to control when tokens are scanned. The Makefiles we provide for the projects will execute the compilation steps for you, but it is worthwhile to understand the steps required.  \n2  \nA flex Input File  \nflex input files are structured as follows:  \n%{  \nDeclarations  \n%}  \nDefinitions  \n%%  \nRules  \n%%  \nUser subroutines  \nThe optional Declarations and User subroutines sections are used for ordinary C code that you want copied verbatim to the generated C file. Declarations are copied to the top of the file, user subroutines to the bottom. The optional Definitions section is where you specify options for the scanner and can set up definitions to give names to regular expressions as a simple substitution mechanism that allows for more readable entries in the Rules section that follows. The required Rules section is where you specified the patterns that identify your tokens and the action to perform upon recognizing each token.  \nflex Rules  \nA rule has a regular expression (called the pattern) and an associated set of C statements (called the action) . The idea is that whenever the scanner reads an input sequence that matches a pattern, it executes the action to process it. This is a substantial generalization of regular expressions that allows the tool to be used in many different contexts.  \nIn specifying patterns, flex supports a fairly rich set of conveniences (character classes, specific repetition, etc.) beyond our formal language definition of a regular expression. These features don't add expressive power, but simply allow you to construct complicated patterns more succinctly. The table below shows some operators to give you an idea of what is available. For more details, see the web or man pages.  \nCharacter classes  \nChar","cbCailzEFqEJF54m","https://ap.wps.com/l/cbCailzEFqEJF54m","pdf",348819,"English","# How It Works\n## Flex Input File Structure\n## Flex Rules and Actions\n## Flex Pattern Operators (Character Classes, Repetition, Anchors, Lookahead)","[{\"question\":\"What is flex and how does it generate a scanner?\",\"answer\":\"flex generates a scanner by taking a specification of regular-expression patterns and C actions, then building and minimizing automata to recognize the specified tokens. It outputs C code implementing the scanner and provides yylex() to scan tokens.\"},{\"question\":\"How is a flex input (.l) file structured?\",\"answer\":\"A flex input file has optional Declarations and User subroutines, an optional Definitions section, and a required Rules section. The Declarations and subroutines are copied verbatim into the generated C file, while Definitions supports named pattern substitution.\"},{\"question\":\"How does flex choose among matching patterns when scanning input?\",\"answer\":\"flex finds the longest possible match among all available patterns. If multiple patterns match an equally long sequence, flex uses the pattern that appears first in the file.\"}]","flex In A Nutshell - Handout 5 - Quick Introduction | PDF"]