SlideShare a Scribd company logo
CSE340 - Principles of
Programming Languages
Lecture 11:
Parser Implementation I
Javier Gonzalez-Sanchez
javiergs@asu.edu
BYENG M1-38
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 2
Parser
BNF
EBNF
A	
AA	
B
Parser
Grammar
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 3
Parser | Grammar 1
<EXPRESSION> à <X> {'|' <X>}
<X> à <Y> {'&' <Y>}
<Y> à ['!'] <R>
<R> à <E> {('>'|'<'|'=='|'!=') <E>}
<E> à <A> {(’+'|'-’) <A>}
<A> à <B> {('*'|'/') <B>}
<B> à ['-'] <C>
<C> à integer|
identifier|'(' <EXPRESSION> ')'
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 4
Parser | Grammar 2
<PROGRAM> à '{' <BODY> '}'
<BODY> à {<EXPRESSION>';'}
<EXPRESSION> à <X> {'|' <X>}
<X> à <Y> {'&' <Y>}
<Y> à ['!'] <R>
<R> à <E> {('>'|'<'|'=='|'!=') <E>}
<E> à <A> {(’+'|'-’) <A>}
<A> à <B> {('*'|'/') <B>}
<B> à ['-'] <C>
<C> à integer|
identifier|'(' <EXPRESSION> ')'
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 5
Parser | Input and Output
{
0;
1 + 2;
3 * (4 + hello);
}
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 6
Parser | Step by Step
For each rule in the grammar {
§  Step 1. left-hand side (new method)
§  Step 2. right-hand side (loops, ifs, call methods)
§  Step 3. identify errors (terminals)
§  Step 4. synchronize errors (first and follow sets)
}
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 7
Parser
public class Parser {
private static Vector<Token> tokens;
private static int currentToken;
public static void RULE_PROGRAM () {}
public static void RULE_BODY () {}
public static void RULE_EXPRESSION () {}
public static void RULE_X () {}
public static void RULE_Y () {}
public static void RULE_R () {}
public static void RULE_E () {}
public static void RULE_A () {}
public static void RULE_B () {}
public static void RULE_C () {}
}
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 8
Parser
public static void RULE_PROGRAM() {
if (tokens.get(currentToken).getWord().equals(“{”)) {
currentToken++;
else
error(1);
RULE_BODY();
if (tokens.get(currentToken).getWord().equals(“}”))
currentToken++;
else
error(2);
}
PROGRAM
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 9
Parser
public static void RULE_BODY() {
while (!tokens.get(currentToken).getWord().equals(“}”)) {
RULE_EXPRESSION();
if (tokens.get(currentToken).getWord().equals(“;”))
currentToken++;
else
error(3);
}
}
BODY
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 10
Parser
public static void RULE_EXPRESSION() {
RULE_X();
while (tokens.get(currentToken).getWord().equals(“|”)) {
currentToken++;
RULE_X();
}
}
EXPRESSION
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 11
Parser
public static void RULE_X() {
RULE_Y();
while (tokens.get(currentToken).getWord().equals(“&”)) {
currentToken++;
RULE_Y();
}
}
X
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 12
Parser
public static void RULE_Y() {
if (tokens.get(currentToken).getWord().equals(“!”)) {
currentToken++;
}
RULE_R();
}
Y
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 13
Parser
public static void RULE_R() {
RULE_E();
while ( tokens.get(currentToken).getWord().equals(“<”)
|tokens.get(currentToken).getWord().equals(“>”)
|tokens.get(currentToken).getWord().equals(“==”)
|tokens.get(currentToken).getWord().equals(“!=”)
) {
currentToken++;
RULE_E();
}
}
R
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 14
Parser
public static void RULE_E() {
RULE_A();
while (tokens.get(currentToken).getWord().equals(“-”)
| tokens.get(currentToken).getWord().equals(“+”)
) {
currentToken++;
RULE_A();
}
}
E
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 15
Parser
public static void RULE_A() {
RULE_B();
while (tokens.get(currentToken).getWord().equals(“/”)
| tokens.get(currentToken).getWord().equals(“*”)
) {
currentToken++;
RULE_B();
}
}
A
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 16
Parser
public static void RULE_B() {
if (tokens.get(currentToken).getWord().equals(“-”)) {
currentToken++;
}
RULE_C();
}
B
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 17
Parser
public static void RULE_C() {
if (tokens.get(currentToken).getToken().equals(“integer”)) {
currentToken++;
} else if (tokens.get(currentToken).getToken().equals(“identifier”)) {
currentToken++;
} else if (tokens.get(currentToken).getWord().equals(“(”)) {
currentToken++;
RULE_EXPRESSION();
if (tokens.get(currentToken).getWord().equals(“)”)) {
currentToken++;
} else error(4);
}
} else { error (5); }
}
C
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 18
Homework
Programming Assignment 2
Level 1
Review and Understand the Source Code
posted in Blackboard. Specially, particularly the use of
DefaultMutableTreeNode)
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 19
Homework
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 20
Homework
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 21
Homework
Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 22
Homework
Programming Assignment 2
Level 2
Modify the Source Code
to include the rules PROGRAM and BODY, EXPRESSION, X, Y, R
(from Grammar 2)
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.
Ad

