SlideShare a Scribd company logo
1 of 19
CSE340 - Principles of
Programming Languages
Lecture 07:
Syntactic Analysis 1
Javier Gonzalez-Sanchez
javiergs@asu.edu
BYENG M1-38
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2
Next Step
þ Lexical Analysis ☐	
 Syntactic Analysis
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 3
Question
For each cases indicate whether it is possible or not to
generate a regular expression or a DFA.
i.  Detect the balance of N parenthesis in a string
that has N parenthesis nested and any characters
in between the parenthesis.
ii.  Is it possible to detect binary strings with the same
quantity of 0’s and 1’s (it does not matter the order
or sequence).
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4
Where are we now?
After lexical analysis, we have a series of tokens.
But we can not:
I.  define a regular expression matching all
expressions with properly balanced parentheses.
II.  i.e., define a regular expression matching all
functions with properly nested block structure.
void a () { b (c); for (;;) {a=(-(1+2)+5); } }
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5
Where are we now?
Now, we want to:
I.  Review the structure described by that series of
tokens
II.  Report errors if those tokens do not properly
encode a structure
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6
High-Level Languages
X,E,G,O,O
#e1,I,I,0,7
@
OPR 19, AX
STO x, AX
LIT 5, AX
OPR 21, AX
LOD #e1,AX
CAL 1, AX
OPR 0, AX
5
Virtual Machine
(interpreter)
// sorce code
int x;
int foo () {
read (x);
print (5);
}
main () {
foo ();
}
Lexer
Parser
Semantic Analyzer
Code Generation
01001010101000010
01010100101010010
10100100000011011
11010010110101111
00010010101010010
10101001010101011
Assembler
compilation execution
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7
Outline
Language
Lexical Analysis
(Lexer)
Rules
Symbols
Token
Tools
Regular Expression
DFA
Syntactic
Analysis
(Parser)
Grammar
(Rules)
Terminal
Non-terminal
Tools
BNF
(Backus-Naur Form)
Syntax Diagrams
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8
Grammar | Example
Describe all legal arithmetic expressions using
addition, subtraction, multiplication, and division with
integer values
E à E OP E
E à integer
OP à + | - | * | /
E à ( E )
Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 9
A Grammar is a collection of
four elements:
§  Set of nonterminal symbols
(uppercase)
§  Set of terminal symbols
(lowercase). Terminals can be
tokens or specific words
§  Set of production rules saying
how each nonterminal can
be converted by a string of
terminals and nonterminals,
§  A start symbol
E à E OP E
E à integer
OP à + | - | * | /
E à ( E )
Grammar | Definition
Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 10
5 / 20
integer operator integer
E à E OP E
E à integer
OP à + | - | * | /
E à ( E )
E
⇒  E OP E
⇒  integer OP E
⇒  integer / E
⇒  integer / integer
Grammar | Derivation
Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 11
5 * ( 7 + 20 )
Integer operator delimiter integer operator integer delimiter
E à E OP E
E à integer
OP à + | - | * | /
E à ( E )
E
⇒  E OP E
⇒  integer OP E
⇒  integer * E
⇒  integer * (E)
⇒ integer * (E OP E)
⇒  integer * (integer OP E)
⇒  integer * (integer + E)
⇒  integer * (integer + integer)
Grammar | Derivation
Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 12
5 * ( 7 + 20 )
Integer operator delimiter integer operator integer delimiter
E à E OP E
E à integer
OP à + | - | * | /
E à ( E )
E
⇒  E OP E
⇒  E OP (E)
⇒  E OP (E OP E)
⇒  E OP (E OP integer)
⇒  E OP (E + integer)
⇒  E OP (integer + integer)
⇒  E * (integer + integer)
⇒  integer * (integer + integer)
Grammar | Derivation
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13
Derivations
§  A leftmost derivation is a derivation in which
each step expands the leftmost
nonterminal.
§  A rightmost derivation is a derivation in
which each step expands the rightmost
nonterminal.
§  Derivation will be very important when we
talk about parsing.
Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 14
Notation 1:
Comp → Mix | Mix Num | Comp Comp
Mix → Elem | ( Comp )
Elem → H|O|C|S|Na|Cl| ...
Num → 1|2|3|4| ...
Notation 2:
<Comp> → <Mix>|<Mix><Num> | <Comp><Comp>
<Mix> → <Elem> | ( <Comp> )
<Elem> → H|O|C|S|Na|Cl| ...
<Num> → 1|2|3|4| ...
H2 O
C O2 (S O4)3
Na Cl
S O3
Grammar | Example
Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 15
C O 2
Comp → Term | Term Num | Comp Comp
Term → Elem | ( Comp )
Elem → H|O|C|S|Na|Cl| ...
Num → 1|2|3|4| …
Comp
⇒  Comp Comp
⇒  Term Comp
⇒  Elem Comp
⇒  C Comp
⇒ C Term Num
⇒  C Elem Num
⇒  CO Num
⇒  CO2
Grammar | Derivation
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16
What about this?
BLOCK → STMT | { STMTS } | { }
STMTS → STMT | STMT STMTS
STMT → EXPR; |
if (EXPR) BLOCK |
while (EXPR) BLOCK |
BLOCK |
. . .
EXPR → EXPR + EXPR |
EXPR – EXPR |
EXPR * EXPR |
identifier |
integer |
...
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17
Homework
Using the rules in the previous slide, apply derivation to show that the following
expression is syntactically correct
while ( 5 ) { if ( 6 ) { } }
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18
Homework
Review Recursion
Solve the Problem Set #1 in preparation for your exam
CSE340 - Principles of Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2015
Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.

