Docsity

Syntax Analysis (Parsing), Summaries of Compilers

Partial preview of the text

Download Syntax Analysis (Parsing) and more Summaries Compilers in PDF only on Docsity!

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

 SLR、LR & LALR

 Concluding Remarks/Looking Ahead

Parsing During Compilation

  • Parser works on a stream of tokens.
  • The smallest item is a token.

regular

expressions

  • also technically part

or parsing

  • includes augmenting

info on tokens in source,

type checking, semantic

analysis

  • uses a grammar (CFG) to

check structure of tokens

  • produces a parse tree
  • syntactic errors and

recovery

  • recognize correct syntax
  • report errors

interm

repres

errors

lexical

analyzer

parser

rest of

front end

symbol

table

source

program

parse

tree get next

token

token

Lexical Analysis Review: NFA2DFA

put  - closure({s 0

}) as an unmarked state into the set of DFA

(DStates)

while (there is one unmarked S 1

in DStates) do

mark S 1

for (each input symbol a)

S

2

  - closure(move(S 1

,a))

if (S 2

is not in DStates) then

add S 2

into DStates as an unmarked state

transfunc[S 1

,a]S 2

  • the start state of DFA is -closure({s 0
  • a state S in DStates is an accepting state of DFA if a state in S is an

accepting state of NFA

Lexical Analysis Review: RE2DFA

  • Create the syntax tree of (r) #
  • Calculate the functions: followpos, firstpos, lastpos, nullable
  • Put firstpos(root) into the states of DFA as an unmarked state.
  • while (there is an unmarked state S in the states of DFA) do
    • mark S
    • for each input symbol a do
      • let s 1

,...,s n

are positions in S and symbols in those positions are a

  • S

’followpos(s 1

)...followpos(s n

)

  • move(S,a)S’
  • if ( S’ is not empty and not in the states of DFA)
    • put S’ into the states of DFA as an unmarked state.
  • the start state of DFA is firstpos(root)
  • the accepting states of DFA are all states containing the position of #

Lexical Analysis Review: Minimizing DFA

  • partition the set of states into two groups:
    • G 1

: set of accepting states

  • G 2

: set of non-accepting states

  • For each new group G
    • partition G into subgroups such that states s 1

and s 2

are in the same group if

for all input symbols a, states s 1

and s 2

have transitions to states in the same group.

  • Start state of the minimized DFA is the group containing

the start state of the original DFA.

  • Accepting states of the minimized DFA are the groups containing

the accepting states of the original DFA.

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 identifier ‘printf’

Error Processing

• Detecting errors

• Finding position at which they occur

• Clear / accurate presentation

• Recover (pass over) to continue and find later errors

• Don’t impact compilation of “correct” programs

Error Recovery Strategies

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

found ( end, “;”, “}”, etc. )

-- Decision of designer

E.g., (1 + + 2) * 3 skip +

-- Problems:

skip input miss declaration – causing more errors

miss errors in skipped material

-- Advantages:

simple suited to 1 error per statement

Phrase 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

E.g., x=1 ; y=

Error Recovery Strategies – (3)

Past

  • Slow recompilation cycle (even once a day)
  • Find as many errors in one cycle as possible
  • Researchers could not let go of the topic

Present

  • Quick recompilation cycle
  • Users tend to correct one error/cycle
  • Complex error recovery is less compelling
  • Panic-mode seems enough

Parsing During Compilation

  • Parser works on a stream of tokens.
  • The smallest item is a token.

regular

expressions

  • also technically part

or parsing

  • includes augmenting

info on tokens in source,

type checking, semantic

analysis

  • uses a grammar (CFG) to

check structure of tokens

  • produces a parse tree
  • syntactic errors and

recovery

  • recognize correct syntax
  • report errors

interm

repres

errors

lexical

analyzer

parser

rest of

front end

symbol

table

source

program

parse

tree get next

token

token

CFLs

RE vs. CFG

  • Regular Expressions

 Basis of lexical analysis

 Represent regular languages

  • Context Free Grammars

 Basis of parsing

 Represent language constructs

 Characterize context free languages

Reg. Lang.

EXAMPLE: (a+b)*abb

A0 - > aA0 | bA0 | aA

A1 - > bA

A2 - > bA

A3 - > ɛ

EXAMPLE: { (

i )

i | i>=0 } Non-regular language (Why)

Context-Free Grammars

  • Inherently recursive structures of a programming language are defined

by a context-free grammar.

  • In a context-free grammar, we have:
    • A finite set of terminals (in our case, this will be the set of tokens)
    • A finite set of non-terminals (syntactic-variables)
    • A finite set of productions rules in the following form
      • A   where A is a non-terminal andis a string of terminals

and non-terminals (including the empty string)

  • A start symbol (one of the non-terminal symbol)
  • Example:

E  E + E | E – E | E * E | E / E | - E

E  ( E )

E  id

E  num

Example Grammar

exprexpr op expr

expr( expr )

expr- expr

exprid

op+

op-

op__*

op/

Black : NT

Blue : T

expr : S

8 Production rules

Example Grammar

A fragment of Cool:

EXPR  if EXPR then EXPR else EXPR fi

| while EXPR loop EXPR pool

| id