0% found this document useful (0 votes)
26 views4 pages

Lab Assignment 1

This document provides a step-by-step guide for setting up and using JFlex, including prerequisites, installation instructions, and how to create a specification file for recognizing various programming constructs. It outlines the process of running JFlex, compiling the generated Java file, and testing with input files. Additionally, it includes lab assignments to implement specifications for C programming constructs and other tasks involving lexeme recognition.

Uploaded by

chatgpt.20290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Lab Assignment 1

This document provides a step-by-step guide for setting up and using JFlex, including prerequisites, installation instructions, and how to create a specification file for recognizing various programming constructs. It outlines the process of running JFlex, compiling the generated Java file, and testing with input files. Additionally, it includes lab assignments to implement specifications for C programming constructs and other tasks involving lexeme recognition.

Uploaded by

chatgpt.20290
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Assignment – 1

JFlex
Instructions to getting started with JFlex
0. Prerequisites
 Install JDK
 Set the environment variable PATH to include JDK bin location
 Open command prompt and check if java and javac commands are working

1. Install JFlex
 Download JFlex compressed archive as per the instructions
in https://jflex.de/manual.html#Installing
 Place the extracted content in a folder, say C:\JFLEX
 Note the full path where jflex-1.9.1 is installed
 Make changes to jflex.bat in bin directory to set JFLEX_HOME correctly
 In my case JFLEX_HOME is C:\JFLEX\jflex-1.9.1

2. Prepare the specification File (standalone.flex)


 The initial lines are to be added by default
 After this regular experssion + action can be added
 The first 2 rules starting with “name “ … are retained from example file (can be
ignored right now)
 The rules to recognize ‘if’ keyword, identifier, integer and real are added below
them
 You can add more along these lines.

—————–standalone.flex——————-

%public
%class Subst
%standalone

%unicode

%{
String name;
%}

%%
"name " [a-zA-Z]+ { name = yytext().substring(5); }
[Hh] "ello" { System.out.print(yytext()+" "+name+"!"); }
"if" { System.out.print(yytext() + " IF_KWD"); }
[a-zA-Z][a-zA-Z0-9]* { System.out.print(yytext() + " IDENTIFIER"); }
[0-9]+ { System.out.print(yytext() + " INTEGER"); }
[0-9]+ "\." [0-9]+ { System.out.print(yytext() + " REAL"); }
.+ { System.out.println(yytext() + " UNRECOGNIZED TOKEN"); }

3. Run jflex with spec file as the input


 To run JFLEX: jflex <options> <specification-file>
 Open command prompt
 jflex standalone.flex
 This will produce Subst.java file
 Something as below will be printed to the terminal

Reading "standalone.flex"
Constructing NFA : 44 states in NFA
Converting NFA to DFA :
....................
22 states before minimization, 19 states in minimized DFA
Old file "Subst.java" saved as "Subst.java~"
Writing code to "Subst.java"

4. Compile Subst.java
 javac Subst.java

5. Prepare the input file (input.txt)


——input.txt——

abc12
253
12.34
if
abc12

6. Run Subst with the above input


 java Subst input.txt
 You should see the following output
 abc12 IDENTIFIER
 253 INTEGER
 12.34 REAL
 if IF_KWD
_KWD

Lab Assignment
1. Implement a specification file that includes regular expressions to recognize
each and every C programming language construct.
a. All keywords
b. Identifiers
c. Integers
d. Floating point numbers
e. Whitespaces
f. All kinds of parantheses: (, ), [, ], {, }
g. semi colon, comma
h. Arithmetic operators: +, -, *, /, %, =, ++, --
i. Comparison operators: =, !=, <. >, <=, >=
j. Boolean operators: &&, ||, !
k. Bitwise operators: &, |, ^, !, <<, >>
l. Pointer operators: *, ->, &, .
m. Macros: #include, #define, #ifdef, #ifndef, #endif
n. Single line and Multi line comments
2. Demonstrate the correct identification of each lexeme.
. Use one or more C programs that cover the entire range of C constructs.
a. Run and show the correct categorization of each lexeme for each
program.
3. Try the following and check what happens
. Place the keyword rules below identifier rules.
a. Place < and = rules above <= rule.
b. Place rules of bitwise operators above Boolean operators
4. If a lexeme does not match any regular expression, you must throw an error.
How do you do it in JFlex? Explore and find out.
5. Whenever you recognize an identifier, you have to add it to an ArrayList.
Implement it.
6. Add the number of semi-colons encountered by using a count variable. This
tells you the number of statements in your program (roughly).
7. Write a separate specification file to recognize phone number, email id,
website link. Show it works with good examples.
8. Write a separate specification file to recognize roman numbers. Show it works
with different examples.
Test cases for C specification

1. short int long float double char if else for while do static extern auto typedef
……
2. abcd ab12 a1b2c3 a123 _____ a_b ab_ _a ……
3. 123 +123 -123 0 -0 0123 000123 0000 ……
4. 1.23 +123.0 -12.3 0.0 -0.1 01.23 0.00123 0.000 …….
5. Since space char, multiple space chars, single tab space, multiple tab space,
single newline, multiple new lines, combination of these
6. ( [ { ) ] } (){}[]
7. a; a,b
8. a+b a-b a*b a/b a%b +a -a ++a a++ --a a-- a+b-
12*16.5/10%c a-- - --b ++a - b++ a=b a==b
9. a<10 a<=10 a>b a>=b a==b a!=b
10. a !a a && b a||b !a&&b !(a||b)
11. 5>>1 10<<2 a|3 b&4 !a&!b
12. int a char **c &c student->age x.y
13. #define #ifdef #ifndef #endif #include
14. // This is a comment
/* This is a
multi-line
comment */
15. 2ab_c $# #$
16. stdio.h, math.h, string.h, unistd.h

You might also like