CSC376 · 2081 · 5 marks
What is a type expression? List the properties of LL(1) grammar.
What is a type expression? List the properties of LL(1) grammar. [5]
Quick answer
The type of a language construct is denoted by a type expression. A type expression is either a basic type or is formed by applying an operator called a type constructor to other type expressions. The sets of basic types and constructors depend on the language being type-checked.
Type Expression and Properties of LL(1) Grammar
Part 1: Type Expression (2 marks)
Definition
Type Constructors Include:
-
Basic Types
- Primitive types such as
int,float,char,bool, etc. - A special basic type
erroris used to signal type errors.
- Primitive types such as
-
Arrays
- If
Tis a type expression, thenarray(I, T)is a type expression denoting an array with elements of typeTand index setI. - Example:
array(0...99, int)
- If
-
Products
- If
T1andT2are type expressions, then their Cartesian productT1 x T2is a type expression. - Example:
int x int
- If
-
Pointers
- If
Tis a type expression, thenpointer(T)is a type expression denoting the type "pointer to an object of type T".
- If
-
Functions
- A function maps a domain type to a range type, expressed as
T1 -> T2.
- A function maps a domain type to a range type, expressed as
Part 2: Properties of LL(1) Grammar (3 marks)
Definition of LL(1) Grammar
LL(1) grammar is a class of context-free grammar that can be parsed by a non-recursive predictive (top-down) parser using a single lookahead symbol without backtracking. The name LL(1) stands for:
- L - Left to right scanning of input
- L - Leftmost derivation
- 1 - One lookahead symbol
Properties of LL(1) Grammar
A grammar G is said to be LL(1) if and only if the following conditions hold:
-
No Left Recursion
- The grammar must not contain any left-recursive productions.
- Example: A production of the form
A -> Aαis not allowed in LL(1) grammar.
-
No Ambiguity
- The grammar must be unambiguous. An ambiguous grammar cannot be LL(1) because it would lead to multiple entries in the parsing table for the same (non-terminal, terminal) pair.
-
Left Factoring
- For any non-terminal
Awith two or more productions, the productions must not have a common prefix. If they do, left factoring must be applied. - That is, for any two productions
A -> α | β,FIRST(α) ∩ FIRST(β) = ∅.
- For any non-terminal
-
FIRST and FOLLOW Condition
- For each non-terminal
Awith productionsA -> α | β:FIRST(α) ∩ FIRST(β) = ∅- If
α =>* ε, thenFIRST(β) ∩ FOLLOW(A) = ∅
- This ensures that the parsing table has at most one production per cell M[A, a].
- For each non-terminal
-
Deterministic Parsing Table
- The LL(1) parsing table M[A, a] must have at most one entry for every non-terminal
Aand terminala. Multiple entries indicate grammar conflicts and disqualify the grammar from being LL(1).
- The LL(1) parsing table M[A, a] must have at most one entry for every non-terminal
-
Single Lookahead is Sufficient
- Only one input symbol of lookahead is needed at each step to determine the correct production to apply, making parsing efficient and backtrack-free.
Summary: A type expression represents the type of a language construct using basic types and constructors like arrays, products, and functions. LL(1) grammar must be non-left-recursive, unambiguous, left-factored, and must produce a conflict-free parsing table using only one lookahead symbol.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.