Syntax and Semantics

Introduction

Who must use PL definitions ?

Syntax is the form or structure of the expressions, statements and program units.

Semantics the meaning of the expressions, statements and program units.

 

Languages - terminology

Example: index = 2 * count + 17; (sentence)

lexeme token
index identifier
= equal_sign
2 int_literal
* mult_op
count identifier
+ plus_op
17 int_literal
; semicolon

Languages - formal definition

There are two formal ways to define languages, recognizer and generator.

We will be concerned with generators.

Formal Methods of Specifying Language Generators or Grammars

 

Context-Free Grammar

Formally, a language L is defined in terms of a quadruple L(T,N,P,S) where:

L(T,N,P,S) is then the set of all sequences of terminal symbols, νi , which can be generated from S according to a specific set of rules.

Mathematically:    L =  {ν : S →  ν and ν Є T* }

At about the same time that BNF was being devised, the linguist Noam Chomsky was developing the theory of grammars. One special type of grammar he identified is called a  context-free grammar, or cfg.

A language is said to be context-free iff it can be defined in terms of a context-free set of productions. The productions are themselves context free iff each LHS consists of a single nonterminal symbol, X, which can be replaced by the RHS, regardless of the symbols which immediately precede or follow X.

It turns out that this concept is equivalent to languages defined by BNF.

 

Backus-Naur Form (BNF)

BNF is an example of a metalanguage, i.e. a language used to describe another language
 

As discussed earlier, BNF defines a Language, L in terms of a quadruple:

Each production in R has the following form:

In BNF, these non-terminals are abstractions used to represent classes of syntactic structures
and act like like syntactic variables.

Simple BNF Example 1 - Definition of a binary number

Simple BNF Example 2 - Grammar for a Small Language



Uses of BNF

Recognition Using BNF

 

Derivation

Parse Tree

saa

 

Extended Backus-Naur Form  (EBNF)

 

 

Syntax Diagrams