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...
Six numbered tokens are distributed to 8 different people. a) How many ways can the tokens...
Six numbered tokens are distributed to 8 different people. a) How many ways can the tokens be distributed? How many ways can the tokens be distributed if each token must be given to a different person? b) How many ways can the tokens be distributed such that person A receives at least 2 tokens? c) How many ways can the tokens be distributed such that 3 are given to one person, and 2 to another person? d) How many ways...
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 an example BNF grammar, be able to identify the tokens which a lexical analyzer would...
For an example BNF grammar, be able to identify the tokens which a lexical analyzer would be able to produce. For an example BNF grammar, and a list of tokens, construct a state diagram of the language’s lexical analyzer.
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...
Write a program that uses an array for time and temperature. The program should contain an...
Write a program that uses an array for time and temperature. The program should contain an array with 24 elements, each of which is holding a temperature for a single hour. Your program should have a function that lists all of the temperatures that are held in the array. Temperatures should be listed to console with one entry per line. Your program should have a function that lists a single temperature entry. You should ask the user for a number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT