SlideShare una empresa de Scribd logo
1 de 27
CSE340 - Principles of
Programming Languages
Lecture 09:
Grammars 1
Javier Gonzalez-Sanchez
javiergs@asu.edu
BYENG M1-38
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2
Ambiguity
•  A grammar is said to be ambiguous if there is at
least one string with two or more parse trees.
•  Note that ambiguity is a property of grammars, not
languages.
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 3
Ambiguity
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4
Solution
•  If a grammar can be made unambiguous at all, it is
usually made unambiguous through layering.
•  Have exactly one way to build each piece of the
string.
•  Have exactly one way of combining pieces back
together.
•  Recursive constructions
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5
Layering
Rule 1
Rule 2
Rule 3
Rule 4
Rule 5
. . . . .
Leaf (Terminals, i.e., Tokens)
Root
Start symbol
Layers
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6
Layering
1	
 2	
 4	
 5	
3	
inputs:
§  1 + 2 + 3 < 4 * 5
§  1 * 2 + 3 + 4 < 5
§  1 < 2 + 3 + 4 * 5
§  1 + 2 < 3 * 4 + 5
§  1 + 2 * 3 < 4 + 5
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7
Exercise
Provide a Grammar that is Not ambiguous
for arithmetic expressions
10 * 20 + 15
Precedence of operators and Associativity
þ (10 * 20) + 15
ý 10 * (20 + 15)
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8
Exercise | Precedence
New Grammar:
<E> à <__> + <__>
< > à <__> - <__>
< > à <__> * <__>
< > à <__> / <__>
< > à - <__>
Original Grammar:
E à E OP E
E à integer
OPà + | - | * | /
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9
Exercise | Hand written notes
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10
Exercise | Hand written notes
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11
Exercise | Precedence
<E> à <A> | <A> {'+' <A>} | <A> {'-' <A>}
<A> à <B> | <B> {'*' <B>} | <B> {'/' <B>}
<B> à '-'<C> | <C>
<C> à integer
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12
Exercise | Precedence
<E> à <A> {(’+'|'-’) <A>}
<A> à <B> {('*'|'/') <B>}
<B> à ['-'] <C>
<C> à integer
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13
Syntax Diagrams
E
A
B
C
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 14
Exercise 1
Include rules for handling parenthesis into the previous
grammar. The grammar should accept as correct the
following expressions:
10 * 20 + 15
(10 * 20) + 15
10 * (20 + 15)
(10) * (20) + (15)
(10 * 20 + 15)
10 * (20) + 15
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15
Exercise 1 | Hand written notes
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16
Syntax Diagrams
E
A
B
C
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17
Exercise 2 | Hand written notes
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18
Exercise 2 | Hand written notes
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 19
Exercise 2
Include rules to accept variables names (identifiers) in
expressions. The grammar should accept as correct the
following expressions:
A * 20 + time
(x * y) + 15
10 * (ASU + cse340)
(10) * (20) + (15)
(hello * world + Arizona)
10 * (counter) + 15
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 20
Syntax Diagrams
E
A
B
C
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 21
Exercise 3
Provide a Grammar that is
Not ambiguous
with Precedence of operators and Associativity for this:
10 + 20 > 15 & -10 != 1 | 20 / ( 10 + 1) < 5
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 22
Exercise 3 | Note
Precedence of operators
|
&
!
< > == != <= >=
+ -
* /
-
( )
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 23
Exercise 3 | Hand written notes
...
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 24
Exercise 3 | Hand written notes
{ }
{ }
{ } { } { } { }...
{ } { }
{ } { }
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 25
Exercise 3
R
EXPRESSION
X
Y
Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 26
Exercise 3
E
A
B
C
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.

Más contenido relacionado

Destacado

Destacado (20)

Micazxpl - Intelligent Sensors Network project report
Micazxpl - Intelligent Sensors Network project reportMicazxpl - Intelligent Sensors Network project report
Micazxpl - Intelligent Sensors Network project report
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Uip Romain
Uip RomainUip Romain
Uip Romain
 
