Question

In: Computer Science

Write a lex program that can identify each of the following tokens. The program should produce...

Write a lex program that can identify each of the following tokens. The program should produce a line
for each token where the first word is token itself followed by the token type.


• Identifiers: name of variables and functions (identifier name should be started by a letter
followed by letters or digits and maximum eight characters in length).


• Keywords: if, then, else, for, while, do, switch, case etc.
• Numbers: integer, float (float number format that supports in C++/Java)


• Operators:
• Arithmetic operators (+,-,*,/)
• Relational operators (<, <=, >, >=, ==, !=)
• Conditional operators ( &&, ||, !, )
• Increment/decrement operators (++, --)
• Bitwise operators (&, |)
• Assignment operator (=)


• Special Symbols: (, ), {,},; .. etc

Solutions

Expert Solution

SOLUTION:-

%option noyywrap

%{

            #include<stdio.h>

%}

TYPE int|char|bool|float|double|void|for|do|while|if|else|return|switch|case|then

DIGIT [0-9]

%%

{TYPE} {printf("Token %s is keywords\n ",yytext);}

[a-zA-Z_][a-zA-Z0-9]* {printf("Token %s is an identifiers\n ",yytext);}

"+"|"-"|"*"|"/" {printf("Token %s is arithmatic operators\n",yytext);}

"=="|"!="|">"|"<"|"<="|">=" {printf("Token %s is relational operators\n",yytext);}

"&&"|"||"|"!" {printf("Token %s is conditional operators\n",yytext);}

"++"|"--"   {printf("Token %s is Increment/decrement operators\n",yytext);}

"&"|"|" {printf("Token %s is bitwise operators\n",yytext);}

"=" {printf("Token %s is assignment operators\n",yytext);}

^{DIGIT}+                               {printf("Token %s is integer constant.\n",yytext);}

^{DIGIT}+[.]+{DIGIT}+            {printf("Token %s is floating number constant.\n",yytext);}

"("|")"|"{"|"}"|";" {printf("Token %s is special symbols \n",yytext);}

.                         ;

%%

int main()                                              

{

printf("Enter herer :\n");

yylex();

return 0;

}


Related Solutions

Write a program that reads a file line by line, and reads each line’s tokens to...
Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects.  Each line from the file to read will contain two strings for first and last name, and three floats for three test grades.  After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average...
Write a program to produce an array of integer random numbers. Your program should find out...
Write a program to produce an array of integer random numbers. Your program should find out from the user how many numbers to store. It should then generate and store that many random integers (the random numbers must be between 1 and 999 inclusive). The program should then determine the smallest number, the largest number, and the average of all the numbers stored in the array. Finally, it should print out all the numbers on the screen, five numbers to...
You should write a small C++ program that performs the following tasks: First, your program should...
You should write a small C++ program that performs the following tasks: First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to...
Write a simple airline ticket reservation program. The program should display a menu with the following...
Write a simple airline ticket reservation program. The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of flights. Create a linked list...
Answer the questions and Identify the table that should be used for each of the following...
Answer the questions and Identify the table that should be used for each of the following situations in the space provided, then show the calculations to solve the problem below. FV – Future Value of 1 PV – Present Value of 1 FVA – Future Value of an Annuity PVA – Present Value of an Annuity __________ 5. An investment firm has determined that the market price of a 5-year $100,000 bond yielding 6% is $104,212. This is based on...
For the following program segments, write a program that shows the estimated runtime for each piece....
For the following program segments, write a program that shows the estimated runtime for each piece. Run it on your computer when n=1, 10, 100, 1000, 10000, 100000, 1000000 for ten times each so that you can observe the differences in performance among these segments. Segment1:        for (sum=0, i=1; i<=n; i++)                         sum = sum + i; Segment2:        for (sum=0, i=1; i<=n; i++)                                                 for (j=1; j<=i; j++)                                                             sum++; Segment3:        sum= n * (n+1)/2
Write functions for each of the following problems. Each problem should be solved by writing a...
Write functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. (a) Write a function that uses recursion to raise a number to a power. The function should take two arguments, the number to be raised to the power (floating point) and the power (a non-negative int). (10 Points) (b) Write a boolean function named isMember that takes two arguments: an array of...
Write a program that asks the user for an angle, entered in radians. The program should...
Write a program that asks the user for an angle, entered in radians. The program should then display the sine, cosine, and tangent of the angle. (Use the sin, cos, and tan library functions to determine these values.) The output should be displayed in fixed-point notation, rounded to four decimal places of precision Take your previous Angle Calculator program and modify it to do a table of trig values. The columns will be: Degrees, Sine, Cosine, Tangent,. And the rows...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT