Why in my flex lexer sheet does the score increase in one case and not increase in another?

My purpose (it is not evaluated, and I get nothing from its solution) is to write lexer / scanner / tokenizer (however you want to call it). flex is used for this class. The lexer is written for Object Object Oriented Language or COOL.

In this language, multi-line comments begin and end as follows:

(* line 1
line 2
line 3 *)

These comments may be nested. In other words, the following is true:

(* comment1 start (* comment 2 start (* comment 3 *) comemnt 2 end *) comment 1 end *)

Strings in this language are regular quotation marks, as in C. Here is an example:

"This is a string"
"This is another string"

There is also an additional rule that says that there can be no EOF in a comment or line. For example, the following is not valid:

(* comment <EOF>
"My string <EOF>

. , \n.

, :

lexer EOF , 1, , EOF , .

, lexer

Line 1: (* this is a comment <EOF>

:

`# 2 "EOF "

, :

Line 1: "This is a string <EOF>

:

`# 1 ERROR " EOF"

, ( ). , . , , .

    <BLOCK_COMMENT>{
  [^\n*)(]+ ; /* Eat the comment in chunks */
  ")" ; /* Eat a lonely right paren */
  "(" ; /* Eat a lonely left paren */
  "*" ; /* Eat a lonely star */
  \n curr_lineno++; /* increment the line count */
}

  /*
       Can't have EOF in the middle of a block comment
     */
<BLOCK_COMMENT><<EOF>>  {
    cool_yylval.error_msg = "EOF in comment";
  /*
     Need to return to INITIAL, otherwise the program will be stuck
     in the infinite loop. This was determined experimentally.
   */
  BEGIN(INITIAL);
  return ERROR;
}

  /* Match <back slash>\n or \n */
<STRING>\\\n|\n {
  curr_lineno++;
}
<STRING><<EOF>> {
    /* String may not have an EOF character */
  cool_yylval.error_msg = "String contains EOF character";

  /*
     Need to return to INITIAL, otherwise the program will be stuck
     in the infinite loop. This was determined experimentally.
   */
  BEGIN(INITIAL);
  return ERROR;
}

, :

, ?

.

+4
source share

Source: https://habr.com/ru/post/1599667/


All Articles