Formal Grammars Why Study Grammars? What’s a Grammar? August 24, 2014 Parsing Brian A. Malloy Classes of . . . Regular Grammars ◭◭ ◮◮ ◭ ◮ Slide 1 of 14 Go Back Full Screen Quit
1. Why Study Grammars? Why Study Grammars? • Grammars started when Noam Chomsky, What’s a Grammar? early 1950’s, attempted to provide a pre- Parsing cise specification for the structure of natu- Classes of . . . ral language. Regular Grammars • Chomsky wanted to specify the syntax of ◭◭ ◮◮ language using precise math rules. ◭ ◮ • Chomsky’s work influenced others to study properties of strings. Slide 2 of 14 • With computers, people learned that all Go Back forms of information can be represeneted as strings: numbers, name, pictures, sound, Full Screen . . . Quit
• The collection of strings, known as a lan- guage, became central to computer science. Why Study Grammars? What’s a Grammar? • The syntax of programming languages Parsing can be specified by a grammar. Classes of . . . • Fractals and L-Systems can be specified or Regular Grammars generated with a grammar: ◭◭ ◮◮ ◭ ◮ Slide 3 of 14 Go Back Full Screen Quit
2. What’s a Grammar? Why Study Grammars? • Def: Formally, a grammar G is a four tu- What’s a Grammar? ple (N, T, S, P) where N & T are disjount Parsing sets of symbols known as non-terminals and Classes of . . . terminals , S ∈ N is the start symbol , and P Regular Grammars is a relation on N ∪ T of production rules . • N : non-terminals are generally represented ◭◭ ◮◮ as cap letters, and do not appear in the language; they are used to derive sentences ◭ ◮ in the language. Slide 4 of 14 • T : terminals are symbols in the language • S is one of the non-terminals that indicates Go Back where to start when deriving a sentence in the language. Full Screen • P : rules used to derive a sentence. Quit
3. Parsing Why Study Grammars? • parsing is the process of recognizing a string What’s a Grammar? in the language. Parsing Classes of . . . • This is accomplished by breaking the string Regular Grammars into symbols and analyzing each symbol against the grammar of the language. ◭◭ ◮◮ • Most languages have their strings struc- ◭ ◮ tured according to the syntax specified by the grammar. Slide 5 of 14 • a parse tree is a step-by-step illustration of a Go Back derivation of a sentence using the grammar. Full Screen Quit
3.1. How it works Why Study Grammars? • Start with start symbol What’s a Grammar? • Try to regenerate the sentence by applying Parsing productions. Classes of . . . • Determine production by looking at next Regular Grammars terminal in sentence. ◭◭ ◮◮ ◭ ◮ Slide 6 of 14 Go Back Full Screen Quit
3.2. Intuition Why Study Grammars? • Try to find a leftmost derivation, What’s a Grammar? • by searching for parse trees, Parsing Classes of . . . • using a top-down expansion of the gram- Regular Grammars mar rules! ◭◭ ◮◮ • Tokens are consumed from left to right. ◭ ◮ Slide 7 of 14 Go Back Full Screen Quit
3.3. Example: Expression Grammar Why Study Grammars? Derive 3 + 2 * 7: What’s a Grammar? expr : expr ’+’ expr Parsing | expr ’*’ expr Classes of . . . | DIGIT Regular Grammars ◭◭ ◮◮ ◭ ◮ Slide 8 of 14 Go Back Full Screen Quit
3.4. Example: Second Derivation Why Study Grammars? Derive 3 + 2 * 7: What’s a Grammar? • Which one is correct? Parsing • If there are two different parse trees for the Classes of . . . same grammar, what does this mean? Regular Grammars ◭◭ ◮◮ ◭ ◮ Slide 9 of 14 Go Back Full Screen Quit
4. Classes of Grammars: Why Study Grammars? The Chomsky Hierarchy What’s a Grammar? Parsing Classes of . . . Regular Grammars ◭◭ ◮◮ ◭ ◮ • A grammar defines or generates a language • A grammar enables the use of a computer Slide 10 of 14 to systematically model a language • A language is an infinite set of strings or Go Back symbols Full Screen Quit
4.1. The Hierarchy Why Study Grammars? Grammar Language Machine Rules What’s a Grammar? Type 0 RE Turing Machine α → β Parsing Type 1 CSG LBA αAβ → αγβ Classes of . . . Type 2 CFG NPDA A → γ Regular Grammars Type 3 Regular FSA A → aB ◭◭ ◮◮ • We’re interested in Regular and CFGs • Regular grammars can specify tokens ◭ ◮ • CFGs can specify language constructs, or syntactic categories Slide 11 of 14 Go Back Full Screen Quit
4.2. Set Inclusion Why Study Grammars? What’s a Grammar? Parsing Classes of . . . Regular Grammars ◭◭ ◮◮ ◭ ◮ Slide 12 of 14 Go Back Full Screen Quit
5. Regular Grammars Why Study Grammars? • Can specify terminals , or tokens in the lan- What’s a Grammar? guage: Parsing – Reserved or keywords: if , while , . . . Classes of . . . – Constants: 3 . 5, 75 Regular Grammars – Special symbols: ( ; :? . . . ◭◭ ◮◮ • Operators: – + one or more repetitions ◭ ◮ – ∗ zero or more repetitions Slide 13 of 14 – | or – Parens for grouping Go Back – Concatenation: one char followed by another Full Screen Quit
5.1. Describe these Regular Expressions Why Study Grammars? • 01 ∗ 0 What’s a Grammar? • (0 | 1) ∗ Parsing • 0 + 1 + Classes of . . . • 0 ∗ 1 ∗ Regular Grammars • a + b + • [ A − Z ] ∗ ◭◭ ◮◮ • [0 − 9] + ◭ ◮ • [ A − Za − z ][ A − Za − z 0 − 9] ∗ Slide 14 of 14 Go Back Full Screen Quit
Recommend
More recommend