210 likes | 379 Views
Chapter 3. Context-Free Grammars and Parsing. parser. syntax tree. sequence of tokens. The Parsing Process. Duties of parser: Determine correct syntax Build Syntax Tree (if necessary) Error reporting and recovery. Context-Free Grammars. Specification of programming language
E N D
Chapter 3 Context-Free Grammars and Parsing
parser syntax tree sequence of tokens The Parsing Process • Duties of parser: • Determine correct syntax • Build Syntax Tree (if necessary) • Error reporting and recovery
Context-Free Grammars • Specification of programming language • Somewhat like regular expressionsExcept… • Definitions can be recursive • No meta-symbol for repetition
Context-Free GrammarExample exp exp op exp | ( exp ) | num op +|-|*
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
Derivation • A derivation is a sequence of replacements of structure names by choices on the right-hand side of grammar rules.
Derivation Example • exp exp op exp [exp exp op exp] • exp op num [exp num] • exp * num [op *] • (exp) * num [exp (exp)] • (exp op exp) * num [exp exp op exp] • (exp op num) * num [exp num] • (exp – num) * num [op -] • (num – num) * num [exp num]
Other Context-Free Grammars • E (E) | a • E E +a • statement if-stmt | otherif-stmt if (exp) statement |if (exp) statement else statementexp 0 | 1
Right recursive and Left recursive • Consider the grammarA A a | a • A Aa Aaa Aaaa … • Conversely considerA A a | a • A aA aaA aaaA • The first grammar is left recursiveThe second is right recursive
empty • ‘ε’ matches the empty string • so the regular expression: a*Looks like A Aa | ε • What does the following grammar doA (A)A | ε
Another Example • statement if-stmt | otherif-stmt if (exp) statement |if (exp) statement else-partelse-part else statement | εexp 0 | 1
Another Example • stmt-sequence stmt; stmt-sequence | stmtstmt s • stmt-sequence stmt; stmt-sequence | εstmt s Notice any differences??
Parse Trees • Consider the string of tokens( num – num ) * num • How many derivations are there?? • Parse-Trees show the derivation without worrying about ordering
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
exp exp exp op number + number Parse-Tree exp exp op exp | ( exp ) | num op +|-|* what is the corresponding derivation?
Left-most derivation • A left-most derivation is a derivation in which the leftmost nonterminal is replaced at each step in the derivation.
exp exp exp op number + number Parse-Tree exp exp op exp | ( exp ) | num op +|-|* what is the corresponding left-most derivation?
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
exp exp exp op number + number Example If the string to parse was “3+4” + 3 4 The parse tree The abstract syntax tree tree
Other Examples • What about parse trees and abstract syntax trees for the following grammar? • statement if-stmt | otherif-stmt if (exp) statement |if (exp) statement else statementexp 0 | 1
Other Examples • What about parse trees and abstract syntax trees for this grammar? • stmt-sequence stmt; stmt-sequence | stmtstmt s