Download presentation
Presentation is loading. Please wait.
Published byClarence Kennedy Modified over 9 years ago
1
Chapter 3 Context-Free Grammars and Parsing
2
The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build Syntax Tree (if necessary) Error reporting and recovery
3
Context-Free Grammars Specification of programming language Somewhat like regular expressions Except… –Definitions can be recursive –No meta-symbol for repetition
4
Context-Free Grammar Example exp exp op exp | ( exp ) | num op +|-|*
5
Derivations and Languages Context-free grammar rules determine the set of legal strings of token symbols for the structures defined by the rules. Example: “(34-3)*42” corresponds to (num – num) * num Example: “(34-3*42” is not a legal expression Grammar rules determine the legal strings of token symbols by means of derivations
6
Derivation A derivation is a sequence of replacements of structure names by choices on the right-hand side of grammar rules.
7
Derivation 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]
8
Other Context-Free Grammars E (E) | a E E + a statement if-stmt | other if-stmt if (exp) statement | if (exp) statement else statement exp 0 | 1
9
Right recursive and Left recursive Consider the grammar A A a | a –A Aa Aaa Aaaa … Conversely consider A A a | a –A aA aaA aaaA The first grammar is left recursive The second is right recursive
10
empty ‘ε’ matches the empty string so the regular expression: a* Looks like A Aa | ε What does the following grammar do A (A)A | ε
11
Another Example statement if-stmt | other if-stmt if (exp) statement | if (exp) statement else-part else-part else statement | ε exp 0 | 1
12
Another Example stmt-sequence stmt; stmt-sequence | stmt stmt s stmt-sequence stmt; stmt-sequence | ε stmt s Notice any differences??
13
Parse Trees Consider the string of tokens ( num – num ) * num How many derivations are there?? Parse-Trees show the derivation without worrying about ordering
14
Parse Trees A parse tree corresponding to a derivation –labeled tree –interior nodes are labeled by nonterminals –leaf nodes are labeled by terminals –children of each internal node represent the replacement of the associated nonterminal –one step of the derivation
15
Parse-Tree exp op number + exp exp op exp | ( exp ) | num op +|-|* what is the corresponding derivation?
16
Left-most derivation A left-most derivation is a derivation in which the leftmost nonterminal is replaced at each step in the derivation.
17
Parse-Tree exp op number + exp exp op exp | ( exp ) | num op +|-|* what is the corresponding left-most derivation?
18
Abstract Syntax Trees parse trees contain more information than is absolutely necessary for a compiler to produce executable code A shorthand notation for a parse tree is called an abstract syntax tree
19
Example exp op number + If the string to parse was “3+4” The parse tree The abstract syntax tree tree 34 +
20
Other Examples What about parse trees and abstract syntax trees for the following grammar? statement if-stmt | other if-stmt if (exp) statement | if (exp) statement else statement exp 0 | 1
21
Other Examples What about parse trees and abstract syntax trees for this grammar? stmt-sequence stmt; stmt-sequence | stmt stmt s
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.