Question
import java.io.; %% %{ public static int numIdentifiers = 0; public static int numKeywords = 0; public static int numCount = 0; public static int
import java.io.; %%
%{ public static int numIdentifiers = 0; public static int numKeywords = 0; public static int numCount = 0; public static int numComments =0; public static int numQuoted = 0; public static void main(String argv[]) throws java.io.IOException { A2 yy = new A2( new FileReader("A2.input") ); while (yy.yylex() > 0);
FileWriter fw = new FileWriter("A2.output");
// writes to file fw.write("identifiers: " + numIdentifiers + " "); fw.write("keywords: " + numKeywords + " "); fw.write("numbers: " + numCount + " "); fw.write("comments: " + numComments + " "); fw.write("quotedString: " + numQuoted);
fw.close(); } %} %notunix %integer %state COMMENT, QSTRING %class A2 %eofval{ return; %eofval}
%% "int" { System.out.println("INT recognized"); numKeywords=numKeywords+1;} [a-zA-Z][a-zA-Z0-9] { numIdentifiers = numIdentifiers+1; System.out.println("ID is ...wack " + yytext());} | |. {}
%% "int" { System.out.println("INT recognized"); numKeywords=numKeywords+1;} [a-zA-Z][a-zA-Z0-9]* { numIdentifiers = numIdentifiers+1; System.out.println("ID is ...wack " + yytext());} | |. {} //this is where you add and modify expressions if you can do that for the test cases that would be nice (edited)
3 A22: Scanner generation using JLex (5 marks) 3.1 Purpose Understand the lexical definition of a language. Generate a scanner using JLex. 3.2 Assignment Specification The task is to write a JLex specification for Tiny language. Please note that in this assignment we don't need to use all the grammar definitions there. Only the lexical part is needed. You will write a JLex specification named "A2.lex". We will run the following commands 2 \begin{tabular}{l} \hline Algorithm 1 Simulate DFA (dragon book p. 151) \\ \hline Input An input string x, a DFA with start state s0, move (s,c) function that moves state \\ s to a new state on input c, accepting states F. \\ Output "yes" if D accepts x, "no" otherwise. \\ s=s0; \\ while (c=nextChar ())!= eof d do \\ s= move(s,c); \\ end while \\ if s F then \\ return "yes" \\ end if \\ return "no" \end{tabular} to generate the scanner, compile it, and run it. JLex installation instruction is documented here. You can find some other useful links such as Simple.lex here. > java JLex.Main A2.lex > javac A2.lex. java > java A2 You should take extra care on the file names. Make sure all the three commands can run without problem, and the A2.output file is generated. If any of the three commands fails, you will receive very low marks, even 0 , no matter how good the remaining part of your program is. The A2.class program will read a text file named "A2.input", and produce a file named "A2.output" which contains following five lines: identifiers : Number0fIdentifiers keywords : Number0fKeyowrds numbers : Number0fintergers0rRealNumbers comments : Number0fComments quotedString : NumberDfQuotedStrings Here are the sample A2.input and the corresponding output file A2.output. Note that this time you only need to count the occurrences of the identifiers, keywords, etc. You do not need to remove the duplicates as in last assignment. Note that you don't need to write any Java programs. The scanner is generated from your lex specificationStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started