201004 - brain computer interaction
201004 - brain computer interaction201004 - brain computer interaction
201004 - brain computer interaction
 
Awesome Powerpoint Monday Morning #5
Awesome Powerpoint Monday Morning #5Awesome Powerpoint Monday Morning #5
Awesome Powerpoint Monday Morning #5
 
Eprotect
EprotectEprotect
Eprotect
 
Slides boekpresentatie 'Sociale Media en Journalistiek'
Slides boekpresentatie 'Sociale Media en Journalistiek'Slides boekpresentatie 'Sociale Media en Journalistiek'
Slides boekpresentatie 'Sociale Media en Journalistiek'
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Phenomenal Oct 15, 2009
Phenomenal Oct 15, 2009Phenomenal Oct 15, 2009
Phenomenal Oct 15, 2009
 
Monaco 020909
Monaco 020909Monaco 020909
Monaco 020909
 
Huizenprijzen in Amsterdam
Huizenprijzen in AmsterdamHuizenprijzen in Amsterdam
Huizenprijzen in Amsterdam
 
Week11 Presentation Group-C
Week11 Presentation Group-CWeek11 Presentation Group-C
Week11 Presentation Group-C
 
lectura
lecturalectura
lectura
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
201001 Face-Based Emotion Recognition
201001 Face-Based Emotion Recognition201001 Face-Based Emotion Recognition
201001 Face-Based Emotion Recognition
 
fugitive emission ball valve
fugitive emission ball valvefugitive emission ball valve
fugitive emission ball valve
 
Accomplishment, Aspirations & Challenges: Boston
Accomplishment, Aspirations & Challenges: BostonAccomplishment, Aspirations & Challenges: Boston
Accomplishment, Aspirations & Challenges: Boston
 
201101 mLearning
201101 mLearning201101 mLearning
201101 mLearning
 
Tabagisme et thrombose habbal
Tabagisme et thrombose habbalTabagisme et thrombose habbal
Tabagisme et thrombose habbal
 
Monaco 020909
Monaco 020909Monaco 020909
Monaco 020909
 

Similar a 201506 CSE340 Lecture 09 (15)

201506 - CSE340 Lecture 08
201506 - CSE340 Lecture 08201506 - CSE340 Lecture 08
201506 - CSE340 Lecture 08
 
201506 CSE340 Lecture 11
201506 CSE340 Lecture 11201506 CSE340 Lecture 11
201506 CSE340 Lecture 11
 
201506 CSE340 Lecture 18
201506 CSE340 Lecture 18201506 CSE340 Lecture 18
201506 CSE340 Lecture 18
 
201506 CSE340 Lecture 21
201506 CSE340 Lecture 21201506 CSE340 Lecture 21
201506 CSE340 Lecture 21
 
201505 CSE340 Lecture 03
201505 CSE340 Lecture 03201505 CSE340 Lecture 03
201505 CSE340 Lecture 03
 
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
 
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 05
201707 CSE110 Lecture 05   201707 CSE110 Lecture 05
201707 CSE110 Lecture 05
 
201707 CSE110 Lecture 08
201707 CSE110 Lecture 08  201707 CSE110 Lecture 08
201707 CSE110 Lecture 08
 
201505 CSE340 Lecture 02
201505 CSE340 Lecture 02201505 CSE340 Lecture 02
201505 CSE340 Lecture 02
 
201506 CSE340 Lecture 13
201506 CSE340 Lecture 13201506 CSE340 Lecture 13
201506 CSE340 Lecture 13
 
201707 CSE110 Lecture 23
201707 CSE110 Lecture 23   201707 CSE110 Lecture 23
201707 CSE110 Lecture 23
 
201801 CSE240 Lecture 01
201801 CSE240 Lecture 01201801 CSE240 Lecture 01
201801 CSE240 Lecture 01
 

Más de 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 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
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 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
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
 

