+ All Categories
Home > Documents > Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree...

Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree...

Date post: 19-Jan-2016
Category:
Upload: clarence-kennedy
View: 234 times
Download: 2 times
Share this document with a friend
21
Chapter 3 Chapter 3 Context-Free Grammars Context-Free Grammars and Parsing and Parsing
Transcript
Page 1: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Chapter 3Chapter 3

Context-Free Grammars Context-Free Grammars

and Parsingand Parsing

Page 2: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

The Parsing ProcessThe Parsing Process

sequence of tokens syntax treeparser

Duties of parser:

• Determine correct syntax• Build Syntax Tree (if necessary)• Error reporting and recovery

Page 3: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Context-Free GrammarsContext-Free Grammars

Specification of programming Specification of programming languagelanguage

Somewhat like regular expressionsSomewhat like regular expressionsExcept…Except…– Definitions can be recursiveDefinitions can be recursive– No meta-symbol for repetitionNo meta-symbol for repetition

Page 4: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Context-Free GrammarContext-Free GrammarExampleExample

exp exp op exp | ( exp ) | num

op +|-|*

Page 5: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Derivations and LanguagesDerivations and LanguagesContext-free grammar rules determine the set of Context-free grammar rules determine the set of legal strings of token symbols for the structures legal strings of token symbols for the structures defined by the rules.defined by the rules.

Example: “(34-3)*42” corresponds toExample: “(34-3)*42” corresponds to(num – num) * num(num – num) * num

Example: “(34-3*42” is not a legal expressionExample: “(34-3*42” is not a legal expression

Grammar rules determine the legal strings of Grammar rules determine the legal strings of token symbols by means of token symbols by means of derivationsderivations

Page 6: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

DerivationDerivationA A derivationderivation is a sequence of replacements of is a sequence of replacements of structure names by choices on the right-hand structure names by choices on the right-hand side of grammar rules.side of grammar rules.

Page 7: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Derivation ExampleDerivation Example

(1) exp exp op exp [exp exp op exp](2) exp op num [exp num](3) exp * num [op *](4) (exp) * num [exp (exp)](5) (exp op exp) * num [exp exp op exp](6) (exp op num) * num [exp num](7) (exp – num) * num [op -](8) (num – num) * num [exp num]

Page 8: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Other Context-Free GrammarsOther Context-Free GrammarsE E (E) | (E) | aa

E E E E ++ aa

statement statement if-stmt | if-stmt | otherotherif-stmt if-stmt ifif (exp) statement | (exp) statement | ifif (exp) statement (exp) statement elseelse statement statementexp exp 00 | | 11

Page 9: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Right recursive and Left recursiveRight recursive and Left recursiveConsider the grammarConsider the grammarA A A a | a A a | a– A A Aa Aa Aaa Aaa Aaaa … Aaaa …

Conversely considerConversely considerA A A a | a A a | a– A A aA aA aaA aaA aaaA aaaA

The first grammar is left recursiveThe first grammar is left recursiveThe second is right recursiveThe second is right recursive

Page 10: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

emptyempty

‘‘εε’ matches the empty string’ matches the empty string

so the regular expression: a*so the regular expression: a*Looks like A Looks like A Aa | Aa | εε

What does the following grammar doWhat does the following grammar doA A (A)A | (A)A | εε

Page 11: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Another ExampleAnother Example

statement statement if-stmt | if-stmt | otherotherif-stmt if-stmt ifif (exp) statement | (exp) statement | ifif (exp) statement else-part (exp) statement else-partelse-part else-part elseelse statement | statement | εε exp exp 00 | | 11

Page 12: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Another ExampleAnother Examplestmt-sequence stmt-sequence stmt stmt;; stmt-sequence | stmt stmt-sequence | stmtstmt stmt ss

stmt-sequence stmt-sequence stmt stmt;; stmt-sequence | stmt-sequence | εεstmt stmt ss

Notice any differences??

Page 13: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Parse TreesParse Trees

Consider the string of tokensConsider the string of tokens( num – num ) * num( num – num ) * num

How many derivations are there??How many derivations are there??

Parse-Trees show the derivation Parse-Trees show the derivation without worrying about orderingwithout worrying about ordering

Page 14: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Parse TreesParse TreesA parse tree corresponding to a derivationA parse tree corresponding to a derivation– labeled tree labeled tree – interior nodes are interior nodes are

labeled by nonterminalslabeled by nonterminals

– leaf nodes are leaf nodes are labeled by terminalslabeled by terminals

– children of each internal node children of each internal node represent the replacement of the associated represent the replacement of the associated nonterminalnonterminal

– one step of the derivationone step of the derivation

Page 15: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Parse-TreeParse-Tree

exp

expexp op

number number+

exp exp op exp | ( exp ) | num

op +|-|*

what is thecorrespondingderivation?

Page 16: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Left-most derivationLeft-most derivationA A left-most derivationleft-most derivation is a derivation in which is a derivation in which the leftmost nonterminal is replaced at each step the leftmost nonterminal is replaced at each step in the derivation.in the derivation.

Page 17: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Parse-TreeParse-Tree

exp

expexp op

number number+

exp exp op exp | ( exp ) | num

op +|-|*

what is thecorrespondingleft-mostderivation?

Page 18: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Abstract Syntax TreesAbstract Syntax Trees

parse trees contain more information parse trees contain more information than is absolutely necessary for a than is absolutely necessary for a compiler to produce executable codecompiler to produce executable code

A shorthand notation for a parse tree A shorthand notation for a parse tree is called an is called an abstract syntax treeabstract syntax tree

Page 19: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

ExampleExample

exp

expexp op

number number+

If the string to parse was “3+4”

The parse tree The abstract syntax tree tree

3 4

+

Page 20: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Other ExamplesOther ExamplesWhat about parse trees and abstract syntax trees What about parse trees and abstract syntax trees for the following grammar?for the following grammar?

statement statement if-stmt | if-stmt | otherotherif-stmt if-stmt ifif (exp) statement | (exp) statement | ifif (exp) statement (exp) statement elseelse statement statementexp exp 00 | | 11

Page 21: Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.

Other ExamplesOther ExamplesWhat about parse trees and abstract syntax trees What about parse trees and abstract syntax trees for this grammar?for this grammar?

stmt-sequence stmt-sequence stmt stmt;; stmt-sequence | stmt stmt-sequence | stmtstmt stmt ss


Recommended