Transcription

JFlex Regular ExpressionsLecture 17Section 3.5, JFlex ManualRobb T. KoetherHampden-Sydney CollegeWed, Feb 25, 2015Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 20151 / 32

1Running JFlex2JFlex ords4AssignmentRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 20152 / 32

Outline1Running JFlex2JFlex ords4AssignmentRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 20153 / 32

The JAR FilesJFlex 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 ExpressionsWed, Feb 25, 20154 / 32

Running JFlexThe lexical analyzer generator is the Main class in the JFlexfolder.To create a lexical analyzer from the file filename.flex, typejava jflex.Main filename.flexThis 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 ExpressionsWed, Feb 25, 20155 / 32

Running the Lexical AnalyzerExample (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 ExpressionsWed, Feb 25, 20156 / 32

Outline1Running JFlex2JFlex ords4AssignmentRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 20157 / 32

JFlex RulesEach 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 ExpressionsWed, Feb 25, 20158 / 32

JFlex Regular ExpressionsRegular 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 ExpressionsWed, Feb 25, 20159 / 32

JFlex Regular ExpressionsRegularExpressionrr?r*r r srsMatchesOne occurrence of rZero or one occurrence of rZero or more occurrences of rOne or more occurrences of rr or sr concatenated with sr and s are regular expressions.Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201510 / 32

JFlex Regular ExpressionsParentheses 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 ExpressionsWed, Feb 25, 201511 / 32

JFlex Regular ExpressionsBrackets [ ] 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 ExpressionsWed, Feb 25, 201512 / 32

JFlex Regular ExpressionsA 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 ExpressionsWed, Feb 25, 201513 / 32

JFlex Escape SequencesEscapeSequence\n\r\b\t\fMatchesnewline (LF)carriage return (CR)backspace (BS)tab (TAB)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 ExpressionsWed, Feb 25, 201514 / 32

Outline1Running JFlex2JFlex ords4AssignmentRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201515 / 32

JFlex ExampleExample (JFlex Example)Design a JFlex file that will recognizeIdentifiersIntegersFloating-point numbersString literalsCommentsKeywordsRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201516 / 32

Outline1Running JFlex2JFlex ords4AssignmentRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201517 / 32

IdentifiersExample (Identifiers)letter [A-Za-z]digit [0-9]id {letter}({letter} {digit} " ")*Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201518 / 32

Outline1Running JFlex2JFlex ords4AssignmentRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201519 / 32

IntegersExample (Integers)digitsignnum [0-9] " " "-" {sign}?{digit} Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201520 / 32

IntegersExample (Integers)digitsignnum [0-9] " " "-" {sign}?{digit} What about octal numbers?Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201520 / 32

IntegersExample (Integers)digitsignnum [0-9] " " "-" {sign}?{digit} What about octal numbers?What about hexadecimal numbers?Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201520 / 32

JFlex ExampleExample (Floating-Point Numbers)digitsignnumpartexpfpnum [0-9]" " "-"{digit}*({digit}. .{digit}){digit}*E{sign}?{digit} {sign}?{numpart}{exp}?Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201521 / 32

Outline1Running JFlex2JFlex ords4AssignmentRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201522 / 32

JFlex ExampleExample (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 ExpressionsWed, Feb 25, 201523 / 32

JFlex ExampleExample (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 ExpressionsWed, Feb 25, 201523 / 32

JFlex Example"Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201524 / 32

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

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

JFlex Example\""not ",not \ ,not newlineRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201524 / 32

JFlex Examplenot newline\""not ",not \ ,not newlineRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201524 / 32

JFlex Examplenot newline\""not ",not \ ,not newlineRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201524 / 32

Outline1Running JFlex2JFlex ords4AssignmentRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201525 / 32

JFlex ExampleExample (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 ExpressionsWed, Feb 25, 201526 / 32

JFlex ExampleExample (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 ExpressionsWed, Feb 25, 201527 / 32

JFlex ExampleExample (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 ExpressionsWed, Feb 25, 201527 / 32

JFlex Example/Robb T. Koether (Hampden-Sydney College)*JFlex Regular ExpressionsWed, Feb 25, 201528 / 32

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

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

JFlex Example\/**/not *,not \Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201528 / 32

JFlex Example\/**/not /not *,not \Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201528 / 32

JFlex Exampleany\/**/not /not *,not \Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201528 / 32

JFlex Exampleany\/**/not /not *,not \Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201528 / 32

Outline1Running JFlex2JFlex ords4AssignmentRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201529 / 32

JFlex ExampleThe 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 ExpressionsWed, Feb 25, 201530 / 32

Outline1Running JFlex2JFlex ords4AssignmentRobb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201531 / 32

AssignmentAssignmentRead Section 3.5, which is about lex, not JFlex, but they are verysimilar.Robb T. Koether (Hampden-Sydney College)JFlex Regular ExpressionsWed, Feb 25, 201532 / 32

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