More Related Content

Viewers also liked (20)

Demonstration Presentation
Demonstration PresentationDemonstration Presentation
Demonstration Presentation
Emily Reyes
 
201506 CSE340 Lecture 12
201506 CSE340 Lecture 12201506 CSE340 Lecture 12
201506 CSE340 Lecture 12
Javier Gonzalez-Sanchez
 
Thirst Upload 800x600 1215534320518707 8
Thirst Upload 800x600 1215534320518707 8Thirst Upload 800x600 1215534320518707 8
Thirst Upload 800x600 1215534320518707 8
andrearsya
 
Team Visit
Team VisitTeam Visit
Team Visit
etalcomendras
 
OORPT Dynamic Analysis
OORPT Dynamic AnalysisOORPT Dynamic Analysis
OORPT Dynamic Analysis
lienhard
 
Secret Vineyard Animation Bible
Secret Vineyard Animation BibleSecret Vineyard Animation Bible
Secret Vineyard Animation Bible
PMCHUGH
 
Livre resumes 2007 angeio
Livre resumes 2007 angeioLivre resumes 2007 angeio
Livre resumes 2007 angeio
sfa_angeiologie
 
Barya Perception
Barya PerceptionBarya Perception
Barya Perception
etalcomendras
 
악플과 악플의 재생산
악플과 악플의 재생산악플과 악플의 재생산
악플과 악플의 재생산
JaeGeun Kim
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
dphil002
 
2013 cch basic principles ch04
2013 cch basic principles ch042013 cch basic principles ch04
2013 cch basic principles ch04
dphil002
 
Login Seminars Blackboard Directions 2009
Login Seminars Blackboard Directions 2009Login Seminars Blackboard Directions 2009
Login Seminars Blackboard Directions 2009
Ohio LETC
 
Cluster 15
Cluster 15Cluster 15
Cluster 15
etalcomendras
 
open office
open officeopen office
open office
guest2956cc80
 
Tax planning introduction fall 2012
Tax planning introduction fall 2012Tax planning introduction fall 2012
Tax planning introduction fall 2012
dphil002
 
201506 CSE340 Lecture 09
201506 CSE340 Lecture 09201506 CSE340 Lecture 09
201506 CSE340 Lecture 09
Javier Gonzalez-Sanchez
 
201506 CSE340 Lecture 16
201506 CSE340 Lecture 16201506 CSE340 Lecture 16
201506 CSE340 Lecture 16
Javier Gonzalez-Sanchez
 
201004 - brain computer interaction
201004 - brain computer interaction201004 - brain computer interaction
201004 - brain computer interaction
Javier Gonzalez-Sanchez
 
Demonstration Presentation
Demonstration PresentationDemonstration Presentation
Demonstration Presentation
Emily Reyes
 
