80 likes | 175 Views
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. Lexical analysis {week 04}. from Concepts of Programming Languages , 9th edition by Robert W. Sebesta, Addison-Wesley, 2010, ISBN 0-13-607347-6. Syntax analysis (i).
E N D
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. Lexical analysis{week 04} from Concepts of Programming Languages, 9th edition by Robert W. Sebesta, Addison-Wesley, 2010, ISBN 0-13-607347-6
Syntax analysis (i) • Syntax analysisconsists oftwo key components: • A low-level componentcalled a lexical analyzer • Based on a regular grammar • A high-level component calleda syntax analyzer or parser • Based on a context-free grammar or BNF
Syntax analysis (ii) • The syntax analyzer(parser)uses a context-free grammaror BNF • BNF is clear and concise • BNF is exact • BNF is readable by humansand machines alike • BNF is relatively easy to maintain
Lexical analysis (i) • Lexical analysisidentifiesthe small-scale languageconstructs • Including names, operators,numeric literals, punctuation,line numbers (BASIC), etc. • Such small-scale language constructsare called lexemes
Lexical analysis (ii) • Why separate lexical analysisfrom syntax analysis? • Simplicity: simpler approachesare suitable for lexical analysis • Efficiency: focuses optimizationefforts on lexical analysis andsyntax analysis separately • Portability: lexical analyzer notalways portable (due to file I/O), whereassyntax analyzer may remain portable
Lexical analysis (iii) • The lexical analyzer is simplya pattern matcher • Identifies and isolates lexemes • Lexemes are logical substringsof the source program thatbelong together • Lexical analyzer assigns codescalled tokens to the lexemes • e.g. sumis a lexeme; and IDENTis the token
Building a lexical analyzer • 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
What next? • Read and studyCh.4 (§4.1-§4.2) • Do Exercises at the end of Chapter 4 • Draw a complete state transition diagram for a valid floating-point numeric literal