c - lex program on counting no of comment lines

C - lex program on counting no of comment lines

To create a Lex program (lexical analyzer) that counts the number of comment lines in a C source file, you can define rules to recognize different types of comments in C (single-line // and multi-line /* */) and track the number of lines that match these comment patterns. Here's how you can implement this:

Lex Program Example

Save the following code in a file named comment_counter.l:

%{ #include <stdio.h> int comment_line_count = 0; %} /* Define tokens */ %x IN_COMMENT %x IN_LINE_COMMENT /* Rules */ %% "//" { BEGIN(IN_LINE_COMMENT); } "/*" { BEGIN(IN_COMMENT); } <IN_COMMENT>"*/" { BEGIN(INITIAL); } <IN_LINE_COMMENT>\n { BEGIN(INITIAL); comment_line_count++; } <IN_COMMENT>.|\n /* Skip multi-line comments */ <IN_LINE_COMMENT>. /* Skip single-line comments */ . /* Skip all other characters */ %% int main(int argc, char* argv[]) { /* Initialize the lexer */ yylex(); /* Print the count of comment lines */ printf("Number of comment lines: %d\n", comment_line_count); return 0; } 

Explanation:

  • Initial Setup (`%{ ... %}):

    • Here, we include necessary headers and declare a global variable comment_line_count to keep track of the number of comment lines.
  • Lexer States (%x ... %x):

    • IN_COMMENT and IN_LINE_COMMENT are defined as lexer states to manage the different states of comments.
  • Rules (%% ... %%):

    • "//": Enters IN_LINE_COMMENT state when encountering //.
    • "/*": Enters IN_COMMENT state when encountering /*.
    • <IN_COMMENT>"*/": Exits IN_COMMENT state upon encountering */.
    • <IN_LINE_COMMENT>\n: Exits IN_LINE_COMMENT state and increments comment_line_count upon encountering newline (\n).
  • Skipping Characters:

    • <IN_COMMENT>.|\n and <IN_LINE_COMMENT>.: Skip characters inside comments to avoid counting them as comment lines.
  • Main Function:

    • main(): Initializes the lexer (yylex()) and prints the count of comment lines (comment_line_count) after parsing the input file.

Building and Running:

  1. Compile the Lex program using:

    lex comment_counter.l 
  2. Generate C Code:

    cc lex.yy.c -o comment_counter -ll 
  3. Run the executable with a C source file as input:

    ./comment_counter input.c 

Replace input.c with your C source file. The program will count and print the number of comment lines in the file based on the rules defined.

Notes:

  • This Lex program assumes basic C comment structure handling (// for single-line and /* */ for multi-line comments).
  • Adjust the rules if your C source files have different comment patterns or additional complexities.

This example provides a basic framework for counting comment lines in C using Lex. Customize it further based on specific requirements or additional features needed for your lexer.

Examples

  1. Count the number of single-line comments in a C file using Lex.

    • Description: Implement a Lex program to count the occurrences of single-line comments in a C source file.
    • Code:
      %{ int comment_count = 0; %} %% "//".*\n { comment_count++; } "/*"([^*]|\*+[^*/])*\*+"/" { comment_count++; } .|\n ; %% int main() { yylex(); printf("Total single-line comments: %d\n", comment_count); return 0; } 
  2. Count both single-line and multi-line comments in a C file using Lex.

    • Description: Extend the Lex program to count both single-line and multi-line comments in a C source file.
    • Code:
      %{ int single_line_comments = 0; int multi_line_comments = 0; %} %% "//".*\n { single_line_comments++; } "/*"([^*]|\*+[^*/])*\*+"/" { multi_line_comments++; } .|\n ; %% int main() { yylex(); printf("Total single-line comments: %d\n", single_line_comments); printf("Total multi-line comments: %d\n", multi_line_comments); return 0; } 
  3. Exclude commented-out code from comment line count in a Lex program.

    • Description: Modify the Lex program to exclude lines that are commented-out code (lines with commented text but not recognized as actual comments).
    • Code:
      %{ int comment_lines = 0; int in_comment = 0; %} %x COMMENT %% "//".*\n { comment_lines++; } "/*" { in_comment = 1; BEGIN(COMMENT); } <COMMENT>"*/" { in_comment = 0; BEGIN(INITIAL); } <COMMENT>.|\n ; .|\n ; %% int main() { yylex(); printf("Total comment lines: %d\n", comment_lines); return 0; } 
  4. Implement a Lex program to count comment lines excluding empty lines.

    • Description: Write a Lex program that counts only non-empty lines containing comments in a C source file.
    • Code:
      %{ int comment_lines = 0; int in_comment = 0; %} %x COMMENT %% "//".*\n { if (yytext[0] != '/' && yytext[1] != '/') comment_lines++; } "/*" { in_comment = 1; BEGIN(COMMENT); } <COMMENT>"*/" { in_comment = 0; BEGIN(INITIAL); } <COMMENT>.|\n ; .|\n ; %% int main() { yylex(); printf("Total comment lines (excluding empty): %d\n", comment_lines); return 0; } 
  5. Count the number of lines containing only comments in a C file using Lex.

    • Description: Develop a Lex program that counts lines where the entire content is a comment (both single-line and multi-line).
    • Code:
      %{ int comment_lines = 0; int in_comment = 0; %} %x COMMENT %% "//".*\n { if (yytext[0] != '/' && yytext[1] != '/') comment_lines++; } "/*" { in_comment = 1; BEGIN(COMMENT); } <COMMENT>"*/" { in_comment = 0; BEGIN(INITIAL); comment_lines++; } <COMMENT>.|\n ; .|\n ; %% int main() { yylex(); printf("Total lines with only comments: %d\n", comment_lines); return 0; } 
  6. Handle nested multi-line comments while counting comment lines in a Lex program.

    • Description: Extend the Lex program to correctly handle nested multi-line comments when counting comment lines in C source files.
    • Code:
      %{ int comment_lines = 0; int in_comment = 0; %} %x COMMENT %% "//".*\n { if (yytext[0] != '/' && yytext[1] != '/') comment_lines++; } "/*" { in_comment = 1; BEGIN(COMMENT); comment_lines++; } <COMMENT>"*/" { in_comment = 0; BEGIN(INITIAL); } <COMMENT>"/*" { comment_lines++; } <COMMENT>.|\n ; .|\n ; %% int main() { yylex(); printf("Total comment lines (handling nested comments): %d\n", comment_lines); return 0; } 
  7. Count comment lines and lines of code separately in a Lex program.

    • Description: Modify the Lex program to differentiate and count lines of code versus comment lines in a C source file.
    • Code:
      %{ int code_lines = 0; int comment_lines = 0; int in_comment = 0; %} %x COMMENT %% "//".*\n { if (yytext[0] != '/' && yytext[1] != '/') comment_lines++; else code_lines++; } "/*" { in_comment = 1; BEGIN(COMMENT); comment_lines++; } <COMMENT>"*/" { in_comment = 0; BEGIN(INITIAL); comment_lines++; } <COMMENT>.|\n ; .|\n { code_lines++; } %% int main() { yylex(); printf("Total comment lines: %d\n", comment_lines); printf("Total lines of code: %d\n", code_lines); return 0; } 
  8. Exclude preprocessor directives from comment line count in a Lex program.

    • Description: Adjust the Lex program to exclude lines that are preprocessor directives (starting with #) from the comment line count.
    • Code:
      %{ int comment_lines = 0; int in_comment = 0; %} %x COMMENT %% "//".*\n { if (yytext[0] != '/' && yytext[1] != '/') comment_lines++; } "/*" { in_comment = 1; BEGIN(COMMENT); comment_lines++; } <COMMENT>"*/" { in_comment = 0; BEGIN(INITIAL); comment_lines++; } <COMMENT>.|\n ; "^#".*\n ; %% int main() { yylex(); printf("Total comment lines (excluding preprocessor directives): %d\n", comment_lines); return 0; } 
  9. Implement a Lex program to count comment lines across multiple C source files.

    • Description: Extend the Lex program to process and count comment lines across several C source files in a directory or project.
    • Code:
      %{ int comment_lines = 0; int in_comment = 0; %} %x COMMENT %% "//".*\n { if (yytext[0] != '/' && yytext[1] != '/') comment_lines++; } "/*" { in_comment = 1; BEGIN(COMMENT); comment_lines++; } <COMMENT>"*/" { in_comment = 0; BEGIN(INITIAL); comment_lines++; } <COMMENT>.|\n ; .|\n ; %% int main() { while (yylex()) {} printf("Total comment lines: %d\n", comment_lines); return 0; } 
  10. Count lines with inline comments as separate from other comment lines in a Lex program.

    • Description: Modify the Lex program to differentiate lines with inline comments (//) from other types of comment lines in C source files.
    • Code:
      %{ int inline_comment_lines = 0; int other_comment_lines = 0; int in_comment = 0; %} %% "//".*\n { inline_comment_lines++; } "/*" { in_comment = 1; BEGIN(COMMENT); other_comment_lines++; } <COMMENT>"*/" { in_comment = 0; BEGIN(INITIAL); other_comment_lines++; } <COMMENT>.|\n ; .|\n ; %% int main() { yylex(); printf("Total inline comment lines: %d\n", inline_comment_lines); printf("Total other comment lines: %d\n", other_comment_lines); return 0; } 

More Tags

android-timepicker django-class-based-views select-options java-me sparse-matrix client pdfrw type-conversion require gson

More Programming Questions

More Tax and Salary Calculators

More Biology Calculators

More Trees & Forestry Calculators

More Stoichiometry Calculators