Lab 2: Developing Lexical Analysis Programs using Flex in Windows

Ace your studies with our custom writing services! We've got your back for top grades and timely submissions, so you can say goodbye to the stress. Trust us to get you there!


Order a Similar Paper Order a Different Paper

Lab 2: Developing Lexical Analysis Programs using Flex in Windows

 

Student Name:                                                          Student Id:

 

 

Lab Instructions:
Please show your work to the instructor present in the lab before submitting.

Submission Due: End of laboratory class, submit the file on Moodle at least 10 minutes before the end of laboratory class.

Total Marks   = 10 marks

Marks will be given only to students who attend and participate during 2-hour laboratory class. Submission on Moodle is mandatory as an evidence of participation.

 

Learning Outcomes:

 

LO2 Design simple languages and protocols suitable for a variety of applications
LO3 Generate code for simple languages and protocols using suitable tools

 

 

 

 

Marking Criteria:

 

Task Details Submission Requirements Marks
 

Task 1

Explanation: Submit Descriptive explanation in the word file.

Output: Copy the output in the same world file

1
 

Task 2

Source Code: Copy the source code in the word file and submit the actual source file.

 

1.5
Program Output: Copy the output in the same world file 1
 

Task 3

Source Code: Copy the source code in the word file and submit the actual source file. 1.5
Program Output: Copy the output in the same world file 1
 

Task 4

Source Code: Copy the source code in the word file and submit the actual source file. 2
Program Output: Copy the output in the same world file 2

 

 

 

Objective:

This lab introduces you on how to develop C programs to perform Lexical Analysis using Flex in Windows.

Structure of LEX source program:

 

{definitions}

%%

{rules}

%%

{user subroutines/code section}

%% is a delimiter to the mark the beginning of the Rule section. The second %% is optional, but the first is required to mark the beginning of the rules. The definitions and the code

/subroutines are often omitted.

 

Lab Exercise:

You are provided with code files and explanation as following:

  • l

This program checks for the specific characters in the input string, and outputs/counts number of Uppercase and Lowercase characters.

Following are the specified rules within the program code file:

%%

[A-Z] {printf(“Uppercase\t”);Upper++;}

[a-z] {printf(“Lowercase\t”);Lower++;}

%%

[A-Z] {printf(“Uppercase\t”);Upper++;} rule checks for uppercase letters in the string, the [A-Z] regular expression does the task for us. Once the rule is matched, the provided C code inside {} curly braces executes, which prints “Uppercase” on the screen and increments the value of “Upper” variable by one using “Upper++” statement.

Similarly, [a-z] {printf(“Lowercase\t”);Lower++;} rule does the same for lowercase letters with an exception that this time the [a-z] regular expression is used to check for the lowercase letters within the string.

  • l

This program checks for the specific characters of the input string, and outputs/counts vowel and consonants within that string.

Following is the code snippet for the rules specified in this program:

%%

“a”|”e”|”i”|”o”|”u”|”A”|”E”|”I”|”O”|”U” {printf(“is a VOWEL”);vowel++;}

[a-zA-z] {printf(“Is a Consonant”);cons++;}

%%

Rule 1:

“a”|”e”|”i”|”o”|”u”|”A”|”E”|”I”|”O”|”U” {printf(“is a VOWEL”);vowel++;}

This rule checks for the vowels, whether uppercase or lowercase, in the provided string. “a”|”e”|”i”|”o”|”u”|”A”|”E”|”I”|”O”|”U” is the regular expression to do the matching. Subsequently, the C code within curly braces will print “is a VOWEL” on screen if the rule is matched, and increment the value of “vowel” variable by “vowel++” statement.

Rule 2:

[a-zA-z] {printf(“Is a Consonant”);cons++;}

This rule checks for the consonants. Anything inputted except the letters specified in Rule 1 would be treated as consonant. So, “Is a Consonant” would be printed on screen and “cons” variable’s value would be incremented by one using “cons++” statement.

 

 

  • l

This program checks for programmatic constructs (If, else, and assignment operator) appearing in your program and prints the corresponding construct on screen.

Following is the code snippet for the rules specified in this program:

%%

“if”|”IF”|”If”|”iF” {printf(“IF Appeared\t”);}

“else”|”ELSE”|”Else” {printf(“ELSE Appeared\t”);}

“for”|”FOR”|”For {printf(“FOR Appeared\t”);}

“=” {printf(“Equals Appeared\t”);}

%%

 

Rule 1:

“if”|”IF”|”If”|”iF” {printf(“IF Appeared\t”);}

This rule checks for the appearance of “IF” construct. Subsequently, the C code within curly braces will print “IF Appeared” on screen if the rule is matched.

Rule 2:

“else”|”ELSE”|”Else” {printf(“ELSE Appeared\t”);}

This rule checks for the appearance of “ELSE” construct. Subsequently, the C code within curly braces will print “ELSE Appeared” on screen if the rule is matched.

Rule 3:

“for”|”FOR”|”For {printf(“FOR Appeared\t”);}

This rule checks for the appearance of “FOR” construct. Subsequently, the C code within curly braces will print “FOR Appeared” on screen if the rule is matched.

Rule 4:

“=” {printf(“Equals Appeared\t”);}

This rule checks for the appearance of “=” operator. Subsequently, the C code within curly braces will print “Equals Appeared” on screen if the rule is matched.

Notes:

  • You can use Lab 1 manual to know how to execute the provided programs.
  • When you want to finish entering the input, please enter “ctrl + z” in windows console. The output would be shown afterwards. “ctrl + z” allows the program to return from the yylex() function and execute the rest of the C code.

 

Lab Tasks:

  • Execute the “token.l” program and explain the functionality of the program as explained in the manual above.

Answer:

 

 

 

 

 

 

 

 

 

 

Screenshots of Output:

 

 

 

 

  • Modify the “vowel.l” program, so that now it prints/counts the number of uppercase vowels, lowercase vowels, uppercase consonants, and lowercase consonants separately.

 

Source Code:

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

 

 

 

 

 

 

 

  • Modify the program “prog.l”, so that now it recognizes arithmetic operators in C programming language and prints the name of corresponding operator if recognized. You can use following operator table as a guide:

 

Source Code:

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

 

 

 

 

 

 

 

  • Write a program “identifyletter.l”, which identifies the letters in the input string from English letters (A-Z) and prints the corresponding letter.

 

Source Code:

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

 

 

 

 

 

 

 

 

 

Submission Instructions:

  • Submit your answers/code and screenshots of output in this word file by renaming it in the format “SC_SEN2110_Lab{#}_05_03_2020_Student ID” and uploading on Moodle in the appropriate submission link.
  • Please also submit source (.l) files of your code.

Note: Please conform to the naming convention of the file.

 

 

References:

 

[1] https://www.youtube.com/watch?v=AI-jwky_mqM&list=WL&t=0s&index=54

 

[2] http://www.cittumkur.org

 

 

END OF LABORATORY

 

Writerbay.net

Looking for top-notch essay writing services? We've got you covered! Connect with our writing experts today. Placing your order is easy, taking less than 5 minutes. Click below to get started.


Order a Similar Paper Order a Different Paper