CSCE 330 Programming Language Structures Ch 2 Syntax

  • Slides: 15
Download presentation
CSCE 330 Programming Language Structures Ch. 2: Syntax and Semantics Fall 2005 Marco Valtorta

CSCE 330 Programming Language Structures Ch. 2: Syntax and Semantics Fall 2005 Marco Valtorta mgv@cse. sc. edu UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Syntax and Semantics • Syntax is the set of rules that specify the composition

Syntax and Semantics • Syntax is the set of rules that specify the composition of programs from letters, digits and other characters. • Semantics is the set of rules that specify what the result/outcome of a program is. • Problems with English language description of Syntax and Semantics: – verbosity – ambiguity UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Syntax • What is syntax? – syntax vs. lexical rules – Regular languages and

Syntax • What is syntax? – syntax vs. lexical rules – Regular languages and context-free languages • Backus Normal Form (a. k. a. Backus-Naur Form)BNF – A syntax metalanguage – Derivation vs. recognition – Syntax Diagram – Extended BNF (EBNF) UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

BNF History In Java: <If. Then. Stat> : : = if (<Expr>) <Stat> <If.

BNF History In Java: <If. Then. Stat> : : = if (<Expr>) <Stat> <If. Then. Else. Stat> : : = if (<Expr>) <Stat. No. Short. If> else <Stat> UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Recursive Descent Parsing • Parsing is the process of constructing a parse tree •

Recursive Descent Parsing • Parsing is the process of constructing a parse tree • A recursive descent parser is a kind of leftmost parser with very limited lookahead • Recursive descent parsers are built directly from (E)BNF rules • Recursive descent parsers do not work with left-recursive grammars • We provide a simple example for parsing terms made of factors UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

The Concept of Binding • entities (e. g. , variables, statements, subprograms, declarations. .

The Concept of Binding • entities (e. g. , variables, statements, subprograms, declarations. . . ) have attributes (e. g. , for variable: name, type, storage area) • Binding is the specification of the exact nature of an attribute. • When does binding occur? Binding time. – language definition time – language implementation time – compile time – run time • Example: the Fortran type INTEGER is bound partly at language definition time, partly at language implementation time. • static (established before run-time, cannot be changed) and dynamic binding UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Variables • • Name Scope Lifetime Value – l-value (memory location) and r-value (contents

Variables • • Name Scope Lifetime Value – l-value (memory location) and r-value (contents of a memory location) • Type UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Variable Scope • Variables have scope: the range of program instructions over which the

Variable Scope • Variables have scope: the range of program instructions over which the variable is known, and therefore manipulable – scope binding can be static or dynamic – dynamic scoping is easy to implement, but more confusing for most programmers – most modern languages use static scoping UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Variable Type • type is the range of values a variable can take, together

Variable Type • type is the range of values a variable can take, together with operations to create, access, and modify values • variable type declarations – implicit in FORTRAN, explicit almost in any other language – dynamic binding between variables and types is unusual • APL and SNOBOL 4 have it UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Variable Value • binding of variable and value is dynamic, except for symbolic constants

Variable Value • binding of variable and value is dynamic, except for symbolic constants • Algol v. Pascal: manifest constants • reference (pointer) • access path (chain of pointers) – primary means of accessing anonymous variables • shared objects UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Semantics • (So-called) Static Semantics – Context-Sensitive Grammars • Scope and Type – Attribute

Semantics • (So-called) Static Semantics – Context-Sensitive Grammars • Scope and Type – Attribute Grammars • Operational Semantics • Denotational Semantics • Axiomatic Semantics UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Previewing Postscript In this course, most notes from the instructor are in Postscript format

Previewing Postscript In this course, most notes from the instructor are in Postscript format – Postscript previewers are installed on the departmental Unix machines » “gv is available on all solaris machines” (P. O’Keefe) – and on the College Windows machines » “GSView is located in all of the prototype machines. I copied the shortcuts in cse apps folder” (H. Naik) – a link to obtaining Post. Script previewers is provided on the course web site. It points (indirectly) to http: //www. cs. wisc. edu/~ghost/ UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Static Semantics • Static semantics are used to check type and scope rules •

Static Semantics • Static semantics are used to check type and scope rules • Semantics is a misnomer in this context • We provide only an example – for checking the type rules of the variable and expression in the assignment statement of a simple language – using attribute grammars (which are a formalization of contextual grammars) UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Declarations, Expressions, Commands • A command is executed to update variables and perform I/O

Declarations, Expressions, Commands • A command is executed to update variables and perform I/O • An expression is evaluated to yield a value • A declaration is elaborated (at compile time) to produce bindings. It may also have the side effect of allocating and initializing variables UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering

Axiomatic, Denotational, and Operational Semantics • Axiomatic semantics formalizes language commands by describing how

Axiomatic, Denotational, and Operational Semantics • Axiomatic semantics formalizes language commands by describing how their execution causes a state change. The state is formalized by a first-order logic sentence. The change is formalized by an inference rule • Denotational semantics associates each language command with a function from the state of the program before execution to the state after execution • Operational semantics associates each language command to a sequence of commands in a simple abstract processor UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering