SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
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

Más contenido relacionado

La actualidad más candente

Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2MOHIT TOMAR
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ LanguageWay2itech
 
Lecture 3 basic syntax and semantics
Lecture 3  basic syntax and semanticsLecture 3  basic syntax and semantics
Lecture 3 basic syntax and semanticsalvin567
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process ModelsHassan A-j
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in JavaJin Castor
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming LanguageAhmad Idrees
 
Coding and testing in Software Engineering
Coding and testing in Software EngineeringCoding and testing in Software Engineering
Coding and testing in Software EngineeringAbhay Vijay
 
Chapter 13 software testing strategies
Chapter 13 software testing strategiesChapter 13 software testing strategies
Chapter 13 software testing strategiesSHREEHARI WADAWADAGI
 

La actualidad más candente (20)

Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Introduction to C# Programming
Introduction to C# ProgrammingIntroduction to C# Programming
Introduction to C# Programming
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 
Variables in C and C++ Language
Variables in C and C++ LanguageVariables in C and C++ Language
Variables in C and C++ Language
 
White box testing
White box testingWhite box testing
White box testing
 
C#.NET
C#.NETC#.NET
C#.NET
 
Lecture 3 basic syntax and semantics
Lecture 3  basic syntax and semanticsLecture 3  basic syntax and semantics
Lecture 3 basic syntax and semantics
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process Models
 
Parsing
ParsingParsing
Parsing
 
Path Testing
Path TestingPath Testing
Path Testing
 
Software quality assurance
Software quality assuranceSoftware quality assurance
Software quality assurance
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
 
Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Coding and testing in Software Engineering
Coding and testing in Software EngineeringCoding and testing in Software Engineering
Coding and testing in Software Engineering
 
White box ppt
White box pptWhite box ppt
White box ppt
 
Chapter 13 software testing strategies
Chapter 13 software testing strategiesChapter 13 software testing strategies
Chapter 13 software testing strategies
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 

Similar a 6. describing syntax and semantics

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

Más de Zambales National High School

Más de 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
 

Último

Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptxAneriPatwari
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxryandux83rd
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 

Último (20)

Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
ARTERIAL BLOOD GAS ANALYSIS........pptx
ARTERIAL BLOOD  GAS ANALYSIS........pptxARTERIAL BLOOD  GAS ANALYSIS........pptx
ARTERIAL BLOOD GAS ANALYSIS........pptx
 
Introduction to Research ,Need for research, Need for design of Experiments, ...
Introduction to Research ,Need for research, Need for design of Experiments, ...Introduction to Research ,Need for research, Need for design of Experiments, ...
Introduction to Research ,Need for research, Need for design of Experiments, ...
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
 
Chi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical VariableChi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical Variable
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Employablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptxEmployablity presentation and Future Career Plan.pptx
Employablity presentation and Future Career Plan.pptx
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 

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