2081 12

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]

Compiler Design and ConstructionChapter 2: Lexical, Syntax and Semantic AnalysisTop-down parsing

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:

  1. Basic Types

    • Primitive types such as int, float, char, bool, etc.
    • A special basic type error is used to signal type errors.
  2. Arrays

    • If T is a type expression, then array(I, T) is a type expression denoting an array with elements of type T and index set I.
    • Example: array(0...99, int)
  3. Products

    • If T1 and T2 are type expressions, then their Cartesian product T1 x T2 is a type expression.
    • Example: int x int
  4. Pointers

    • If T is a type expression, then pointer(T) is a type expression denoting the type "pointer to an object of type T".
  5. Functions

    • A function maps a domain type to a range type, expressed as T1 -> T2.

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:

  1. 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.
  2. 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.
  3. Left Factoring

    • For any non-terminal A with 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(β) = ∅.
  4. FIRST and FOLLOW Condition

    • For each non-terminal A with productions A -> α | β:
      • FIRST(α) ∩ FIRST(β) = ∅
      • If α =>* ε, then FIRST(β) ∩ FOLLOW(A) = ∅
    • This ensures that the parsing table has at most one production per cell M[A, a].
  5. Deterministic Parsing Table

    • The LL(1) parsing table M[A, a] must have at most one entry for every non-terminal A and terminal a. Multiple entries indicate grammar conflicts and disqualify the grammar from being LL(1).
  6. 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.