0% found this document useful (0 votes)
99 views

Compiler Construction Assignment

This document contains 3 programming problems related to text processing and regular expressions: 1. Write a scanner to convert a 32-bit hexadecimal number to decimal. It uses a for loop to iterate through each character and convert it to a long integer based on its position. 2. Write a program to count the number of capital letters in a string using flex. It defines rules to match capital letters and other characters, and increments a counter for each capital letter. 3. Write a program to count the number of lines and total characters in input text using flex. It declares two counters and defines rules to increment the line counter for newlines and character counter for all other characters.

Uploaded by

atiqa ansar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views

Compiler Construction Assignment

This document contains 3 programming problems related to text processing and regular expressions: 1. Write a scanner to convert a 32-bit hexadecimal number to decimal. It uses a for loop to iterate through each character and convert it to a long integer based on its position. 2. Write a program to count the number of capital letters in a string using flex. It defines rules to match capital letters and other characters, and increments a counter for each capital letter. 3. Write a program to count the number of lines and total characters in input text using flex. It declares two counters and defines rules to increment the line counter for newlines and character counter for all other characters.

Uploaded by

atiqa ansar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGNMENT

Problem 1-Write a scanner for 32-bit hexadecimal numbers

package hexconverter;

import java.util.*;

/**

* @author Steven

*/

public class Main {

Scanner scanner = new Scanner(System.in);

public void doWork() {

System.err.println("Please enter the internal representation: ");

String hex;

hex = scanner.next();

hex = hex.toUpperCase();

long count = 1;

long ans = 0;

for (int i = 7; i >= 0; i--) {

Character c = hex.charAt(i);

if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' &&
c != '6' && c != '7' && c != '8' && c != '9') {

int num = fixLetters(c);

ans = ans + (num * count);

count = count * 16;

} else {

String s = c.toString(c);

long num = Integer.parseInt(s);


ans = ans + (num * count);

count = count * 16;

if (ans > 2147483647) {

System.out.println("is negative");

} else {

System.out.println(ans);

public int fixLetters(Character c) {

if (c.equals('A')) {

return 10;

} else if (c.equals('B')) {

return 11;

} else if (c.equals('C')) {

return 12;

} else if (c.equals('D')) {

return 13;

} else if (c.equals('E')) {

return 14;

} else if (c.equals('F')) {

return 15;

} else {

return 0;

} }

public static void main(String[] args) {

// TODO code application logic here


Main a = new Main();

a.doWork(); }

Problem 2: Count the number of characters in a string


/*** Definition Section has one variable
which can be accessed inside yylex() 
and main() ***/
%{
int count = 0;
%}
  
/*** Rule Section has three rules, first rule 
matches with capital letters, second rule
matches with any character except newline and 
third rule does not take input after the enter***/
%%
[A-Z] {printf("%s capital letter\n", yytext);
       count++;}
.     {printf("%s not a capital letter\n", yytext);}
\n    {return 0;}
%%
  
/*** Code Section prints the number of
capital letter present in the given input***/
int yywrap(){}
int main(){
  
// Explanation:
// yywrap() - wraps the above rule section
/* yyin - takes the file pointer 
          which contains the input*/
/* yylex() - this is the main flex function
          which runs the Rule Section*/
// yytext is the text in the buffer
  
// Uncomment the lines below 
// to take input from file
// FILE *fp;
// char filename[50];
// printf("Enter the filename: \n");
// scanf("%s",filename);
// fp = fopen(filename,"r");
// yyin = fp;
  
yylex();
printf("\nNumber of Captial letters " 
      "in the given input - %d\n", count);
  
return 0;
}
Problem 3: Count the number of characters and number of lines in
the input
/* Decalring two counters one for number 

of lines other for number of characters */


%{
int no_of_lines = 0;
int no_of_chars = 0;
%}
  
/***rule 1 counts the number of lines, 
rule 2 counts the number of characters 
and rule 3 specifies when to stop 
taking input***/
%%
\n      ++no_of_lines;
.       ++no_of_chars;
end     return 0;
%%
  
/*** User code section***/
int yywrap(){}
int main(int argc, char **argv)
{
  
yylex();
printf("number of lines = %d, number of chars = %d\n",
       no_of_lines, no_of_chars );
  
return 0;
}

You might also like