Docsity

Deriving Parse Trees: Top-Down and Bottom-Up Approaches - Prof. Aaron Block, Study notes of Programming Languages

Toggle sidebar
bg1
The University of North Carolina at Chapel Hill
Lecture 4: Syntactic Analysis
COMP 524 Programming Language Concepts
Stephen Olivier
January 27, 2009
Based on notes by N. Fisher, F. Hernandez-Campos, and D. Stotts
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f

Partial preview of the text

Download Deriving Parse Trees: Top-Down and Bottom-Up Approaches - Prof. Aaron Block and more Study notes Programming Languages in PDF only on Docsity!

Lecture 4: Syntactic Analysis

COMP 524 Programming Language Concepts Stephen Olivier January 27, 2009

Based on notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

Goal of Lecture

Scanner (lexical analysis) Parser (syntax analysis) intermediate code gen.^ Semantic analysis & Machine-independent optimization (optional) Target code generation. optimization (optional)^ Machine-specific

Symbol Table

Character Stream Token Stream Parse Tree Abstract syntax tree Modified intermediate form Machine language Modified target language

This includes context-free grammar.

Review: Regular Expression Rules

• A RE consist of:

• A character (e.g., “0”, “1”, ...)

• The empty string (i.e., “ε”)

• Two REs next to each other (e.g., “ denote concatenation. non_negative_digit digit ”) to

• Two REs separated by “|” next to each other (e.g., “ non_negative_digit | digit ”) to denote one RE or the other.

• An RE followed by “*” (called the or more iterations of the RE. Kleene star ) to denote zero

• Parentheses (in order to avoid ambiguity).

Review: Regular Expression Rules

• A RE consist of:

• A character (e.g., “0”, “1”, ...)

• The empty string (i.e., “ε”)

• Two REs next to each other (e.g., “ denote concatenation. non_negative_digit digit ”) to

• Two REs separated by “|” next to each other (e.g., “ non_negative_digit | digit ”) to denote one RE or the other.

• An RE followed by “*” (called the or more iterations of the RE. Kleene star ) to denote zero

• Parentheses (in order to avoid ambiguity).

A RE is Thus, REs cannot NEVER defined in terms of itself! define recursive

statements.

Context-Free Grammars

• Context-Free Grammars except that they can handle ( CFGs recursion ) are similar to REs.

expr → id | number | - expr | ( expr ) | expr op expr op → + | - | * | /

Arithmetic expression with parentheses

Context-Free Grammars

• Context-Free Grammars except that they can handle ( CFGs recursion ) are similar to REs.

expr → id | number | - expr | ( expr ) | expr op expr op → + | - | * | /

Arithmetic expression with parentheses

Each rule is called a production.

Context-Free Grammars

• Context-Free Grammars except that they can handle ( CFGs recursion ) are similar to REs.

expr → id | number | - expr | ( expr ) | expr op expr op → + | - | * | /

Arithmetic expression with parentheses

Non-terminals the CFG and can appear on both the are symbols that are defined by left and

right side of “→”.

Context-Free Grammars

• Context-Free Grammars except that they can handle ( CFGs recursion ) are similar to REs.

expr → id | number | - expr | ( expr ) | expr op expr op → + | - | * | /

Arithmetic expression with parentheses

In the book, non-terminals are written in but in other literature they are written in italics ,

.

Context-Free Grammars

• Context-Free Grammars except that they can handle ( CFGs recursion ) are similar to REs.

expr → id | number | - expr | ( expr ) | expr op expr op → + | - | * | /

Arithmetic expression with parentheses

typewriter In the book non-terminals are written in font, others write it in “normal”

font.

BNF

• Technically, the Kleene star (*) and parentheses are not allowed under the CFG rules, called Backus-Naur Form

( BNF ).

• However, for convenience, we will use Extended BNF that includes the Kleene star, parentheses, and the

Kleene Plus iterations.” (^ + ), which stands for “one or more

Derivation

• A derive a string of terminals from the start symbol.” derivation is “a series of replacement operations that

expr expr op expr expr op id expr + id

expr op expr + id expr op id + id expr * id + id id * id + id (slope) * (x) + (intercept)

slope * x + intercept

Derivation

• A derive a string of terminals from the start symbol.” derivation is “a series of replacement operations that

expr expr op expr expr op id expr + id

expr op expr + id expr op id + id expr * id + id id * id + id (slope) * (x) + (intercept)

slope * x + intercept

denotes “derived from”

Parse Tree

• A parse tree is the graphical representation of the derivation.

expr expr op expr expr op expr id (slope) * id (x)

  • id (intercept)

Parse Tree

• A parse tree is the graphical representation of the derivation.

expr expr op expr expr op expr id (slope) * id (x)

  • id (intercept)

This parse tree constructs the formula (slope*x) + intercept