Download presentation
Presentation is loading. Please wait.
Published byAnya Faries Modified over 9 years ago
1
1 Week 2 Questions / Concerns Schedule this week: Homework1 & Lab1a due at midnight on Friday. Sherry will be in Klamath Falls on Friday Lexical Analyzer
2
2 In-Class Exercise #3 Create a test program as input to Lab1b Add some preprocessor directives 1 - short program with one function. 1 – slightly longer program with at least 2 functions. Identify all the keywords of your language (your subset) Identify all the operators Identify all the symbols Identify all constants
3
3 Test Program: Subset of C #define MAX 10 int globalx; int main() { int x, i; scanf(“%d”, &x); //scanf needs a format & a lvalue if (x <= MAX) { printf(“%d\n”, x); } else { globalx = x; i = x; while (i >= 0) { globalx = globalx + 1; --I; } } return 0; }
4
4 List of Keywords int void char double if else while scanf printf return
5
5 List of operators = += -= *= /= %= == != >= + - * / % ++ --
6
6 List of symbols (){};‘“,(){};‘“,
7
7 List of Constants int: 35 double: 23.33 char: ‘a’ string literal: “%d\n” Identifiers:Letters & digits, must start with letter abc12x, index, i, j
8
8 Lexical Analyzer Either process each line at a time or load the entire program into a buffer. Two ways to break program text into tokens: Use delimiters (whitespace, ;, (, ), =,, …… etc.) Use regular expressions or DFA Need a keyword table to check for keywords Need to produce a list of tokens Symbol table is not needed in this step. It checks for lexical errors (illegal characters/tokens) but syntax checking is not done in this step. Syntax errors in input test program are not checked in lexical analyzer.
9
9 DFA for identifiers Regular Expression for identifiers ( a..zA..Z)(a..zA..Z0-9)* a..zA..Z a..zA..Z0..9
10
10 Implementing DFA 1. Simulate the DFA with states and transitions
11
DFA as graphs A Deterministic Finite Automate (DFA) can be viewed as a graph (abstract data structure) For example, this is a graph with 2 vertices (q0, q1) and 2 edges. In this case, the edges are directed. a q0 q1 a
12
Graph representation A graph can be represented as –Adjacency list –Incidence list –Adjacency matrix –Incidence matrix In our case, we want to represent our DFA as a transition table.
13
Machine 1: A machine that accepts even number of a’s. a a q0 q1 a q0 q1 q0 Final State: q0
14
Machine 2: A machine that accepts integers 0..9 q0 q1 0 q0 q1 Final State: q1 0..9 1 q1 2 3 4 5 6 7 8 9
15
Machine 2: A machine that accepts integers 0..9 q0 q1 0..9 a a q0 q1 Machine 1: Even number of a’s
16
Combining two machines: Correct version (trap states implied) 1..9 q0 q1 0..9 a a q2 q3 a
17
q1 0 q0 1 q1 2 3 4 5 6 7 8 9 a q2 q3 q2 q3
18
18 Implementing DFA 1. Simulate the DFA with states and transitions 2. Simulate the DFA with looping constructs.
19
19 DFA_ID function if (isalpha(first character in input buffer)) { //check for more letters and digits while (isalnum(next character in input buffer)) //keep grabbing characters and put them in //currentToken buffer } //Check the currentToken to see if it’s a keyword and set //its type correctly. //Add it to the token list
20
20 Lexical Analyzer Lexical Analyzer is just a combination of many DFAs DFA_ID DFA_number DFA_symbol DFA_operator DFA_stringliteral “ Anything but “ “
21
Other misc. notes about Lab#1 The program is to be written in C++. You are welcome to use STL, Boost or other C++ libraries. Documentation is a part of the grade. Please follow the CSET coding guidelines posted on the class webpage. Please submit your data files with the lab so Sherry can test your lab.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.