More Related Content

What's hot

Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
Ahmed Khateeb
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
emailharmeet
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
Senthil Kumar
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
Haqnawaz Ch
 

What's hot (20)

201506 CSE340 Lecture 19
201506 CSE340 Lecture 19201506 CSE340 Lecture 19
201506 CSE340 Lecture 19
 
201506 CSE340 Lecture 13
201506 CSE340 Lecture 13201506 CSE340 Lecture 13
201506 CSE340 Lecture 13
 
201505 CSE340 Lecture 06
201505 CSE340 Lecture 06201505 CSE340 Lecture 06
201505 CSE340 Lecture 06
 
201506 CSE340 Lecture 09
201506 CSE340 Lecture 09201506 CSE340 Lecture 09
201506 CSE340 Lecture 09
 
Prefix Postfix
Prefix PostfixPrefix Postfix
Prefix Postfix
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
201506 CSE340 Lecture 11
201506 CSE340 Lecture 11201506 CSE340 Lecture 11
201506 CSE340 Lecture 11
 
Lab 1
Lab 1Lab 1
Lab 1
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
 
Circular queues
Circular queuesCircular queues
Circular queues
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
My lecture infix-to-postfix
My lecture infix-to-postfixMy lecture infix-to-postfix
My lecture infix-to-postfix
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
Conversion from infix to prefix using stack
Conversion from infix to prefix using stackConversion from infix to prefix using stack
Conversion from infix to prefix using stack
 
Arithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded CArithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded C
 
Arithmetic and logic operations in c
Arithmetic and logic operations in cArithmetic and logic operations in c
Arithmetic and logic operations in c
 
Booth algorithm
Booth algorithmBooth algorithm
Booth algorithm
 

Viewers also liked

Developing distributed analysis pipelines with shared community resources usi...
Developing distributed analysis pipelines with shared community resources usi...Developing distributed analysis pipelines with shared community resources usi...
Developing distributed analysis pipelines with shared community resources usi...
Brad Chapman
 
201103 emotional impacts on digital media
201103 emotional impacts on digital media201103 emotional impacts on digital media
201103 emotional impacts on digital media
Javier Gonzalez-Sanchez
 
Laserendoveineux b anastasie 1 er partie
Laserendoveineux  b anastasie   1 er partieLaserendoveineux  b anastasie   1 er partie
Laserendoveineux b anastasie 1 er partie
sfa_angeiologie
 