Thirst Upload 800x600 1215534320518707 8
Thirst Upload 800x600 1215534320518707 8Thirst Upload 800x600 1215534320518707 8
Thirst Upload 800x600 1215534320518707 8
andrearsya
 
OORPT Dynamic Analysis
OORPT Dynamic AnalysisOORPT Dynamic Analysis
OORPT Dynamic Analysis
lienhard
 
Secret Vineyard Animation Bible
Secret Vineyard Animation BibleSecret Vineyard Animation Bible
Secret Vineyard Animation Bible
PMCHUGH
 
Livre resumes 2007 angeio
Livre resumes 2007 angeioLivre resumes 2007 angeio
Livre resumes 2007 angeio
sfa_angeiologie
 
악플과 악플의 재생산
악플과 악플의 재생산악플과 악플의 재생산
악플과 악플의 재생산
JaeGeun Kim
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
dphil002
 
2013 cch basic principles ch04
2013 cch basic principles ch042013 cch basic principles ch04
2013 cch basic principles ch04
dphil002
 
Login Seminars Blackboard Directions 2009
Login Seminars Blackboard Directions 2009Login Seminars Blackboard Directions 2009
Login Seminars Blackboard Directions 2009
Ohio LETC
 
Tax planning introduction fall 2012
Tax planning introduction fall 2012Tax planning introduction fall 2012
Tax planning introduction fall 2012
dphil002
 

Similar to 201506 CSE340 Lecture 11 (20)

201506 CSE340 Lecture 13
201506 CSE340 Lecture 13201506 CSE340 Lecture 13
201506 CSE340 Lecture 13
Javier Gonzalez-Sanchez
 
201506 CSE340 Lecture 18
201506 CSE340 Lecture 18201506 CSE340 Lecture 18
201506 CSE340 Lecture 18
Javier Gonzalez-Sanchez
 
201506 CSE340 Lecture 14
201506 CSE340 Lecture 14201506 CSE340 Lecture 14
201506 CSE340 Lecture 14
Javier Gonzalez-Sanchez
 
201506 - CSE340 Lecture 08
201506 - CSE340 Lecture 08201506 - CSE340 Lecture 08
201506 - CSE340 Lecture 08
Javier Gonzalez-Sanchez
 
201506 CSE340 Lecture 17
201506 CSE340 Lecture 17201506 CSE340 Lecture 17
201506 CSE340 Lecture 17
Javier Gonzalez-Sanchez
 
201707 CSE110 Lecture 08
201707 CSE110 Lecture 08  201707 CSE110 Lecture 08
201707 CSE110 Lecture 08
Javier Gonzalez-Sanchez
 
201505 CSE340 Lecture 02
201505 CSE340 Lecture 02201505 CSE340 Lecture 02
201505 CSE340 Lecture 02
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
Javier Gonzalez-Sanchez
 
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Transferring Software Testing Tools to Practice (AST 2017 Keynote)Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Tao Xie
 
201707 CSE110 Lecture 05
201707 CSE110 Lecture 05   201707 CSE110 Lecture 05
201707 CSE110 Lecture 05
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
Javier Gonzalez-Sanchez
 
201505 - CSE340 Lecture 01
201505 - CSE340 Lecture 01201505 - CSE340 Lecture 01
201505 - CSE340 Lecture 01
Javier Gonzalez-Sanchez
 
201505 CSE340 Lecture 03
201505 CSE340 Lecture 03201505 CSE340 Lecture 03
201505 CSE340 Lecture 03
Javier Gonzalez-Sanchez
 
Mutation @ Spotify
Mutation @ Spotify Mutation @ Spotify
Mutation @ Spotify
STAMP Project
 
Web lab programs
Web lab programsWeb lab programs
Web lab programs
SRINIVASUNIVERSITYEN
 
201707 CSE110 Lecture 04
201707 CSE110 Lecture 04   201707 CSE110 Lecture 04
201707 CSE110 Lecture 04
Javier Gonzalez-Sanchez
 
201506 CSE340 Lecture 10
201506 CSE340 Lecture 10201506 CSE340 Lecture 10
201506 CSE340 Lecture 10
Javier Gonzalez-Sanchez
 
Evolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveEvolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and Effective
Naresha K
 
201707 CSE110 Lecture 07
201707 CSE110 Lecture 07  201707 CSE110 Lecture 07
201707 CSE110 Lecture 07
Javier Gonzalez-Sanchez
 
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Transferring Software Testing Tools to Practice (AST 2017 Keynote)Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Transferring Software Testing Tools to Practice (AST 2017 Keynote)
Tao Xie
 
Evolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveEvolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and Effective
Naresha K
 
Ad

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
Javier Gonzalez-Sanchez
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
Javier Gonzalez-Sanchez
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
Javier Gonzalez-Sanchez
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 10
201801 CSE240 Lecture 10201801 CSE240 Lecture 10
201801 CSE240 Lecture 10
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 09
201801 CSE240 Lecture 09201801 CSE240 Lecture 09
201801 CSE240 Lecture 09
Javier Gonzalez-Sanchez
 
201801 CSE240 Lecture 08
201801 CSE240 Lecture 08201801 CSE240 Lecture 08
201801 CSE240 Lecture 08
Javier Gonzalez-Sanchez
 
Ad

Recently uploaded (20)

Unit Two - Java Architecture and OOPS
Unit Two  -   Java Architecture and OOPSUnit Two  -   Java Architecture and OOPS
Unit Two - Java Architecture and OOPS
Nabin Dhakal
 
Multi-Agent Era will Define the Future of Software
Multi-Agent Era will Define the Future of SoftwareMulti-Agent Era will Define the Future of Software
Multi-Agent Era will Define the Future of Software
Ivo Andreev
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
cram_advancedword2007version2025final.ppt
cram_advancedword2007version2025final.pptcram_advancedword2007version2025final.ppt
cram_advancedword2007version2025final.ppt
ahmedsaadtax2025
 
Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025
Web Designer
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
How to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber PluginHow to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber Plugin
eGrabber
 
File Viewer Plus 7.5.5.49 Crack Full Version
File Viewer Plus 7.5.5.49 Crack Full VersionFile Viewer Plus 7.5.5.49 Crack Full Version
File Viewer Plus 7.5.5.49 Crack Full Version
raheemk1122g
 
Hyper Casual Game Developers Company
Hyper  Casual  Game  Developers  CompanyHyper  Casual  Game  Developers  Company
Hyper Casual Game Developers Company
Nova Carter
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
Legacy Code Nightmares , Hellscapes, and Lessons Learned.pdf
Legacy Code Nightmares , Hellscapes, and Lessons Learned.pdfLegacy Code Nightmares , Hellscapes, and Lessons Learned.pdf
Legacy Code Nightmares , Hellscapes, and Lessons Learned.pdf
Ortus Solutions, Corp
 
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t IgnoreWhy CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Shubham Joshi
 
Lumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free CodeLumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free Code
raheemk1122g
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
How to Create a Crypto Wallet Like Trust.pptx
How to Create a Crypto Wallet Like Trust.pptxHow to Create a Crypto Wallet Like Trust.pptx
How to Create a Crypto Wallet Like Trust.pptx
riyageorge2024
 
Hydraulic Modeling And Simulation Software Solutions.pptx
Hydraulic Modeling And Simulation Software Solutions.pptxHydraulic Modeling And Simulation Software Solutions.pptx
Hydraulic Modeling And Simulation Software Solutions.pptx
julia smits
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 
Unit Two - Java Architecture and OOPS
Unit Two  -   Java Architecture and OOPSUnit Two  -   Java Architecture and OOPS
Unit Two - Java Architecture and OOPS
Nabin Dhakal
 
Multi-Agent Era will Define the Future of Software
Multi-Agent Era will Define the Future of SoftwareMulti-Agent Era will Define the Future of Software
Multi-Agent Era will Define the Future of Software
Ivo Andreev
 
wAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptxwAIred_LearnWithOutAI_JCON_14052025.pptx
wAIred_LearnWithOutAI_JCON_14052025.pptx
SimonedeGijt
 
