SlideShare a Scribd company logo
1 of 18
Download to read offline
Copyright © 2006 The McGraw-Hill Companies, Inc.
Programming Languages
Tucker and Noonan
Chapter 2
Syntax
A language that is simple to parse for the compiler is also
simple to parse for the human programmer.
N. Wirth
Copyright © 2006 The McGraw-Hill Companies, Inc.
Contents
2.1 Grammars
2.1.1 Backus-Naur Form
2.1.2 Derivations
2.1.3 Parse Trees
2.1.4 Associativity and Precedence
2.1.5 Ambiguous Grammars
2.2 Extended BNF
2.3 Syntax of a Small Language: Clite
2.3.1 Lexical Syntax
2.3.2 Concrete Syntax
2.4 Compilers and Interpreters
2.5 Linking Syntax and Semantics
2.5.1 Abstract Syntax
2.5.2 Abstract Syntax Trees
2.5.3 Abstract Syntax of Clite
Copyright © 2006 The McGraw-Hill Companies, Inc.
2.3 Syntax of a Small Language: Clite
Motivation for using a subset of C:
Grammar
Language (pages) Reference
Pascal 5 Jensen & Wirth
C 6 Kernighan & Richie
C++ 22 Stroustrup
Java 14 Gosling, et. al.
The Clite grammar fits on one page (next 3 slides),
so it’s a far better tool for studying language design.
Copyright © 2006 The McGraw-Hill Companies, Inc.
Fig. 2.7 Clite Grammar: Statements
Program → int main ( ) { Declarations Statements }
Declarations → { Declaration }
Declaration → Type Identifier [ [ Integer ] ] { , Identifier [ [ Integer ] ] }
Type → int | bool | float | char
Statements → { Statement }
Statement → ; | Block | Assignment | IfStatement | WhileStatement
Block → { Statements }
Assignment → Identifier [ [ Expression ] ] = Expression ;
IfStatement → if ( Expression ) Statement [ else Statement ]
WhileStatement → while ( Expression ) Statement
Copyright © 2006 The McGraw-Hill Companies, Inc.
Fig. 2.7 Clite Grammar: Expressions
Expression → Conjunction { || Conjunction }
Conjunction → Equality { && Equality }
Equality → Relation [ EquOp Relation ]
EquOp → == | !=
Relation → Addition [ RelOp Addition ]
RelOp → < | <= | > | >=
Addition → Term { AddOp Term }
AddOp → + | -
Term → Factor { MulOp Factor }
MulOp → * | / | %
Factor → [ UnaryOp ] Primary
UnaryOp → - | !
Primary → Identifier [ [ Expression ] ] | Literal | ( Expression ) |
Type ( Expression )
Copyright © 2006 The McGraw-Hill Companies, Inc.
Fig. 2.7 Clite grammar: lexical level
Identifier → Letter { Letter | Digit }
Letter → a | b | … | z | A | B | … | Z
Digit → 0 | 1 | … | 9
Literal → Integer | Boolean | Float | Char
Integer → Digit { Digit }
Boolean → true | False
Float → Integer . Integer
Char → ‘ ASCII Char ‘
Copyright © 2006 The McGraw-Hill Companies, Inc.
Issues Not Addressed by this Grammar
• Comments
• Whitespace
• Distinguishing one token <= from two tokens < =
• Distinguishing identifiers from keywords like if
These issues are addressed by identifying two levels:
– lexical level
– syntactic level
Copyright © 2006 The McGraw-Hill Companies, Inc.
2.3.1 Lexical Syntax
Input: a stream of characters from the ASCII set, keyed
by a programmer.
Output: a stream of tokens or basic symbols, classified
as follows:
– Identifiers e.g., Stack, x, i, push
– Literals e.g., 123, 'x', 3.25, true
– Keywords bool char else false float if int
main true while
– Operators = || && == != < <= > >= + - * / !
– Punctuation ; , { } ( )
Copyright © 2006 The McGraw-Hill Companies, Inc.
Whitespace
Whitespace is any space, tab, end-of-line character (or
characters), or character sequence inside a comment
No token may contain embedded whitespace
(unless it is a character or string literal)
Example:
>= one token
> = two tokens
Copyright © 2006 The McGraw-Hill Companies, Inc.
Whitespace Examples in Pascal
while a < b do legal - spacing between tokens
while a<b do spacing not needed for <
whilea<bdo illegal - can’t tell boundaries
whilea < bdo between tokens
Copyright © 2006 The McGraw-Hill Companies, Inc.
Comments
Not defined in grammar
Clite uses // comment style of C++
Copyright © 2006 The McGraw-Hill Companies, Inc.
Identifier
Sequence of letters and digits, starting with a letter
if is both an identifier and a keyword
Most languages require identifiers to be distinct from
keywords
In some languages, identifiers are merely predefined
(and thus can be redefined by the programmer)
Copyright © 2006 The McGraw-Hill Companies, Inc.
program confusing;
const true = false;
begin
if (a<b) = true then
f(a)
else …
Redefining Identifiers can be dangerous
Copyright © 2006 The McGraw-Hill Companies, Inc.
Should Identifiers be case-sensitive?
Older languages: no. Why?
– Pascal: no.
– Modula: yes
– C, C++: yes
– Java: yes
– PHP: partly yes, partly no. What about
orthogonality?
Copyright © 2006 The McGraw-Hill Companies, Inc.
2.3.2 Concrete Syntax
Based on a parse of its Tokens
; is a statement terminator
(Algol-60, Pascal use ; as a separator)
Rule for IfStatement is ambiguous:
“The else ambiguity is resolved by connecting
an else with the last encountered else-less if.”
[Stroustrup, 1991]
Copyright © 2006 The McGraw-Hill Companies, Inc.
Expressions in Clite
13 grammar rules
Use of meta braces – operators are left associative
C++ expressions require 4 pages of grammar rules
[Stroustrup]
C uses an ambiguous expression grammar
[Kernighan and Ritchie]
Copyright © 2006 The McGraw-Hill Companies, Inc.
Associativity and Precedence
Clite Operator Associativity
Unary - ! none
* / left
+ - left
< <= > >= none
== != none
&& left
|| left
Copyright © 2006 The McGraw-Hill Companies, Inc.
Clite Equality, Relational Operators
… are non-associative.
(an idea borrowed from Ada)
Why is this important?
In C++, the expression:
if (a < x < b)
is not equivalent to
if (a < x && x < b)
But it is error-free!
So, what does it mean?

