Docsity

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

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