SlideShare a Scribd company logo
Programming Languages
Building a Web Brower
Instructor : Westley Weimer
1
2
3
Syntactical Analysis
(identifier, number, …)
Syntactical
Analysis
Parse TreeList of Tokens
∙∙∙
the process of turning a sequence of tokens into a parse tree
4
Parse Tree
represent the syntactic structure of a string according to some grammar
"1+2+3"
exp → exp + exp
exp → exp – exp
exp → number
exp
exp exp+
exp exp+
num num
num
1 2 3
⇒
5
Why Parse Tree?
although this tree structure is cumbersome for us, it's very convenient for computers
"<b>wecome to web page</b>"
html → elt html
html → E
elt → word
elt → to word tc
to → <word>
tc → </word>
html
elt html
to tchtml
word
E
b
< > < /elt
word
welcome
html …
elt html
……
6
How Parse Tree?
Top-down VS Bottom-up
exp
exp exp+
exp exp+
num num
num
1 2 3
①
②
③
④
⑤
exp
exp exp+
exp exp+
num num
num
1 2 3
⑤
④
③
②
①
7
How Parse Tree?
S
ACTION GOTO
, a $ LIST ELE
0 s3 1 2
1 s4 acc
2 r2
3 r3 r3
4 s3 5
5 r1
STEP STACK
IN
PUT
ACTION TREE
1 0 a, a$ shift 3 Node(a)
2 0 a 3 , a$ reduce 3 Tree(3)
3 0 ELE , a$ GOTO 2
4 0 ELE 2 , a$ reduce 2 Tree(2)
5 0 LIST , a$ GOTO 1
6 0 LIST 1 , a$ shift 4 Node(,)
7 0 LIST 1 , 4 a$ shift 3 Node(a)
8 0 LIST 1 , 4 a 3 $ reduce 3 Tree(3)
9 0 LIST 1 , 4 ELE $ reduce 1 Tree(1)
10 0 LIST $ GOTO 1
11 0 LIST 1 $ accept Return
LIST
LIST
ELEMENT
a , a
ELEMENT
①
②
④
⑨
⑧
⑦⑥
G = ({LIST, ELEMENT}, {, , a}, P, LIST)
P : LIST → LIST , ELEMENT
P : LIST → ELEMENT
P : ELEMENT → a
8
exp
exp exp+
exp exp+
num num
num
1 2 3
+
+ 3
1 2
Abstact Syntax Tree
not representing every detail appearing in the real syntax
⇒
9
Python Lex-Yacc
A computer program that generates parser
ParserToken Parse Tree
YaccInput
Definition section
%%
Rules section
%%
C code section
10
tokens = (
‘LANGLE’, # <
‘LANGLESLASH’, # </
‘RANGLE’, # >
‘RANGLESLASH’, # />
‘EQUAL’, # =
‘STRING’, # “love”
‘WORD’, # like
)
1. Define Name of Token
11
G : exp → number
→ def p_exp_number(p):
’exp : NUMBER’
p[0] = (“number”, p[1])
G : exp → not exp
→ def p_exp_not(p):
’exp : NOT exp’
p[0] = (“not”, p[2])
2. Define Grammar
12
→ jslexer = lex.lex()
jsparser = yacc.yacc()
jsast = jsparser.parse(jscode, lexer=jslexer)
print jsast
3. Building and Using the Parser
13
Python Code
14
Input(Jscode) = myfun()
→ (‘call’, ‘myfun’, [])
Input(Jscode) = myfun(11,12,13)
→ (‘call’, ‘myfun’, [(‘number’, 11.0), (‘number’, 12.0)])
Output
15
- Changing the starting symbol
- Precedence
- Tracking Line Number
Notice
16
start = ‘arg’
→ def p_exp(p):
‘exp : NUMBER’
def p_arg(p):
’arg : exp’
Changing the starting symbol
the first rule defines the starting grammar rule
17
→ 1 - 2 - 3
Precedence
precedence = ((‘left’, ‘PLUS’, ‘MINUS’), //↑lower
(‘left’, ‘TIMES’, ‘DIVIDE’)) //↓higher
–
3–
21
–
–1
32?
“-4” “2”
18
→ def p_exp(p)
’exp : exp PLUS exp’
line = p.lineno(2) # line number of the PLUS token
index = p.lexpos(2) # Position of the PLUS token
Tracking Line Number
tracks the line number and position of all tokens
19

More Related Content

PPT
Unit vii wp ppt
PDF
Advanced perl finer points ,pack&amp;unpack,eval,files
PDF
Word games in c
PPTX
Powershell enum
PDF
Python and sysadmin I
PPT
Unix And C
PPTX
C Homework Help
PPT
01c shell
Unit vii wp ppt
Advanced perl finer points ,pack&amp;unpack,eval,files
Word games in c
Powershell enum
Python and sysadmin I
Unix And C
C Homework Help
01c shell

What's hot (20)

