Syntax Analysis
Syntax Analysis
[SWE-310]
Syntax Analysis
Compiler Construction [SWE-310] Syntax Analysis
Syntax Analysis
<expression> (<expression> + id ) * id
<expression> ( id + id ) * id
Compiler Construction [SWE-310] Syntax Analysis
Types of Grammars
Level Grammar
3 Regular
2 Context-Free
1 Context-Sensitive
0 Unrestricted
Compiler Construction [SWE-310] Syntax Analysis
Context-Free Grammar (CFG)
1. T: Set of Terminals
Terminals are the basic symbols from which
strings are formed.
The word “token" is a synonym for
“terminal“.
E.g. if, then, else, *, ;
Compiler Construction [SWE-310] Syntax Analysis
Context-Free Grammar (CFG)
2. N: Set of Non-terminals
Non-terminals are syntactic variables that
denote sets of strings.
impose a hierarchal structure on the
language.
Compiler Construction [SWE-310] Syntax Analysis
Context-Free Grammar (CFG)
3. S: Start Symbol
One non-terminals is distinguished as start
symbol.
Compiler Construction [SWE-310] Syntax Analysis
Context-Free Grammar (CFG)
4. P: Productions
Productions specify the manner in which the terminals and non-
terminals can be combined to form strings.
Each production consists of:
(a) A nonterminal called the head or left side of the production; this
production defines some of the strings denoted by the head.
(b) The symbol . Sometimes ::= has been used in place of the
arrow.
(c) A body or right side consisting of zero or more terminals and non-
terminals. The components of the body describe one way in which
strings of the nonterminal at the head can be constructed.
Compiler Construction [SWE-310] Syntax Analysis
Context-Free Grammar (CFG)
E.g. The CFG of simple arithmetic expression can be
defined by following productions:
expr expr op term
expr ( expr )
expr - expr
expr id
Op +
Op -
Op *
Op /
Op ↑
Compiler Construction [SWE-310] Syntax Analysis
Notational Conventions for CFG
1. Terminals:
Compiler Construction [SWE-310] Syntax Analysis
Notational Conventions for CFG
2. Non-terminals:
Compiler Construction [SWE-310] Syntax Analysis
Notational Conventions for CFG
4. Strings of Terminals:
Compiler Construction [SWE-310] Syntax Analysis
Notational Conventions for CFG
1. Derivation:
Production is a re-writing rule in which the
non-terminal on the left is replaced by the
strings on the right side of productions.
E.g. E - E, states that E with a minus sign
is also an expression. OR
E ==> - E, implies E derives – E
Also,
E ==> - E ==> - ( E ) ==> - ( id )
Compiler Construction [SWE-310] Syntax Analysis
Derivation
2. Parse Trees:
A graphical representation for a derivation that
filters out the choice regarding replacement order
of non-terminals.
Each interior node of a parse tree represents the
application of a production.
The interior node is labeled with the non-terminal
in the head of the production.
The children of the node are labeled, from left to
right, by the symbols in the body of the
production by which the non-terminal are
replaced during the derivation.
Compiler Construction [SWE-310] Syntax Analysis
Parse Trees
E.g. Construct parse tree for the following derivation:
E ==> - E ==> - ( E ) ==> - ( E+ E ) ==> - (id + E ) ==> - ( id + id )
Compiler Construction [SWE-310] Syntax Analysis
Parse Trees
a+(b*c) (a+b)*c
Which of the above parse tree is right?
Compiler Construction [SWE-310] Syntax Analysis
Parse Tree Examples
id * num + id – num / id
Compiler Construction [SWE-310] Syntax Analysis
Parse Tree Examples
OR
Parse Tree#2
Compiler Construction [SWE-310] Syntax Analysis
Eliminating Ambiguity