【正文】
“ a b c \ ” m a c ” “ a b c \ Analyzing process [1] input buffer regular expression yytext Analyzing process [2] \[^]*\ “ a b c \ ” m a c ” “ a b c \ “ yyleng = 6 “ a b c \ ” m a c ” “ a b c \ ” m a c ” \[^]*\ “ a b c \ “ “ a b c \ “ a b c \ ” m a c ” \[^]*\ “ a b c \ “ m “ a b c \ ” m a c ” \[^]*\ “ a b c \ “ m a unput character ” input buffer regular expression yytext Analyzing process [3] “ a b c \ ” m a c ” \[^]*\ “ a b c \ “ m a c “ a b c \ ” m a c ” \[^]*\ “ a b c \ “ m a c “ fails “ a b c \ “ m a c “ yytext \0 yyleng = 10 Starting condition (開始狀態(tài) ) ? flex provides a mechanism for conditionally activating rules. Any rule whose pattern is prefixed with sc will only be active when the scanner is in the start condition named sc. ? Start conditions are declared in the definitions (first) section of the input using unindented lines beginning with either `%s39。 %x example %% examplefoo do_something()。 The following three lex input are equivalent Inclusive . exclusive How to recognize ment in C, /* … */ CMNT is an exclusive starting condition If read /*, change to CMNT If read */, back to INTIAL Can you explain output? Exercise ? C++ support another kind of ment, starting by //, write a regular expression to recognize this kind of ment and build it into flex input file. Write a C program with Cment and C++ment to test scanner generated by flex. ? Depict state transition diagram for Cment and C++ ment, write code to implement this state transition diagram and measure program size. Do you think flex helps you identify Cment very well? ? Can you have other method to identify Cment by using flex? Hint: use flex to identify /*, then write code to find */ by yyinput() or input() OutLine ? What is lex ? Regular expression ? Finite state machine ? Content of flex ? Application scan configuration file of linear programming Cprogram analyzer m ins u b je c t to , 0Tz c xA x b x??? Application 1: configuration file of Linear Programming objective /objective constraint /constraint x1 x2 x4 x5 integer real number + * token C++ment = = = Objective: read configuration file, extract coefficient of vector c, b and matrix A, then output c, b, A You need to add rule for C++ment substitution rule how many lines are processed definition of code of token driver: show all tokens [1] 1. Space character is removed automatically 2. It is not necessary to keep space character between two tokens since flex would identify them very well driver: show all tokens [2] Exercise ? Complete input file for flex (add rule to deal with C++ment) and test the scanner for different cases. ? Depict state transition diagram to collect information from configuration file and construct vector c, b and matrix A S0 S1 S2 objective constraint Applicatoin2: C program analyzer token Lexeme identifier x1 integer 1234 real , Arithmetic operator +, , *, /, % Increment operator ++, Arithmetic assignment operator +=, =, *=, /=, %=, = Relational operator ==, !=, , , =, = Boolean logical operator amp。 , . , “ “ , ? ? Conditional operator ? : Escape sequence \n, \t, \