SlideShare a Scribd company logo
Programming Languages
Building a Web Brower
Instructor : Westley Weimer
1
2
3
Lexical Analysis
the process of converting a sequence of characters into a sequence of tokens
Lexical
Analysis
String
(I like you, …)
List of Tokens
4
Token
the smallest unit of a programming language that has a meaning
"I <b> like"
word
start of tag
word
end of tag
word
I
<
b
>
like
5
"I <b>like</b> you"
I
<
b
>
Like
</
b
>
you
WORD
LANGLE
WORD
RANGLE
WORD
LANGLESLASH
WORD
RANGLE
WORD
word
start of tag
word
end of tag
word
start of closing tag
word
end of closing tag
word
6
Python Lex-Yacc
A computer program that generates lexical analyzers
Lexical
Analysis
String Token
LexInput
Definition section
%%
Rules section
%%
C code section
7
tokens = (
‘LANGLE’, # <
‘LANGLESLASH’, # </
‘RANGLE’, # >
‘RANGLESLASH’, # />
‘EQUAL’, # =
‘STRING’, # “love”
‘WORD’, # like
)
1. Define Name of Token
8
→ t_RANGLES = r’>’
→ def t_RANGLES(token):
r’>’
return token
→ def t_NUMBER(token):
r’[0-9]+’
token.value = int(token.value)
return token
2. Define Type of Token
9
→ htmllexer = lex.lex()
htmllexer.input(htmlcode)
while True:
tok = htmllexer.token()
if not tok: break
print tok
3. Building and Using the Lexer
10
Python Code
11
Output
Input(Htmlcode) = This is <b>my</b> webpage!
→ LexToken(WORD, ‘This’, 1, 0)
LexToken(WORD, ‘is’, 1, 5)
LexToken(LANGLE, ‘<’, 1, 8)
LexToken(WORD, ‘b’, 1, 9)
LexToken(RANGLE, ‘>’, 1, 10)
LexToken(WORD, ‘my’, 1, 11)
LexToken(LANGLESLASH, ‘</’, 1, 13)
LexToken(WORD, ‘b’, 1, 15)
LexToken(RANGLE, ‘>’, 1, 16)
LexToken(WORD, ‘webpage!’, 1, 18)
12
- Rule Order
- White Space
- Tracking Line Number
- Comment
Notice
13
→ def t_STRING(token)
r’”[^”]*”’
return token
→ def t_WORD(token)
r’[^ <>n]+’
return token
Rule Order
?
“Hello”
Ordering token definitions is of prime importance.
First One Wins!
14
→ def t_WHITESPACES(token)
r’ ’
pass
→ t_ignore = ‘ ‘ # ‘ tvr’
White Space
Tokens do not refer to white space
15
→ def t_newline(token)
r’n’
token.lexer.lineno += 1
pass
Tracking Line Number
Lex knows nothing about line numbers
16
→ states = (
(‘htmlcomment’, ‘exclusive’),
)
→ def t_htmlcomment(t)
r’<!--’
t.lexer.begin(‘htmlcomment’)
Comment
Not find words or strings or numbers or tags
→ def t_htmlcomment_end(t)
r’-->’
t.lexer.lineno += t.value.count(‘n’)
t.lexer.begin(‘INITIAL’)
→ def t_htmlcomment_error(t)
t.lexer.skip(1)
17

More Related Content

PPTX
codin9cafe[2015.01.29]Interactive 3D graphics - 박희수
PPTX
codin9cafe[2015.02.04]Git의 기초(2) - 김병수
PPTX
codin9cafe[2015.05.20]Dom - 안민영
PPTX
codin9cafe[2015.03.04]Open course(programming languages) - 장철호(Ch Jang)
PPTX
codin9cafe[2015.03. 18]Git 브랜치 - 김병수
PPTX
Open course(programming languages) 20150211
PPTX
codin9cafe[2015.01.22]Intro to computer science - 최지선
PPTX
silsilah nabi dan rasul
codin9cafe[2015.01.29]Interactive 3D graphics - 박희수
codin9cafe[2015.02.04]Git의 기초(2) - 김병수
codin9cafe[2015.05.20]Dom - 안민영
codin9cafe[2015.03.04]Open course(programming languages) - 장철호(Ch Jang)
codin9cafe[2015.03. 18]Git 브랜치 - 김병수
Open course(programming languages) 20150211
codin9cafe[2015.01.22]Intro to computer science - 최지선
silsilah nabi dan rasul

Similar to codin9cafe[2015.01.28]Open course(programming languages) - 장철호(Ch Jang) (20)

PPTX
Lexical Analysis - NLP - WPS Office.pptx
PPT
Lexical Analysis
PPTX
04LexicalAnalysissnsnjmsjsjmsbdjjdnd.pptx
PPT
Data design and analysis of computing tools
PPT
Lecturer-05 lex anylser (1).pptrjyghsgst
PDF
COMPILER DESIGN.pdf
PPTX
Cd ch2 - lexical analysis
PPTX
Role-of-lexical-analysis
PPTX
LexicalAnalysis chapter2 i n compiler design.pptx
PPTX
Ch03-LexicalAnalysis chapter2 in compiler design.pptx
PPT
1.Role lexical Analyzer
PPTX
Lexical Analysis - Compiler Design
DOCX
Compiler Design
PPT
Lexical Analysis
PDF
Lexical
PPTX
Lexical analyzer
PDF
role of lexical parser compiler design1-181124035217.pdf
PPTX
A Role of Lexical Analyzer
PPTX
Chapter 2.pptx compiler design lecture note
PPTX
Compiler Construction ( lexical analyzer).pptx
Lexical Analysis - NLP - WPS Office.pptx
Lexical Analysis
04LexicalAnalysissnsnjmsjsjmsbdjjdnd.pptx
Data design and analysis of computing tools
Lecturer-05 lex anylser (1).pptrjyghsgst
COMPILER DESIGN.pdf
Cd ch2 - lexical analysis
Role-of-lexical-analysis
LexicalAnalysis chapter2 i n compiler design.pptx
Ch03-LexicalAnalysis chapter2 in compiler design.pptx
1.Role lexical Analyzer
Lexical Analysis - Compiler Design
Compiler Design
Lexical Analysis
Lexical
Lexical analyzer
role of lexical parser compiler design1-181124035217.pdf
A Role of Lexical Analyzer
Chapter 2.pptx compiler design lecture note
Compiler Construction ( lexical analyzer).pptx
Ad

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
sap open course for s4hana steps from ECC to s4
PPT
Teaching material agriculture food technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Encapsulation theory and applications.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Electronic commerce courselecture one. Pdf
Chapter 3 Spatial Domain Image Processing.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The AUB Centre for AI in Media Proposal.docx
sap open course for s4hana steps from ECC to s4
Teaching material agriculture food technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Assigned Numbers - 2025 - Bluetooth® Document
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Dropbox Q2 2025 Financial Results & Investor Presentation
Review of recent advances in non-invasive hemoglobin estimation
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation_ Review paper, used for researhc scholars
Encapsulation theory and applications.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Electronic commerce courselecture one. Pdf
Ad

codin9cafe[2015.01.28]Open course(programming languages) - 장철호(Ch Jang)