ODP
Programming Under Linux In Python
PDF
Bash Script Disk Space Utilization Report and EMail
PDF
File Space Usage Information and EMail Report - Shell Script
PDF
Basic NLP with Python and NLTK
ODP
Python quickstart for programmers: Python Kung Fu
PDF
Creating a Name seperator Custom Control using C#
PPTX
Introduction to Python and TensorFlow
PPTX
Unix Basic Commands
PPTX
Commit2015 kharchenko - python generators - ext
PDF
Shell Script to Extract IP Address, MAC Address Information
PPTX
File handling in c language
PPTX
Lecture 6: linked list
PPT
Introduction to Perl
PPTX
2015 555 kharchenko_ppt
PDF
Impala: A Modern, Open-Source SQL Engine for Hadoop
PPT
Unix command-line tools
PPT
Manipulating strings
PPTX
Python 101++: Let's Get Down to Business!
PPTX
Linked list
Programming Under Linux In Python
Bash Script Disk Space Utilization Report and EMail
File Space Usage Information and EMail Report - Shell Script
Basic NLP with Python and NLTK
Python quickstart for programmers: Python Kung Fu
Creating a Name seperator Custom Control using C#
Introduction to Python and TensorFlow
Unix Basic Commands
Commit2015 kharchenko - python generators - ext
Shell Script to Extract IP Address, MAC Address Information
File handling in c language
Lecture 6: linked list
Introduction to Perl
2015 555 kharchenko_ppt
Impala: A Modern, Open-Source SQL Engine for Hadoop
Unix command-line tools
Manipulating strings
Python 101++: Let's Get Down to Business!
Linked list
Ad

Viewers also liked (20)