RCMSL Phenomenal Aug 13 And 20, 2009
RCMSL Phenomenal Aug 13 And 20, 2009RCMSL Phenomenal Aug 13 And 20, 2009
RCMSL Phenomenal Aug 13 And 20, 2009
etalcomendras
 
RCMSL Phenomenal July 16, 2009
RCMSL Phenomenal July 16, 2009RCMSL Phenomenal July 16, 2009
RCMSL Phenomenal July 16, 2009
etalcomendras
 
CQRS introduction
CQRS introductionCQRS introduction
CQRS introduction
Yura Taras
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
dphil002
 

Viewers also liked (20)

Developing distributed analysis pipelines with shared community resources usi...
Developing distributed analysis pipelines with shared community resources usi...Developing distributed analysis pipelines with shared community resources usi...
Developing distributed analysis pipelines with shared community resources usi...
 
악플과 악플의 재생산
악플과 악플의 재생산악플과 악플의 재생산
악플과 악플의 재생산
 
201505 CSE340 Lecture 03
201505 CSE340 Lecture 03201505 CSE340 Lecture 03
201505 CSE340 Lecture 03
 
2009 04 москва, совазс
2009 04 москва, совазс2009 04 москва, совазс
2009 04 москва, совазс
 
Eprotect
EprotectEprotect
Eprotect
 
Eurowerks 2009 Slideshow
Eurowerks 2009 SlideshowEurowerks 2009 Slideshow
Eurowerks 2009 Slideshow
 
Thehub bocconi law
Thehub   bocconi lawThehub   bocconi law
Thehub bocconi law
 
201103 emotional impacts on digital media
201103 emotional impacts on digital media201103 emotional impacts on digital media
201103 emotional impacts on digital media
 
Laserendoveineux b anastasie 1 er partie
Laserendoveineux  b anastasie   1 er partieLaserendoveineux  b anastasie   1 er partie
Laserendoveineux b anastasie 1 er partie
 
open office
open officeopen office
open office
 
Thirst Upload 800x600 1215534320518707 8
Thirst Upload 800x600 1215534320518707 8Thirst Upload 800x600 1215534320518707 8
Thirst Upload 800x600 1215534320518707 8
 
RCMSL Phenomenal Aug 13 And 20, 2009
RCMSL Phenomenal Aug 13 And 20, 2009RCMSL Phenomenal Aug 13 And 20, 2009
RCMSL Phenomenal Aug 13 And 20, 2009
 
RCMSL Phenomenal July 16, 2009
RCMSL Phenomenal July 16, 2009RCMSL Phenomenal July 16, 2009
RCMSL Phenomenal July 16, 2009
 
LiveOffice Email Archiving & Compliance 201
LiveOffice Email Archiving & Compliance 201LiveOffice Email Archiving & Compliance 201
LiveOffice Email Archiving & Compliance 201
 
200905 - Sociable machines
200905 - Sociable machines200905 - Sociable machines
200905 - Sociable machines
 
CQRS introduction
CQRS introductionCQRS introduction
CQRS introduction
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
201001 amt
201001 amt201001 amt
201001 amt
 
Syndrome metabolique et maladies vasculaires
Syndrome metabolique et maladies vasculairesSyndrome metabolique et maladies vasculaires
Syndrome metabolique et maladies vasculaires
 
Cluster 13
Cluster 13Cluster 13
Cluster 13
 

Similar to 201506 CSE340 Lecture 07

Winter 10 Undecidability.pptx
Winter 10 Undecidability.pptxWinter 10 Undecidability.pptx
Winter 10 Undecidability.pptx
HarisPrince
 
ContextFreeGrammars.pptx
ContextFreeGrammars.pptxContextFreeGrammars.pptx
ContextFreeGrammars.pptx
PEzhumalai
 

Similar to 201506 CSE340 Lecture 07 (20)

201506 - CSE340 Lecture 08
201506 - CSE340 Lecture 08201506 - CSE340 Lecture 08
201506 - CSE340 Lecture 08
 
201505 CSE340 Lecture 02
201505 CSE340 Lecture 02201505 CSE340 Lecture 02
201505 CSE340 Lecture 02
 
