0% found this document useful (0 votes)
24 views24 pages

Lecture 4 - Context-Free Grammars

cfg

Uploaded by

prantostart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views24 pages

Lecture 4 - Context-Free Grammars

cfg

Uploaded by

prantostart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Context-Free Grammars

Lecture 4
Sections 4.1 - 4.2

Rokan Uddin Faruqui

Associate Professor
Dept of Computer Science and Engineering
University of Chittaong, Bangladesh
Email: [email protected]
The Phases of Compilation
Source Program

Scanner (Lexical analysis)


Token
Symbol
Parser (Syntax analysis
Frontend Table
Tree
Semantic analysis
Tree
Intermediate code generation
IR
Machine-independent code optimization
IR
Target code generation
Backend Target Code
Machine-specific code improvement (optional)
Target Code
Rokan Uddin Faruqui CSE, CU Context-Free Grammars 2
Syntax Analysis

position = initial + rate * 60;

id1 = id2 + id3 ∗ num ;

id1 +

id2 *

id3 num

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 3


Outline

1 Context-Free Grammars
Parse Trees
Leftmost and Rightmost Derivations

2 Ambiguity

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 4


Context-Free Grammars

Outline

1 Context-Free Grammars
Parse Trees
Leftmost and Rightmost Derivations

2 Ambiguity

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 5


Context-Free Grammars

How do we specify language syntax?

Context-Free Grammars,
uses special notation (BNF – Backus Naur Form),
consist of set of rules (productions).
Example:

if (x=2) print("yep"); else print("nope");

Corresponds to a rule:

stm → if (expr) stm else stm

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 6


Context-Free Grammars

Context-Free Grammars

Definition (Context-free grammar)


A context-free grammar, or CFG, consists of set terminals, a set of
nonterminals, a start symbol, and a set of production.
The terminals are the tokens.
The nonterminals will eventually be “replaced” by terminals
(tokens), matching a grammatical pattern.
The start symbol is a nonterminal.
For each production,
The left side, or head, is a nonterminal.
The right side, body, is any string of terminals and nonterminals,
including the empty string.

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 7


Context-Free Grammars

Example
Example (Context-free grammar)
Let the terminals be the tokens

{+, *, (, ), id}.

Let the nonterminals be {E}, which is also the start symbol.


Let the productions be

E→E + E
E→E ∗ E
E→(E)
E → id

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 8


Context-Free Grammars

Example

Context-free grammars
We can use the grammar to derive strings of terminals if we
Begin with the start symbol.
Repeatedly replace a nonterminal with the body of a production of
which that nonterminal is the head, until the resulting string
consists only of terminals.

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 9


Context-Free Grammars

Example

Example (Context-free grammar)


Use the grammar of the example to derive the string

(id + id) * id.

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 10


Context-Free Grammars

Example

Example (Context-free grammar)


We typically group together all the productions for a single
nonterminal.

E → E + E | E ∗ E | ( E ) | id

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 11


Context-Free Grammars Parse Trees

Outline

1 Context-Free Grammars
Parse Trees
Leftmost and Rightmost Derivations

2 Ambiguity

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 12


Context-Free Grammars Parse Trees

Parse Trees

Definition (Parse Tree)


A parse tree is a tree structure that represents a derivation.
The root node is the start symbol.
Every interior node is a nonterminal.
Every leaf node is a terminal.
The children of each nonterminal node are the nonterminals and
terminals of the body of a production for that nonterminal.

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 13


Context-Free Grammars Parse Trees

Example

Example (Parse Tree)


Draw a parse tree for the derivation of the string

(id + id) * id.

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 14


Context-Free Grammars Parse Trees

Example

Example (Parse Tree)

E * E

( E ) id

E + E

id id

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 15


Context-Free Grammars Parse Trees

Example

Example (Parse Tree)


The parse tree is not the same as the syntax tree, which we will study
later.

+ id

id id

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 16


Context-Free Grammars Leftmost and Rightmost Derivations

Outline

1 Context-Free Grammars
Parse Trees
Leftmost and Rightmost Derivations

2 Ambiguity

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 17


Context-Free Grammars Leftmost and Rightmost Derivations

Leftmost and Rightmost Derivations

Definition (Leftmost derivation)


A leftmost derivation of a string is a derivation in which, at each step,
the leftmost nonterminal is replaced with a string.

Definition (Rightmost derivation)


A rightmost derivation of a string is a derivation in which, at each
step, the rightmost nonterminal is replaced with a string.

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 18


Context-Free Grammars Leftmost and Rightmost Derivations

Example

Example (Leftmost and rightmost derivations)


Using the grammar

E → E + E | E ∗ E | ( E ) | id

find leftmost and rightmost derivations of (id+id)*id.

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 19


Ambiguity

Outline

1 Context-Free Grammars
Parse Trees
Leftmost and Rightmost Derivations

2 Ambiguity

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 20


Ambiguity

Ambiguity

Some grammars provide more than one way to derive a string.


For example, id+id*id can be derived in two different ways using
the grammar rules

E → E + E | E ∗ E | ( E ) | id

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 21


Ambiguity

Ambiguity

Definition (Ambiguous grammar)


A grammar is ambiguous if its language contains a string that has
more than one leftmost derivation under that grammar.

Definition (Inherently ambiguous language)


A language is inherently ambiguous if every grammar for that language
is ambiguous.

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 22


Ambiguity

Example

Example (Unambiguous Grammar)


The same language can be derived unambiguously from the following
grammar.

E→E+T |T
T →T *F |F
F → ( E ) | id

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 23


Ambiguity

Example

Example (Unambiguous Grammar)


Using the grammar

E→E+T |T
T →T *F |F
F → ( E ) | id

Find a leftmost derivation of id+id*id.


Find a leftmost derivation of id*id+id.
Draw the parse trees.

Rokan Uddin Faruqui CSE, CU Context-Free Grammars 24

You might also like