College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.

Slides:



Advertisements
Similar presentations
1 2.Lexical Analysis 2.1Tasks of a Scanner 2.2Regular Grammars and Finite Automata 2.3Scanner Implementation.
Advertisements

COS 320 Compilers David Walker. Outline Last Week –Introduction to ML Today: –Lexical Analysis –Reading: Chapter 2 of Appel.
From Cooper & Torczon1 The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language?
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #4 Lexical.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
Lexical Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 2.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
Chapter 3 Chang Chi-Chung. The Structure of the Generated Analyzer lexeme Automaton simulator Transition Table Actions Lex compiler Lex Program lexemeBeginforward.
COS 320 Compilers David Walker. Outline Last Week –Introduction to ML Today: –Lexical Analysis –Reading: Chapter 2 of Appel.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
1 Chapter 3 Scanning – Theory and Practice. 2 Overview Formal notations for specifying the precise structure of tokens are necessary  Quoted string in.
Automata and Regular Expression Discrete Mathematics and Its Applications Baojian Hua
Lexical Analysis The Scanner Scanner 1. Introduction A scanner, sometimes called a lexical analyzer A scanner : – gets a stream of characters (source.
Scanner 1. Introduction A scanner, sometimes called a lexical analyzer A scanner : – gets a stream of characters (source program) – divides it into tokens.
1 Scanning Aaron Bloomfield CS 415 Fall Parsing & Scanning In real compilers the recognizer is split into two phases –Scanner: translate input.
CPSC 388 – Compiler Design and Construction Scanners – Finite State Automata.
1 Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Compiler Construction Lexical Analysis. The word lexical means textual or verbal or literal. The lexical analysis implemented in the “SCANNER” module.
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 - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
COMP 3438 – Part II - Lecture 2: Lexical Analysis (I) Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
Lexical Analysis I Specifying Tokens Lecture 2 CS 4318/5531 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper and Torczon.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
Scanning & FLEX CPSC 388 Ellen Walker Hiram College.
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP.
1 November 1, November 1, 2015November 1, 2015November 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Lexical Analysis: Finite Automata CS 471 September 5, 2007.
Scanner Introduction to Compilers 1 Scanner.
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.
C Chuen-Liang Chen, NTUCS&IE / 35 SCANNING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,
1st Phase Lexical Analysis
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Chapter 2 Scanning. Dr.Manal AbdulazizCS463 Ch22 The Scanning Process Lexical analysis or scanning has the task of reading the source program as a file.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
1 Compiler Construction Vana Doufexi office CS dept.
LEX SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
LEX & Yacc Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Compiler Chapter 4. Lexical Analysis Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Department of Software & Media Technology
Scanner Scanner Introduction to Compilers.
Chapter 2 Scanning – Part 1 June 10, 2018 Prof. Abdelaziz Khamis.
Finite-State Machines (FSMs)
Using SLK and Flex++ Followed by a Demo
Finite-State Machines (FSMs)
Compiler Lecture 1 CS510.
Chapter 3: Lexical Analysis
Review: Compiler Phases:
Lecture 5: Lexical Analysis III: The final bits
CS 3304 Comparative Languages
Scanner Scanner Introduction to Compilers.
CS 3304 Comparative Languages
Scanner Scanner Introduction to Compilers.
4b Lexical analysis Finite Automata
Lexical Analysis - An Introduction
Compiler Structures 2. Lexical Analysis Objectives
Scanner Scanner Introduction to Compilers.
Compiler Construction
Scanner Scanner Introduction to Compilers.
Scanner Scanner Introduction to Compilers.
Presentation transcript:

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation Techniques Dr. Ying JIN Associate Professor Oct. 2007

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -2- Outline 2.1 Overview General Function of a Scanner Some Issues about Scanning 2.2 Finite Automata Definition and Implementation of DFA Non-Determinate Finite Automata Transforming NFA into DFA Minimizing DFA 2.3 Regular Expressions Definition of Regular Expressions Regular Definition From Regular Expression to DFA 2.4 Design and Implementation of a Scanner Developing a Scanner from DFA A Scanner Generator – Lex √ √ √

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques Design and Implementation of a Scanner Developing a Scanner Manually A Scanner Generator – Lex

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -4- Developing a Scanner Manually Develop a Scanner Lexical Definition in Regular Expression NFA DFA transforming minimized DFA minimizing implement

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -5- Implement Scanner with DFA Implementation of DFA –J–Just checking whether a string is acceptable by the DFA; Implementation of a Scanner –n–not checking; –b–but recognizing an acceptable string(word) and establish its internal representation <token-type, semantic information>

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -6- Defining Lexical Structure of ToyL letter = a|…|z|A|…|Z digit = 0|…|9 NZ-digit = 1|…|9 Reserved words: Reserved = var| if | then | else| while| read| write| int Identifiers: letter digit* Constant: integer: int = NZ-digit digit* | 0 Other symbols: syms = + |- | * |/ | > | < | = | : | ; | ( | ) | { | } Lexical structure: lex = Reserved | identifier |int | syms

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -7- DFA for ToyL start done ID IntNum Assign NZ-digit digit other +, -, *, /, > | < | = | : | ; | ( | ) | { | } : = letter digit other refer to those symbols that are not allowed! Reserved(key)-words will be decided by checking identifier in reserved(key)-words table;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -8- Developing a Scanner from DFA Input: a sequence of symbols, with a special symbol EOF as the end of the sequence; Output: a sequence of tokens;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -9- Developing a Scanner from DFA Token Type: typedef enum { IDE, NUM, ASS, // 标识符,整数,赋值号 PLUS, MINUS, MUL, // +, - , * , DIV, GT, LT, EQ, // /, >, <, = SEMI, LPAREN, RPAREN // ;, (. ) LG, RG, COLON, // {, }, : VAR, THEN, ELSE, INT, WHILE, READ, WRITE, IF } TkType //keywords

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -10- Developing a Scanner from DFA Data Structure for TOKEN: struct Token {TkType type; string val[50];}

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -11- Developing a Scanner from DFA Global Variables: - string str[50]; store the string has been read already; - int len = 0; the length of the str - Token tk; current token - Token TokenList[100]; ---- the sequence of tokens - int total = 0; the number of tokens generated

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -12- Developing a Scanner from DFA Predefined Functions: - ReadNext() --- read current symbol to CurrentChar, if current symbol is EOF returns false; else returns true; - IsKeyword(str) --- checking whether str is one of keywords, if str is a keyword, it returns the number of the keywords; else it returns -1;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -13- Developing a Scanner from DFA if (not ReadNext()) exit; start: case CurrentChar of “ 1..9 ” : str[len] = CurrentChar; len++; goto IntNum ; “ a..z ”, “ A..Z ” : str[len] = CurrentChar; len++; goto ID; “ : ” : goto Assign; “ + ” : tk.type =PLUS; if (not ReadNext()) exit; …… ( “ -,*,/, >, <, =, :, ;, (, ), {, } ” ) other: error();

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -14- Developing a Scanner from DFA IntNum: if (not ReadNext()) {if len !=0 error; exit;}; case CurrentChar of “ 0..9 ” : str[len] = CurrentChar; len++; goto IntNum ; other: tk.type = NUM, strcpy(tk.val, str);

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -15- Developing a Scanner from DFA ID: if (not ReadNext()) {if len !=0 error; exit;}; case CurrentChar of “ 0..9 ” : str[len] = CurrentChar; len++; goto ID; “ a..z ”, “ A..Z ” : str[len] = CurrentChar; len++; goto ID; other: if IsKeyword(str) {tk.type = IsKeyword(str) } else {tk.type = IDE, strcpy(tk.val, str) }; goto done;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -16- Developing a Scanner from DFA Assign: if (not ReadNext()) {if len !=0 error; exit;}; case CurrentChar of “ = ” : Tk.type = ASS; if (not ReadNext()) exit; goto Done ; other:error();

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -17- Developing a Scanner from DFA Done: TokenList[total] = tk; // add new token to the token list total ++; // len = 0; //start storing new token string strcpy(str, “” ); // reset the token string goto start; //start scanning new token

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -18- A Scanner Generator – Lex Different versions of Lex; flex is distributed by GNU compiler package produced by the Free Software Foundation, which is freely available from Internet; flex.l RE-like definition lexyy.c ( yylex() )

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -19- Summary for §2. Scanning

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -20- Summary About finite automata –Definition of DFA ( , start state, set of states, set of terminate states, f) –Definition of NFA ( , set of start states, set of states, set of terminate states, f) –Differences between NFA and DFA Number of start states  Allows more than one ledges for a state and one same symbol

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -21- Summary About finite automata –From NFA to DFA main idea solve problem –Minimizing DFA main idea solve problem –Implementing DFA

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -22- Summary About regular expressions –Definition of regular expression –Regular definition –From regular expression to NFA main idea solving problem –Defining lexical structure with regular expression

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -23- Summary About scanner –Defining lexical structure of the programming language with regular expression –Transforming regular expression into NFA –Transforming NFA into DFA –Minimizing DFA –Implementing DFA

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -24- Summary Original Problem –Develop a Scanner Read source program in the form of stream of characters, and recognize tokens with respect to lexical rules of source language; General techniques –Use RE to define Lexical structure; –RE -> NFA -> DFA -> minimized DFA -> implement General Problem –Use RE/FA to define the structural rules –Check whether the input meets the structural rules

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -25- Summary Application in similar problems –Use RE(DFA) to formally describe the structures Strings Security policies Interface specification –Check whether a string meets the structural rules; Whether certain execution meets security policies; Properties checking

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -26-

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -27- Reading Assignment Topic: How to develop a parser( 语法分析器 ) ? Objectives –Get to know What is a parser? (input, output, functions) The Syntactical structure of a C programs? Different Parsing techniques and their main idea? References –Optional textbooks Hand in a report either in English or in Chinese, and one group will be asked to give a presentation at the beginning of next class; Tips: –Collect more information from textbooks and internet; –Establish your own opinion;