Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java JFlex code first import java.io.*; import java.util.*; %% %{ public int idcount = 0; public int kcount = 0; public int ccount =

In Java JFlex

code first

import java.io.*; import java.util.*; %% %{ public int idcount = 0; public int kcount = 0; public int ccount = 0; public int numcount=0; public int qcount = 0; public static void main(String argv[]) throws java.io.IOException { A2 yy = new A2(new FileReader("A2.input")); while (yy.yylex()>=0){ yy.yylex(); } } %} %notunix %integer %state COMMENT %line %class A2 %eofval{ return; %eofval}

Keywords = MAIN|BEGIN|END ID = [a-zA-Z_][a-zA-Z0-9_]* Num = [0-9]*[.0-9]*

%% {Keywords} {kcount++;} {Num} { numcount++;} {ID} { idcount++; } "/**" {yybegin(COMMENT);} "**/" {yybegin(YYINITIAL);} . {} | {} . {}

and this is the A2.input file

/** keywords (such as read and if) and identifiers should not be counted in * comments. * Comments can be longer than one line. **/ MAIN f() BEGIN you are not required to check the syntax of the program; so all these words are identifiers; except "quoted" strings and keywords; to check how many lines and words I have; type "wc A2.input" in "luna.cs"; your number of characters and lines may vary from sample output, depends on how you save this file; to count the identifers and keywords; you have to count me one by one; note that A1_A2 is not ONE identifier; as defined in the language lexicon specification; A1 and A2 should returned as two identifiers; special characters such as !@#$% and tab may also appear to test your program; both 123 and 123.45 are numbers; while 12,34 are two numbers; /** you can put more than one comment line with characters like * and ) **/

here is some more tricky part: " /** this is a quoted string **/ " but /** "this is a comment " **/ END

here is my questions:

1. after i run my code through cmd, it just give me a list of "current count is .....". why this happens?

2. what should i do if i want to print out the variable? i tried System.out.println("Total keywords is "+ kcount); in %eofval{ %eofval}

but it does not work

3. how to rewrite my code inorder to let it print ccount? its related to

"/**" {yybegin(COMMENT);} "**/" {yybegin(YYINITIAL);} . {}

and the answer suppose to be 3

Thanks a lot!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions