SlideShare a Scribd company logo
1 of 30
TextBook
CSCI18 - Concepts of Programming languages
Concepts of Programming
Languages, Robert W.
Sebesta, (10th edition),
Addison-Wesley Publishing
Company
Chapter 3
Describing Syntax & Semantics
CSCI18 - Concepts of Programming languages
Topics
• Introduction
• The General Problem of Describing Syntax
• Formal Methods of Describing Syntax
• Attribute Grammars
• Describing the Meanings of Programs:
• Dynamic Semantics
CSCI18 - Concepts of Programming languages
Introduction
• Syntax: the form or structure of the expressions,
statements, and program units
• Semantics: the meaning of the expressions,
statements, and program units
• Syntax and semantics provide a language’s definition
– Users of a language definition
• Other language designers
• Implementers
• Programmers (the users of the language)
CSCI18 - Concepts of Programming languages
The General Problem of Describing Syntax:
Terminology
• A sentence is a string of characters over some
alphabet
• A language is a set of sentences
• A lexeme is the lowest level syntactic unit of a
language (e.g., *, sum, begin)
• A token is a category of lexemes (e.g.,
identifier)
CSCI18 - Concepts of Programming languages
Formal Definition of Languages
• Recognizers
– A recognition device reads input strings over the alphabet
of the language and decides whether the input strings
belong to the language
– Example: syntax analysis part of a compiler
• Generators
– A device that generates sentences of a language
– One can determine if the syntax of a particular sentence is
syntactically correct by comparing it to the structure of the
generator
CSCI18 - Concepts of Programming languages
BNF and Context-Free Grammars
• Context-Free Grammars
– Developed by Noam Chomsky in the mid-1950s
– Language generators, meant to describe the syntax of
natural languages
– Define a class of languages called context-free languages
• Backus-Naur Form (1959)
– Invented by John Backus to describe Algol 58
– BNF is equivalent to context-free grammars
CSCI18 - Concepts of Programming languages
BNF Fundamentals
• In BNF, abstractions are used to represent classes of syntactic structures --
they act like syntactic variables (also called nonterminal symbols, or just
terminals)
• Terminals are lexemes or tokens
• A rule has a left-hand side (LHS), which is a non-terminal, and a right-hand
side (RHS), which is a string of terminals and/or non-terminals
• Non-terminals are often enclosed in angle brackets
– Examples of BNF rules:
<ident_list> → identifier | identifier, <ident_list>
<if_stmt> → if <logic_expr> then <stmt>
• Grammar: a finite non-empty set of rules
• A start symbol is a special element of the nonterminals of a grammar
CSCI18 - Concepts of Programming languages
BNF Rules
• An abstraction (or nonterminal symbol) can
have more than one RHS
<stmt> → <single_stmt>
| begin <stmt_list> end
CSCI18 - Concepts of Programming languages
Describing Lists
• Syntactic lists are described using recursion
<ident_list> → ident
| ident, <ident_list>
• A derivation is a repeated application of rules,
starting with the start symbol and ending with
a sentence (all terminal symbols)
CSCI18 - Concepts of Programming languages
An Example Grammar & Derivation
<program> → <stmts>
<stmts> → <stmt> |
<stmt> ; <stmts>
<stmt> → <var> = <expr>
<var> → a | b | c | d
<expr> → <term> + <term> |
<term> - <term>
<term> → <var> | const
<program> => <stmts> => <stmt>
=> <var> = <expr>
=> a = <expr>
=> a = <term> + <term>
=> a = <var> + <term>
=> a = b + <term>
=> a = b + const
CSCI18 - Concepts of Programming languages
An Example Derivation
<program> => <stmts> => <stmt>
=> <var> = <expr>
=> a = <expr>
=> a = <term> + <term>
=> a = <var> + <term>
=> a = b + <term>
=> a = b + const
CSCI18 - Concepts of Programming languages
Derivations
• Every string of symbols in a derivation is a
sentential form
• A sentence is a sentential form that has only
terminal symbols
• A leftmost derivation is one in which the
leftmost nonterminal in each sentential form
is the one that is expanded
CSCI18 - Concepts of Programming languages
Parse Tree
• A hierarchical representation of a derivation
CSCI18 - Concepts of Programming languages
Ambiguity in Grammars
• A grammar is ambiguous if and only if it generates a sentential
form that has two or more distinct parse trees
• some string that it can generate in more than one way
• semantic information is needed to select the intended parse
• For example, in C the following:
– x * y ; can be interpreted as either:
• the declaration of an identifier named y of type pointer-to-x, or
• an expression in which x is multiplied by y and then the result is
discarded.
CSCI18 - Concepts of Programming languages
An Ambiguous Expression Grammar
CSCI18 - Concepts of Programming languages
Associativity of Operators
• Operator associativity can also be indicated by a grammar
CSCI18 - Concepts of Programming languages
Extended BNF
• Optional parts (0 or one) are placed in
brackets [ ]
<proc_call> → ident [(<expr_list>)]
• Alternative parts of RHSs are placed inside
parentheses and separated via vertical bars
<term> → <term> (+|-) const
• Repetitions (0 or more) are placed inside
braces { }
<ident> → letter {letter|digit}
CSCI18 - Concepts of Programming languages
BNF and EBNF
CSCI18 - Concepts of Programming languages
Semantics
• BNF in the form of CFG is used to describe the syntax
of a programming language
• It can not describe the semantics (meaning) of the
programming language statements, expressions, etc.
• There are two categories of semantics:
– Static semantics: semantic rules that can be checked
during compile time (i.e., prior to runtime). As an example,
variables in a program must be "declared" before they are
referenced.
– Dynamic semantics: semantic rules that apply during the
execution of a program.
CSCI18 - Concepts of Programming languages
Attribute Grammars
• Attribute grammars (AGs) have additions to
CFGs to carry some semantic info on parse
tree nodes
• Primary value of AGs:
– Static semantics specification
– Compiler design (static semantics checking)
CSCI18 - Concepts of Programming languages
Attribute Grammars : Definition
• Def: An attribute grammar is a context-free
grammar G = (S, N, T, P) with the following
additions:
– For each grammar symbol x there is a set A(x) of
attribute values
– Each rule has a set of functions that define certain
attributes of the nonterminals in the rule
– Each rule has a (possibly empty) set of predicates
to check for attribute consistency
CSCI18 - Concepts of Programming languages
Attribute Grammars: Definition
• Let X0  X1 ... Xn be a rule
• Functions of the form S(X0) = f(A(X1), ... , A(Xn))
define synthesized attributes
• Functions of the form I(Xj) = f(A(X0), ... , A(Xn)),
for i <= j <= n, define inherited attributes
• Initially, there are intrinsic attributes on the
leaves
CSCI18 - Concepts of Programming languages
Attribute Grammars: An Example
• Syntax
<assign> -> <var> = <expr>
<expr> -> <var> + <var> | <var>
<var> A | B | C
• actual_type: synthesized for <var> and
<expr>
• expected_type: inherited for <expr>
CSCI18 - Concepts of Programming languages
Attribute Grammar (continued)
• Syntax rule: <expr>  <var>[1] + <var>[2]
Semantic rules:
<expr>.actual_type  <var>[1].actual_type
Predicate:
<var>[1].actual_type == <var>[2].actual_type
<expr>.expected_type == <expr>.actual_type
• Syntax rule: <var>  id
Semantic rule:
<var>.actual_type  lookup (<var>.string)
CSCI18 - Concepts of Programming languages
Attribute Grammars (continued)
• How are attribute values computed?
– If all attributes were inherited, the tree could be
decorated in top-down order.
– If all attributes were synthesized, the tree could be
decorated in bottom-up order.
– In many cases, both kinds of attributes are used,
and it is some combination of top-down and
bottom-up that must be used.
CSCI18 - Concepts of Programming languages
Attribute Grammars (continued)
<expr>.expected_type  inherited from parent
<var>[1].actual_type  lookup (A)
<var>[2].actual_type  lookup (B)
<var>[1].actual_type =? <var>[2].actual_type
<expr>.actual_type  <var>[1].actual_type
<expr>.actual_type =? <expr>.expected_type
CSCI18 - Concepts of Programming languages
Semantics
• There is no single widely acceptable notation
or formalism for describing semantics
• Several needs for a methodology and
notation for semantics:
– Programmers need to know what statements mean
– Compiler writers must know exactly what language constructs do
– Correctness proofs would be possible
– Compiler generators would be possible
– Designers could detect ambiguities and inconsistencies
CSCI18 - Concepts of Programming languages
Operational Semantics
• Operational Semantics
– Describe the meaning of a program by executing
its statements on a machine, either simulated or
actual. The change in the state of the machine
(memory, registers, etc.) defines the meaning of
the statement
• To use operational semantics for a high-level
language, a virtual machine is needed
CSCI18 - Concepts of Programming languages
Denotational Semantics
• Based on recursive function theory
• The most abstract semantics description
method
• Originally developed by Scott and Strachey
(1970)
CSCI18 - Concepts of Programming languages

