Lexical Analysis An Introduction Copyright 2003 Keith D
- Slides: 23
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use.
The Front End Source code Front End IR Back End Machine code Errors The purpose of the front end is to deal with the input language • Perform a membership test: code source language? • Is the program well-formed (semantically) ? • Build an IR version of the code for the rest of the compiler The front end is not monolithic
The Front End Source code Scanner tokens Scanner • Maps stream of characters into words Basic unit of syntax x = x + y ; becomes <id, x> <eq, => <id, x> <pl, +> <id, y> <sc, ; > IR Parser Errors Speed is an issue in scanning use a specialized Characters that form a word are its lexeme recognizer • • Its part of speech (or syntactic category) is called its token type • Scanner discards white space & (often) comments
The Front End Source code Scanner tokens Parser IR Parser Errors • Checks stream of classified words (parts of speech) for grammatical correctness • Determines if code is syntactically well-formed • Guides checking at deeper levels than syntax • Builds an IR representation of the code We’ll return to parsing later
The Big Picture • Language syntax is specified with parts of speech, not words • Syntax checking matches parts of speech against a grammar 1. goal expr 2. expr op term 3. | term S = goal 4. term number 5. | id N = { goal, expr, term, op } 6. op 7. P = { 1, 2, 3, 4, 5, 6, 7} + | T = { number, id, +, - } –
The Big Picture • Language syntax is specified with parts of speech, not words • Syntax checking matches parts of speech against a grammar 1. goal expr 2. expr op term 3. | term S = goal 4. term number 5. | id N = { goal, expr, term, op } 6. op 7. P = { 1, 2, 3, 4, 5, 6, 7} + | T = { number, id, +, - } – No words here! Parts of speech, not words!
The Big Picture Why study lexical analysis? • We want to avoid writing scanners by hand • We want to harness material from theory of computation class source code Scanner parts of speech & words tables or code specifications Goals: Scanner Generator Represent words as indices into a global table Specifications written as To simplify specification & expressions” implementation of scanners “regular To understand the underlying techniques and technologies
Regular Expressions Lexical patterns form a regular language *** any finite language is regular *** Regular expressions (REs) describe regular languages Regular Expression (over alphabet Ever type “rm *. o a. out” ? ) • is a RE denoting the set { } • If a is in , then a is a RE denoting {a} • If x and y are REs denoting L(x) and L(y) then x |y is an RE denoting L(x) L(y) xy is an RE denoting L(x)L(y) x* is an RE denoting L(x)* Precedence is closure, then concatenation, then alternation
Set Operations (review) These definitions should be well known
Examples of Regular Expressions Identifiers: Letter (a|b|c| … |z|A|B|C| … |Z) Digit (0|1|2| … |9) Identifier Letter ( Letter | Digit )* Numbers: Integer (+|-| ) (0| (1|2|3| … |9)(Digit *) ) Decimal Integer. Digit Real * ( Integer | Decimal ) E (+|-| ) Digit Complex ( Real , Real ) Numbers can get much more complicated! *
Regular Expressions (the point) Regular expressions can be used to specify the words to be translated to parts of speech by a lexical analyzer Using results from automata theory and theory of algorithms, we can automatically build recognizers from regular expressions We study REs and associated theory to automate scanner construction !
Example (from Lab 1) Consider the problem of recognizing register names Register r (0|1|2| … | 9)* • Allows registers of arbitrary number • Requires at least one digit RE corresponds to a recognizer (or DFA) (0|1|2| … 9) r S 0 S 1 S 2 accepting state Transitions on other inputs go to an error state, s e Recognizer for Register
Example (continued) DFA operation • Start in state S 0 & take transitions on each input character • DFA accepts a word x iff x leaves it in a final state (S 2 ) (0|1|2| … 9) r S 0 (0|1|2| … 9) S 1 S 2 accepting state So, Recognizer for Register • r 17 takes it through s 0, s 1, s 2 and accepts • r takes it through s 0, s 1 and fails • a takes it straight to se
Example ( continued) To be useful, recognizer must turn into code Char next character State s 0 while (Char EOF) State (State, Char) Char next character if (State is a final state ) then report success else report failure Skeleton recognizer r 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 s 0 s 1 se se s 1 se s 2 se se se Table encoding RE All others
Example ( continued) To be useful, recognizer must turn into code Char next character State s 0 while (Char EOF) State (State, Char) perform specified action Char next character if (State is a final state ) then report success else report failure Skeleton recognizer r 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 s 0 s 1 start se error s 1 se error s 2 add se error se error Table encoding RE All others
What if we need a tighter specification? r Digit* allows arbitrary numbers • Accepts r 00000 • Accepts r 99999 • What if we want to limit it to r 0 through r 31 ? Write a tighter regular expression Register r ( (0|1|2) (Digit | ) | (4|5|6|7|8|9) | (3|30|31) ) Register r 0|r 1|r 2| … |r 31|r 00|r 01|r 02| … |r 09 Produces a more complex DFA • Has more states • Same cost per transition • Same basic implementation
Tighter register specification (continued) The DFA for Register r ( (0|1|2) (Digit | ) | (4|5|6|7|8|9) | (3|30|31) ) (0|1|2| … 9) S 2 S 3 0, 1, 2 S 0 r S 1 3 S 5 0, 1 S 6 4, 5, 6, 7, 8, 9 S 4 • Accepts a more constrained set of registers • Same set of actions, more states
Tighter register specification (continued) r 0, 1 2 3 4 -9 All others s 0 s 1 se se se s 1 se s 2 s 5 s 4 se s 2 se s 3 s 3 se se se s 4 se se se s 5 se s 6 se se se se Table encoding RE for the tighter register specification Runs in the same skeleton recognizer
Extra Slides Start Here
Review of Scanners from Comp 314 • Lexical Analysis Strategy: Simulation of Finite Automaton States, characters, actions State transition (state, charclass) determines next state • Next character function Reads next character into buffer Computes character class by fast table lookup • Transitions from state to state Current state and next character determine (via ) § Next state and action to be performed § Some actions preload next character • Identifiers distinguished from keywords by hashed lookup This differs from EAC advice (discussion later) Permits translation of identifiers into <type, symbol_index> § Keywords each get their own type
A Lexical Analysis Example
Comp 314 Lexical Scan Code current = START_STATE; token = ""; // assume next character has been preloaded into a buffer while (current != EX) { int char. Class = inputstream->this. Class(); switch (current->action(char. Class)) { case SKIP: inputstream->advance(); break; case ADD: char* t = token; int n = : : strlen(t); token = new char[n + 2]; : : strcpy(token, t); token[n] = inputstream->this. Char(); token[n+1] = 0; delete [] t; inputstream->advance(); break; case NAME: Entry * e = sym. Table->lookup(token); token. Type = (e->type==NULL_TYPE ? NAME_TYPE : e->type); break; . . . } current = current->next. State(char. Class); }
Tighter register specification (continued)
- Partial lexical correspondences
- Copyright 2003
- Copyright 2003 pearson education inc
- Copyrightfrance
- Copyright 2003
- Syntax and lexical analysis
- Charles keith
- Lexical analysis input buffering
- Syntax analysis
- Lexical analysis
- Lexical analysis finite automata
- Steə
- Regex analyser
- Lexical and syntax analysis
- Lexical analyser generator
- Lexical analysis calculator
- Pairwise disjointness test
- Panic mode recovery in lexical analysis
- Lexical analysis example
- Task of lexical analyzer
- Longest match rule in lexical analysis
- Minimisation
- Lexical and syntax analysis
- Lexical analysis