PPT
Chapter Seven(1)
PPT
01. introduction
PPT
Optimisation
PPTX
Back patching
PPT
Chapter Seven(2)
PPTX
The dag representation of basic blocks
PPTX
Compiler Optimization Presentation
PPT
Lecture 16 17 code-generation
PPTX
Run time administration
PPT
Chapter 6 intermediate code generation
PPT
Query processing-and-optimization
PPTX
Basic Blocks and Flow Graphs
PPTX
Peephole optimization techniques in compiler design
PPTX
Three address code In Compiler Design
PDF
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
PPTX
Code generation
PPTX
Top down and botttom up Parsing
PPT
13. Query Processing in DBMS
Chapter Seven(1)
01. introduction
Optimisation
Back patching
Chapter Seven(2)
The dag representation of basic blocks
Compiler Optimization Presentation
Lecture 16 17 code-generation
Run time administration
Chapter 6 intermediate code generation
Query processing-and-optimization
Basic Blocks and Flow Graphs
Peephole optimization techniques in compiler design
Three address code In Compiler Design
Query Processing and Optimisation - Lecture 10 - Introduction to Databases (1...
Code generation
Top down and botttom up Parsing
13. Query Processing in DBMS
Ad

Similar to Open course(programming languages) 20150225 (20)

PDF
Ch04
ZIP
Round PEG, Round Hole - Parsing Functionally
PDF
Perl6 Regexen: Reduce the line noise in your code.
PDF
Syntaxdirected (1)
PDF
PDF
PPT
Antlr V3
PDF
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf
PPT
Ch2 (1).ppt
PDF
Python_Cheat_Sheet_Keywords_1664634397.pdf
PDF
Python_Cheat_Sheet_Keywords_1664634397.pdf
PDF
14-Strings-In-Python strings with oops .pdf
PPT
Compiler design lessons notes from Semester
PDF
Writing a compiler in go
PDF
仕事で使うF#
PPT
Introduction to Python
PPT
Scala presentation by Aleksandar Prokopec
PPT
An Annotation Framework for Statically-Typed Syntax Trees
PPTX
P3 2018 python_regexes
Ch04
Round PEG, Round Hole - Parsing Functionally
Perl6 Regexen: Reduce the line noise in your code.
Syntaxdirected (1)
Antlr V3
Python Cheatsheet_A Quick Reference Guide for Data Science.pdf
Ch2 (1).ppt
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
14-Strings-In-Python strings with oops .pdf
Compiler design lessons notes from Semester
Writing a compiler in go
仕事で使うF#
Introduction to Python
Scala presentation by Aleksandar Prokopec
An Annotation Framework for Statically-Typed Syntax Trees
P3 2018 python_regexes

More from JangChulho (7)

PPTX
Jpo(javascript perpormance optimization) 20150707
PPTX
Open course(programming languages) 20150318
PPTX
Open course(programming languages) 20150304
PPTX
Open course(programming languages) 20150211
PPTX
Open course(programming languages) 20150128
PPTX
Open course(programming languages) 20150121
PPTX
Open course(programming languages) 20150114
Jpo(javascript perpormance optimization) 20150707
Open course(programming languages) 20150318
Open course(programming languages) 20150304
Open course(programming languages) 20150211
Open course(programming languages) 20150128
Open course(programming languages) 20150121
Open course(programming languages) 20150114

Recently uploaded (20)

PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Transform Your Business with a Software ERP System
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
medical staffing services at VALiNTRY
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
System and Network Administration Chapter 2
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Digital Strategies for Manufacturing Companies
Operating system designcfffgfgggggggvggggggggg
Wondershare Filmora 15 Crack With Activation Key [2025
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Transform Your Business with a Software ERP System
How to Migrate SBCGlobal Email to Yahoo Easily
CHAPTER 2 - PM Management and IT Context
Odoo POS Development Services by CandidRoot Solutions
Understanding Forklifts - TECH EHS Solution
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
medical staffing services at VALiNTRY
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
System and Network Administration Chapter 2
wealthsignaloriginal-com-DS-text-... (1).pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Design an Analysis of Algorithms I-SECS-1021-03
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Softaken Excel to vCard Converter Software.pdf
Digital Strategies for Manufacturing Companies

Open course(programming languages) 20150225

  • 1. Programming Languages Building a Web Brower Instructor : Westley Weimer 1
  • 2. 2
  • 3. 3 Syntactical Analysis (identifier, number, …) Syntactical Analysis Parse TreeList of Tokens ∙∙∙ the process of turning a sequence of tokens into a parse tree
  • 4. 4 Parse Tree represent the syntactic structure of a string according to some grammar "1+2+3" exp → exp + exp exp → exp – exp exp → number exp exp exp+ exp exp+ num num num 1 2 3 ⇒
  • 5. 5 Why Parse Tree? although this tree structure is cumbersome for us, it's very convenient for computers "<b>wecome to web page</b>" html → elt html html → E elt → word elt → to word tc to → <word> tc → </word> html elt html to tchtml word E b < > < /elt word welcome html … elt html ……
  • 6. 6 How Parse Tree? Top-down VS Bottom-up exp exp exp+ exp exp+ num num num 1 2 3 ① ② ③ ④ ⑤ exp exp exp+ exp exp+ num num num 1 2 3 ⑤ ④ ③ ② ①
  • 7. 7 How Parse Tree? S ACTION GOTO , a $ LIST ELE 0 s3 1 2 1 s4 acc 2 r2 3 r3 r3 4 s3 5 5 r1 STEP STACK IN PUT ACTION TREE 1 0 a, a$ shift 3 Node(a) 2 0 a 3 , a$ reduce 3 Tree(3) 3 0 ELE , a$ GOTO 2 4 0 ELE 2 , a$ reduce 2 Tree(2) 5 0 LIST , a$ GOTO 1 6 0 LIST 1 , a$ shift 4 Node(,) 7 0 LIST 1 , 4 a$ shift 3 Node(a) 8 0 LIST 1 , 4 a 3 $ reduce 3 Tree(3) 9 0 LIST 1 , 4 ELE $ reduce 1 Tree(1) 10 0 LIST $ GOTO 1 11 0 LIST 1 $ accept Return LIST LIST ELEMENT a , a ELEMENT ① ② ④ ⑨ ⑧ ⑦⑥ G = ({LIST, ELEMENT}, {, , a}, P, LIST) P : LIST → LIST , ELEMENT P : LIST → ELEMENT P : ELEMENT → a
  • 8. 8 exp exp exp+ exp exp+ num num num 1 2 3 + + 3 1 2 Abstact Syntax Tree not representing every detail appearing in the real syntax ⇒
  • 9. 9 Python Lex-Yacc A computer program that generates parser ParserToken Parse Tree YaccInput Definition section %% Rules section %% C code section
  • 10. 10 tokens = ( ‘LANGLE’, # < ‘LANGLESLASH’, # </ ‘RANGLE’, # > ‘RANGLESLASH’, # /> ‘EQUAL’, # = ‘STRING’, # “love” ‘WORD’, # like ) 1. Define Name of Token
  • 11. 11 G : exp → number → def p_exp_number(p): ’exp : NUMBER’ p[0] = (“number”, p[1]) G : exp → not exp → def p_exp_not(p): ’exp : NOT exp’ p[0] = (“not”, p[2]) 2. Define Grammar
  • 12. 12 → jslexer = lex.lex() jsparser = yacc.yacc() jsast = jsparser.parse(jscode, lexer=jslexer) print jsast 3. Building and Using the Parser
  • 14. 14 Input(Jscode) = myfun() → (‘call’, ‘myfun’, []) Input(Jscode) = myfun(11,12,13) → (‘call’, ‘myfun’, [(‘number’, 11.0), (‘number’, 12.0)]) Output
  • 15. 15 - Changing the starting symbol - Precedence - Tracking Line Number Notice
  • 16. 16 start = ‘arg’ → def p_exp(p): ‘exp : NUMBER’ def p_arg(p): ’arg : exp’ Changing the starting symbol the first rule defines the starting grammar rule
  • 17. 17 → 1 - 2 - 3 Precedence precedence = ((‘left’, ‘PLUS’, ‘MINUS’), //↑lower (‘left’, ‘TIMES’, ‘DIVIDE’)) //↓higher – 3– 21 – –1 32? “-4” “2”
  • 18. 18 → def p_exp(p) ’exp : exp PLUS exp’ line = p.lineno(2) # line number of the PLUS token index = p.lexpos(2) # Position of the PLUS token Tracking Line Number tracks the line number and position of all tokens
  • 19. 19