201505 - CSE340 Lecture 01
201505 - CSE340 Lecture 01201505 - CSE340 Lecture 01
201505 - CSE340 Lecture 01
 
201505 CSE340 Lecture 04
201505 CSE340 Lecture 04201505 CSE340 Lecture 04
201505 CSE340 Lecture 04
 
201707 CSE110 Lecture 04
201707 CSE110 Lecture 04   201707 CSE110 Lecture 04
201707 CSE110 Lecture 04
 
201506 CSE340 Lecture 12
201506 CSE340 Lecture 12201506 CSE340 Lecture 12
201506 CSE340 Lecture 12
 
201506 CSE340 Lecture 10
201506 CSE340 Lecture 10201506 CSE340 Lecture 10
201506 CSE340 Lecture 10
 
Lecture5 syntax analysis_1
Lecture5 syntax analysis_1Lecture5 syntax analysis_1
Lecture5 syntax analysis_1
 
201707 CSE110 Lecture 08
201707 CSE110 Lecture 08  201707 CSE110 Lecture 08
201707 CSE110 Lecture 08
 
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
 
Winter 10 Undecidability.pptx
Winter 10 Undecidability.pptxWinter 10 Undecidability.pptx
Winter 10 Undecidability.pptx
 
ContextFreeGrammars.pptx
ContextFreeGrammars.pptxContextFreeGrammars.pptx
ContextFreeGrammars.pptx
 
ContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptx
 
Compiler worksheet
Compiler worksheetCompiler worksheet
Compiler worksheet
 
Functional Programming in Pattern-Match-Oriented Programming Style <Programmi...
Functional Programming in Pattern-Match-Oriented Programming Style <Programmi...Functional Programming in Pattern-Match-Oriented Programming Style <Programmi...
Functional Programming in Pattern-Match-Oriented Programming Style <Programmi...
 
Dynamic Semantics
Dynamic SemanticsDynamic Semantics
Dynamic Semantics
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201506 CSE340 Lecture 17
201506 CSE340 Lecture 17201506 CSE340 Lecture 17
201506 CSE340 Lecture 17
 
Module For Mathematics
Module For Mathematics Module For Mathematics
Module For Mathematics
 

More from Javier Gonzalez-Sanchez

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 
201801 CSE240 Lecture 10
201801 CSE240 Lecture 10201801 CSE240 Lecture 10
201801 CSE240 Lecture 10
 
201801 CSE240 Lecture 09
201801 CSE240 Lecture 09201801 CSE240 Lecture 09
201801 CSE240 Lecture 09
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 

