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

Preview:

Citation preview

Chapter 3Chapter 3

Context-Free Grammars Context-Free Grammars

and Parsingand Parsing

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

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

Context-Free GrammarContext-Free GrammarExampleExample

exp exp op exp | ( exp ) | num

op +|-|*

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

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.

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]

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

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

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 | εε

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

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??

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

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

Parse-TreeParse-Tree

exp

expexp op

number number+

exp exp op exp | ( exp ) | num

op +|-|*

what is thecorrespondingderivation?

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.

Parse-TreeParse-Tree

exp

expexp op

number number+

exp exp op exp | ( exp ) | num

op +|-|*

what is thecorrespondingleft-mostderivation?

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

ExampleExample

exp

expexp op

number number+

If the string to parse was “3+4”

The parse tree The abstract syntax tree tree

3 4

+

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

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