An Introduction to JLex/JavaCC

Slides:



Advertisements
Similar presentations
Compiler construction in4020 – lecture 2 Koen Langendoen Delft University of Technology The Netherlands.
Advertisements

Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
1 JavaCUP JavaCUP (Construct Useful Parser) is a parser generator Produce a parser written in java, itself is also written in Java; There are many parser.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #4 Lexical.
Introduction to Lex/JLex
Tools for building compilers Clara Benac Earle. Tools to help building a compiler C –Lexical Analyzer generators: Lex, flex, –Syntax Analyzer generator:
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
Professor Yihjia Tsai Tamkang University
Scanning with Jflex.
1 Material taught in lecture Scanner specification language: regular expressions Scanner generation using automata theory + extra book-keeping.
1 Scanning Aaron Bloomfield CS 415 Fall Parsing & Scanning In real compilers the recognizer is split into two phases –Scanner: translate input.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
A brief [f]lex tutorial Saumya Debray The University of Arizona Tucson, AZ
CS 536 Spring Learning the Tools: JLex Lecture 6.
Compiler Construction Lexical Analysis Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Lexical analysis S a, b a B.
Lexical analysis S a, b a B. 2 Lexical analysis in perspective LEXICAL ANALYZER – Scan Input – Remove White Space, New Line, … – Identify.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
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.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Lesson 10 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CPSC 388 – Compiler Design and Construction Scanners – JLex Scanner Generator.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 3, 09/11/2003 Prof. Roy Levow.
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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
LEX (04CS1008) A tool widely used to specify lexical analyzers for a variety of languages We refer to the tool as Lex compiler, and to its input specification.
Jianguo Lu 1 Explanation on Assignment 2, part 1 DFASimulator.java –The algorithm is on page 116, dragon book. DFAInput.txt The Language is: 1* 0 1* 0.
Lexical Analysis: Finite Automata CS 471 September 5, 2007.
JLex Lecture 4 Mon, Jan 24, JLex JLex is a lexical analyzer generator in Java. It is based on the well-known lex, which is a lexical analyzer generator.
CPS 506 Comparative Programming Languages Syntax Specification.
Introduction to Lexical Analysis and the Flex Tool. © Allan C. Milne Abertay University v
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Lexical Analysis: DFA Minimization & Wrap Up. Automating Scanner Construction PREVIOUSLY RE  NFA ( Thompson’s construction ) Build an NFA for each term.
The Role of Lexical Analyzer
C Chuen-Liang Chen, NTUCS&IE / 35 SCANNING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei,
Lexical Analysis – Part II EECS 483 – Lecture 3 University of Michigan Wednesday, September 13, 2006.
Scanner Generation Using SLK and Flex++ Followed by a Demo Copyright © 2015 Curt Hill.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 2: Lexical Analysis.
LECTURE 6 Scanning Part 2. FROM DFA TO SCANNER In the previous lectures, we discussed how one might specify valid tokens in a language using regular expressions.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
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.
CS 3304 Comparative Languages
Lexical analysis S a, b a B.
Lecture 2 Lexical Analysis
Tutorial On Lex & Yacc.
Chapter 2 :: Programming Language Syntax
Chapter 2 Scanning – Part 1 June 10, 2018 Prof. Abdelaziz Khamis.
Using SLK and Flex++ Followed by a Demo
RegExps & DFAs CS 536.
JLex Lecture 4 Mon, Jan 26, 2004.
CPSC 388 – Compiler Design and Construction
Lecture 5: Lexical Analysis III: The final bits
CS 3304 Comparative Languages
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CS 3304 Comparative Languages
Chapter 2 :: Programming Language Syntax
Lexical Analysis - An Introduction
Chapter 2 :: Programming Language Syntax
Compiler Design 3. Lexical Analyzer, Flex
Presentation transcript:

An Introduction to JLex/JavaCC Professor Yihjia Tsai Tamkang University

Automating Lexical Analysis Overall picture Scanner generator NFA RE Java scanner program String stream DFA Minimize DFA Simulate DFA Tokens

Inside lexical analyzer generator Classes in JLex: CAccept CAcceptAnchor CAlloc CBunch CDfa CDTrans CEmit CError CInput CLexGen CMakeNfa CMinimize CNfa CNfa2Dfa CNfaPair CSet CSimplifyNfa CSpec CUtility Main SparseBitSet ucsb How does a lexical analyzer work? Get input from user who defines tokens in the form that is equivalent to regular grammar Turn the regular grammar into a NFA Convert the NFA into DFA Generate the code that simulates the DFA