201506 CSE340 Lecture 07

  • 1. CSE340 - Principles of Programming Languages Lecture 07: Syntactic Analysis 1 Javier Gonzalez-Sanchez javiergs@asu.edu BYENG M1-38 Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2 Next Step þ Lexical Analysis ☐ Syntactic Analysis
  • 3. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 3 Question For each cases indicate whether it is possible or not to generate a regular expression or a DFA. i.  Detect the balance of N parenthesis in a string that has N parenthesis nested and any characters in between the parenthesis. ii.  Is it possible to detect binary strings with the same quantity of 0’s and 1’s (it does not matter the order or sequence).
  • 4. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4 Where are we now? After lexical analysis, we have a series of tokens. But we can not: I.  define a regular expression matching all expressions with properly balanced parentheses. II.  i.e., define a regular expression matching all functions with properly nested block structure. void a () { b (c); for (;;) {a=(-(1+2)+5); } }
  • 5. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5 Where are we now? Now, we want to: I.  Review the structure described by that series of tokens II.  Report errors if those tokens do not properly encode a structure
  • 6. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6 High-Level Languages X,E,G,O,O #e1,I,I,0,7 @ OPR 19, AX STO x, AX LIT 5, AX OPR 21, AX LOD #e1,AX CAL 1, AX OPR 0, AX 5 Virtual Machine (interpreter) // sorce code int x; int foo () { read (x); print (5); } main () { foo (); } Lexer Parser Semantic Analyzer Code Generation 01001010101000010 01010100101010010 10100100000011011 11010010110101111 00010010101010010 10101001010101011 Assembler compilation execution
  • 7. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7 Outline Language Lexical Analysis (Lexer) Rules Symbols Token Tools Regular Expression DFA Syntactic Analysis (Parser) Grammar (Rules) Terminal Non-terminal Tools BNF (Backus-Naur Form) Syntax Diagrams
  • 8. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8 Grammar | Example Describe all legal arithmetic expressions using addition, subtraction, multiplication, and division with integer values E à E OP E E à integer OP à + | - | * | / E à ( E )
  • 9. Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 9 A Grammar is a collection of four elements: §  Set of nonterminal symbols (uppercase) §  Set of terminal symbols (lowercase). Terminals can be tokens or specific words §  Set of production rules saying how each nonterminal can be converted by a string of terminals and nonterminals, §  A start symbol E à E OP E E à integer OP à + | - | * | / E à ( E ) Grammar | Definition
  • 10. Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 10 5 / 20 integer operator integer E à E OP E E à integer OP à + | - | * | / E à ( E ) E ⇒  E OP E ⇒  integer OP E ⇒  integer / E ⇒  integer / integer Grammar | Derivation
  • 11. Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 11 5 * ( 7 + 20 ) Integer operator delimiter integer operator integer delimiter E à E OP E E à integer OP à + | - | * | / E à ( E ) E ⇒  E OP E ⇒  integer OP E ⇒  integer * E ⇒  integer * (E) ⇒ integer * (E OP E) ⇒  integer * (integer OP E) ⇒  integer * (integer + E) ⇒  integer * (integer + integer) Grammar | Derivation
  • 12. Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 12 5 * ( 7 + 20 ) Integer operator delimiter integer operator integer delimiter E à E OP E E à integer OP à + | - | * | / E à ( E ) E ⇒  E OP E ⇒  E OP (E) ⇒  E OP (E OP E) ⇒  E OP (E OP integer) ⇒  E OP (E + integer) ⇒  E OP (integer + integer) ⇒  E * (integer + integer) ⇒  integer * (integer + integer) Grammar | Derivation
  • 13. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13 Derivations §  A leftmost derivation is a derivation in which each step expands the leftmost nonterminal. §  A rightmost derivation is a derivation in which each step expands the rightmost nonterminal. §  Derivation will be very important when we talk about parsing.
  • 14. Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 14 Notation 1: Comp → Mix | Mix Num | Comp Comp Mix → Elem | ( Comp ) Elem → H|O|C|S|Na|Cl| ... Num → 1|2|3|4| ... Notation 2: <Comp> → <Mix>|<Mix><Num> | <Comp><Comp> <Mix> → <Elem> | ( <Comp> ) <Elem> → H|O|C|S|Na|Cl| ... <Num> → 1|2|3|4| ... H2 O C O2 (S O4)3 Na Cl S O3 Grammar | Example
  • 15. Javier Gonzalez-Sanchez | CSE340 | Summer 2013 | 15 C O 2 Comp → Term | Term Num | Comp Comp Term → Elem | ( Comp ) Elem → H|O|C|S|Na|Cl| ... Num → 1|2|3|4| … Comp ⇒  Comp Comp ⇒  Term Comp ⇒  Elem Comp ⇒  C Comp ⇒ C Term Num ⇒  C Elem Num ⇒  CO Num ⇒  CO2 Grammar | Derivation
  • 16. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16 What about this? BLOCK → STMT | { STMTS } | { } STMTS → STMT | STMT STMTS STMT → EXPR; | if (EXPR) BLOCK | while (EXPR) BLOCK | BLOCK | . . . EXPR → EXPR + EXPR | EXPR – EXPR | EXPR * EXPR | identifier | integer | ...
  • 17. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17 Homework Using the rules in the previous slide, apply derivation to show that the following expression is syntactically correct while ( 5 ) { if ( 6 ) { } }
  • 18. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18 Homework Review Recursion Solve the Problem Set #1 in preparation for your exam
  • 19. CSE340 - Principles of Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2015 Disclaimer. These slides can only be used as study material for the class CSE340 at ASU. They cannot be distributed or used for another purpose.