UNIHIVE

Study Hub UNIPORT CSC 396

CSC 396 · Automata theory, Computability and Formal Languages

Syntax, semantics, binding and scope

Describing syntax with BNF, kinds of semantics, variables and binding times, static and dynamic scope, blocks and parameter passing.

Our notes

2 min read

Syntax and semantics

  • Syntax: the form of programs (which strings are valid). Described by grammars.
  • Semantics: the meaning of valid programs. Described informally (manuals) or formally: operational (what an abstract machine does), denotational (mathematical functions) or axiomatic (pre- and postconditions).

BNF

Backus-Naur Form (used for ALGOL 60, 1960) writes grammar rules:
<assign> ::= <id> := <expr>
<expr> ::= <expr> + <term> | <term>
<term> ::= <term> * <factor> | <factor>
<factor> ::= ( <expr> ) | <id>
<id> ::= a | b | c
Non-terminals are in angle brackets; | separates alternatives. EBNF adds [ ] for optional and { } for repetition.

Variables and their attributes

A variable has a name, address, value, type, lifetime and scope.

Binding

A binding links an attribute to an entity; the binding time is when it happens:

BindingTimeExample
meaning of *language designmultiplication
size of intlanguage implementation32 bits
type of a variablecompile time (static) or run time (dynamic)Pascal: compile; Python: run
address of a localrun time (when the procedure is called)stack allocation
valuerun timeassignment

Static typing finds errors early; dynamic typing is more flexible.

Lifetime and storage

  • Static variables exist for the whole run.
  • Stack-dynamic (locals) exist while their procedure runs.
  • Heap-dynamic are created explicitly (new) and freed (dispose) or collected automatically (garbage collection).

Scope

The part of the program where a name can be used.

  • Static (lexical) scope: decided by the program text: look in the current block, then the block around it.
  • Dynamic scope: decided by the calling sequence at run time.

Example: x = 1 globally; procedure P prints x; procedure Q declares its own x = 2 and calls P. Static scope prints 1; dynamic scope prints 2. Most languages (Pascal, C, Java, Python) use static scope.

Parameter passing

  • By value: a copy (Pascal default, C).
  • By reference: the caller's variable itself (Pascal var, C++ &).
  • By value-result: copy in, copy back out at the end.
  • By name: re-evaluate the argument each time it is used (ALGOL 60).

Exams usually ask

  • Distinguish syntax from semantics and write BNF rules.
  • Explain binding and binding times with examples.
  • Compare static and dynamic scope with a traced example; describe parameter-passing methods.

Common mistake

Thinking a program with correct syntax is correct. Syntax only says it is well formed; semantics decides what it does.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

No reading links here yet

Our team adds free books, videos and practice for each topic, and opens every link before it goes up.

All CSC 396 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.