More Related Content

Similar to ch02c.pdf

Meta Languages Railroad Diagrams Student Version
Meta Languages Railroad Diagrams Student VersionMeta Languages Railroad Diagrams Student Version
Meta Languages Railroad Diagrams Student VersionKelly Bauer
 
117 A Outline 25
117 A Outline 25117 A Outline 25
117 A Outline 25wasntgosu
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorialwasntgosu
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Guillaume Laforge
 
AspectC++: Language Proposal and Prototype Implementation
AspectC++: Language Proposal and Prototype ImplementationAspectC++: Language Proposal and Prototype Implementation
AspectC++: Language Proposal and Prototype Implementationdinomasch
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perlmegakott
 
International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)IJERD Editor
 
International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)IJERD Editor
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++K Durga Prasad
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Keyappasami
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Introduction to Laws
Introduction to LawsIntroduction to Laws
Introduction to Lawsnkpart
 

Similar to ch02c.pdf (20)

Preparing for Scala 3
Preparing for Scala 3Preparing for Scala 3
Preparing for Scala 3
 
Meta Languages Railroad Diagrams Student Version
Meta Languages Railroad Diagrams Student VersionMeta Languages Railroad Diagrams Student Version
Meta Languages Railroad Diagrams Student Version
 
117 A Outline 25
117 A Outline 25117 A Outline 25
117 A Outline 25
 
Generics Tutorial
Generics TutorialGenerics Tutorial
Generics Tutorial
 
copa-ii.pptx
copa-ii.pptxcopa-ii.pptx
copa-ii.pptx
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007
 
AspectC++: Language Proposal and Prototype Implementation
AspectC++: Language Proposal and Prototype ImplementationAspectC++: Language Proposal and Prototype Implementation
AspectC++: Language Proposal and Prototype Implementation
 
How a Compiler Works ?
How a Compiler Works ?How a Compiler Works ?
How a Compiler Works ?
 
What's in a Name?
What's in a Name?What's in a Name?
What's in a Name?
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
 
International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)
 
International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)International Journal of Engineering Research and Development (IJERD)
International Journal of Engineering Research and Development (IJERD)
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
C++ book
C++ bookC++ book
C++ book
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Introduction to Laws
Introduction to LawsIntroduction to Laws
Introduction to Laws
 

Recently uploaded

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 

Recently uploaded (20)

Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 