How scanner generator is used Write the scanner specification; Generate the scanner program using scanner generator; Compile the scanner program; Run the scanner program on input streams, and produce sequences of tokens. Scanner generator (e.g., JLex) Scanner definition (e.g., JLex spec) Scanner program, e.g., Scanner.java Java compiler Scanner program (e.g., Scanner.java) Scanner.class Scanner Scanner.class Input stream Sequence of tokens

JLex specification JLex specification consists of three parts User Java code, to be copied verbatim into the scanner program; %% JLex directives, macro definitions, commonly used to specify letters, digits, whitespace; Regular expressions and actions: Specify how to divide input into tokens; Regular expressions are followed by actions; Print error messages; return token codes; There is no need to put characters back to input (done by JLex) The three sections are separated by “%%”

First JLex Example simple.lex Recognize int and identifiers. %% %{ public static void main(String argv[]) throws java.io.IOException { MyLexer yy = new MyLexer(System.in); while (true){ yy.yylex(); } %} %notunix %type void %class MyLexer %eofval{ return; %eofval} IDENTIFIER = [a-zA-z_][a-zA-Z0-9_]* "int" { System.out.println("INT recognized");} {IDENTIFIER} { System.out.println("ID is ..." + yytext());} \r\n {} . {}

Code generated will be in simple.lex.java class MyLexer { public static void main(String argv[]) throws java.io.IOException { MyLexer yy = new MyLexer(System.in); while (true){ yy.yylex(); } public void yylex(){ ... ... case 5:{ System.out.println("INT recognized"); } case 7:{ System.out.println("ID is ..." + yytext()); } Copied from internal code directive

Running the JLex example Steps to run the JLex D:\214>java JLex.Main simple.lex Processing first section -- user code. Processing second section -- JLex declarations. Processing third section -- lexical rules. Creating NFA machine representation. NFA comprised of 22 states. Working on character classes.::::::::. NFA has 10 distinct character classes. Creating DFA transition table. Working on DFA states........... Minimizing DFA transition table. 9 states after removal of redundant states. Outputting lexical analyzer code. D:\214>move simple.lex.java MyLexer.java D:\214>javac MyLexer.java D:\214>java MyLexer int myid0 INT recognized ID is ...myid0

Exercises Try to modify JLex directives in the previous JLex spec, and observe whether it is still working. If it is not working, try to understand the reason. Remove “%notunix” directive; Change “return;” to “return null;”; Remove “%type void”; ... ... Move the Identifier regular expression before the “int” RE. What will happen to the input “int”? What if you remove the last line (line 19, “. {}”) ?

Extend the example: add returning and use classes class UseLexer { public static void main(String [] args) throws java.io.IOException { Token t; MyLexer2 lexer=new MyLexer2(System.in); while ((t=lexer.yylex())!=null) System.out.println(t.toString()); } class Token { String type; String text; int line; Token(String t, String txt, int l) { type=t; text=txt; line=l; } public String toString(){ return text+" " +type + " " +line; } %% …. Continued

Example Continued %notunix %line %type Token %class MyLexer2 %eofval{ return null; %eofval} IDENTIFIER = [a-zA-z_][a-zA-Z0-9_]* %% "int" { return(new Token("INT", yytext(), yyline));} {IDENTIFIER} { return(new Token("ID", yytext(), yyline));} \r\n {} . {}

Code generated from mylexer2.lex class UseLexer { public static void main(String [] args) throws java.io.IOException { Token t; MyLexer2 lexer=new MyLexer2(System.in); while ((t=lexer.yylex())!=null) System.out.println(t.toString()); } class Token { String type; String text; int line; Token(String t, String txt, int l) { type=t; text=txt; line=l; } public String toString(){ return text+" " +type + " " +line; } Class MyLexer2 { public Token yylex(){ ... ... case 5: { return(new Token("INT", yytext(), yyline)); } case 7: { return(new Token("ID", yytext(), yyline)); }

Running the extended lex specification mylexer2.lex D:\214>java JLex.Main mylexer2.lex Processing first section -- user code. Processing second section -- JLex declarations. Processing third section -- lexical rules. Creating NFA machine representation. NFA comprised of 22 states. Working on character classes.::::::::. NFA has 10 distinct character classes. Creating DFA transition table. Working on DFA states........... Minimizing DFA transition table. 9 states after removal of redundant states. Outputting lexical analyzer code. D:\214>move mylexer2.lex.java MyLexer2.java D:\214>javac MyLexer2.java D:\214>java UseLexer int int INT 0 x1 x1 ID 1

Another example 1 import java.io.IOException; 2 %% 3 %public 2 %% 3 %public 4 %class Numbers_1 5 %type void 6 %eofval{ 7 return; 8 %eofval} 9 10 %line 11 %{ public static void main (String args []) { 12 Numbers_1 num = new Numbers_1(System.in); 13 try { 14 num.yylex(); 15 } catch (IOException e) { System.err.println(e); } 16 } 17 %} 18 19 %% 20 \r\n { System.out.println("--- " + (yyline+1)); 21 } 22 .*\r\n { System.out.print ("+++ " + (yyline+1)+"\t"+yytext()); 23 }

User code This code is copied verbatim into the lexical analyzer source file that JLex outputs, at the top of the file. Package declarations; Imports of an external class Class definitions Generated code package declarations; import packages; Class definitions; class Yylex { ... ... } Yylex class is the default lexer class name. It can be changed to other class name using %class directive.

JLex directives Internal code to lexical analyzer class Marco Definition State Declaration Character Counting Lexical Analyzer Component title Specifying the Return Value on End-of-File Specifying an Interface to Implement Java CUP Compatibility

Regular expression rules General form: regularExpression { action} Example: {IDENTIFIER} { System.out.println("ID is ..." + yytext());} Interpretation: Patten to be matched code to be executed when the pattern is matched Code generated in MyLexer: “ case 2: { System.out.println("ID is ..." + yytext());} “

Internal Code to Lexical Analyzer Class %{ …. %} directive permits the declaration of variables and functions internal to the generated lexical analyzer General form: %{ <code > %} Effect: <code > will be copied into the Lexer class, such as MyLexer. class MyLexer{ …..<code> …… } Example public static void main(String argv[]) throws java.io.IOException { MyLexer yy = new MyLexer(System.in); while (true){ yy.yylex(); } Difference with the user code section It is copied inside the lexer class (e.g., the MyLexer class)

Macro Definition Purpose: define once and used several times; A must when we write large lex specification. General form of macro definition: <name> = <definition> should be contained on a single line Macro name should be valid identifiers Macro definition should be valid regular expressions Macro definition can contain other macro expansions, in the standard {<name>} format for macros within regular expressions. Example Definition (in the second part of JLex spec): IDENTIFIER = [a-zA-z_][a-zA-Z0-9_]* ALPHA=[A-Za-z_] DIGIT=[0-9] ALPHA_NUMERIC={ALPHA}|{DIGIT} Use (in the third part): {IDENTIFIER} {return new Token(ID, yytext()); }

State directive Same string could be matched by different regular expressions, according to its surrounding environment. String “int” inside comment should not be recognized as a reserved word, not even an identifier. Particularly useful when you need to analyze mixed languages;

State Directive Continued For example, in JSP, Java programs can be imbedded inside HTML blocks. Once you are inside Java block, you follow the Java syntax. But when you are out of the Java block, you need to follow the HTML syntax. In java “int” should be recognized as a reserved word; In HTML “int” should be recognized just as a usual string. States inside JLex <HTMLState> %{ { yybegin(JavaState); } <HTMLState> “int” {return string; } <JavaState> %} { yybegin(HTMLState); } <JavaState> “int” {return keyword; }

State Directive (cont.) Mechanism to mix FA states and REs Declaring a set of “start states” (in the second part of JLex spec) %state state0 [, state1, state2, …. ] How to use the state (in the third part of JLex spec): RE can be prefixed by the set of start states in which it is valid; We can make a transition from one state to another with input RE yybegin(STATE) is the command to make transition to STATE; YYINITIAL : implicit start state of yylex(); But we can change the start state; Example (from the sample JLex spec): %state COMMENT %% <YYINITIAL>if {return tok(sym.IF,”IF”);} <YYINITIAL>[a-z]+ {return tok(sym.ID, yytext());} <YYINITIAL>”(*” {yybegin(COMMENT);} <COMMENT>”*)” {yybegin(YYINITIAL);} <COMMENT>. {}

Character and line counting Sometimes it is useful to know where exactly the token is in the text. Token position is implemented using line counting and char counting. Character counting is turned off by default, activated with the directive “%char” Create an instance variable yychar in the scanner; zero-based character index of the first character on the matched region of text. Line counting is turned off by default, activated with the directive “%line” Create an instance variable yyline in the scanner; zero-based line index at the beginning of the matched region of text. Example “int” { return (new Yytoken(4,yytext(),yyline,yychar,yychar+3)); }

Lexical Analyzer Component Titles Change the name of generated lexical analyzer class %class <name> the tokenizing function %function <name> the token return type %type <name> Default names class Yylex { /* lexical analyzer class */ public Yytoken /* the token return type */ yylex() { …} /* the tokenizing function */ ==> Yylex.yylex() returns Yytoken type

Specifying an Interface to implement Form: %implements <classname> Allows the user to specify an interface which the Yylex class will implement. The generated parser class declaration will look like: class MyLexer implements classname { …….

Regular Expression Rules Specifies rules for breaking the input stream into tokens Regular Expression + Actions (java code) [<states>] <expression> { <action>} when matched with more than one rule, choose the rule that matches the longest string; choose the rule that is given first in the Jlex spec. Refer the “int” and IDENTIFIER example. The rules given in a JLex specification should match all possible input. An error will be raised if the generated lexer receives input that does not match any of its rules put the following rule at the bottom of RE spec . {java.lang.System.out.println(“Error:” + yytext());} dot(.) will match any input except for the newline.

Available Lexical Values within Action code java.lang.String yytext() matched portion of the character input stream; always active. int yychar Zero-based character index of the first character in the matched portion of the input stream; activated by %char directive. int yyline Zero-based line number of the start of the matched portion of the input stream; activated by %line directive.

Regular expression in JLex • Special characters: ? + | ( ) ˆ $ / ; . = < > [ ] { } ” \ and blank After \ the special characters lose their special meaning. (Example: \+) Between double quotes ” all special characters but \ and ” lose their special meaning. (Example: ”+”) The following escape sequences are recognized: \b \n \t \f \r. With [ ] we can describe sets of characters. [abc] is the same as (a|b|c) With [ˆ ] we can describe sets of characters. [ˆ\n\”] means anything but a newline or quotes [ˆa–z] means anything but a lower-case letter We can use . as a shortcut for [ˆ\n] $: denotes the end of a line. If $ ends a regular expression, the expression matched only at the end of a line.

Concluding remarks About JLex Focused on Lexical Analysis Process, Including Regular Expressions Finite Automaton Conversion Lex Interplay among all these various aspects of lexical analysis Regular grammar=regular expression Regular expression NFA DFA lexer The next step in the compilation process is Parsing: Context free grammar; Top-down parsing and bottom up parsing.

Using javaCC for lexical analysis javacc is a “top-down” parser generator. Some parser generators (such as yacc , bison, and JavaCUP) need a separate lexical-analyzer generator. With javaCC, you can specify the tokens within the parser generator.

javaCC specification of a lexer

Live demo Run javaCC Compile files RUN the lexer. Look at the token and constant structure. look at main()

Dealing with errors Error reporting: 123e+q Could consider it an invalid token (lexical error) or return a sequence of valid tokens 123, e, +, q, and let the parser deal with the error.

Lexical error correction? Sometimes interaction between the Scanner and parser can help especially in a top-down (predictive) parse The parser, when it calls the scanner, can pass as an argument the set of allowable tokens. Suppose the Scanner sees calss in a context where only a top-level definition is allowed. Not too hard to guess what is meant. Scanner can guess that class was intended, generate a warning message, and return to parsing. Why should a compiler halt if it can figure out, with high probability, what was intended? Most lexical errors are character insertion, deletion, replacement, or transposition. PLC compiler from mid 1970’s did this well.

Same symbol, different meaning. How can the scanner distinguish between binary minus and unary minus? x = -a; vs x = 3 – a; It can’t. It has to simply pass MINUS back to the parser and let it distinguish.

Scanner “troublemakers” Unclosed strings Unclosed comments.

Building Faster Scanners from the DFA Table-driven recognizers waste a lot of effort Read (& classify) the next character Find the next state Assign to the state variable Branch back to the top We can do better Encode state & actions in the code Do transition tests locally Generate ugly, spaghetti-like code (it is OK, this is automatically generated code) Takes (many) fewer operations per input character state = s0 ; string = ; char = get_next_char(); while (char != eof) { state = (state,char); string = string + char; char = get_next_char(); } if (state in Final) then report acceptance; else report failure;

Javacc Overview Generates a top down parser. Could be used for generating a Prolog parser which is in LL. Generates a parser in Java. Hence can be integrated with any Java based Prolog compiler/interpreter to continue our example. Token specification and grammar specification structures are in the same file => easier to debug.

Types of Productions in Javacc There can be four different kinds of Productions. Javacode For something that is not context free or is difficult to write a grammar for. eg) recognizing matching braces and error processing. Regular Expressions Used to describe the tokens (terminals) of the grammar. BNF Standard way of specifying the productions of the grammar. Token Manager Declarations The declarations and statements are written into the generated Token Manager (lexer) and are accessible from within lexical actions.

Javacc Look-ahead mechanism Exploration of tokens further ahead in the input stream. Backtracking is unacceptable due to performance hit. By default Javacc has 1 token look-ahead. Could specify any number for look-ahead. Two types of look-ahead mechanisms Syntactic A particular token is looked ahead in the input stream. Semantic Any arbitrary Boolean expression can be specified as a look-ahead parameter. eg) A -> aBc and B -> b ( c )? Valid strings: “abc” and “abcc”

References Jlex Manual Compilers Principles, Techniques and Tools, Aho, Sethi, and Ullman