LexicalAnalysis

LexicalAnalysis - Lexical Analysis Regular expressions and...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
cs5363 1 Lexical Analysis Regular expressions and Finite Automata
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cs5363 2 Phases of compilation Compilers Read input program optimization translate into machine code front end mid end back end Lexical analysis parsing Semantic analysis ……… Code gen Assembler Linker Characters Words/strings Sentences/ statements Meaning ……… translation
Background image of page 2
cs5363 3 Lexical analysis The first phase of compilation Also known as lexer, scanner Takes a stream of characters and returns tokens (words) Each token has a “type” and an optional “value” Called by the parser each time a new token is needed. IF LPARAN <ID “a”> EQ <ID “b”> RPARAN <ID “c”> ASSIGN <ID “a”> if (a == b) c = a;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
cs5363 4 Lexical analysis Typical tokens of programming languages Reserved words: class, int, char, bool,… Identifiers: abc, def, mmm, mine,… Constant numbers: 123, 123.45, 1.2E3… Operators and separators: (, ), <, <=, +, -, … Goal recognize token classes, report error if a string does not match any class A single reserved word: CLASS, INT, CHAR,… A single operator: LE, LT, ADD,… A single separator: LPARAN, RPARAN, COMMA,… The group of all identifiers: <ID “a”>, <ID “b”>,… The group of all integer constant: <INTNUM 1>,… The group of all floating point numbers <FLOAT 1.0>… Each token class could be
Background image of page 4
cs5363 5 Simple recognizers c NextChar() if (c ‘f’) then do something else c NextChar() if (c ‘e’) then do something else c NextChar() if (c ‘e’) then do something else report success s0 s1 s2 s3 f e e c NextChar(); if (c = ‘0’) then report success else if (c < ‘1’ or c > ‘9’) then do something else c NextChar() while (c >= ‘0’ and c <= ‘9’) c NextChar() report success s0 s2 s1 0 1..9 0..9
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Multiple token recognizers c NextChar() if (c ‘f’) then if (c ‘w’) then do something else c NextChar() if (c `h’) then do something else …… else c NextChar() if (c ‘e’) then if (c ‘i’) then do something else … else c NextChar()
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

LexicalAnalysis - Lexical Analysis Regular expressions and...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online