jflex regular expressions - hampden-sydney...

47
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

Upload: others

Post on 01-Nov-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Regular ExpressionsLecture 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

Page 2: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

1 Running JFlex

2 JFlex Rules

3 ExampleIdentifiersNumbersStringsCommentsKeywords

4 Assignment

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

Page 3: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

Outline

1 Running JFlex

2 JFlex Rules

3 ExampleIdentifiersNumbersStringsCommentsKeywords

4 Assignment

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

Page 4: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

The JAR Files

JFlex uses a number of Java class.These classes have been compiled and are stored in the Javaarchive file flex-1.6.0.jar.Assignment 6 will have instructions on how to download andinstall this file.

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

Page 5: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

Running JFlex

The lexical analyzer generator is the Main class in the JFlexfolder.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

Page 6: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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 abuffered 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

Page 7: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

Outline

1 Running JFlex

2 JFlex Rules

3 ExampleIdentifiersNumbersStringsCommentsKeywords

4 Assignment

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

Page 8: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Rules

Each JFlex rule consists of a regular expression and an action tobe taken when the expression is matched.The associated action is a segment of Java code, enclosed inbraces { }.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

Page 9: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Regular Expressions

Regular expressions are expressed using ASCII characters (32 -126).The following characters are metacharacters.

? * + | ( ) ˆ $ . [ ] { } " \

Metacharacters have special meaning; they do not representthemselves.All other characters represent themselves.

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

Page 10: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Regular Expressions

RegularExpression Matches

r One occurrence of rr? Zero or one occurrence of rr* Zero or more occurrences of rr+ One or more occurrences of rr|s r or srs 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

Page 11: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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 thebeginning of a line.If a regular expression ends with $, then it is matched only at theend 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

Page 12: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Regular Expressions

Brackets [ ] match any single character listed within thebrackets.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 anycharacter except those listed.

[ˆA-Za-z] matches any nonletter.

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

Page 13: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Regular Expressions

A single character within double quotes " " or after \ representsitself, except for n, r, b, t, and f.Metacharacters lose their special meaning and representthemselves when they stand alone within single quotes or follow \.

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

Page 14: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Escape Sequences

EscapeSequence 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

Page 15: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

Outline

1 Running JFlex

2 JFlex Rules

3 ExampleIdentifiersNumbersStringsCommentsKeywords

4 Assignment

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

Page 16: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

Example (JFlex Example)Design a JFlex file that will recognize

IdentifiersIntegersFloating-point numbersString literalsCommentsKeywords

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

Page 17: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

Outline

1 Running JFlex

2 JFlex Rules

3 ExampleIdentifiersNumbersStringsCommentsKeywords

4 Assignment

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

Page 18: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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

Page 19: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

Outline

1 Running JFlex

2 JFlex Rules

3 ExampleIdentifiersNumbersStringsCommentsKeywords

4 Assignment

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

Page 20: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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

Page 21: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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

Page 22: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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

Page 23: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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

Page 24: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

Outline

1 Running JFlex

2 JFlex Rules

3 ExampleIdentifiersNumbersStringsCommentsKeywords

4 Assignment

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

Page 25: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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 theregular expression.

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

Page 26: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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 theregular expression.

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

Page 27: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

"

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

Page 28: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

" "

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

Page 29: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

"

\

"

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

Page 30: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

"

\

"

not ",not \ ,

not newline

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

Page 31: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

"

\

"

not ",not \ ,

not newline

not newline

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

Page 32: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

"

\

"

not ",not \ ,

not newline

not newline

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

Page 33: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

Outline

1 Running JFlex

2 JFlex Rules

3 ExampleIdentifiersNumbersStringsCommentsKeywords

4 Assignment

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

Page 34: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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

Page 35: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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 theregular expression.

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

Page 36: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

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 theregular expression.

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

Page 37: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

*/

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

Page 38: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

*/ /*

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

Page 39: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

*/ /*

\

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

Page 40: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

*

not *,not \

/ /*

\

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

Page 41: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

*

not *,not \

/ /*

not /

\

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

Page 42: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

*

not *,not \

any

/ /*

not /

\

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

Page 43: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

*

not *,not \

any

/ /*

not /

\

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

Page 44: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

Outline

1 Running JFlex

2 JFlex Rules

3 ExampleIdentifiersNumbersStringsCommentsKeywords

4 Assignment

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

Page 45: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

JFlex Example

The regular expression for a keyword is the very same sequenceof 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

Page 46: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

Outline

1 Running JFlex

2 JFlex Rules

3 ExampleIdentifiersNumbersStringsCommentsKeywords

4 Assignment

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

Page 47: JFlex Regular Expressions - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures...1 Running JFlex 2 JFlex Rules 3 Example Identifiers Numbers Strings Comments

Assignment

AssignmentRead Section 3.5, which is about lex, not JFlex, but they are verysimilar.

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