130 likes | 239 Views
COP 3402 System Software. Recitation Notes 21 st September 2007. 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 a n b n. Context Free Languages.
E N D
COP 3402 System Software Recitation Notes 21st September 2007
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
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.
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.
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.
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.
Syntax Graphs Visual Way to represent BNF Grammars
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
Another Example • Try to write a BNF Grammar that validates strings of the type anbn. • Hint: anbn can be rewritten as aan-1bn-1b.
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