JFlex Regular Expressions

Similar documents
JFlex. Lecture 16 Section 3.5, JFlex Manual. Robb T. Koether. Hampden-Sydney College. Mon, Feb 23, 2015

Introduction to Compiler Design

Recognition of Tokens

The Critical-Path Algorithm

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

Fundamental Data Types

CSC 467 Lecture 3: Regular Expressions

Minimal Spanning Trees

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Recursive Descent Parsers

Basic PHP Lecture 17

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

LR Parsing - Conflicts

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

Rotations and Translations

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

LR Parsing - The Items

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

Lexical Analysis - Flex

More Examples. Lex/Flex/JLex

Lexical Analysis. Chapter 1, Section Chapter 3, Section 3.1, 3.3, 3.4, 3.5 JFlex Manual

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Formal Languages and Compilers Lecture VI: Lexical Analysis

Building the Abstract Syntax Trees

Density Curves Sections

Lexical Analysis and jflex

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Solving Recursive Sequences by Iteration

An introduction to Flex

The Decreasing-Time Algorithm

CS 301. Lecture 05 Applications of Regular Languages. Stephen Checkoway. January 31, 2018

Implementing Linked Lists

Simple Lexical Analyzer

Flex and lexical analysis. October 25, 2016

An overview of Java, Data types and variables

Scope and Parameter Passing

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

Sampling Distribution Examples Sections 15.4, 15.5

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

Programming in C++ 4. The lexical basis of C++

Figure 2.1: Role of Lexical Analyzer

Module 8 - Lexical Analyzer Generator. 8.1 Need for a Tool. 8.2 Lexical Analyzer Generator Tool

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 3

2 nd Week Lecture Notes

Structure of Programming Languages Lecture 3

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

Outline CS4120/4121. Compilation in a Nutshell 1. Administration. Introduction to Compilers Andrew Myers. HW1 out later today due next Monday.

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Scope and Parameter Passing

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Using Lex or Flex. Prof. James L. Frankel Harvard University

Friends and Unary Operators

Java Basic Datatypees

A program that performs lexical analysis may be termed a lexer, tokenizer, or scanner, though scanner is also a term for the first stage of a lexer.

Introduction to Programming (Java) 2/12

MP 3 A Lexer for MiniJava

CSCI312 Principles of Programming Languages!

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Chapter 3 -- Scanner (Lexical Analyzer)

Street-Routing Problems

The structure of a compiler

Appendix. As a quick reference, here you will find all the metacharacters and their descriptions. Table A-1. Characters

Chapter 3 Lexical Analysis

Displaying Distributions - Quantitative Variables

I/O and Parsing Tutorial

Flex and lexical analysis

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Interpreter. Scanner. Parser. Tree Walker. read. request token. send token. send AST I/O. Console

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

The Class Construct Part 1

Compiler course. Chapter 3 Lexical Analysis

ML 4 A Lexer for OCaml s Type System

Lexical Analysis. Chapter 2

Chapter 2. Lexical Elements & Operators

LECTURE 6 Scanning Part 2

Scheduling and Digraphs

The Pairwise-Comparison Method

Lexical Analysis. Lecture 3-4

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Lexical Analysis. Lecture 2-4

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

The x86 Instruction Set

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 2

Chapter 4. Lexical analysis. Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Introduction to Lex & Yacc. (flex & bison)

Parsing and Pattern Recognition

The Traveling Salesman Problem Brute Force Method

CS 541 Spring Programming Assignment 2 CSX Scanner

JME Language Reference Manual

4 Programming Fundamentals. Introduction to Programming 1 1

Data Types Literals, Variables & Constants

Typescript on LLVM Language Reference Manual

Transcription:

JFlex Regular Expressions Lecture 17 Section 3.5, JFlex Manual Robb T. Koether Hampden-Sydney College Wed, Feb 25, 2015 Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 1 / 32

1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments Keywords 4 Assignment Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 2 / 32

Outline 1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments Keywords 4 Assignment Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 3 / 32

The JAR Files JFlex uses a number of Java class. These classes have been compiled and are stored in the Java archive file flex-1.6.0.jar. Assignment 6 will have instructions on how to download and install this file. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 4 / 32

Running JFlex The lexical analyzer generator is the Main class in the JFlex folder. To create a lexical analyzer from the file filename.flex, type java jflex.main filename.flex This produces a file Yylex.java (or whatever we named it), which must be compiled to create the lexical analyzer. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 5 / 32

Running the Lexical Analyzer Example (Using the Yylex Class) InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); Yylex lexer = new Yylex(br); token = lexer.yylex(); To run the lexical analyzer, a Yylex object must first be created. The Yylex constructor has one parameter, specifying a Reader. We will convert standard input, which is an input stream, to a buffered reader. Then call the function yylex() to get the next token. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 6 / 32

Outline 1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments Keywords 4 Assignment Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 7 / 32

JFlex Rules Each JFlex rule consists of a regular expression and an action to be taken when the expression is matched. The associated action is a segment of Java code, enclosed in braces { }. Typically, the action will be to return the appropriate token. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 8 / 32

