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

Fundamental design concepts
Fundamental design conceptsFundamental design concepts
Fundamental design conceptssrijavel
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Programming Fundamental Presentation
Programming Fundamental PresentationProgramming Fundamental Presentation
Programming Fundamental Presentationfazli khaliq
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / TranslatorsProject Student
 
Programming languages and concepts by vivek parihar
Programming languages and concepts by vivek pariharProgramming languages and concepts by vivek parihar
Programming languages and concepts by vivek pariharVivek Parihar
 
Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9koolkampus
 
White box black box & gray box testing
White box black box & gray box testingWhite box black box & gray box testing
White box black box & gray box testingHimanshu
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysisIffat Anjum
 
Object oriented software engineering concepts
Object oriented software engineering conceptsObject oriented software engineering concepts
Object oriented software engineering conceptsKomal Singh
 
Quality and productivity factors
Quality and productivity factorsQuality and productivity factors
Quality and productivity factorsNancyBeaulah_R
 
Software requirements specification
Software requirements specificationSoftware requirements specification
Software requirements specificationlavanya marichamy
 
Uml in software engineering
Uml in software engineeringUml in software engineering
Uml in software engineeringMubashir Jutt
 
White Box Testing
White Box TestingWhite Box Testing
White Box TestingAlisha Roy
 
Introduction to Software Engineering
Introduction to Software EngineeringIntroduction to Software Engineering
Introduction to Software EngineeringSaqib Raza
 
Acceptance testing
Acceptance testingAcceptance testing
Acceptance testingCOEPD HR
 
Object Oriented Design in Software Engineering SE12
Object Oriented Design in Software Engineering SE12Object Oriented Design in Software Engineering SE12
Object Oriented Design in Software Engineering SE12koolkampus
 

What's hot (20)

Fundamental design concepts
Fundamental design conceptsFundamental design concepts
Fundamental design concepts
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Rad model
Rad modelRad model
Rad model
 
Programming Fundamental Presentation
Programming Fundamental PresentationProgramming Fundamental Presentation
Programming Fundamental Presentation
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Programming Languages / Translators
Programming Languages / TranslatorsProgramming Languages / Translators
Programming Languages / Translators
 
Programming languages and concepts by vivek parihar
Programming languages and concepts by vivek pariharProgramming languages and concepts by vivek parihar
Programming languages and concepts by vivek parihar
 
Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9Formal Specification in Software Engineering SE9
Formal Specification in Software Engineering SE9
 
White box black box & gray box testing
White box black box & gray box testingWhite box black box & gray box testing
White box black box & gray box testing
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
push down automata
push down automatapush down automata
push down automata
 
Object oriented software engineering concepts
Object oriented software engineering conceptsObject oriented software engineering concepts
Object oriented software engineering concepts
 
Quality and productivity factors
Quality and productivity factorsQuality and productivity factors
Quality and productivity factors
 
Software process
Software processSoftware process
Software process
 
Software requirements specification
Software requirements specificationSoftware requirements specification
Software requirements specification
 
Uml in software engineering
Uml in software engineeringUml in software engineering
Uml in software engineering
 
White Box Testing
White Box TestingWhite Box Testing
White Box Testing
 
Introduction to Software Engineering
Introduction to Software EngineeringIntroduction to Software Engineering
Introduction to Software Engineering
 
Acceptance testing
Acceptance testingAcceptance testing
Acceptance testing
 
Object Oriented Design in Software Engineering SE12
Object Oriented Design in Software Engineering SE12Object Oriented Design in Software Engineering SE12
Object Oriented Design in Software Engineering SE12
 

Similar to 6. describing syntax and semantics

CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptxObsa2
 
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.pdfFutureKids1
 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)bolovv
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnfTaha Shakeel
 
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.pdfarpitaeron555
 
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmmUnit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmmDhruvKushwaha12
 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdfTANZINTANZINA
 
UNIT 1 part II.ppt
UNIT 1 part II.pptUNIT 1 part II.ppt
UNIT 1 part II.pptRanjeet Reddy
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit IIIManoj Patil
 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysisSudhaa Ravi
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler DesignKuppusamy P
 

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

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

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 

Recently uploaded (20)

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
CĂłdigo Creativo y Arte de Software | Unidad 1
CĂłdigo Creativo y Arte de Software | Unidad 1CĂłdigo Creativo y Arte de Software | Unidad 1
CĂłdigo Creativo y Arte de Software | Unidad 1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

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