Último

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
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...ICS
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
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...panagenda
 
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 GoalsJhone kinadey
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
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 🔝✔️✔️Delhi Call girls
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
+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
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Último (20)

Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
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...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
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...
 
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
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
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 🔝✔️✔️
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
+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...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

201506 CSE340 Lecture 09

  • 1. CSE340 - Principles of Programming Languages Lecture 09: Grammars 1 Javier Gonzalez-Sanchez javiergs@asu.edu BYENG M1-38 Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 2 Ambiguity •  A grammar is said to be ambiguous if there is at least one string with two or more parse trees. •  Note that ambiguity is a property of grammars, not languages.
  • 3. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 3 Ambiguity
  • 4. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 4 Solution •  If a grammar can be made unambiguous at all, it is usually made unambiguous through layering. •  Have exactly one way to build each piece of the string. •  Have exactly one way of combining pieces back together. •  Recursive constructions
  • 5. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 5 Layering Rule 1 Rule 2 Rule 3 Rule 4 Rule 5 . . . . . Leaf (Terminals, i.e., Tokens) Root Start symbol Layers
  • 6. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 6 Layering 1 2 4 5 3 inputs: §  1 + 2 + 3 < 4 * 5 §  1 * 2 + 3 + 4 < 5 §  1 < 2 + 3 + 4 * 5 §  1 + 2 < 3 * 4 + 5 §  1 + 2 * 3 < 4 + 5
  • 7. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 7 Exercise Provide a Grammar that is Not ambiguous for arithmetic expressions 10 * 20 + 15 Precedence of operators and Associativity þ (10 * 20) + 15 ý 10 * (20 + 15)
  • 8. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 8 Exercise | Precedence New Grammar: <E> à <__> + <__> < > à <__> - <__> < > à <__> * <__> < > à <__> / <__> < > à - <__> Original Grammar: E à E OP E E à integer OPà + | - | * | /
  • 9. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 9 Exercise | Hand written notes
  • 10. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 10 Exercise | Hand written notes
  • 11. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 11 Exercise | Precedence <E> à <A> | <A> {'+' <A>} | <A> {'-' <A>} <A> à <B> | <B> {'*' <B>} | <B> {'/' <B>} <B> à '-'<C> | <C> <C> à integer
  • 12. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 12 Exercise | Precedence <E> à <A> {(’+'|'-’) <A>} <A> à <B> {('*'|'/') <B>} <B> à ['-'] <C> <C> à integer
  • 13. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 13 Syntax Diagrams E A B C
  • 14. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 14 Exercise 1 Include rules for handling parenthesis into the previous grammar. The grammar should accept as correct the following expressions: 10 * 20 + 15 (10 * 20) + 15 10 * (20 + 15) (10) * (20) + (15) (10 * 20 + 15) 10 * (20) + 15
  • 15. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 15 Exercise 1 | Hand written notes
  • 16. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 16 Syntax Diagrams E A B C
  • 17. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 17 Exercise 2 | Hand written notes
  • 18. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 18 Exercise 2 | Hand written notes
  • 19. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 19 Exercise 2 Include rules to accept variables names (identifiers) in expressions. The grammar should accept as correct the following expressions: A * 20 + time (x * y) + 15 10 * (ASU + cse340) (10) * (20) + (15) (hello * world + Arizona) 10 * (counter) + 15
  • 20. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 20 Syntax Diagrams E A B C
  • 21. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 21 Exercise 3 Provide a Grammar that is Not ambiguous with Precedence of operators and Associativity for this: 10 + 20 > 15 & -10 != 1 | 20 / ( 10 + 1) < 5
  • 22. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 22 Exercise 3 | Note Precedence of operators | & ! < > == != <= >= + - * / - ( )
  • 23. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 23 Exercise 3 | Hand written notes ...
  • 24. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 24 Exercise 3 | Hand written notes { } { } { } { } { } { }... { } { } { } { }
  • 25. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 25 Exercise 3 R EXPRESSION X Y
  • 26. Javier Gonzalez-Sanchez | CSE340 | Summer 2015 | 26 Exercise 3 E A B C
  • 27. 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.