40 likes | 93 Views
Learn how to build a lexical analyzer using state transition diagrams to identify tokens in an input stream. Understand the correct representation of integer and floating-point literals for effective programming. Find errors in sample diagrams and improve your programming skills.
E N D
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. In-class diagrams (9/20/2010){week 04} from Concepts of Programming Languages, 9th edition by Robert W. Sebesta, Addison-Wesley, 2010, ISBN 0-13-607347-6
Building a lexical analyzer (i) • To build a lexical analyzer, we first must define the state transition diagram • Describes how the token is identified • Consists of a start state andat least one final state • Transitions occur based oncharacters encountered onthe input stream • What’s wrong with this diagram? minus(-) start neg digit (0-9) digit (0-9) digit (0-9) int
Building a lexical analyzer (ii) minus(-) start neg handle all valid integer literals, including 0, 1, -1, 100, 123, etc. 0 digit (1-9) digit (1-9) digit (0-9) int int
Building a lexical analyzer (iii) 0 dot minus(-) digit (1-9) digit (0-9) start neg dot dot 0 dbl digit (1-9) digit (0-9) digit (1-9) int int dot dot handle all valid floating-point numbers