Lexical Analysis: Regular Expressions CS 471 September 3, 2007.

Slides:



Advertisements
Similar presentations
COS 320 Compilers David Walker. Outline Last Week –Introduction to ML Today: –Lexical Analysis –Reading: Chapter 2 of Appel.
Advertisements

Lexical Analysis Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou.
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
Context-Free Grammars Lecture 7
Lexical Analysis Textbook:Modern Compiler Design Chapter 2.1
Lexical Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 2.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Compiler Construction
COS 320 Compilers David Walker. Outline Last Week –Introduction to ML Today: –Lexical Analysis –Reading: Chapter 2 of Appel.
Lexical Analysis Recognize tokens and ignore white spaces, comments
1 Scanning Aaron Bloomfield CS 415 Fall Parsing & Scanning In real compilers the recognizer is split into two phases –Scanner: translate input.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
Lexical Analysis Mooly Sagiv Schrierber Wed 10:00-12:00 html:// Textbook:Modern.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lexical Analysis (I) Compiler Baojian Hua
Lexical Analysis I Specifying Tokens Lecture 2 CS 4318/5531 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper and Torczon.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Lexical Analysis: Regular Expressions CS 671 January 22, 2008.
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
LEX (04CS1008) A tool widely used to specify lexical analyzers for a variety of languages We refer to the tool as Lex compiler, and to its input specification.
Lexical Analysis: Finite Automata CS 471 September 5, 2007.
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
CS375 Compilers Lexical Analysis 4 th February, 2010.
CPS 506 Comparative Programming Languages Syntax Specification.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
IN LINE FUNCTION AND MACRO Macro is processed at precompilation time. An Inline function is processed at compilation time. Example : let us consider this.
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
Introduction to Compiling
Introduction CPSC 388 Ellen Walker Hiram College.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Lexical Analysis.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 2: Lexical Analysis.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
1 Compiler Construction Vana Doufexi office CS dept.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
Lecture 2 Lexical Analysis
CSc 453 Lexical Analysis (Scanning)
Chapter 1 Introduction.
PROGRAMMING LANGUAGES
Compiler Lecture 1 CS510.
Lecture 2: General Structure of a Compiler
Lexical Analysis & Syntactic Analysis
Review: Compiler Phases:
CSE401 Introduction to Compiler Construction
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CS 3304 Comparative Languages
Compiler design.
Compiler Structures 2. Lexical Analysis Objectives
Chapter 10: Compilers and Language Translation
Compiler Construction
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Presentation transcript:

Lexical Analysis: Regular Expressions CS 471 September 3, 2007

CS 471 – Fall A program that translates a program in one language to another language the essential interface between applications & architectures Typically lowers the level of abstraction analyzes and reasons about the program & architecture We expect the program to be optimized i.e., better than the original ideally exploiting architectural strengths and hiding weaknesses Last Time … Compiler High-Level Programming Languages Machine Code Error Messages

CS 471 – Fall Phases of a Compiler Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Source program Target program Lexical Analyzer Group sequence of characters into lexemes – smallest meaningful entity in a language (keywords, identifiers, constants) Characters read from a file are buffered – helps decrease latency due to i/o. Lexical analyzer manages the buffer Makes use of the theory of regular languages and finite state machines Lex and Flex are tools that construct lexical analyzers from regular expression specifications

CS 471 – Fall Phases of a Compiler Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Source program Target program Parser Convert a linear structure – sequence of tokens – to a hierarchical tree-like structure – an AST The parser imposes the syntax rules of the language Work should be linear in the size of the input (else unusable)  type consistency cannot be checked in this phase Deterministic context free languages and pushdown automata for the basis Bison and yacc allow a user to construct parsers from CFG specifications

CS 471 – Fall Phases of a Compiler Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Source program Target program Semantic Analysis Calculates the program’s “meaning” Rules of the language are checked (variable declaration, type checking) Type checking also needed for code generation (code gen for a + b depends on the type of a and b)

CS 471 – Fall Phases of a Compiler Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Source program Target program Intermediate Code Generation Makes it easy to port compiler to other architectures (e.g. Pentium to MIPS) Can also be the basis for interpreters (such as in Java) Enables optimizations that are not machine specific

CS 471 – Fall Phases of a Compiler Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Source program Target program Intermediate Code Optimization Constant propagation, dead code elimination, common sub-expression elimination, strength reduction, etc. Based on dataflow analysis – properties that are independent of execution paths

CS 471 – Fall Phases of a Compiler Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Source program Target program Native Code Generation Intermediate code is translated into native code Register allocation, instruction selection Native Code Optimization Peephole optimizations – small window is optimized at a time

CS 471 – Fall Administration 1. PA1 on website (from book): C Primer 2. Questionnaire Results…

CS 471 – Fall A Quick Linux Primer Tar – archiving program Gzip/Bzip2 – compression Cvs/Svn – version control Ssh – secure shell Scp/Sftp – secure copy Make/Scons – build/run utility Other useful tools: –Man! –Which –Locate –Diff (or sdiff)