Buy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training techBuy vs. Build: Unlocking the right path for your training tech
Buy vs. Build: Unlocking the right path for your training tech
Rustici Software
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
Download 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-ActivatedDownload 4k Video Downloader Crack Pre-Activated
Download 4k Video Downloader Crack Pre-Activated
Web Designer
 
cram_advancedword2007version2025final.ppt
cram_advancedword2007version2025final.pptcram_advancedword2007version2025final.ppt
cram_advancedword2007version2025final.ppt
ahmedsaadtax2025
 
Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025
Web Designer
 
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.pptPassive House Canada Conference 2025 Presentation [Final]_v4.ppt
Passive House Canada Conference 2025 Presentation [Final]_v4.ppt
IES VE
 
How to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber PluginHow to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber Plugin
eGrabber
 
File Viewer Plus 7.5.5.49 Crack Full Version
File Viewer Plus 7.5.5.49 Crack Full VersionFile Viewer Plus 7.5.5.49 Crack Full Version
File Viewer Plus 7.5.5.49 Crack Full Version
raheemk1122g
 
Hyper Casual Game Developers Company
Hyper  Casual  Game  Developers  CompanyHyper  Casual  Game  Developers  Company
Hyper Casual Game Developers Company
Nova Carter
 
Do not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your causeDo not let staffing shortages and limited fiscal view hamper your cause
Do not let staffing shortages and limited fiscal view hamper your cause
Fexle Services Pvt. Ltd.
 
Legacy Code Nightmares , Hellscapes, and Lessons Learned.pdf
Legacy Code Nightmares , Hellscapes, and Lessons Learned.pdfLegacy Code Nightmares , Hellscapes, and Lessons Learned.pdf
Legacy Code Nightmares , Hellscapes, and Lessons Learned.pdf
Ortus Solutions, Corp
 
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t IgnoreWhy CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Why CoTester Is the AI Testing Tool QA Teams Can’t Ignore
Shubham Joshi
 
Lumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free CodeLumion Pro Crack + 2025 Activation Key Free Code
Lumion Pro Crack + 2025 Activation Key Free Code
raheemk1122g
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
How to Create a Crypto Wallet Like Trust.pptx
How to Create a Crypto Wallet Like Trust.pptxHow to Create a Crypto Wallet Like Trust.pptx
How to Create a Crypto Wallet Like Trust.pptx
riyageorge2024
 
Hydraulic Modeling And Simulation Software Solutions.pptx
Hydraulic Modeling And Simulation Software Solutions.pptxHydraulic Modeling And Simulation Software Solutions.pptx
Hydraulic Modeling And Simulation Software Solutions.pptx
julia smits
 
Best HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRMBest HR and Payroll Software in Bangladesh - accordHRM
Best HR and Payroll Software in Bangladesh - accordHRM
accordHRM
 

