
Lexical Analysis Compiler Design CSE 504 1 Introduction 2 Regular Expressions 3 Finite State Automata 4 Generating Lexical Analyzers Last modifled: Mon Feb 18 2013 at 16:20:58 EST Version: 1.5 02:19:15 2013/02/19 Compiled at 17:09 on 2015/03/02 Compiler Design Lexical Analysis CSE 504 1 / 55 Introduction Syntax Analysis Structure of a Language Grammars: Notation to succinctly represent the structure of a language. Example: Stmt −! if Expr then Stmt else Stmt Stmt −! while Expr do Stmt Stmt −! do Stmt until Expr . Expr −! Expr + Expr . Compiler Design Lexical Analysis CSE 504 2 / 55 Introduction Syntax Analysis Grammars Stmt −! if Expr then Stmt else Stmt Terminal symbols: if, then, else Terminal symbols represent group of characters in input language: Tokens. Analogous to words. Nonterminal symbols: Stmt, Expr Nonterminal symbols represent a sequence of terminal symbols. Analogous to sentences. Compiler Design Lexical Analysis CSE 504 3 / 55 Introduction Syntax Analysis Phases of Syntax Analysis 1 Identify the words: Lexical Analysis. Converts a stream of characters (input program) into a stream of tokens. Also called Scanning or Tokenizing. 2 Identify the sentences: Parsing. Derive the structure of sentences: construct parse trees from a stream of tokens. Compiler Design Lexical Analysis CSE 504 4 / 55 Introduction Syntax Analysis Applications of Languages Command Interpreters: csh, perl, ... Programming: FORTRAN, SmallTalk, ... Document Structuring: troff,LATEX, HTML, ... Page Definition: PostScript, PCL, ... Databases: SQL, ... Hardware Design: VHDL, VeriLog, ... ... and many many more Compiler Design Lexical Analysis CSE 504 5 / 55 Introduction Syntax Analysis Language Processing Flexible control: programmable combination of primitive operations. Express input to the system in a well defined language. Translate the input into the sequence of primitive operations. Direct execution Byte code emulation Object code compilation Language processing techniques have evolved over the last 30 years. In almost every domain, at least three steps can be identified: lexical analysis, parsing, and syntax-directed translation. Compiler Design Lexical Analysis CSE 504 6 / 55 Introduction Lexical Analysis Lexical Analysis Convert a stream of characters into a stream of tokens. Simplicity: Conventions about \words" are often different from conventions about \sentences". Efficiency: Word identification problem has a much more efficient solution than sentence identification problem. Portability: Character set, special characters, device features. Compiler Design Lexical Analysis CSE 504 7 / 55 Introduction Lexical Analysis Terminology Token: Name given to a family of words. e.g., integer constant Lexeme: Actual sequence of characters representing a word. e.g., 32894 Pattern: Notation used to identify the set of lexemes represented by a token. e.g., [0 − 9]+ Compiler Design Lexical Analysis CSE 504 8 / 55 Introduction Lexical Analysis Terminology A few more examples: Token Sample Lexemes Pattern while while while integer constant 32894, -1093, 0 [0-9]+ identifier buffer size [a-zA-Z]+ Compiler Design Lexical Analysis CSE 504 9 / 55 Introduction Lexical Analysis Patterns How do we compactly represent the set of all lexemes corresponding to a token? For instance: The token integer constant represents the set of all integers: that is, all sequences of digits (0{9), preceded by an optional sign (+ or −). Obviously, we cannot simply enumerate all lexemes. Use Regular Expressions. Compiler Design Lexical Analysis CSE 504 10 / 55 Regular Expressions Expressions & Meaning Regular Expressions Notation to represent (potentially) infinite sets of strings over alphabet Σ. a: stands for the set fag that contains a single string a. a j b: stands for the set fa; bg that contains two strings a and b. Analogous to Union. ab: stands for the set fabg that contains a single string ab. Analogous to Product. (ajb)(ajb): stands for the set faa; ab; ba; bbg. a∗: stands for the set f, a; aa; aaa;:::g that contains all strings of zero or more a's. Analogous to closure of the product operation. stands for the empty string. Compiler Design Lexical Analysis CSE 504 11 / 55 Regular Expressions Expressions & Meaning Regular Expressions Examples of Regular Expressions over fa; bg: (ajb)∗: Set of strings with zero or more a's and zero or more b's: f, a; b; aa; ab; ba; bb; aaa; aab;:::g (a∗b∗): Set of strings with zero or more a's and zero or more b's such that all a's occur before any b: f, a; b; aa; ab; bb; aaa; aab; abb;:::g (a∗b∗)∗: Set of strings with zero or more a's and zero or more b's: f, a; b; aa; ab; ba; bb; aaa; aab;:::g Compiler Design Lexical Analysis CSE 504 12 / 55 Regular Expressions Expressions & Meaning Language of Regular Expressions Let R be the set of all regular expressions over Σ. Then, Empty String: 2 R Unit Strings: α 2 Σ ) α 2 R Concatenation: r1; r2 2 R ) r1r2 2 R Alternative: r1; r2 2 R ) (r1 j r2) 2 R Kleene Closure: r 2 R ) r ∗ 2 R Compiler Design Lexical Analysis CSE 504 13 / 55 Regular Expressions Expressions & Meaning Regular Expressions Example: (a j b)∗ L0 = fg L1 = L0 · fa; bg = fg · fa; bg = fa; bg L2 = L1 · fa; bg = fa; bg · fa; bg = faa; ab; ba; bbg L3 = L2 · fa; bg . 1 [ L = Li = f, a; b; aa; ab; ba; bb;:::g i=0 Compiler Design Lexical Analysis CSE 504 14 / 55 Regular Expressions Expressions & Meaning Semantics of Regular Expressions Semantic Function L : Maps regular expressions to sets of strings. L() = fg L(α) = fαg (α 2 Σ) L(r1 j r2) = L(r1) [L(r2) L(r1 r2) = L(r1) ·L(r2) L(r ∗) = fg [ (L(r) ·L(r ∗)) Compiler Design Lexical Analysis CSE 504 15 / 55 Regular Expressions Expressions & Meaning Computing the Semantics L(a) = fag L(a j b) = L(a) [L(b) = fag [ fbg = fa; bg L(ab) = L(a) ·L(b) = fag · fbg = fabg L((a j b)(a j b)) = L(a j b) ·L(a j b) = fa; bg · fa; bg = faa; ab; ba; bbg Compiler Design Lexical Analysis CSE 504 16 / 55 Regular Expressions Expressions & Meaning Computing the Semantics of Closure Example: L((a j b)∗) = fg [ (L(a j b) ·L((a j b)∗)) L0 = fg Base case L1 = fg [ (fa; bg · L0) = fg [ (fa; bg · fg) = f, a; bg L2 = fg [ (fa; bg · L1) = fg [ (fa; bg · f, a; bg) = f, a; b; aa; ab; ba; bbg . ∗ L((a j b) ) = L1 = f, a; b; aa; ab; ba; bb;:::g Compiler Design Lexical Analysis CSE 504 17 / 55 Regular Expressions Expressions & Meaning Another Example L((a∗b∗)∗) : L(a∗) = f, a; aa;:::g L(b∗) = f, b; bb;:::g L(a∗b∗) = f, a; b; aa; ab; bb; aaa; aab; abb; bbb;:::g L((a∗b∗)∗) = fg [f, a; b; aa; ab; bb; aaa; aab; abb; bbb;:::g [f, a; b; aa; ab; ba; bb; aaa; aab; aba; abb; baa; bab; bba; bbb;:::g . = f, a; b; aa; ab; ba; bb;:::g Compiler Design Lexical Analysis CSE 504 18 / 55 Regular Expressions Regular Definitions Regular Definitions Assign \names" to regular expressions. For example, digit −! 0 j 1 j · · · j 9 natural −! digit digit∗ Shorthands: a+: Set of strings with one or more occurrences of a. a?: Set of strings with zero or one occurrences of a. Example: integer −! (+|−)?digit+ Compiler Design Lexical Analysis CSE 504 19 / 55 Regular Expressions Regular Definitions Regular Definitions: Examples float −! integer. fraction integer −! (+|−)? no leading zero no leading zero −! (nonzero digit digit∗) j 0 fraction −! no trailing zero exponent? no trailing zero −! (digit∗ nonzero digit) j 0 exponent −! (E j e) integer digit −! 0 j 1 j · · · j 9 nonzero digit −! 1 j 2 j · · · j 9 Compiler Design Lexical Analysis CSE 504 20 / 55 Regular Expressions Regular Definitions Regular Definitions and Lexical Analysis Regular Expressions and Definitions specify sets of strings over an input alphabet. They can hence be used to specify the set of lexemes associated with a token. That is, regular expressions and definitions can be used as the pattern language How do we decide whether an input string belongs to the set of strings specified by a regular expression? Compiler Design Lexical Analysis CSE 504 21 / 55 Regular Expressions Regular Definitions Using Regular Definitions for Lexical Analysis Q: Is ababbaabbb in L(((a∗b∗)∗)? A: Hm. Well. Let's see. L((a∗b∗)∗) = fg [f, a; b; aa; ab; bb; aaa; aab; abb; bbb;:::g [f, a; b; aa; ab; ba; bb; aaa; aab; aba; abb; baa; bab; bba; bbb;:::g . = ??? Compiler Design Lexical Analysis CSE 504 22 / 55 Regular Expressions Regular Definitions Recognizers Construct automata that recognize strings belonging to a language. Finite State Automata ) Regular Languages Finite State ! cannot maintain arbitrary counts. Push Down Automata ) Context-free Languages Stack is used to maintain counter, but only one counter can go arbitrarily high. Compiler Design Lexical Analysis CSE 504 23 / 55 Finite State Automata Recognizers Recognizing Finite Sets of Strings Identifying words from a small, finite, fixed vocabulary is straightforward. For instance, consider a stack machine with push, pop, and add operations with two constants: 0 and 1. We can use the automaton: p 0 1 a u o integer_constant d s p d h pop add push Compiler Design Lexical Analysis CSE 504 24 / 55 Finite State Automata Recognizers Finite State Automata Represented by a labeled directed graph. A finite set of states (vertices). Transitions between states (edges). Labels on transitions are drawn from Σ [ fg. One distinguished start state. One or more distinguished final states. Compiler Design Lexical Analysis CSE 504 25 / 55 Finite State Automata Recognizers Finite State Automata: An Example Consider the Regular Expression (a j b)∗a(a j b). L((a j b)∗a(a j b)) = faa; ab; aaa; aab; baa; bab; aaaa; aaab; abaa; abab; baaa;:::g.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages28 Page
-
File Size-