$25
The lab is designed to introduce the students to the basics steps of a compiler Design. After that the lab will focuses and dive deep into the first compilation step which is Lexical analysis. Basic techniques of coding and required tools will also be shown to students.
II. Lesson Fit:
The lab gives a hand on experience of the knowledge of theory class.
III. Learning Outcome:
After this lecture, the students will be able to:
a. Understand and visualize the Lexical Analysis step.
b. Identifying the “Token” for a particular program.
c. Creating own version of Lexical analyzer.
IV. Anticipated Challenges and Possible Solutions
a. Finding out the identifiers and numerical values from the given code.
Possible Solutions:
i. Use regular expression.
ii. Use methods of java String class.
VI. Activity Detail
a. Hour: 2
Discussion: Basic Concepts & Regular Expressions
1. What does a Lexical Analyzer do?
2. How does it Work?
3. Formalizing Token Definition & Recognition
Problem task:
i. Task 1 (Page 3- 4)
Assignment 1
In this assignment, your job is to program a simple lexical analyzer that will build a symbol table from given stream of chars. You will need to read a file named "input.txt" to collect all chars. For simplicity, input file will be a JAVA codes containing methods. Then you will identify all the method name and parameter list. See the example for more details. You can assume that, there will be a space after each keywords. But, removal of space will add bonus point.
Lab 1: Activity List
Task 1: For the following sample program find all the tokens.
Note that a lexical Analyzer is responsible for
1. Scan Input
2. Remove WS, NL, ...
3. Identify Tokens
4. Create Symbol Table (ST)
5. Insert Tokens into ST
6. Generate Errors
Input:
public class A { int x, int y; public int add(int a, int b)
{ return a+b;
}
public static int subtract(int a, int b)
{ return a-b;
} public void add() { return x+y; }
public static void main(String [] args)
{ System.out.println(subtract(10,5)); A a=new A(); a.add(20,10);
}
}
Output:
Methods:
add (int a, int b) add () subtract ()
Bonus: If you can output the methods name with their return type.
Methods:
add (int a, int b), return type: int add (), return type: void subtract (), return type: int