To understand how flex works, it will be enlightening to see the file lex.yy.c that it generates. We all know that the output of the lexical analyzer (flex) is the C function yylex(). While the actual generated yylex() routine is complex, its outline is provided below.
1 int yylex(void)Here are some useful information to help in understanding the big picture.
2 {
3 YY_USER_INIT;
4 while (1) {
5 read_input_and_match_pattern();
6 int yy_act = find_correct_action();
7 switch( yy_act ) {
8 case 1:
9 YY_RULE_SETUP;
10 /* user-defined action */
11 YY_BREAK;
12 break;
13 /* more-user-defined-actions */
14 case 82:
15 YY_RULE_SETUP;
16 /* pre-defined action */
17 YY_BREAK;
18 break;
19 /* more-pre-defined-actions */
20 default:
21 /* error: no action */
22 }
23 }
24 }
- The macros YY_USER_INIT, YY_RULE_SETUP, YY_BREAK can be defined by the user. They are the hooks provided by the tool, so that you can have a little bit of customization for the generated yylex() function. Under normal circumstances, you will be pointing them to your own functions.
- I hope you are aware of the flex rules that you specify. The rules have patterns in the left hand side and user-defined actions in the right hand side. These user defined actions would be part of the switch-case statement. This information would be very useful for you to understand where the C-code that you write in the flex input file goes.
- Apart from the user-defined actions, there are also pre-defined actions. The predefined action for any character is to print it to the standard output. Similarly, there are predefined actions for the end-of-file.
- If you look at the above yylex() function, you will notice that it will not return unless the user-defined action uses the return statement. When you have to make flex and yacc work together, then you have to return from the lex generated scanner after every token.
0 comments:
Post a Comment