ch02c.pdf

  • 1. Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages Tucker and Noonan Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth
  • 2. Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 2.1 Grammars 2.1.1 Backus-Naur Form 2.1.2 Derivations 2.1.3 Parse Trees 2.1.4 Associativity and Precedence 2.1.5 Ambiguous Grammars 2.2 Extended BNF 2.3 Syntax of a Small Language: Clite 2.3.1 Lexical Syntax 2.3.2 Concrete Syntax 2.4 Compilers and Interpreters 2.5 Linking Syntax and Semantics 2.5.1 Abstract Syntax 2.5.2 Abstract Syntax Trees 2.5.3 Abstract Syntax of Clite
  • 3. Copyright © 2006 The McGraw-Hill Companies, Inc. 2.3 Syntax of a Small Language: Clite Motivation for using a subset of C: Grammar Language (pages) Reference Pascal 5 Jensen & Wirth C 6 Kernighan & Richie C++ 22 Stroustrup Java 14 Gosling, et. al. The Clite grammar fits on one page (next 3 slides), so it’s a far better tool for studying language design.
  • 4. Copyright © 2006 The McGraw-Hill Companies, Inc. Fig. 2.7 Clite Grammar: Statements Program → int main ( ) { Declarations Statements } Declarations → { Declaration } Declaration → Type Identifier [ [ Integer ] ] { , Identifier [ [ Integer ] ] } Type → int | bool | float | char Statements → { Statement } Statement → ; | Block | Assignment | IfStatement | WhileStatement Block → { Statements } Assignment → Identifier [ [ Expression ] ] = Expression ; IfStatement → if ( Expression ) Statement [ else Statement ] WhileStatement → while ( Expression ) Statement
  • 5. Copyright © 2006 The McGraw-Hill Companies, Inc. Fig. 2.7 Clite Grammar: Expressions Expression → Conjunction { || Conjunction } Conjunction → Equality { && Equality } Equality → Relation [ EquOp Relation ] EquOp → == | != Relation → Addition [ RelOp Addition ] RelOp → < | <= | > | >= Addition → Term { AddOp Term } AddOp → + | - Term → Factor { MulOp Factor } MulOp → * | / | % Factor → [ UnaryOp ] Primary UnaryOp → - | ! Primary → Identifier [ [ Expression ] ] | Literal | ( Expression ) | Type ( Expression )
  • 6. Copyright © 2006 The McGraw-Hill Companies, Inc. Fig. 2.7 Clite grammar: lexical level Identifier → Letter { Letter | Digit } Letter → a | b | … | z | A | B | … | Z Digit → 0 | 1 | … | 9 Literal → Integer | Boolean | Float | Char Integer → Digit { Digit } Boolean → true | False Float → Integer . Integer Char → ‘ ASCII Char ‘
  • 7. Copyright © 2006 The McGraw-Hill Companies, Inc. Issues Not Addressed by this Grammar • Comments • Whitespace • Distinguishing one token <= from two tokens < = • Distinguishing identifiers from keywords like if These issues are addressed by identifying two levels: – lexical level – syntactic level
  • 8. Copyright © 2006 The McGraw-Hill Companies, Inc. 2.3.1 Lexical Syntax Input: a stream of characters from the ASCII set, keyed by a programmer. Output: a stream of tokens or basic symbols, classified as follows: – Identifiers e.g., Stack, x, i, push – Literals e.g., 123, 'x', 3.25, true – Keywords bool char else false float if int main true while – Operators = || && == != < <= > >= + - * / ! – Punctuation ; , { } ( )
  • 9. Copyright © 2006 The McGraw-Hill Companies, Inc. Whitespace Whitespace is any space, tab, end-of-line character (or characters), or character sequence inside a comment No token may contain embedded whitespace (unless it is a character or string literal) Example: >= one token > = two tokens
  • 10. Copyright © 2006 The McGraw-Hill Companies, Inc. Whitespace Examples in Pascal while a < b do legal - spacing between tokens while a<b do spacing not needed for < whilea<bdo illegal - can’t tell boundaries whilea < bdo between tokens
  • 11. Copyright © 2006 The McGraw-Hill Companies, Inc. Comments Not defined in grammar Clite uses // comment style of C++
  • 12. Copyright © 2006 The McGraw-Hill Companies, Inc. Identifier Sequence of letters and digits, starting with a letter if is both an identifier and a keyword Most languages require identifiers to be distinct from keywords In some languages, identifiers are merely predefined (and thus can be redefined by the programmer)
  • 13. Copyright © 2006 The McGraw-Hill Companies, Inc. program confusing; const true = false; begin if (a<b) = true then f(a) else … Redefining Identifiers can be dangerous
  • 14. Copyright © 2006 The McGraw-Hill Companies, Inc. Should Identifiers be case-sensitive? Older languages: no. Why? – Pascal: no. – Modula: yes – C, C++: yes – Java: yes – PHP: partly yes, partly no. What about orthogonality?
  • 15. Copyright © 2006 The McGraw-Hill Companies, Inc. 2.3.2 Concrete Syntax Based on a parse of its Tokens ; is a statement terminator (Algol-60, Pascal use ; as a separator) Rule for IfStatement is ambiguous: “The else ambiguity is resolved by connecting an else with the last encountered else-less if.” [Stroustrup, 1991]
  • 16. Copyright © 2006 The McGraw-Hill Companies, Inc. Expressions in Clite 13 grammar rules Use of meta braces – operators are left associative C++ expressions require 4 pages of grammar rules [Stroustrup] C uses an ambiguous expression grammar [Kernighan and Ritchie]
  • 17. Copyright © 2006 The McGraw-Hill Companies, Inc. Associativity and Precedence Clite Operator Associativity Unary - ! none * / left + - left < <= > >= none == != none && left || left
  • 18. Copyright © 2006 The McGraw-Hill Companies, Inc. Clite Equality, Relational Operators … are non-associative. (an idea borrowed from Ada) Why is this important? In C++, the expression: if (a < x < b) is not equivalent to if (a < x && x < b) But it is error-free! So, what does it mean?