201506 CSE340 Lecture 11

  • 1. CSE340 - Principles of Programming Languages Lecture 11: Parser Implementation I Javier Gonzalez-Sanchez javiergs@asu.edu BYENG M1-38 Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 2 Parser BNF EBNF A AA B Parser Grammar
  • 3. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 3 Parser | Grammar 1 <EXPRESSION> à <X> {'|' <X>} <X> à <Y> {'&' <Y>} <Y> à ['!'] <R> <R> à <E> {('>'|'<'|'=='|'!=') <E>} <E> à <A> {(’+'|'-’) <A>} <A> à <B> {('*'|'/') <B>} <B> à ['-'] <C> <C> à integer| identifier|'(' <EXPRESSION> ')'
  • 4. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 4 Parser | Grammar 2 <PROGRAM> à '{' <BODY> '}' <BODY> à {<EXPRESSION>';'} <EXPRESSION> à <X> {'|' <X>} <X> à <Y> {'&' <Y>} <Y> à ['!'] <R> <R> à <E> {('>'|'<'|'=='|'!=') <E>} <E> à <A> {(’+'|'-’) <A>} <A> à <B> {('*'|'/') <B>} <B> à ['-'] <C> <C> à integer| identifier|'(' <EXPRESSION> ')'
  • 5. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 5 Parser | Input and Output { 0; 1 + 2; 3 * (4 + hello); }
  • 6. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 6 Parser | Step by Step For each rule in the grammar { §  Step 1. left-hand side (new method) §  Step 2. right-hand side (loops, ifs, call methods) §  Step 3. identify errors (terminals) §  Step 4. synchronize errors (first and follow sets) }
  • 7. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 7 Parser public class Parser { private static Vector<Token> tokens; private static int currentToken; public static void RULE_PROGRAM () {} public static void RULE_BODY () {} public static void RULE_EXPRESSION () {} public static void RULE_X () {} public static void RULE_Y () {} public static void RULE_R () {} public static void RULE_E () {} public static void RULE_A () {} public static void RULE_B () {} public static void RULE_C () {} }
  • 8. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 8 Parser public static void RULE_PROGRAM() { if (tokens.get(currentToken).getWord().equals(“{”)) { currentToken++; else error(1); RULE_BODY(); if (tokens.get(currentToken).getWord().equals(“}”)) currentToken++; else error(2); } PROGRAM
  • 9. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 9 Parser public static void RULE_BODY() { while (!tokens.get(currentToken).getWord().equals(“}”)) { RULE_EXPRESSION(); if (tokens.get(currentToken).getWord().equals(“;”)) currentToken++; else error(3); } } BODY
  • 10. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 10 Parser public static void RULE_EXPRESSION() { RULE_X(); while (tokens.get(currentToken).getWord().equals(“|”)) { currentToken++; RULE_X(); } } EXPRESSION
  • 11. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 11 Parser public static void RULE_X() { RULE_Y(); while (tokens.get(currentToken).getWord().equals(“&”)) { currentToken++; RULE_Y(); } } X
  • 12. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 12 Parser public static void RULE_Y() { if (tokens.get(currentToken).getWord().equals(“!”)) { currentToken++; } RULE_R(); } Y
  • 13. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 13 Parser public static void RULE_R() { RULE_E(); while ( tokens.get(currentToken).getWord().equals(“<”) |tokens.get(currentToken).getWord().equals(“>”) |tokens.get(currentToken).getWord().equals(“==”) |tokens.get(currentToken).getWord().equals(“!=”) ) { currentToken++; RULE_E(); } } R
  • 14. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 14 Parser public static void RULE_E() { RULE_A(); while (tokens.get(currentToken).getWord().equals(“-”) | tokens.get(currentToken).getWord().equals(“+”) ) { currentToken++; RULE_A(); } } E
  • 15. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 15 Parser public static void RULE_A() { RULE_B(); while (tokens.get(currentToken).getWord().equals(“/”) | tokens.get(currentToken).getWord().equals(“*”) ) { currentToken++; RULE_B(); } } A
  • 16. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 16 Parser public static void RULE_B() { if (tokens.get(currentToken).getWord().equals(“-”)) { currentToken++; } RULE_C(); } B
  • 17. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 17 Parser public static void RULE_C() { if (tokens.get(currentToken).getToken().equals(“integer”)) { currentToken++; } else if (tokens.get(currentToken).getToken().equals(“identifier”)) { currentToken++; } else if (tokens.get(currentToken).getWord().equals(“(”)) { currentToken++; RULE_EXPRESSION(); if (tokens.get(currentToken).getWord().equals(“)”)) { currentToken++; } else error(4); } } else { error (5); } } C
  • 18. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 18 Homework Programming Assignment 2 Level 1 Review and Understand the Source Code posted in Blackboard. Specially, particularly the use of DefaultMutableTreeNode)
  • 19. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 19 Homework
  • 20. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 20 Homework
  • 21. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 21 Homework
  • 22. Javier Gonzalez-Sanchez | CSE340 |Summer 2015 | 22 Homework Programming Assignment 2 Level 2 Modify the Source Code to include the rules PROGRAM and BODY, EXPRESSION, X, Y, R (from Grammar 2)
  • 23. 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.
  翻译: