!" #
- Dr. Chris Irwin Davis
Email: cid021000@utdallas.edu Phone: (972) 883-3574 Office: ECSS 4.705
Chapter 3 – Describing Syntax and Semantics
CS-4337 Organization of Programming Languages
!" # Chapter 3 Describing Syntax and Semantics CS-4337 - - PowerPoint PPT Presentation
!" # Chapter 3 Describing Syntax and Semantics CS-4337 Organization of Programming Languages Dr. Chris Irwin Davis Email: cid021000@utdallas.edu Phone: (972) 883-3574 O ffi ce: ECSS 4.705 Chapter 3 Topics Introduction The
Email: cid021000@utdallas.edu Phone: (972) 883-3574 Office: ECSS 4.705
Chapter 3 – Describing Syntax and Semantics
CS-4337 Organization of Programming Languages
1-2
Chapter 3 Topics
Dynamic Semantics
1-3
Introduction
expressions, statements, and program units
statements, and program units
definition
– Users of a language definition
1-4
The General Problem of Describing Syntax: Terminology
alphabet
language (e.g., *, sum, begin)
identifier)
Example: Lexemes and Tokens
Lexemes index = 2 * count + 17 ; Tokens identifier equal_sign int_literal mult_op identifier plus_op int_literal semicolon
1-5
Formal Definition of Languages
– 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
Chapter 4
– 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
Formal Methods of Describing Syntax
usually called grammars, are commonly used to describe the syntax of programming languages.
1-6
BNF and 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
– Invented by John Backus to describe the syntax of Algol 58 – BNF is equivalent to context-free grammars
1-7
BNF Fundamentals
syntactic structures — they act like syntactic variables (also called non-terminal symbols, or just non-terminals)
nonterminal, and a right-hand side (RHS), which is a string of terminals and/or nonterminals
BNF Fundamentals (continued)
– Examples of BNF rules:
<ident_list> → identifier | identifier, <ident_list> <if_stmt> → if <logic_expr> then <stmt>
nonterminals of a grammar
1-8
1-9
BNF Rules
have more than one RHS <stmt> → <single_stmt>
| begin <stmt_list> end
<stmt> → <single_stmt> <stmt> → begin <stmt_list> end
1-10
Describing Lists
<ident_list> → ident
| ident, <ident_list>
rules, starting with the start symbol and ending with a sentence (all terminal symbols)
1-11
An Example Grammar
<program> → <stmts> <stmts> → <stmt> | <stmt> ; <stmts> <stmt> → <var> = <expr> <var> → a | b | c | d <expr> → <term> + <term> | <term> - <term> <term> → <var> | const
1-12
An Example Derivation
<program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const
1-13
Derivations
sentential form
terminal symbols
leftmost nonterminal in each sentential form is the one that is expanded
rightmost
1-14
Parse Tree
<program> <stmts> <stmt> const a <var> = <expr> <var> b <term> + <term>
a = b + const
1-15
Ambiguity in Grammars
generates a sentential form that has two or more distinct parse trees
1-16
An Ambiguous Expression Grammar
<expr> → <expr> <op> <expr> | const <op> → / | - <expr> <expr> <expr> <expr> <expr> <expr> <expr> <expr> <expr> <expr> <op> <op> <op> <op> const const const const const const
/ <op>
Ambiguous Grammars
Ambiguous Grammars
Ambiguous Grammars
“The men saw a boy in the park with a telescope”
Logical Languages
– Grammar based on predicate logic – Developed Dr. James Cooke Brown with the goal
languages that people learning it would think in a different way if the hypothesis were true – Loglan is the first among, and the main inspiration for, the languages known as logical languages, which also includes Lojban and Ceqli. – To invesitigate the Sapir-Whorf Hypothesis
1-17
An Unambiguous Expression Grammar
levels of the operators, we cannot have ambiguity
<expr> → <expr> - <term> | <term> <term> → <term> / const| const <expr> <expr> <term> <term> <term> const const const /
Operator Precedence
levels of the operators, we cannot have ambiguity
<assign> → <id> = <expr> <id> → A | B | C <expr> → <expr> + <term> | <term> <term> → <term> * <factor> | <factor> <factor> → ( <expr> ) | <id>
1-18
Associativity of Operators
grammar
<expr> -> <expr> + <expr> | const (ambiguous) <expr> -> <expr> + const | const (unambiguous) <expr> <expr> <expr> <expr> const const const + +
1-19
Extended BNF
<proc_call> → ident [(<expr_list>)]
parentheses and separated via vertical bars
<term> → <term> (+|-) const
<ident_list> → <identifier> {, <identifier>}
1-20
BNF and EBNF
<expr> → <term> |
<expr> + <term> | <expr> - <term> <term> → <factor> | <term> * <factor> | <term> / <factor>
<expr> → <term> {(+ | -) <term>}
<term> → <factor> {(* | /) <factor>}
1-21
Recent Variations in EBNF
Attribute Grammars
1-22
Static Semantics
all of the syntax of programming languages
types of operands in expressions)
be declared before they are used)
1-23
Attribute Grammars
CFGs to carry some semantic info on parse tree nodes
– Static semantics specification – Compiler design (static semantics checking)
1-24
Attribute Grammars : Definition
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
1-25
Attribute Grammars: Definition
define synthesized attributes
for i <= j <= n, define inherited attributes
leaves
1-26
Attribute Grammars: An Example
<proc_def> → procedure <proc_name>[1] <proc_body> end <proc_name>[2];
<proc_name>[1]string == <proc_name>[2].string
1-26
Attribute Grammars: An Example
<assign> → <var> = <expr> <expr> → <var> + <var> | <var> <var> → A | B | C
1-27
Attribute Grammar (continued)
Semantic rules:
<expr>.actual_type ← <var>[1].actual_type
Predicate:
<var>[1].actual_type == <var>[2].actual_type <expr>.expected_type == <expr>.actual_type
Semantic rule:
<var>.actual_type ← lookup (<var>.string)
1-28
Attribute Grammars (continued)
– 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.
1-29
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
!" #
Parse Tree
39
!" #
Computing Attribute Values
40
(Rule 1)
<var>[3].actual_type ← look-up(B) (Rule 4)
(Rule 2)
is either TRUE or FALSE (Rule 2)
!" #
Flow of Attributes in the Tree
41
!" #
A Fully Attributed Parse Tree
42
Semantics
1-30
Semantics
formalism for describing semantics
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
!" #
Semantics
45
Operational Semantics
– Describe the meaning of a program by executing its statements on a machine, either simulated or
(memory, registers, etc.) defines the meaning of the statement
language, a virtual machine is needed
1-31
1-32
Operational Semantics
expensive
– The detailed characteristics of the particular computer would make actions difficult to understand – Such a semantic definition would be machine- dependent
1-33
Operational Semantics (continued)
simulation
– Build a translator (translates source code to the machine code of an idealized computer) – Build a simulator for the idealized computer
– Good if used informally (language manuals, etc.) – Extremely complex if used formally (e.g., VDL), it was used for describing semantics of PL/I.
1-34
Operational Semantics (continued)
manuals, etc.)
Denotational Semantics
method
(1970)
1-35
Denotational Semantics - continued
specification for a language:
entity – Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects
by only the values of the program's variables
1-36
Denotational Semantics: program state
current variables
s = {<i1, v1>, <i2, v2>, …, <in, vn>}
variable name and a state, returns the current value of the variable VARMAP(ij, s) = vj
1-37
Evaluation of Denotational Semantics
programs
programs
language users
1-44
1-45
Axiomatic Semantics
statement type in the language (to allow transformations of logic expressions into more formal logic expressions)
1-46
Axiomatic Semantics (continued)
precondition) states the relationships and constraints among variables that are true at that point in execution
postcondition
precondition that will guarantee the postcondition
1-55
Evaluation of Axiomatic Semantics
the statements in a language is difficult
excellent framework for reasoning about programs, but it is not as useful for language users and compiler writers
programming language is limited for language users or compiler writers
1-56
Denotation Semantics vs Operational Semantics
are defined by coded algorithms
are defined by rigorous mathematical functions
1-57
Summary
meta-languages
– Well-suited for describing the syntax of programming languages
that can describe both the syntax and the semantics of a language
– Operation, Axiomatic, Denotational