+ All Categories
Home > Documents > Lecture 4. Parsing (syntax analysis) - Iowa State...

Lecture 4. Parsing (syntax analysis) - Iowa State...

Date post: 11-Jul-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
64
Lecture 4. Parsing (syntax analysis) Wei Le 2015.9
Transcript
Page 1: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Lecture 4. Parsing (syntax analysis)

Wei Le

2015.9

Page 2: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Outline

I review: languages and grammars

I introduction of parsing

I CFG: context free grammars

I derivation

I ambiguity

Page 3: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

LanguagesI a theoretical conceptI a set of strings: the set can be infiniteI Chomsky Hierarchy (1956): regular (type 3), context-free (type 2),

context-sensitive (type 1), recursively enumerable (type 0)I Computational complexity: the expressive power of (and the

resources needed in order to process) classes of languagesI The more restricted the rules are, the lower in the hierarchy the

languages they generate are

Page 4: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Grammars

I A specification of languages: syntax

I Grammars are used for compression (biology): learn a grammar froma set of strings

I generators (produce strings) or recognizers (match production rules)

I Compiler: recognizers

I A grammar G is a 4-tuple:I Σ: a set of terminalsI V : a set of non-terminalsI S : start symbolI a set of production rules

I L(G ) represents the language of the grammar

Page 5: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used
Page 6: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

What Can Regular Languages Express?

I Finite number of characters

I Infinite, allow repeated patterns – intuition: finite automaton thatruns long enough must repeat states

I Strings whose patterns related to a fixed number of states

I Finite automaton can’t remember of times it has visited a particularstate

Page 7: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used
Page 8: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Context Free Grammar

I the production does not dependent on the context

I no terminals on the left side

I A can be replaced to α independent of the context where A islocated

Page 9: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Examples

The set of strings is expressed using the following:

I {a∗b∗}

I {anbn : n > 0} (e.g., (())))

I {anbncn : n > 0}

Page 10: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Examples

The set of strings is expressed using the following:

I {a∗b∗}I {anbn : n > 0} (e.g., (())))

I {anbncn : n > 0}

Page 11: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Examples

The set of strings is expressed using the following:

I {a∗b∗}I {anbn : n > 0} (e.g., (())))

I {anbncn : n > 0}

Page 12: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Examples

S → aSbS → ε

Page 13: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

I In every derivation the length of the string never decreases

I The term ”context-sensitive” comes from a normal form for thesegrammars,where each production is of the form α1Aα2 → α1βα2

with β 6= ε

I They permit replacement of variable A by string only in the”context” α1 - α2

Page 14: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used
Page 15: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Chomsky Hierarchy: A Summary

Page 16: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Programming Languages and Natural Languages

I Programming languages:I C: all the programs written in CI modern programming languages: context free + some

context-sensitive features

I Natural language: English is not regular, not context free (how toprove it?)

Page 17: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Parser

Lexer: scanner, lexical analyzer Parser: syntax analyzer

Page 18: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Example

Page 19: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Role of Parsers

Page 20: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Parsing and CFG

I Recognize if a string (program) is a language (yes, no)

I Generate a parse tree from the input

I Report/handle errors

I Form of the grammar is importantI Many grammars generate the same languageI Tools (e.g.,Bison) are sensitive to the grammar

Page 21: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Context Free Grammars

Page 22: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

BNF: Bakus-Naur Form

I late 1950s, early 1960s by John Bakus and Peter Naur

I notation systems for context free grammars

I < SheepNoise >::= baa < SheepNoise > |baa reads ”derives”

I Today, many books use an updated form of BNF: →

Page 23: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Example CFGs

Page 24: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

More Understandings for CFGs

Page 25: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Key Ideas

Page 26: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

More Understandings for CFGs

Page 27: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

More Understandings for CFGs

Page 28: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

The Language of a CFG

Page 29: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Terminals

Page 30: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Examples

Page 31: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

Page 32: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

Page 33: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

Page 34: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

Page 35: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

Page 36: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

Page 37: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

Page 38: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

Page 39: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

I A parse tree hasI Terminals at the leavesI Non-terminals at the interior nodes

I An in-order traversal of the leaves is the original input

I In-order traversal: left root right

I The parse tree shows the associativity of operators, the input stringdoes not

I In programming languages, the associativity (or fixity) of anoperator is a property that determines how operators of the sameprecedence are grouped in the absence of parentheses.

Page 40: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

I A parse tree hasI Terminals at the leavesI Non-terminals at the interior nodes

I An in-order traversal of the leaves is the original input

I In-order traversal: left root right

I The parse tree shows the associativity of operators, the input stringdoes not

I In programming languages, the associativity (or fixity) of anoperator is a property that determines how operators of the sameprecedence are grouped in the absence of parentheses.

Page 41: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

I A parse tree hasI Terminals at the leavesI Non-terminals at the interior nodes

I An in-order traversal of the leaves is the original input

I In-order traversal: left root right

I The parse tree shows the associativity of operators, the input stringdoes not

I In programming languages, the associativity (or fixity) of anoperator is a property that determines how operators of the sameprecedence are grouped in the absence of parentheses.

Page 42: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Derivations

I A parse tree hasI Terminals at the leavesI Non-terminals at the interior nodes

I An in-order traversal of the leaves is the original input

I In-order traversal: left root right

I The parse tree shows the associativity of operators, the input stringdoes not

I In programming languages, the associativity (or fixity) of anoperator is a property that determines how operators of the sameprecedence are grouped in the absence of parentheses.

Page 43: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Leftmost and Rightmost Derivations

Page 44: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Rightmost Derivations

Page 45: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Rightmost Derivations

Page 46: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Rightmost Derivations

Page 47: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Rightmost Derivations

Page 48: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Rightmost Derivations

Page 49: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Rightmost Derivations

Page 50: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Rightmost Derivations

Page 51: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Rightmost Derivations

Page 52: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Rightmost Derivations

Page 53: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Summary of Derivations

Page 54: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Revisiting Parsing

I A parser knows the grammar of the programming language

I The parser finds the derivation of a particular input

Page 55: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Ambiguity

Definition: A context-free grammar G is ambiguous if some string hastwo or more distinct derivation trees

I A language can have many grammars, some grammars may beambiguous

I Some language only can have ambiguous grammar (inherentAmbiguity)

I Ambiguity is bad for programming languages, and we want toremove ambiguity

Page 56: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Ambiguity

Definition: A context-free grammar G is ambiguous if some string hastwo or more distinct derivation trees

I A language can have many grammars, some grammars may beambiguous

I Some language only can have ambiguous grammar (inherentAmbiguity)

I Ambiguity is bad for programming languages, and we want toremove ambiguity

Page 57: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Ambiguity

Definition: A context-free grammar G is ambiguous if some string hastwo or more distinct derivation trees

I A language can have many grammars, some grammars may beambiguous

I Some language only can have ambiguous grammar (inherentAmbiguity)

I Ambiguity is bad for programming languages, and we want toremove ambiguity

Page 58: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Ambiguity: Example

Page 59: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Ambiguity: Example

Page 60: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Dealing with Ambiguity

Page 61: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Dealing with Ambiguity

Page 62: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Dealing with Ambiguity

Page 63: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Associtivity Declaration

Page 64: Lecture 4. Parsing (syntax analysis) - Iowa State Universityweb.cs.iastate.edu/~weile/cs440.540/4.Parsing.part1.pdfGrammars I A speci cation of languages: syntax I Grammars are used

Precedence Declaration


Recommended