1 / 10

Flex: A fast Lexical Analyzer Generator

Flex: A fast Lexical Analyzer Generator. CSE470: Spring 2000 Updated by Prasad. Motivation. Use Flex to perform Text Processing In structured programming two tasks occur repeatedly:

viveka
Download Presentation

Flex: A fast Lexical Analyzer Generator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Flex: A fast Lexical Analyzer Generator CSE470: Spring 2000 Updated by Prasad

  2. Motivation • Use Flex to perform Text Processing • In structured programming two tasks occur repeatedly: • Lexical Analysis: Dividing the input into meaningful units. For a C program the units are variables, constants, keywords, operators, punctuation etc. These units also called as tokens. (Use Flex) • Parsing: Involves finding the relationship between input tokens. For a C program, one needs to identify valid expressions, statements, blocks, procedures etc. (Use Yacc or Bison)

  3. An Example • Recognizing all keywords of a Language. Probably also want to identify and remove comments. /* The following loop computes exponent of number */ int i, number, result; result = 1; for (i=0; i<power; i++) { result = result * number; }

  4. About Flex • Flex: Fast Lexical Analyzer Generator. • Produces Lexical Analyzers in a fast and easy manner. • Given a Flex source file, Flex generates an output C source code file lex.yy.c which defines the scanning routine yylex(). • Flex source file (say sample.l) should contain rules for identifying tokens in the input.

  5. Flex Source File (sample.l) Flex Compiler (Flex) Lexical Analyzer Code (lex.yy.c) C Compiler Lexical Analyzer executable Input Text File Output: Tokens Parser

  6. Flex Source • Consists of three sections: definitions, rules and user-defined routines. %{Declaration Section%} Definitions section %%Rules Section%% User routines Section

  7. Rules • Format <regular-expression> {<actions>} • When a Lexical Analyzer is run (or the scanning routine yylex() is called) it analyzes the input for the occurrences of text patterns that match the regular expressions. When ever it finds one it executes the corresponding action. • Flex stores the matched text in a global string variable called yytext.

  8. Examples: [0-9]+ { printf(“An integer %s \n”, yytext); } [a-z][a-z0-9]* { printf(“An identifier %s \n”, yytext);} if | then | begin | end | function { printf(“A Keyword %s \n”, yytext); }

  9. Example to count lines %{ int linecount = 0; %} Digit [0-9] Identifier [a-zA-Z]{a-zA-Z0-9}* %% {Identifier} { printf(“%s: This is an identifier\n”, yytext); } \n { printf(“The line number is %d\n”, ++linecount); } [\t ]+ ; /*Ignore spaces */ . { printf(“Unrecognized character\n”); } %% main() { yylex(); }

  10. Example to remove comments %x comment %% "/*" BEGIN(comment); <comment>[^*\n]* /* eat anything that's not a '*' */ <comment>"*"+[^*/\n]* /* eat up '*'s not followed by '/'s */ <comment>\n ; <comment>"*"+"/" BEGIN(0); %%

More Related