More Related Content

Similar to 8074448.ppt (20)

1 cc
1 cc1 cc
1 cc
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
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
 
Compier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.pptCompier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.ppt
 
UNIT 1 part II.ppt
UNIT 1 part II.pptUNIT 1 part II.ppt
UNIT 1 part II.ppt
 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
 
Introduction
IntroductionIntroduction
Introduction
 
Introduction
IntroductionIntroduction
Introduction
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
sabesta3.ppt
sabesta3.pptsabesta3.ppt
sabesta3.ppt
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Lexical
LexicalLexical
Lexical
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
 
CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptx
 
Compiler design Project
Compiler design ProjectCompiler design Project
Compiler design Project
 
1._Introduction_.pptx
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptx
 
Parsing
ParsingParsing
Parsing
 

Recently uploaded

Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
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
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
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
 
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
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
(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
 
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
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 

Recently uploaded (20)

Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
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...
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
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...
 
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
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
(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...
 
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
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 

8074448.ppt

  • 1. TextBook CSCI18 - Concepts of Programming languages Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company
  • 2. Chapter 3 Describing Syntax & Semantics CSCI18 - Concepts of Programming languages
  • 3. Topics • Introduction • The General Problem of Describing Syntax • Formal Methods of Describing Syntax • Attribute Grammars • Describing the Meanings of Programs: • Dynamic Semantics CSCI18 - Concepts of Programming languages
  • 4. Introduction • Syntax: the form or structure of the expressions, statements, and program units • Semantics: the meaning of the expressions, statements, and program units • Syntax and semantics provide a language’s definition – Users of a language definition • Other language designers • Implementers • Programmers (the users of the language) CSCI18 - Concepts of Programming languages
  • 5. The General Problem of Describing Syntax: Terminology • A sentence is a string of characters over some alphabet • A language is a set of sentences • A lexeme is the lowest level syntactic unit of a language (e.g., *, sum, begin) • A token is a category of lexemes (e.g., identifier) CSCI18 - Concepts of Programming languages
  • 6. Formal Definition of Languages • Recognizers – A recognition device reads input strings over the alphabet of the language and decides whether the input strings belong to the language – Example: syntax analysis part of a compiler • Generators – A device that generates sentences of a language – One can determine if the syntax of a particular sentence is syntactically correct by comparing it to the structure of the generator CSCI18 - Concepts of Programming languages
  • 7. BNF and Context-Free Grammars • Context-Free Grammars – Developed by Noam Chomsky in the mid-1950s – Language generators, meant to describe the syntax of natural languages – Define a class of languages called context-free languages • Backus-Naur Form (1959) – Invented by John Backus to describe Algol 58 – BNF is equivalent to context-free grammars CSCI18 - Concepts of Programming languages
  • 8. BNF Fundamentals • In BNF, abstractions are used to represent classes of syntactic structures -- they act like syntactic variables (also called nonterminal symbols, or just terminals) • Terminals are lexemes or tokens • A rule has a left-hand side (LHS), which is a non-terminal, and a right-hand side (RHS), which is a string of terminals and/or non-terminals • Non-terminals are often enclosed in angle brackets – Examples of BNF rules: <ident_list> → identifier | identifier, <ident_list> <if_stmt> → if <logic_expr> then <stmt> • Grammar: a finite non-empty set of rules • A start symbol is a special element of the nonterminals of a grammar CSCI18 - Concepts of Programming languages
  • 9. BNF Rules • An abstraction (or nonterminal symbol) can have more than one RHS <stmt> → <single_stmt> | begin <stmt_list> end CSCI18 - Concepts of Programming languages
  • 10. Describing Lists • Syntactic lists are described using recursion <ident_list> → ident | ident, <ident_list> • A derivation is a repeated application of rules, starting with the start symbol and ending with a sentence (all terminal symbols) CSCI18 - Concepts of Programming languages
  • 11. An Example Grammar & Derivation <program> → <stmts> <stmts> → <stmt> | <stmt> ; <stmts> <stmt> → <var> = <expr> <var> → a | b | c | d <expr> → <term> + <term> | <term> - <term> <term> → <var> | const <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const CSCI18 - Concepts of Programming languages
  • 12. An Example Derivation <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const CSCI18 - Concepts of Programming languages
  • 13. Derivations • Every string of symbols in a derivation is a sentential form • A sentence is a sentential form that has only terminal symbols • A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded CSCI18 - Concepts of Programming languages
  • 14. Parse Tree • A hierarchical representation of a derivation CSCI18 - Concepts of Programming languages
  • 15. Ambiguity in Grammars • A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees • some string that it can generate in more than one way • semantic information is needed to select the intended parse • For example, in C the following: – x * y ; can be interpreted as either: • the declaration of an identifier named y of type pointer-to-x, or • an expression in which x is multiplied by y and then the result is discarded. CSCI18 - Concepts of Programming languages
  • 16. An Ambiguous Expression Grammar CSCI18 - Concepts of Programming languages
  • 17. Associativity of Operators • Operator associativity can also be indicated by a grammar CSCI18 - Concepts of Programming languages
  • 18. Extended BNF • Optional parts (0 or one) are placed in brackets [ ] <proc_call> → ident [(<expr_list>)] • Alternative parts of RHSs are placed inside parentheses and separated via vertical bars <term> → <term> (+|-) const • Repetitions (0 or more) are placed inside braces { } <ident> → letter {letter|digit} CSCI18 - Concepts of Programming languages
  • 19. BNF and EBNF CSCI18 - Concepts of Programming languages
  • 20. Semantics • BNF in the form of CFG is used to describe the syntax of a programming language • It can not describe the semantics (meaning) of the programming language statements, expressions, etc. • There are two categories of semantics: – Static semantics: semantic rules that can be checked during compile time (i.e., prior to runtime). As an example, variables in a program must be "declared" before they are referenced. – Dynamic semantics: semantic rules that apply during the execution of a program. CSCI18 - Concepts of Programming languages
  • 21. Attribute Grammars • Attribute grammars (AGs) have additions to CFGs to carry some semantic info on parse tree nodes • Primary value of AGs: – Static semantics specification – Compiler design (static semantics checking) CSCI18 - Concepts of Programming languages
  • 22. Attribute Grammars : Definition • Def: An attribute grammar is a context-free grammar G = (S, N, T, P) with the following additions: – For each grammar symbol x there is a set A(x) of attribute values – Each rule has a set of functions that define certain attributes of the nonterminals in the rule – Each rule has a (possibly empty) set of predicates to check for attribute consistency CSCI18 - Concepts of Programming languages
  • 23. Attribute Grammars: Definition • Let X0  X1 ... Xn be a rule • Functions of the form S(X0) = f(A(X1), ... , A(Xn)) define synthesized attributes • Functions of the form I(Xj) = f(A(X0), ... , A(Xn)), for i <= j <= n, define inherited attributes • Initially, there are intrinsic attributes on the leaves CSCI18 - Concepts of Programming languages
  • 24. Attribute Grammars: An Example • Syntax <assign> -> <var> = <expr> <expr> -> <var> + <var> | <var> <var> A | B | C • actual_type: synthesized for <var> and <expr> • expected_type: inherited for <expr> CSCI18 - Concepts of Programming languages
  • 25. Attribute Grammar (continued) • Syntax rule: <expr>  <var>[1] + <var>[2] Semantic rules: <expr>.actual_type  <var>[1].actual_type Predicate: <var>[1].actual_type == <var>[2].actual_type <expr>.expected_type == <expr>.actual_type • Syntax rule: <var>  id Semantic rule: <var>.actual_type  lookup (<var>.string) CSCI18 - Concepts of Programming languages
  • 26. Attribute Grammars (continued) • How are attribute values computed? – If all attributes were inherited, the tree could be decorated in top-down order. – If all attributes were synthesized, the tree could be decorated in bottom-up order. – In many cases, both kinds of attributes are used, and it is some combination of top-down and bottom-up that must be used. CSCI18 - Concepts of Programming languages
  • 27. Attribute Grammars (continued) <expr>.expected_type  inherited from parent <var>[1].actual_type  lookup (A) <var>[2].actual_type  lookup (B) <var>[1].actual_type =? <var>[2].actual_type <expr>.actual_type  <var>[1].actual_type <expr>.actual_type =? <expr>.expected_type CSCI18 - Concepts of Programming languages
  • 28. Semantics • There is no single widely acceptable notation or formalism for describing semantics • Several needs for a methodology and notation for semantics: – Programmers need to know what statements mean – Compiler writers must know exactly what language constructs do – Correctness proofs would be possible – Compiler generators would be possible – Designers could detect ambiguities and inconsistencies CSCI18 - Concepts of Programming languages
  • 29. Operational Semantics • Operational Semantics – Describe the meaning of a program by executing its statements on a machine, either simulated or actual. The change in the state of the machine (memory, registers, etc.) defines the meaning of the statement • To use operational semantics for a high-level language, a virtual machine is needed CSCI18 - Concepts of Programming languages
  • 30. Denotational Semantics • Based on recursive function theory • The most abstract semantics description method • Originally developed by Scott and Strachey (1970) CSCI18 - Concepts of Programming languages