Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.

Similar presentations


Presentation on theme: "UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005."— Presentation transcript:

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

2 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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

3 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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)

4 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering BNF History In Java: ::= if ( ) else

5 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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

6 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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

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

8 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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

9 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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 SNOBOL4 have it

10 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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

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

12 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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 PostScript previewers is provided on the course web site. It points (indirectly) to http://www.cs.wisc.edu/~ghost/

13 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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)

14 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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

15 UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering 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


Download ppt "UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005."

Similar presentations


Ads by Google