Regular Expressions and Lexical Analysis

Slides:



Advertisements
Similar presentations
Lexical Analysis Consider the program: #include main() { double value = 0.95; printf("value = %f\n", value); } How is this translated into meaningful machine.
Advertisements

Compiler Baojian Hua Lexical Analysis (II) Compiler Baojian Hua
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
CS252: Systems Programming Ninghui Li Topic 4: Regular Expressions and Lexical Analysis.
 Lex helps to specify lexical analyzers by specifying regular expression  i/p notation for lex tool is lex language and the tool itself is refered to.
Winter 2007SEG2101 Chapter 81 Chapter 8 Lexical Analysis.
Costas Busch - RPI1 Single Final State for NFAs. Costas Busch - RPI2 Any NFA can be converted to an equivalent NFA with a single final state.
1 Chapter 2: Scanning 朱治平. Scanner (or Lexical Analyzer) the interface between source & compiler could be a separate pass and places its output on an.
Tools for building compilers Clara Benac Earle. Tools to help building a compiler C –Lexical Analyzer generators: Lex, flex, –Syntax Analyzer generator:
Chapter 3 Chang Chi-Chung. The Structure of the Generated Analyzer lexeme Automaton simulator Transition Table Actions Lex compiler Lex Program lexemeBeginforward.
Fall 2004COMP 3351 Regular Expressions. Fall 2004COMP 3352 Regular Expressions Regular expressions describe regular languages Example: describes the language.
A brief [f]lex tutorial Saumya Debray The University of Arizona Tucson, AZ
Finite-State Machines with No Output Longin Jan Latecki Temple University Based on Slides by Elsa L Gunter, NJIT, and by Costas Busch Costas Busch.
1 Flex. 2 Flex A Lexical Analyzer Generator  generates a scanner procedure directly, with regular expressions and user-written procedures Steps to using.
Scanning & Parsing with Lex and YACC
Compilers: lex/3 1 Compiler Structures Objectives – –describe lex – –give many examples of lex's use , Semester 1, Lex.
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
Review: Regular expression: –How do we define it? Given an alphabet, Base case: – is a regular expression that denote { }, the set that contains the empty.
Other Issues - § 3.9 – Not Discussed More advanced algorithm construction – regular expression to DFA directly.
COMP 3438 – Part II - Lecture 2: Lexical Analysis (I) Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
Scanning & FLEX CPSC 388 Ellen Walker Hiram College.
FLEX Fast Lexical Analyzer EECS Introduction Flex is a lexical analysis (scanner) generator. Flex is provided with a user input file or Standard.
Flex: A fast Lexical Analyzer Generator CSE470: Spring 2000 Updated by Prasad.
TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP.
By Neng-Fa Zhou Lexical Analysis 4 Why separate lexical and syntax analyses? –simpler design –efficiency –portability.
Introduction to Lex Ying-Hung Jiang
1 Using Lex. 2 Introduction When you write a lex specification, you create a set of patterns which lex matches against the input. Each time one of the.
1 Using Lex. Flex – Lexical Analyzer Generator A language for specifying lexical analyzers Flex compilerlex.yy.clang.l C compiler -lfl a.outlex.yy.c a.outtokenssource.
COMPILERS AND INTERPRETERS Lesson 3 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker Department.
Introduction to Lex Fan Wu
Lex.
Introduction to Lexical Analysis and the Flex Tool. © Allan C. Milne Abertay University v
Lecture 3 RegExpr  NFA  DFA Topics Lex, Flex Thompson Construction Subset construction (maybe) Readings: , 3.7, 3.6 January 18, 2006 CSCE 531.
Flex Fast LEX analyzer CMPS 450. Lexical analysis terms + A token is a group of characters having collective meaning. + A lexeme is an actual character.
Practical 1-LEX Implementation
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
Overview of Previous Lesson(s) Over View  Symbol tables are data structures that are used by compilers to hold information about source-program constructs.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
C Chuen-Liang Chen, NTUCS&IE / 35 SCANNING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Set 27 HANDLING COMMENTS IN LEX & SEARCHING & SORTING IN C.
1 Steps to use Flex Ravi Chotrani New York University Reviewed By Prof. Mohamed Zahran.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
LEX SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
9-December-2002cse Tools © 2002 University of Washington1 Lexical and Parser Tools CSE 413, Autumn 2002 Programming Languages
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Lexical Analysis.
Chapter 3 Lexical Analysis.
NFAs, scanners, and flex.
Tutorial On Lex & Yacc.
CSc 453 Lexical Analysis (Scanning)
Using SLK and Flex++ Followed by a Demo
Command Line Arguments
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University
Regular Languages.
TDDD55- Compilers and Interpreters Lesson 2
JLex Lecture 4 Mon, Jan 26, 2004.
Single Final State for NFA
Lexical Analysis Why separate lexical and syntax analyses?
Review: Compiler Phases:
Lecture 5: Lexical Analysis III: The final bits
Other Issues - § 3.9 – Not Discussed
Compiler Construction
Compiler Structures 3. Lex Objectives , Semester 2,
Appendix B.1 Lex Appendix B.1 -- Lex.
Systems Programming & Operating Systems Unit – III
Lexical Analysis - Scanner-Contd
Lex Appendix B.1 -- Lex.
Presentation transcript:

