cs 614: theory and construction of compilers lecture 5 fall 2003 department of computer science...

8
CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Upload: marcus-conley

Post on 17-Jan-2018

217 views

Category:

Documents


0 download

DESCRIPTION

Lecture 5©2003 Joel Jones3 Lexing Tool Basics Translate lexing specification to program Regular expressions Action code

TRANSCRIPT

Page 1: CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones

CS 614: Theory and Construction of Compilers

Lecture 5Fall 2003Department of Computer ScienceUniversity of AlabamaJoel Jones

Page 2: CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Lecture 5 ©2003 Joel Jones 2

Introduction to Lexing Tools C is action language

lex flex

Java is action language Jlex

Page 3: CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Lecture 5 ©2003 Joel Jones 3

Lexing Tool Basics Translate lexing specification to program Regular expressions Action code

Page 4: CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Lecture 5 ©2003 Joel Jones 4

Regular Expressions . Matches any single

character * Matches zero or more

copies of the preceding expression

[] Character class which matches any character within the brackets. If it starts with a circumflex ‘^’ negates test. A dash indicates a range

^ Matches the beginning of a line

$ Matches the end of a line \ escapes metacharacters e.g.

“\n” is a newline character, “\*” is a literal asterisk

+ Matches one or more occurrences

? Matches zero or one occurences

Page 5: CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Lecture 5 ©2003 Joel Jones 5

Regular Expression (Continued) | Matches either the

regular expression before or after

“…” Interprets everything inside quotes literally

() Groups a series of regular expression together into a new regular expression

Page 6: CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Lecture 5 ©2003 Joel Jones 6

Regular Expression Examples Digit Integer Optional Unary Minus Decimal Numbers

(0.0, 4.5, but not 0 or 2, or unary minus)

Decimal Numbers (no unary minus)

Decimal Numbers (with unary minus)

Float style exponent (just the exponent, ‘e’ or ‘E’, optionally signed)

Complete Real number

Page 7: CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Lecture 5 ©2003 Joel Jones 7

JLex Structuren user coden %%n JLex directivesn %%n regular expression rules

Page 8: CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Lecture 5 ©2003 Joel Jones 8

Reading & Questions for Next Class JLex webpages

Home Page• http://www.cs.princeton.edu/~appel/modern/java/JLex/

Tutorial• http://bmrc.berkeley.edu/courseware/cs164/fall99/assignment/a1/tutorial.html