Docsity

Syntax Analysis - Programming Language Translation - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Syntax Analysis - Programming Language Translation - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Chapter 4: Syntax Analysis

Part 1: Grammar Concepts

Syntax Analysis - Parsing

 An overview of parsing : Functions &

Responsibilities

 Context Free Grammars: Concepts & Terminology

 Writing and Designing Grammars

 Resolving Grammar Problems / Difficulties

 Top-Down Parsing

 Recursive Descent & Predictive LL

 Bottom-Up Parsing

 LR & LALR

 Key Issues in Error Handling

 Concluding Remarks/Looking Ahead

Parsing During Compilation

interm repres

errors

lexical analyzer parser^

rest of front end

symbol table

source program

parse get next tree token

token

regular expressions

  • also technically part or parsing
  • includes augmenting info on tokens in source, type checking, semantic analysis
  • uses a grammar to check structure of tokens
  • produces a parse tree
  • syntactic errors and recovery
  • recognize correct syntax
  • report errors

Parsing Responsibilities

Syntax Error Identification / Handling

Recall typical error types:

Lexical : Misspellings Syntactic : Omission, wrong order of tokens Semantic : Incompatible types Logical : Infinite loop / recursive call

Majority of error processing occurs during syntax analysis

NOTE: Not all errors are identifiable !! Which ones?

What are some Typical Errors?

#include<stdio.h> int f1(int v) { int i,j=0; for (i=1;i<5;i++) { j=v+f2(i) } return j; } int f2(int u) { int j; j=u+f1(uu) return j; } int main() { int i,j=0; for (i=1;i<10;i++) { j=j+iI; printf(“%d\n”,i); printf("%d\n",f1(j)); return 0; }

Which are “easy” to recover from? Which are “hard”?

As reported by MS VC++ 'f2' undefined; assuming extern returning int syntax error : missing ';' before ‘}‘ syntax error : missing ';' before ‘return‘ fatal error : unexpected end of file found

Error Recovery Strategies

Panic Mode – Discard tokens until a “synchro” token is

found ( end, “;”, “}”, etc. ) -- Decision of designer -- Problems: skip input ⇒miss declaration – causing more errors ⇒miss errors in skipped material -- Advantages: simple ⇒suited to 1 error per statement Phase Level – Local correction on input -- “,” ⇒”;” – Delete “,” – insert “;” -- Also decision of designer -- Not suited to all situations -- Used in conjunction with panic mode to allow less input to be skipped

Motivating Grammars

• Regular Expressions

→ Basis of lexical analysis

→ Represent regular languages

• Context Free Grammars

→ Basis of parsing

→ Represent language constructs

→ Characterize context free languages

Reg. Lang. CFLs

EXAMPLE: an^ bn^ , n ≥ 1 : Is it regular?

Context Free Languages

Regular Languages

Token Structure

Scanner Generation

Context Free Languages

Sentence Structure

Parser Generation

Context Free Languages

Reg. Languag es

Context Free Grammars : A First Look

assign_stmt → id := expr ; expr → term operator term term → id term → real term → integer operator → + operator → -

What do “BLUE” symbols represent?

Derivation: A sequence of grammar rule applications and substitutions that transform a starting non-term into a collection of terminals / tokens.

Simply stated: Grammars / production rules allow us to “rewrite” and “identify” correct syntax.

What do “BLACK” symbols represent?

How is Grammar Used?

Given the rules on the previous slide, suppose

id := real + int;

is input. Is it syntactically correct? How do we know?

expr is represented as: expr → term operator term

Is this accurate / complete?

expr → expr operator term

expr → term

How does this affect the derivations which are possible?

Derivation Example

Let’s derive: id := id + real – integer ; assign_stmt assign_stmt → id := expr ; → id := expr ; expr → expr operator term → id := expr operator term; expr → expr operator term → id := expr operator term operator term; expr → term → id := term operator term operator term; term → id → id := id operator term operator term; operator → + → id := id + term operator term; term → real → id := id + real operator term; operator → - → id := id + real - term; term → integer → id := id + real - integer; assign_stmt → id := expr ; expr → expr operator term expr → term term → id term → real term → integer operator → + operator → - Docsity.com

Example Grammar

expr → expr op expr

expr → ( expr )

expr → - expr

expr → id

op → +

op → -

op → __*

op → /

op → ↑

Black : NT Blue : T expr : S 9 Production rules

To simplify / standardize notation, we offer a synopsis of terminology.

Grammar Concepts

A step in a derivation is zero or one action that replaces a NT with the RHS of a production rule. EXAMPLE: E ⇒ -E (the ⇒ means “derives” in one step) using the production rule: E → -E EXAMPLE: E ⇒ E A E ⇒ E * E ⇒ E * ( E ) DEFINITION: ⇒ derives in one step ⇒ derives in ≥ one step ⇒ derives in ≥ zero steps

EXAMPLES: α A β ⇒ α γ β if A→ γ is a production rule

α 1 ⇒ α 2 ⇒… ⇒ αn → α 1 ⇒ αn ; α ⇒ α for all α

If α ⇒ β and β → γ then α ⇒ γ

How does this relate to Languages?

Let G be a CFG with start symbol S. Then S ⇒W (where W has no non-terminals) represents the language generated by G, denoted L(G). So W∈L(G) ⇔ S ⇒W.

W : is a sentence of G

When S ⇒α (and α may have NTs) it is called a sentential form of G.

EXAMPLE: id * id is a sentence Here’s the derivation: E ⇒ E A E ⇒E * E ⇒ id * E ⇒ id * id

E ⇒ id * id

Sentential forms