Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Compilers Design

M. T. Bennani
Assistant Professor, FST - El Manar University, LISI-INSAT

-Academic Year 2017-2018-

M. T. Bennani Assistant Professor, FST - El Manar University, LISI-INSAT


Lecture IV. Introduction of the Syntax Analysis
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Outline

I Recall the aim of the syntax analysis

I Context-Free Grammars
I Formal Definition
I Derivation

I Writing a Grammar
I Eliminating Ambiguity
I Elimination of Left Recursion
I Left Factoring

I Generate Abstract Syntax Tree

M. T. Bennani Assistant Professor, FST - El Manar University, LISI-INSAT


Lecture IV. Introduction of the Syntax Analysis
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Syntax analysis objective

3 / 13
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Formal Definition
Context-Free Grammar (Grammar)
It consists of terminals, nonterminals, a start symbol, and
productions.
I Terminals: Synonyms of token names (Basic symbols from
which strings are formed).
I Nonterminals: Syntactic variables that denote sets of strings.
They are a composition of terminals and nonterminals. They
impose a hierarchical structure on the language.
I Start symbol: A nonterminal symbol which denotes the
language generated by the grammar.
I Productions: They specify the manner in which the terminals
and nonterminals can be combined to form strings.

4 / 13
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Formal Definition - Example

The following grammar (G1 ):


I Terminals: +,*,(,),id
E → E+T|T
T → T*F|F I Nonterminals: E,T,F
F → (E) | id I Start symbol: E
Productions: E→E+T, etc.
I
I Nonterminal is also called head of left side of the production
I Sometimes the symbol ::= has been used instead of →
I A body of right side consists of zero or more terminals and
nonterminals

5 / 13
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Derivation
I The construction of a parse tree can be made by taking a
derivational view, in which productions are treated as
rewriting rules
I Beginning with the start symbol, each rewriting step
replaces a nonterminal by the body of one of its
productions
I The derivational view corresponds the top-down
construction of a parse tree

Notes
- leftmost derivations: The leftmost nonterminal in each sentential
is always chosen.
- rightmost derivations: The rightmost nonterminal in each
sentential is always chosen.

6 / 13
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Derivation - Example
I The following grammar (G2 ):
E → E + E | E * E | -E | (E) | id
The string (S1 ): -( id + id ) is a sentence of (G2 ) because
I
there is a derivation:
(D1 ): E ⇒ -E ⇒ -(E) ⇒ -( E + E ) ⇒ -( id + E ) ⇒ -( id + id )
I Generate the parse tree related to D1

Notes
- D1 is a leftmost derivation.
- (D2 ): E ⇒ -E ⇒ -(E) ⇒ -( E + E ) ⇒ -( E + id ) ⇒ -( id + id )
- D2 is a rightmost derivation.
Draw the parse tree related to D2 and compare it to the previous
one.

7 / 13
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Ambiguity
I A grammar that produces more than one parse tree for some
sentence is said to be ambiguous.
I An amibiguous grammar is one that produces more than one
leftmost derivation or more than one rightmost derivation for
the same sentence.
Example
The sentence S2 : id + id * id which belongs to G2 shows that the
grammar is ambiguous.
1. Compute a derivation of the sentence S2 and generate its
related parse tree
2. Compute a different derivation of the sentence S2 and
generate its related parse tree
3. Suggest a new definition of ambiguous grammar

8 / 13
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Eliminating Ambiguity
I Stratification associates to each production at most one
process
I If a grammar recognizes a sentence, only one derivation tree is
generated
Example
The following grammar G3 : R → a | b | ... z | ε | R”|”R | R* | (R)
is ambiguous because a | b* has two different meanings.
Steps
1. Build atomic sentences
2. Combine star’s and atomic sentences
3. Concatenate star’s sentences
4. Union of concatenated expressions

9 / 13
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Eliminating Ambiguity - Example

1. Build atomic sentences


A → a | b | ... | z
A→ε
A → (S)
2. Combine star’s and atomic sentences S → A | S*
3. Concatenate star’s sentences C → S | CS
4. Union of concatenated expressions E → C | E”|”C

Exercise
Eliminating Ambiguity of the grammar G2

10 / 13
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Elimination of Left Recursion

Left recursion
I A grammar is left recursive if it has a nonterminal A such that
there is a derivation A ⇒ Aa for some string a.
I Top-down parsing methods cannot handle left-recurcive
grammars.

Basic Idea
A → βB
A → Aα | β { B → αB|ε

Exercise
Translate G1 to eliminate the recursion.

11 / 13
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Left Factoring
Definition
I Left factoring a grammar is a transformation that is useful for
producing a grammar suitable for predictive, or top-down,
parsing.
I When a choice between two alternative productions is not
clear, we may be able to rewrite the productions to defer the
decision until enough of the input has been seen that we can
make the right choice.
Basic Idea
A → αB|γ
A → αβ1 |αβ2 |γ { B → β1 | β2
Exercise
Left factoring the following grammar G4 :
E → if exp then stmt else stmt | if exp then stmt

12 / 13
Introduction Context-Free Grammars Writing a Grammar Actions to Build AST

Definitions
- Abstract Syntax Tree or (syntax tree), represents the hierarchical
syntactic structure of the source program.
- Syntax tree (Abstract structure) is different from parse tree
(Operational structure)

Example
E → T+E|T I E1.val = T.val + E2.val
E → T I E.val = T.val
T → int I T.val = int.val
T → int * T
T → (E)
I T.val = int.val * T.val
I T.val = E.val

13 / 13

You might also like