2 Lexical Analyzer
2 Lexical Analyzer
College of science
Department of Computer Science
Compiler
First Phase of Compiler
Lexical Analyzer
2023-2024
Mzhda Hiwa Hama
Lexical Analyzer
• The lexical analyzer takes a source program as input, and
produces a stream of tokens as output. The lexical analyzer
might recognize particular instances of tokens Typically :
Lexem Token
int Keyword
sum Identifier
= Assignment operator
3 Number
+ Addition operator
2 Number
; Punctuation symbol
.
Token Classes
3. Separator:
.
Functions of Lexical Analyzer
1. It produces stream of tokens.
• The scanner can report about characters that are not valid
tokens (e.g., an illegal or unrecognized symbol) and a few other
malformed entities (illegal characters within a string constant,
unterminated comments, etc.)
Error handling
Example 1 : printf(“Compiler");$
This is a lexical error since an illegal
character $ appears at the end of statement.
There are two ways that we can handle reserved words that
look like identifiers:
• initial is a lexeme that is mapped into the token (id, 2), where
2 points to the symbol-table entry for initial.
Lexical
analyzer
Parser
Simple Example of Symbol table
Lexeme: variable_name
Token Type: IDENTIFIER
Attribute:<symbol_table_entry_ptr>