1. Gyanastra
  2. Compiler Design
  3. Syntax Directed Definitions
  4. Semantic Rules and Semantic Actions Associated with Grammars
Free lesson, no account needed 4 min read 15 practice questions after it
Asked in past papers5 times
  • GATE CSE20232022200520041998

Compiler Design · Syntax Directed Definitions

Semantic Rules and Semantic Actions Associated with Grammars

Overview

A plain grammar defines only which strings are valid (syntax).
To give a grammar meaning, informal notations called semantic rules (semantic actions) are attached to its productions, forming a Syntax Directed Definition (SDD) or, when written as executable actions, a Syntax Directed Translation (SDT).
This matters because it lets the parser do far more than syntax checking — it can evaluate expressions, update the symbol table, and generate code while parsing.

Key concepts

  • Semantic rules / semantic actions: Informal notations attached to each grammar production that give the grammar meaning.
    The two names refer to the same idea; when written as executable actions they are called semantic actions.
  • Plain grammar: Defines only the syntax — which strings are valid.
  • Syntax Directed Definition (SDD): A grammar with semantic rules attached to its productions.
  • Syntax Directed Translation (SDT): The form in which the rules are written as executable semantic actions.
  • Parallel processing: Because actions are attached to productions, many tasks can be done in parallel with parsing. A parser with semantic actions is capable of much more than pure syntax checking.
Figure. The tree preserves leaf order 2 + 3 * 4. The multiplication subtree computes 3 * 4 as 12, and the root combines 2 + 12 as 14.

How it works

  1. The grammar productions drive the parse.
  2. Each time a production is applied (or reduced), its associated semantic rule/action fires.
  3. The action computes a value or performs a side effect.
  4. At the same time as parsing, the parser can perform:
    • Syntax analysis itself
    • Code generation
    • Storing values in the symbol table
    • Intermediate code generation
    • Expression evaluation

Worked examples

Expression evaluation. The example grammar evaluates an arithmetic expression; the evaluation of the string 2+3∗42 + 3 * 4 is carried out.

Grammar:

E -> E + T | T
T -> T * F | F
F -> number

Semantic rules are attached to these productions so that when the parse is carried out on the input string, the value of the expression is computed as the output.

Revision summary

  • A grammar alone specifies only syntax; semantic rules are attached to productions to give meaning.
  • Semantic rules are also called semantic actions.
  • With semantic actions, the parser can simultaneously do syntax analysis, symbol table updates, intermediate code generation, code generation, and expression evaluation.
  • The example grammar for expression evaluation is E -> E + T | T, T -> T * F | F, F -> number, with semantic rules attached to each production.
  • A grammar with semantic rules attached is an SDD; with executable actions it is an SDT.

Previous-year exam insights

  • An SDD uses synthesized attributes (computed from children) and/or inherited attributes (computed from parent/siblings); an S-attributed SDD uses only synthesized attributes and can be evaluated bottom-up during parsing, while an L-attributed SDD allows inherited attributes with restricted dependencies and can be evaluated top-down.
    Asked inGATE 2023GATE 2022GATE 2005GATE 2004
  • In an SDT with embedded semantic actions, the order of evaluation of actions is determined by the parse order; for top-down parsing actions are executed when the production is expanded, and for bottom-up parsing when the production is reduced.
    Asked inGATE 2023GATE 2022
  • A syntax-directed translation scheme can be implemented by attaching actions to grammar symbols (e.g., using markers) to enforce evaluation order, especially for inherited attributes or when actions must occur in the middle of a production.
    Asked inGATE 2023GATE 2022
  • Ambiguous grammars with semantic rules can produce different values for the same input string depending on the parse tree; the value is well-defined only if the grammar is unambiguous or the semantic rules are designed to be associative/commutative appropriately.
    Asked inGATE 2005
  • Type checking is a semantic analysis task typically performed during syntax-directed translation, after parsing, using the parse tree and symbol table information.
    Asked inGATE 1998
  • Yacc (or similar parser generators) can detect and resolve shift-reduce and reduce-reduce conflicts using precedence and associativity declarations; it does not automatically eliminate left recursion.
    Asked inGATE 2005
Pages 1–2 of 2