Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your job is to count the number of identifiers in programs written in our Tiny language. You will write a method that pick out the

Your job is to count the number of identifiers in programs written in our Tiny language.

You will write a method that pick out the identifiers from a text file. Here are the sample input . Your method should return a set of identifers that consists of {f2, x,y,z,F1}. Please note that in this sample program the following are not counted as identifiers:

  • A41, input, output: they are quoted hence they are not treated as identifiers;
  • INT, READ etc.: They are keywords used in our Tiny language hence they should not be picked up.

Here are the test cases for the assignment: case 1, case 2, case 3, case 4, case 5, case 6.

In this assignment you can suppose that there are no comments in the input program written in TINY language.

You will write two different programs to do this:

  1. Program A11.java is not supposed to use regular expressions, not regex package, not the methods involving regular expression in String class or other classes. Your program should use the most primitive method, i.e. look at characters one by one, and write a loop to check whether they are quoted strings, identifiers, etc.
  2. Program A12.java will use java.util.regex.

Case 1:

 INT f2(INT x, INT y ) BEGIN z := x*x - y*y; RETURN z; END INT MAIN F1() BEGIN INT x; READ(x, "A41.input"); INT y; READ(y, "A42.input"); INT z; z := f2(x,y) + f2(y,x); WRITE (z, "A4.output"); END

Case 2:

 BEGIN END INT MAIN AAA, X23Y, F1i A END

Case 3:

aaa&bbb!ccc#ddd%eee&ffff

Case 4:

 INT f2(INT x, INT y ) BEGIN z := x*x - y*y; RETURN z; END INT MAIN f1() BEGIN x2222x INT x1; READ(x, "A41. input"); INT y; READ(y, "A42.input"); INT z; z := f2(x,y) + f2(y,x); WRITE (z, "A4.output"); END

Case 5:

 "A41.input" AA BB CCCC2DDD aaabbb if b c d IF 

Case 6:

AA1 BBBB22222 CCC3333DDDDD A- B- C- xx_yy_~!@#$%^&*()_+}{}[]\|\":";'?><,./  zz BEGIN END 
AA1 BBBB22222 CCC3333DDDDD A- B- C- xx_yy_~!@#$%^&*()_+}{}[]\|\":";'?><,./  zz BEGIN END 

Starter Code for A11:

import java.io.FileReader; import java.io.BufferedReader; import java.util.Set; import java.util.HashSet; public class A11 { static boolean isLetter(int character) { return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z'); } static boolean isLetterOrDigit(int character) { return isLetter(character) || (character >= '0' && character <= '9'); } public static Set getIdentifiers(String filename) throws Exception{ String[] keywordsArray = { "IF", "WRITE", "READ", "RETURN", "BEGIN", "END", "MAIN", "INT", "REAL" }; Set keywords = new HashSet(); Set identifiers = new HashSet(); for (String s : keywordsArray) { keywords.add(s); } FileReader reader = new FileReader(filename); BufferedReader br = new BufferedReader(reader); String line; while ((line = br.readLine()) != null) { int i=0; while (i < line.length()) { if (line.charAt(i)=='\"'){ // throw away quoted strings if (isLetter(line.charAt(i))){ // get the identifier } return identifiers; } } } public static void main(String[] args) throws Exception{ Set ids=getIdentifiers("A1.tiny"); for (String id :ids) System.out.println(id); } }

Starter Code for A12

 import java.io.*; import java.util.HashSet; import java.util.Set; import java.util.regex.*; public class A12 { public static Set getIdRegex(String filename) throws Exception{ String[] keywordsArray = { "IF", "WRITE", "READ", "RETURN", "BEGIN","END", "MAIN", "INT", "REAL" }; Set keywords = new HashSet(); Set identifiers = new HashSet(); for (String s : keywordsArray) keywords.add(s); FileReader reader = new FileReader(filename); BufferedReader br = new BufferedReader(reader); String line; //Pattern idPattern = ......; //Pattern quotedStringPattern = .....; while ((line = br.readLine()) != null) { Matcher m_quotedString = quotedStringPattern.matcher(line); String lineWithoutQuotedStrings = m_quotedString.replaceAll(""); Matcher m = idPattern.matcher(lineWithoutQuotedStrings); while (m.find()) { String id = line.substring(m.start(), m.end()); if (!keywords.contains(id)) identifiers.add(id); } } return identifiers; } public static void main(String[] args) throws Exception{ Set ids=getIdRegex("A1.tiny"); for (String id :ids) System.out.println(id); } }

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

Students also viewed these Databases questions