Presentation is loading. Please wait.

Presentation is loading. Please wait.

Set 27 HANDLING COMMENTS IN LEX & SEARCHING & SORTING IN C.

Similar presentations


Presentation on theme: "Set 27 HANDLING COMMENTS IN LEX & SEARCHING & SORTING IN C."— Presentation transcript:

1 Set 27 HANDLING COMMENTS IN LEX & SEARCHING & SORTING IN C

2 FOUR MORE CONSTRUCTS IN LEX 1. Negative character classes. These resemble character classes, except that the first symbol is “^”. A negative character class such as e.g. [^b3-7] matches any single character except b,3,4,5,6,7.

3 2.Exclusive start conditions (implemented in Flex, but not every version of Lex). These specify additional conditions for matching a string. One defines a name for a start condition, e.g. “special”, by including the following statement at the end of the first section of the Lex definition file (i.e. after the macro definitions, if any): %x special

4 One activates such a start condition, by including in one’s C code BEGIN(special); and deactivates it with the C code BEGIN(0);

5 While the start condition “special” is active, only recursive expressions prefixed by are matched against the source by Lex Example. %x special % [1-9]+ sum = 1; Mary BEGIN(special); Henry sum = 2; Marge BEGIN(0);

6 Here the start condition “special” becomes active when “Mary” is matched against the source, and inactive when “Marge” is matched against the source. While the start condition is not active, Lex will attempt to match [1-9]+ against the source, but will not attempt to match “Henry” or “Marge”. While the start condition is active, Lex will attempt to match “Henry” and Marge”, but not attempt to match [1-9]+

7 THE PROBLEM WITH COMMENTS C, and several other languages, allow multi-line comments that begin with “/*” and endup with “*/”. One could skip over such comments, by providing a r.e. for them, with no C code associated, such as; “/*”([^*]*(“*”[^“/”])*)*“*/” ;

8 The problem with this is that the entire comment will be placed in yytext. Some comments are pages long, and may exceed the maximum size for yytext, causing error. A way around this is via the use of an exclusive start condition, active only while within the comment.

9 Here is an example of the code required to bypass comments, without the risk of overflow: %x comment % “/*” BEGIN(comment); “*/” BEGIN(0);. ; \n ; followed by r.e.’s without the prefix

10 3. ECHO is a built-in function that you can use in the C code associated with regular expressions. It writes out onto the standard output file the contents of yytext, and is useful for debugging. For example: % [1-9]+ {ECHO; … return digit;}

11 4. Matching with an “if” condition If R 1 and R 2 are regular expressions, then R 1 /R 2 is a regular expression which matches the same heads of input as R 1 R 2 but, in the program that Lex creates, after the match only the portion of the head which R 1 matches is removed and placed into yytext. As a result the the portion of the head that R 2 matches is then the head of the input.

12 You can think of R 1 /R 2 as meaning: match R 1 only if R 2 matches what follows

13 EXAMPLE In the C program that Lex constructs [0-9]+/00 with input 3472003abc matches 3472, which is put into yytext. The input after this is then 003abc On the other hand, [0-9]+/00 with input 34723abc does not match the head of the input.

14 BINARY SEARCH C provides the bsearch function for this purpose. It’s specified in STDLIB.H. The items to be searched may be an array of fixed length strings in alphabetic order, e.g. Names[8] [6] = {“Able “, “Betty”, “Cathy”, “David”, “Ed “, “Mary “, “Peter”, “Sean”};

15 The string to be searched for must be of the same size as those in the array, i.e. 5 in the above example. bsearch makes use of an auxiliary function that you have to supply. It’s purpose in this case is to determine which of two strings occurs before the other in the ordering (e.g. alphabetic) that you require.

16 A suitable function for alphabetic ordering is the following: int compare_strings(const void *name1, const void *name2) return(strcmp((char *) name1, (char *) name2));

17 Now we can search the list of 8 names, each of length 5, to see whether a string “Somebody” of length 5 occurs in it, using: bsearch(Somebody, (void *) Names, 8, 5, compare_strings)); It returns a value of NULL if the search is unsuccessful.

18 SORTING VIA QUICKSORT Assume the contents of “Names” are rearranged so that they are not in alphabetic order. We can here employ the C function qsort to sort them into alphabetic order. It too is specified in STDLIB.H.

19 The sort is accomplished, using: qsort((void *) Names, 8, 5, compare_strings) As before, this sorts the list “Names” of 8 strings, each of length 5, using the same compare_strings function described above.


Download ppt "Set 27 HANDLING COMMENTS IN LEX & SEARCHING & SORTING IN C."

Similar presentations


Ads by Google