CSC 396 · Automata theory, Computability and Formal Languages
Our notes
2 min readSyntax 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:
| Binding | Time | Example |
|---|---|---|
| meaning of * | language design | multiplication |
| size of int | language implementation | 32 bits |
| type of a variable | compile time (static) or run time (dynamic) | Pascal: compile; Python: run |
| address of a local | run time (when the procedure is called) | stack allocation |
| value | run time | assignment |
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.