Transcript
Page 1: COP 3402 System Software

COP 3402 System Software

Recitation Notes21st September 2007

Page 2: COP 3402 System Software

Limits Of Regular Expressions

• Regular expressions are very useful for recognizing patterns

• However, there are some patterns they cannot handle.

• An Example is strings of the form anbn

Page 3: COP 3402 System Software

Context Free Languages

• Since regular expressions cannot handle some important patterns, we need a more powerful method of describing patterns.

• Context Free Languages are one such mechanism

• Proposed by Noam Chomsky in the 1950s.

Page 4: COP 3402 System Software

BNF

• BNF is a way to express Context free languages.

• First used in ALGOL 58. • BNF stands for Backus-Naur Form• Named after two members of the ALGOL

design team.

Page 5: COP 3402 System Software

BNF Example for PL/0

• Next slide contains a BNF description that can verify the correctness of any PL/0 program.

• Conventions:– Variables/Tokens are written between “<“ and

”>”.– The “→” symbol can be read as “is defined as”.– “ε “ is the empty string.

Page 6: COP 3402 System Software

BNF Grammer for PL/0

Page 7: COP 3402 System Software

Sample PL/0 Programme• const m = 7, n = 85; • var i,x,y,z,q,r; • procedure mult; • var a, b; • begin • a := x; b := y; z := 0; • while b > 0 do • begin • if odd x then z := z+a; • a := 2*a; • b := b/2; • end • end;• begin• x := m;• y := n;• call mult;• end.

Page 8: COP 3402 System Software

Syntax Graphs

Visual Way to represent BNF Grammars

Page 9: COP 3402 System Software

Syntax Graphs Cont’d

Page 10: COP 3402 System Software

Extended BNF

• Conventions– Optional Constructions are enclosed in square

braces ‘[‘ and ‘]’.– Constructs enclosed in curly braces ‘{‘ and ‘}’ are

repeated zero or more times.

• Example on next slide

Page 11: COP 3402 System Software

EBNF Example for PL/0

Page 12: COP 3402 System Software

Another Example

• Try to write a BNF Grammar that validates strings of the type anbn.

• Hint: anbn can be rewritten as aan-1bn-1b.

Page 13: COP 3402 System Software

Solution

• <string> → a<string>b | ε• Will ensure that – string always has the same number of a’s and b’s– The a’s always come before the b’s in the string.

• As to keeping track of the ‘n’, we don’t care in this example because the number of a’s and b’s will always be matched.s


Top Related