Regular Expressions and Lexical Analysis IUST Compilers - TA Class Morteza Zakeri – Mohsen Amirian Fall 2017

Outline Regular Expressions Convert FA to Regular Expressions Introduction to flex

Regular Expressions

Regular Expressions Regular expressions describe regular languages Example: describes the language

Recursive Definition Primitive regular expressions: Given regular expressions and Are regular expressions

Examples A regular expression: Not a regular expression:

Languages of Regular Expressions : language of regular expression Example

Definition For primitive regular expressions:

Definition (continued) For regular expressions and

Example Regular expression:

Example Regular expression

Example Regular expression

Example Regular expression = { all strings with at least two consecutive 0 }

Example Regular expression = { all strings without two consecutive 0 }

Convert FA to RegExp

Generalized Transition Graph Obtaining the final regular expression:

From construct the equivalent Generalized Transition Graph transition labels are regular expressions Example:

Another Example:

Reducing the states:

Resulting Regular Expression:

In General Removing states:

More complex example

Introduction to flex

Format of the Input File %option noyywrap %{ #include<stdio.h> %} Definitions %% Rules (Regular Expressions) Actions ( C codes) int main() { yylex(); // include the main loop return 0; }

Toy example Definitions Rule and action %{ #include<stdio.h> #include<conio.h> int count; %} digit [0-9] letter [a-zA-Z] %% {letter}({letter}|{digit})* { count++; printf("matched!!!”); } int main(void) { yylex(); printf("number of identifiers = %d\n", count); return 0; } Definitions Rule and action

Toy example: run…

Patterns ‘x’ match the character ’x’ ‘.’ any character (byte) except newline ‘[xyz]’ a character class; in this case, the pattern matches either an ’x’, a ’y’, or a ’z’ ‘[^A-Z]’ a "negated character class", i.e., any character but those in the class. In this case, any character EXCEPT an uppercase letter. ‘r*’ zero or more r’s, where r is any regular expression ‘r+’ one or more r’s ‘r?’ zero or one r’s (that is, “an optional r”) ‘r{2,5}’ anywhere from two to five r’s

Example (0+2)*[(1+3)(0+2)*(1+3)]*

Predefined Variables Name Function int yylex(void) call to invoke lexer, returns token char *yytext pointer to matched string yyleng length of matched string yylval value associated with token int yywrap(void) wrapup, return 1 if done, 0 if not done FILE *yyout output file FILE *yyin input file

Read input from file int main( int argc, char **argv ) { ++argv, --argc; /* skip over program name */ if ( argc > 0 ) yyin = fopen( argv[0], "r" ); } else{ yyin = stdin; yylex(); return 0;

Refer to our video tutorial. Use lex In Windows and Mac: Refer to our video tutorial. In Unix and Linux:

Reference An Introduction to Formal Languages and Automata, Peter Linz Dr. Costas Busch Slides Slides 3 to 20 Flex Online Manual http://dinosaur.compilertools.net/flex/index.html

Thank you!