Understand the lexical definition of a language. Generate a scanner using JLex.
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 to generate the scanner, compile it, and run it.
> 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 3 commands fails, you will receive very low marks, even 0, no matter how good the other 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: NumberOfIdentifiers keywords: NumberOfKeyowrds numbers: NumberOfIntergersOrRealNumbers comments: NumberOfComments quotedString: NumberOfQuotedStringsHere 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 duplciates as in last assignment.
Note that you don't need to write any Java programs. The scanner is generated from your lex specification.
yourMark=0; if ( A2.lex file is not sent properly or file name is incorrect) return; if (java program named A2.lex.java is generated from your lex file) yourMark+=0.5; if ( generated program is compiled correctly && A2.class is generated) { yourMark+=0.5; if (your java program reads A2.input && generates result file A2.output){ for (each of the 5 counts in A2.output) { if (count is correct) { yourMark+=0.8; } } } } yourMark += (lengthOfYourProgram>140)?0:96/lengthOfYourProgram; // length is counted by PHP in terms of words; Note that different programs count the words in different ways. for (each day of your late submission) yourMark=yourMark*0.7;
You only need to submit A2.lex
%class A2
while (yy.yylex()>=0){}; ... %integerNote that while (true){} in simple.lex will cause an infinite loop.
.|\n|\r {}
{ID} {idCount++;} {KEYWORD} {keywordCount++}In this case, keywords like INT will be recognized as an ID instead of a keyword. To solve the problem, put line for KEYWORD in front of the line for ID.
[0-9]+(\.[0-9]+)*In this case 2.3.4.5 would be recognized as one integer, which is wrong. To solve this problem, change * to ?. '?' means repeating zero or once.
%state comment; %% <YYINITIAL>"/**" {...} <comment>i"**/" {... }