Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Language Concepts Please explain what a lexeme is (maybe explain how

ID: 3746845 • Letter: P

Question

Programming Language Concepts
Please explain what a lexeme is (maybe explain how you did one line), please list out how many are in each line, and please list the total amount. 2. How many lexemes does the following Java code contain?
1. public class RepeatString { 2. public static void main(String args[]) { 3. Scanner input = new Scanner(System.in); 4. System.out.println("Enter a string: "); 5. String phrase = input.nextLine(); 6. System.out.println("Enter # times to repeat string: "); 7. int repetitions = input.nextInt(); 8. repeat(phrase, repetitions); 9. } 10. public static void repeat(String str, int reps) { 11. for (int i = 1; i <= reps; i++) { 12. System.out.println(i + ". Repeat: " + str); 13. } 14. } }
Give the number of lexemes on each line (using the line numbers shown) as well as the total number of lexemes.
A picture is attached for clarity. 2. How many lexemes does the following Java code contain? 1. public class Repeatstring t 2. public static void main(String args) Scanner inputnew Scanner (System.in); System.out.println("Enter a string: "); String phrase = input.nextLine ( ); System.out.printIn("Enter # times to repeat string: "); int repetitions -input. nextInt(); repeat(phrase, repetitions): 9. 10. public static void repeat (String str, int reps) for (int i = 1; î

Explanation / Answer

In a compiler , the lexical analyzer processes the input program of stream of characters into individual tokens. The sequence that make up the token is called as a lexeme. A lexeme for a token is identified by the lexical analyzer as an instance of that token. An example of some lexemes and their token is given below.

For example, if we take a simple code

x = a + b; , then the lexeme can be defined as ,

lexemes: { x, =, a, +, b , ; } that is , there are 6 lexemes in this code of line. The corresponding tokens are : {<identifier, 0>, <=>, <identifier, 1>, <+>, <identifier, 2>, <Punctuator ,3> }

Lexemes can be single words or strings of words . In a program , a single word is counted as a lexeme where a string of words ( inside double quotes such as " how are you") is counted as a single lexeme.

In the given program , i will take each line and comment the number of lexemes in that line. So there are total 113 lexmes in this program.

1. public class RepeatString { // there are 4 lexemes

2. public static void main(String args[]) { // there are 11 lexemes.

3. Scanner input = new Scanner(System.in); // there are 11 lexemes

4. System.out.println("Enter a string: "); // there are 9 lexemes

5. String phrase = input.nextLine(); // there are 9 lexemes

6. System.out.println("Enter # times to repeat string: "); // there are 9 lexemes

7. int repetitions = input.nextInt(); // there are 9 lexemes

8. repeat(phrase, repetitions); // there are 7 lexemes

9. } // there is only 1 lexeme

10. public static void repeat(String str, int reps) { // there are 12 lexemes

11. for (int i = 1; i <= reps; i++) { // there are 15 lexemes , here <= is a single lexeme since it is a relational operator and ++ is a single lexeme because it is an increment operator.

12. System.out.println(i + ". Repeat: " + str); // there are 13 lexemes

13. } // there is 1 lexeme

14. } } // there are 2 lexemes.

Lexeme TOKEN while WHILE ( , ) LPAREN ; SEMICOLON = ASSIGNMENT 3 INTEGER x IDENTIFIER <,<=,= ,< >,>=,>   RELATION