SlideShare a Scribd company logo
1 of 19
Describing Syntax
Lesson 6
MANOLO L. GIRON
RMTU
Structure of Programming Language
Syntax
• Is the form of its expressions, statements, and program units.
For example the syntax of a Java while statement is
• while (boolean_expr) statement
Structure of Programming Language
Semantics
• Is the meaning of those expressions, statements, and program
units.
For example the syntax of a Java while statement is
• while (boolean_expr) statement
• The semantics of this statement form is that when the current value of the Boolean
expression is true, the embedded statement is executed. Otherwise, control
continues after the while construct. Then control implicitly returns to the Boolean
expression to repeat the process.
Structure of Programming Language
• Sentences or statement. The strings of a language
Example
index = 2 * count + 17
• Lexemes. Small units.
• Token. a category of its lexemes.
Structure of Programming Language
Example statement or sentence
index = 2 * count + 17
Lexemes Tokens
Index identifier
= equal_sign
2 int_literal
* mult_op
Count identifier
+ plus_op
17 int_literal
; semicolon
Structure of Programming Language
Formal Methods of Describing Syntax
• Metalanguage. is a language that is used to describe another language.
• BNF(Backus-Naur Form) is a metalanguage for programming languages.
Example
A simple Java assignment statement.
The actual definition of <assign> can be given by;
<assign> →<var> =<expression>
Structure of Programming Language
<assign> →<var> =<expression>
• (LHS) The text on the left side of the arrow.
• (RHS) consists of some mixture of tokens, lexemes, and references to other
abstractions.
Structure of Programming Language
• The abstractions in a BNF description, or grammar, are often called
nonterminal symbols, or simply nonterminals.
• The lexemes and tokens of the rules are called terminal symbols, or simply
terminals.
• A BNF description, or grammar, is a collection of rules.
Structure of Programming Language
• Nonterminal symbols can have two or more distinct definitions, representing
two or more possible syntactic forms in the language.
• Multiple definitions can be written as a single rule, with the different definitions
separated by the symbol|, meaning logical OR.
• Java if statement can be described with the rules;
• <if_stmt> → if (<logic_expr> ) <stmt>
• <if_stmt> → if (<logic_expr> )<stmt> else<stmt>
• or with the rule
• <if_stmt> → if (<logic_expr> )<stmt>
|if (<logic_expr> )<stmt> else<stmt>
In these rules, <stmt> represents either a single statement or a compound
statement.
Structure of Programming Language
Describing Lists
• Variable-length lists in mathematics are often written using an ellipsis (. . .);
• BNF does not include the ellipsis, so an alternative method is required for
describing lists of syntactic elements in programming languages.
• For BNF, the alternative is recursion.
• A rule is recursive if its LHS appears in its RHS. The following rules illustrate how
recursion is used to describe lists:
• <ident_list> →identifier
|identifier, <ident_list>
• This defines <ident_list> as either a single token (identifier) or an identifier
followed by a comma and another instance of <ident_list>.
Structure of Programming Language
Grammars and Derivations
• A grammar is a generative device for defining languages.
• The sentences of the language are generated through a sequence of
applications of the rules, beginning with a special nonterminal of the
grammar called the start symbol.
• This sequence of rule applications is called a derivation.
Structure of Programming Language
EXAMPLE
• A Grammar for a Small Language
<program> → begin<stmt_list> end
<stmt_list> →<stmt>
|<stmt> ;<stmt_list>
<stmt> →<var> =<expression>
<var> → A | B | C
<expression> →<var> +<var>
|<var> –<var>
|<var>
Structure of Programming Language
Derivations
• In this derivation, the replaced nonterminal is always the leftmost
nonterminal in the previous sentential form.
• The derivation continues until the sentential form contains no non terminals.
• That sentential form, consisting of only terminals, or lexemes, is the
generated sentence.
Structure of Programming Language
A derivation of a program in this language follows:
<program> => begin<stmt_list> end
=> begin <stmt> ;<stmt_list> end
=> begin <var> =<expression> ;<stmt_list> end
=> begin A =<expression> ;<stmt_list> end
=> begin A =<var> +<var> ;<stmt_list> end
=> begin A = B +<var> ;<stmt_list> end
=> begin A = B + C ;<stmt_list> end
=> begin A = B + C ;<stmt> end
=> begin A = B + C ;<var> =<expression> end
=> begin A = B + C ; B =<expression> end
=> begin A = B + C ; B =<var> end
=> begin A = B + C ; B = C end
Structure of Programming Language
EXAMPLE
• A Grammar for Simple Assignment Statements
<assign> →<id>= <expr>
<id> → A | B | C
<expr> →<id>+ <expr>
|<id>* <expr>
| ( <expr>)
|<id>
Structure of Programming Language
For example, the statement
A = B * ( A + C )
The leftmost derivation:
<assign> =><id> =<expr>
=> A = <expr>
=> A = <id> *<expr>
=> A = B * <expr>
=> A = B * ( <expr>)
=> A = B * ( <id> +<expr>)
=> A = B * ( A + <expr>)
=> A = B * ( A + <id>)
=> A = B * ( A + C )
Structure of Programming Language
Parse Trees
• One of the most attractive features of grammars is that they naturally
describe the hierarchical syntactic structure of the sentences of the languages
they define.
• These hierarchical structures are called parse trees.
Structure of Programming Language
A parse tree for the simple statement
A = B * (A + C)
Structure of Programming Language
REFERENCES
• CONCEPTS OF
PROGRAMMING LANGUAGES
TENTH EDITION
ROBERT W. SEBESTA
Structure of Programming Language