CS 471 – Fall Makefiles Target: source file(s) command proj1 main.oio.odata.o main.cio.hio.cdata.hdata.c

CS 471 – Fall First Step: Lexical Analysis (Tokenizing) Breaking the program down into words or “tokens” Input: stream of characters Output: stream of names, keywords, punctuation marks Side effect: Discards white space, comments Source code: if (b==0) a = “Hi”; Token Stream: Lexical Analysis Parsing

CS 471 – Fall Lexical Tokens Identifiers: x y11 elsex _i00 Keywords: if else while break Integers: L Floating point: e5 0.e-10 Symbols: + * { } ++ = Strings: “x” “He said, \“Are you?\”” Comments: /** don’t change this **/

CS 471 – Fall Lexical Tokens float match0(char *s) /* find a zero */ { if (!strncmp(s, “0.0”, 3)) return 0.; } FLOAT ID(match0) _______ CHAR STAR ID(s) RPAREN LBRACE IF LPAREN BANG _______ LPAREN ID(s) COMMA STRING(0.0) ______ NUM(3) RPAREN RPAREN RETURN REAL(0.0) ______ RBRACE EOF

CS 471 – Fall Ad-hoc Lexer Hand-write code to generate tokens How to read identifier tokens? Token readIdentifier( ) { String id = “”; while (true) { char c = input.read(); if (!identifierChar(c)) return new Token(ID, id, lineNumber); id = id + String(c); }

CS 471 – Fall Problems Don’t know what kind of token we are going to read from seeing first character –if token begins with “i’’ is it an identifier? –if token begins with “2” is it an integer? constant? –interleaved tokenizer code is hard to write correctly, harder to maintain More principled approach: lexer generator that generates efficient tokenizer automatically (e.g., lex, flex)

CS 471 – Fall Issues How to describe tokens unambiguously 2.e0 20.e “” “x” “\\” “\”\’” How to break text down into tokens if (x == 0) a = x<<1; if (x == 0) a = x<1; How to tokenize efficiently –tokens may have similar prefixes –want to look at each character ~1 time

CS 471 – Fall How To Describe Tokens Programming language tokens can be described using regular expressions A regular expression R describes some set of strings L(R) L(R) is the “language” defined by R – L(abc) = { abc } – L(hello|goodbye) = {hello, goodbye} – L([1-9][0-9]*) = _______________ Idea: define each kind of token using RE

CS 471 – Fall Regular expressions Language – set of strings String – finite sequence of symbols Symbols – taken from a finite alphabet Specify languages using regular expressions Symbolaone instance of a Epsilon  empty string AlternationR | Sstring from either L(R) or L(S) ConcatenationR ∙ Sstring from L(R) followed by L(S) RepetitionR*

CS 471 – Fall Convenient Shorthand [abcd] one of the listed characters (a | b | c | d) [b-g] [bcdefg] [b-gM-Qkr] [^ab]anything but one of the listed chars [^a-f] M?Zero or one M M+One or more M M* “a.+*”literally a.+*.Any single character (except \n)

CS 471 – Fall Examples Regular Expression Strings in L(R) digit = [0-9] “0” “1” “2” “3” … posint = digit+ “8” “412” … int = -? posint “-42” “1024” … real = int (ε | (. posint)) “-1.56” “12” “1.0” [a-zA-Z_][a-zA-Z0-9_]* C identifiers Lexer generators support abbreviations – cannot be recursive

CS 471 – Fall More Examples Whitespace: Integers: Hex numbers: Valid UVa User Ids: Loop keywords in C:

CS 471 – Fall Breaking up Text elsex = 0; REs alone not enough: need rules for choosing Most languages: longest matching token wins –even if a shorter token is only way Exception: early FORTRAN (totally whitespace- insensitive) Ties in length resolved by prioritizing tokens RE’s + priorities + longest-matching token rule = lexer definition else x = 0;

CS 471 – Fall Lexer Generator Specification Input to lexer generator: –list of regular expressions in priority order –associated action for each RE (generates appropriate kind of token, other bookkeeping) Output: –program that reads an input stream and breaks it up into tokens according to the REs. (Or reports lexical error -- “Unexpected character” )

CS 471 – Fall Lex: A Lexical Analyzer Generator Lex produces a C program from a lexical specification % DIGITS [0-9]+ ALPHA [A-Za-z] CHARACTER {ALPHA}|_ IDENTIFIER {ALPHA}({CHARACTER}|{DIGITS})* % if{return IF; } {IDENTIFIER}{return ID; } {DIGITS}{return NUM; } ([0-9]+”.”[0-9]*)|([0-9]*”.”[0-9]+){return ____; }.{error(); }

CS 471 – Fall Summary Lexical analyzer converts a text stream to tokens Ad-hoc lexers hard to get right, maintain For most languages, legal tokens conveniently, precisely defined using regular expressions Lexer generators generate lexer code automatically from token RE’s, precedence Next time: –Theory: finite automata –Practice: how lexer generators work Read Chapter 2 Start PA1 if you haven’t already!