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

Chapter 12
Chapter 12Chapter 12
Chapter 12
dphil002
 
Awesome Powerpoint Monday Morning #5
Awesome Powerpoint Monday Morning #5Awesome Powerpoint Monday Morning #5
Awesome Powerpoint Monday Morning #5
SylShannon
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
dphil002
 
Phenomenal Oct 15, 2009
Phenomenal Oct 15, 2009Phenomenal Oct 15, 2009
Phenomenal Oct 15, 2009
etalcomendras
 
Week11 Presentation Group-C
Week11 Presentation Group-CWeek11 Presentation Group-C
Week11 Presentation Group-C
s1160114
 
Accomplishment, Aspirations & Challenges: Boston
Accomplishment, Aspirations & Challenges: BostonAccomplishment, Aspirations & Challenges: Boston
Accomplishment, Aspirations & Challenges: Boston
The Hub Milan
 
Tabagisme et thrombose habbal
Tabagisme et thrombose habbalTabagisme et thrombose habbal
Tabagisme et thrombose habbal
sfa_angeiologie
 

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 (13)

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
 
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
 

Más de Javier Gonzalez-Sanchez

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

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
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Último (20)

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
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
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
 
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 🔝✔️✔️
 
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...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
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 ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%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
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
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 Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
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 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.