More Related Content

What's hot

Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignAkhil Kaushik
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler designSudip Singh
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free GrammarsMarina Santini
 
Unit 1 - Introduction to Software Engineering.ppt
Unit 1 - Introduction to Software Engineering.pptUnit 1 - Introduction to Software Engineering.ppt
Unit 1 - Introduction to Software Engineering.pptDrTThendralCompSci
 
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Md. Imran Hossain Showrov
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction Thapar Institute
 
Fundamentals of Software Engineering
Fundamentals of Software Engineering Fundamentals of Software Engineering
Fundamentals of Software Engineering Madhar Khan Pathan
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory Rajendran
 
Domain-Specific Software Engineering
Domain-Specific Software EngineeringDomain-Specific Software Engineering
Domain-Specific Software Engineeringelliando dias
 
Architecture design in software engineering
Architecture design in software engineeringArchitecture design in software engineering
Architecture design in software engineeringPreeti Mishra
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysisBilalzafar22
 

What's hot (20)

Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
SOFTWARE ENGINEERING
SOFTWARE ENGINEERINGSOFTWARE ENGINEERING
SOFTWARE ENGINEERING
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Unit 1 - Introduction to Software Engineering.ppt
Unit 1 - Introduction to Software Engineering.pptUnit 1 - Introduction to Software Engineering.ppt
Unit 1 - Introduction to Software Engineering.ppt
 
Static analysis
Static analysisStatic analysis
Static analysis
 
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
 
Compiler Design Introduction
Compiler Design Introduction Compiler Design Introduction
Compiler Design Introduction
 
Parsing
ParsingParsing
Parsing
 
Namespaces in C#
Namespaces in C#Namespaces in C#
Namespaces in C#
 
Fundamentals of Software Engineering
Fundamentals of Software Engineering Fundamentals of Software Engineering
Fundamentals of Software Engineering
 
Coding
CodingCoding
Coding
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory
 
Compilers
CompilersCompilers
Compilers
 
Domain-Specific Software Engineering
Domain-Specific Software EngineeringDomain-Specific Software Engineering
Domain-Specific Software Engineering
 
Prgramming paradigms
Prgramming paradigmsPrgramming paradigms
Prgramming paradigms
 
ASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS ANDASSIGNMENT STATEMENTS AND
ASSIGNMENT STATEMENTS AND
 
Architecture design in software engineering
Architecture design in software engineeringArchitecture design in software engineering
Architecture design in software engineering
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysis
 

Similar to 6. describing syntax and semantics

Similar to 6. describing syntax and semantics (20)

CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptx
 
