150 likes | 160 Views
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics. Fall 2005 Marco Valtorta mgv@cse.sc.edu. Syntax and Semantics. Syntax is the set of rules that specify the composition of programs from letters, digits and other characters.
E N D
CSCE 330Programming Language StructuresCh.2: Syntax and Semantics Fall 2005 Marco Valtorta mgv@cse.sc.edu
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
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)
BNF History In Java: <IfThenStat> ::= if (<Expr>) <Stat> <IfThenElseStat> ::= if (<Expr>) <StatNoShortIf> else <Stat>
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
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
Variables • Name • Scope • Lifetime • Value • l-value (memory location) and r-value (contents of a memory location) • Type
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
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
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
Semantics • (So-called) Static Semantics • Context-Sensitive Grammars • Scope and Type • Attribute Grammars • Operational Semantics • Denotational Semantics • Axiomatic Semantics
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/
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)
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
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