JFlex Regular Expressions Regular expressions are expressed using ASCII characters (32-126). The following characters are metacharacters.? * + ( ) ˆ $. [ ] { } " \ Metacharacters have special meaning; they do not represent themselves. All other characters represent themselves. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 9 / 32

JFlex Regular Expressions Regular Expression Matches r One occurrence of r r? Zero or one occurrence of r r* Zero or more occurrences of r r+ One or more occurrences of r r s r or s rs r concatenated with s r and s are regular expressions. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 10 / 32

JFlex Regular Expressions Parentheses are used for grouping. The expression ("+" "-")? represents an optional plus or minus sign. If a regular expression begins with ˆ, then it is matched only at the beginning of a line. If a regular expression ends with $, then it is matched only at the end of a line. The dot. matches any non-newline character. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 11 / 32

JFlex Regular Expressions Brackets [ ] match any single character listed within the brackets. For example, [abc] matches a or b or c. [A-Za-z] matches any letter. If the first character after [ is ˆ, then the brackets match any character except those listed. [ˆA-Za-z] matches any nonletter. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 12 / 32

JFlex Regular Expressions A single character within double quotes " " or after \ represents itself, except for n, r, b, t, and f. Metacharacters lose their special meaning and represent themselves when they stand alone within single quotes or follow \. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 13 / 32

JFlex Escape Sequences Escape Sequence Matches \n newline (LF) \r carriage return (CR) \b backspace (BS) \t tab (TAB) \f form feed (FF) The character c is matched by c, "c", and \c. The character n is matched by n and "n", but not \n. The character? is matched by "?" and \?, but not?. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 14 / 32

Outline 1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments Keywords 4 Assignment Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 15 / 32

JFlex Example Example (JFlex Example) Design a JFlex file that will recognize Identifiers Integers Floating-point numbers String literals Comments Keywords Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 16 / 32

Outline 1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments Keywords 4 Assignment Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 17 / 32

Identifiers Example (Identifiers) letter = [A-Za-z] digit = [0-9] id = {letter}({letter} {digit} "_")* Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 18 / 32

Outline 1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments Keywords 4 Assignment Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 19 / 32

Integers Example (Integers) digit = [0-9] sign = "+" "-" num = {sign}?{digit}+ Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 20 / 32

Integers Example (Integers) digit = [0-9] sign = "+" "-" num = {sign}?{digit}+ What about octal numbers? Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 20 / 32

Integers Example (Integers) digit = [0-9] sign = "+" "-" num = {sign}?{digit}+ What about octal numbers? What about hexadecimal numbers? Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 20 / 32

JFlex Example Example (Floating-Point Numbers) digit = [0-9] sign = "+" "-" numpart = {digit}*({digit}..{digit}){digit}* exp = E{sign}?{digit}+ fpnum = {sign}?{numpart}{exp}? Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 21 / 32

Outline 1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments Keywords 4 Assignment Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 22 / 32

JFlex Example Example (String Literals) What regular expression would describe string literals? "hello" represents hello. "\"hello\"" represents "hello". "\h\e\l\l\o" represents hello. String literals may not include newlines. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 23 / 32

JFlex Example Example (String Literals) What regular expression would describe string literals? "hello" represents hello. "\"hello\"" represents "hello". "\h\e\l\l\o" represents hello. String literals may not include newlines. Draw a transition diagram for a DFA and then use it to write the regular expression. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 23 / 32

JFlex Example " Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 24 / 32

JFlex Example " " Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 24 / 32

JFlex Example \ " " Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 24 / 32

JFlex Example \ " " not ", not \, not newline Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 24 / 32

JFlex Example not newline \ " " not ", not \, not newline Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 24 / 32

JFlex Example not newline \ " " not ", not \, not newline Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 24 / 32

Outline 1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments Keywords 4 Assignment Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 25 / 32

JFlex Example Example (Inline Comments) What regular expression would describe inline comments? Inline comments begin with // and extend up to, but not including, the next newline (or EOF). Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 26 / 32

JFlex Example Example (Multiline Comments) What regular expression would describe multiline comments? Multiline comments begin with /* and end with */. In between, there may occur any characters, including newlines. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 27 / 32

JFlex Example Example (Multiline Comments) What regular expression would describe multiline comments? Multiline comments begin with /* and end with */. In between, there may occur any characters, including newlines. Draw a transition diagram for a DFA and then use it to write the regular expression. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 27 / 32

JFlex Example / * Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 28 / 32

JFlex Example / * * / Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 28 / 32

JFlex Example \ / * * / Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 28 / 32

JFlex Example \ / * * / not *, not \ Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 28 / 32

JFlex Example \ / * * / not *, not \ not / Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 28 / 32

JFlex Example any / * * / \ not *, not \ not / Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 28 / 32

JFlex Example any \ / * * / not *, not \ not / Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 28 / 32

Outline 1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments Keywords 4 Assignment Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 29 / 32

JFlex Example The regular expression for a keyword is the very same sequence of characters. For example, the regular expression for if is if. How is JFlex to distinguish between keywords and identifiers? Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 30 / 32

Outline 1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments Keywords 4 Assignment Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 31 / 32

Assignment Assignment Read Section 3.5, which is about lex, not JFlex, but they are very similar. Robb T. Koether (Hampden-Sydney College) JFlex Regular Expressions Wed, Feb 25, 2015 32 / 32