8074448.ppt
8074448.ppt8074448.ppt
8074448.ppt
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
CS-4337_03_Chapter3- syntax and semantics.pdf
CS-4337_03_Chapter3- syntax and semantics.pdfCS-4337_03_Chapter3- syntax and semantics.pdf
CS-4337_03_Chapter3- syntax and semantics.pdf
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnf
 
match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdf
 
Syntax
SyntaxSyntax
Syntax
 
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmmUnit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf
 
UNIT 1 part II.ppt
UNIT 1 part II.pptUNIT 1 part II.ppt
UNIT 1 part II.ppt
 
Lexical1
Lexical1Lexical1
Lexical1
 
sabesta3.ppt
sabesta3.pptsabesta3.ppt
sabesta3.ppt
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit III
 
Plc part 2
Plc  part 2Plc  part 2
Plc part 2
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysis
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Parsing
ParsingParsing
Parsing
 

More from Zambales National High School

More from Zambales National High School (20)

8. digital integrated circuit
8. digital integrated circuit8. digital integrated circuit
8. digital integrated circuit
 
7. transformer and diode
7. transformer and diode7. transformer and diode
7. transformer and diode
 
5. resistor and capacitor application
5. resistor and capacitor application5. resistor and capacitor application
5. resistor and capacitor application
 
6. transistor
6. transistor6. transistor
6. transistor
 
4. resistor and capacitor
4. resistor and capacitor4. resistor and capacitor
4. resistor and capacitor
 
2. Basic Electronics Circuit
2. Basic Electronics Circuit2. Basic Electronics Circuit
2. Basic Electronics Circuit
 
3. basic electrical and electronic symbol
3. basic electrical and electronic symbol3. basic electrical and electronic symbol
3. basic electrical and electronic symbol
 
11. abstraction and capsulation
11. abstraction and capsulation11. abstraction and capsulation
11. abstraction and capsulation
 
10. sub program
10. sub program10. sub program
10. sub program
 
9. control statement
9. control statement9. control statement
9. control statement
 
8. data types
8. data types8. data types
8. data types
 
7. name binding and scopes
7. name binding and scopes7. name binding and scopes
7. name binding and scopes
 
5. evolution
5. evolution5. evolution
5. evolution
 
4. processor
4. processor4. processor
4. processor
 
3. criteria
3. criteria3. criteria
3. criteria
 
2. pl domain
2. pl domain2. pl domain
2. pl domain
 
1. reason why study spl
1. reason why study spl1. reason why study spl
1. reason why study spl
 
18. the components of the system unit
18. the components of the system unit18. the components of the system unit
18. the components of the system unit
 
17. software for home, personal, and educational
17. software for home, personal, and educational17. software for home, personal, and educational
17. software for home, personal, and educational
 
16. graphics and multimedia software
16. graphics and multimedia software16. graphics and multimedia software
16. graphics and multimedia software
 

Recently uploaded

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 

Recently uploaded (20)

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 

