1 / 15

Syntax and Semantics in Programming: Understanding Rules and Outcomes

Explore syntax and semantics in programming languages, dive into binding concepts, variables, and semantics types. This guide covers static semantics, scope, type rules, and language constructs with examples. Learn about axiomatic, denotational, and operational semantics methods.

jlyla
Download Presentation

Syntax and Semantics in Programming: Understanding Rules and Outcomes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCE 330Programming Language StructuresCh.2: Syntax and Semantics Fall 2005 Marco Valtorta mgv@cse.sc.edu

  2. 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. 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. BNF History In Java: <IfThenStat> ::= if (<Expr>) <Stat> <IfThenElseStat> ::= if (<Expr>) <StatNoShortIf> else <Stat>

  5. 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. 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. Variables • Name • Scope • Lifetime • Value • l-value (memory location) and r-value (contents of a memory location) • Type

  8. 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. 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. 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. Semantics • (So-called) Static Semantics • Context-Sensitive Grammars • Scope and Type • Attribute Grammars • Operational Semantics • Denotational Semantics • Axiomatic Semantics

  12. 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. 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. 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. 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

More Related