6. describing syntax and semantics

  • 1. Describing Syntax Lesson 6 MANOLO L. GIRON RMTU Structure of Programming Language
  • 2. Syntax • Is the form of its expressions, statements, and program units. For example the syntax of a Java while statement is • while (boolean_expr) statement Structure of Programming Language
  • 3. Semantics • Is the meaning of those expressions, statements, and program units. For example the syntax of a Java while statement is • while (boolean_expr) statement • The semantics of this statement form is that when the current value of the Boolean expression is true, the embedded statement is executed. Otherwise, control continues after the while construct. Then control implicitly returns to the Boolean expression to repeat the process. Structure of Programming Language
  • 4. • Sentences or statement. The strings of a language Example index = 2 * count + 17 • Lexemes. Small units. • Token. a category of its lexemes. Structure of Programming Language
  • 5. Example statement or sentence index = 2 * count + 17 Lexemes Tokens Index identifier = equal_sign 2 int_literal * mult_op Count identifier + plus_op 17 int_literal ; semicolon Structure of Programming Language
  • 6. Formal Methods of Describing Syntax • Metalanguage. is a language that is used to describe another language. • BNF(Backus-Naur Form) is a metalanguage for programming languages. Example A simple Java assignment statement. The actual definition of <assign> can be given by; <assign> →<var> =<expression> Structure of Programming Language
  • 7. <assign> →<var> =<expression> • (LHS) The text on the left side of the arrow. • (RHS) consists of some mixture of tokens, lexemes, and references to other abstractions. Structure of Programming Language
  • 8. • The abstractions in a BNF description, or grammar, are often called nonterminal symbols, or simply nonterminals. • The lexemes and tokens of the rules are called terminal symbols, or simply terminals. • A BNF description, or grammar, is a collection of rules. Structure of Programming Language
  • 9. • Nonterminal symbols can have two or more distinct definitions, representing two or more possible syntactic forms in the language. • Multiple definitions can be written as a single rule, with the different definitions separated by the symbol|, meaning logical OR. • Java if statement can be described with the rules; • <if_stmt> → if (<logic_expr> ) <stmt> • <if_stmt> → if (<logic_expr> )<stmt> else<stmt> • or with the rule • <if_stmt> → if (<logic_expr> )<stmt> |if (<logic_expr> )<stmt> else<stmt> In these rules, <stmt> represents either a single statement or a compound statement. Structure of Programming Language
  • 10. Describing Lists • Variable-length lists in mathematics are often written using an ellipsis (. . .); • BNF does not include the ellipsis, so an alternative method is required for describing lists of syntactic elements in programming languages. • For BNF, the alternative is recursion. • A rule is recursive if its LHS appears in its RHS. The following rules illustrate how recursion is used to describe lists: • <ident_list> →identifier |identifier, <ident_list> • This defines <ident_list> as either a single token (identifier) or an identifier followed by a comma and another instance of <ident_list>. Structure of Programming Language
  • 11. Grammars and Derivations • A grammar is a generative device for defining languages. • The sentences of the language are generated through a sequence of applications of the rules, beginning with a special nonterminal of the grammar called the start symbol. • This sequence of rule applications is called a derivation. Structure of Programming Language
  • 12. EXAMPLE • A Grammar for a Small Language <program> → begin<stmt_list> end <stmt_list> →<stmt> |<stmt> ;<stmt_list> <stmt> →<var> =<expression> <var> → A | B | C <expression> →<var> +<var> |<var> –<var> |<var> Structure of Programming Language
  • 13. Derivations • In this derivation, the replaced nonterminal is always the leftmost nonterminal in the previous sentential form. • The derivation continues until the sentential form contains no non terminals. • That sentential form, consisting of only terminals, or lexemes, is the generated sentence. Structure of Programming Language
  • 14. A derivation of a program in this language follows: <program> => begin<stmt_list> end => begin <stmt> ;<stmt_list> end => begin <var> =<expression> ;<stmt_list> end => begin A =<expression> ;<stmt_list> end => begin A =<var> +<var> ;<stmt_list> end => begin A = B +<var> ;<stmt_list> end => begin A = B + C ;<stmt_list> end => begin A = B + C ;<stmt> end => begin A = B + C ;<var> =<expression> end => begin A = B + C ; B =<expression> end => begin A = B + C ; B =<var> end => begin A = B + C ; B = C end Structure of Programming Language
  • 15. EXAMPLE • A Grammar for Simple Assignment Statements <assign> →<id>= <expr> <id> → A | B | C <expr> →<id>+ <expr> |<id>* <expr> | ( <expr>) |<id> Structure of Programming Language
  • 16. For example, the statement A = B * ( A + C ) The leftmost derivation: <assign> =><id> =<expr> => A = <expr> => A = <id> *<expr> => A = B * <expr> => A = B * ( <expr>) => A = B * ( <id> +<expr>) => A = B * ( A + <expr>) => A = B * ( A + <id>) => A = B * ( A + C ) Structure of Programming Language
  • 17. Parse Trees • One of the most attractive features of grammars is that they naturally describe the hierarchical syntactic structure of the sentences of the languages they define. • These hierarchical structures are called parse trees. Structure of Programming Language
  • 18. A parse tree for the simple statement A = B * (A + C) Structure of Programming Language
  • 19. REFERENCES • CONCEPTS OF PROGRAMMING LANGUAGES TENTH EDITION ROBERT W. SEBESTA Structure of Programming Language