פרק 4 ניתוח התחביר

  • View
    114

  • Download
    2

Embed Size (px)

DESCRIPTION

4 . ( analysis ). lexical analyser. syntax analyser. semantic analyser. intermediate code generator. code optimizer. ( synthesis ). code generator. - PowerPoint PPT Presentation

Transcript

  • syntax analyser

  • (Syntax) -

  • " " : " " ( -ASU ' 179181)

  • "

  • " if (id relop num) id();

  • (top-down) LL (bottom-up) LR

  • " " "

  • : , : : - :

    :

  • "

  • (panic mode) (phrase level) (error productions) (global correction)

  • (Panic Mode Recovery) " ", (synchronization token) (, ) : - ; { end

  • (Phrase Level Recovery) "

    : -

    ( )

  • " (Error Production Recovery)

    :stmt if ( expr ) then stmt else stmt then

  • (Global Correction) , " : , , ,

  • (Grammar) (Language)G (grammar) G = T, N, P, ST (terminals) -N (nonterminals) T N = P (productions) - / , - ( )S (start symbol) S N

    L(G) (language) - " ( )

  • (Context Freedom) (context free grammar) A A ( )

    (context free language) "

  • (Parser) " " (scanner) :expr expr op exprexpr ( expr ) expr - exprexpr idop +op -op op /op :id + - / ( )

    :expr op

  • . - a b c . . .. + - / . . . . ( ) , ; . . ..0 1 . . . 9. id if . . .

  • . - A B C . . .. S S . stmt expr op . . .-. - X Y Z . . . ( ). - u v . . . z . - . . .

  • | A 1 A 1 | 2 | . . . | k A 2 . . . A k ( )E E A E | ( E ) | - E | idA + | - | | / |

  • (Derivation) X - , X : :-(E) -(E A E) -(E + E) -(E + id) -(id + id)

    -(E) -(id + id)+

  • (Language) L(G) " G L(G) = { w | S w } w (sentence) (equivalent) sentential S (sentence) sentential +

  • (Leftmost) (Rightmost) : ( ) 1 (leftmost) "A : w A w ( w T* ) 1 (rightmost) "A : A u u ( u T* )

    lmlmrmrm

  • -

    -

    :- ( E + E ) - ( id + E )- ( E + E ) - ( E + id ) lm rmlmrm

  • : B : B : -B - = - : E E + E : - ( E ) - ( E + E )

  • - (Ambiguity) - (ambigous) - 1 ( ) ( ) 2 ( ):E E + E E E E id + E E + E E id + E E id + E E id + id E id + id E id + id id id + id id

  • - - - (disambiguating rules)

  • -else (dangling else):stmt if expr then stmt| if expr then stmt else stmt| otherif E1 then S1 else if E2 then S2 else S3

    "

  • if E1 then if E2 then S1 else S2 " :

  • - " , -else -then , then -else then else stmt matched_stmt | unmatched_stmtmatched_stmt if expr then matched_stmt else matched_stmt | otherunmatched_stmt if expr then stmt | if expr then matched_stmt else unmatched_stmt

  • (Left Recursion) : A A (left recursion) , A A : A A | " : A A A A | +

  • :E E + T | TT T F | FF ( E ) | id :E T EE + T E | T F T T F T | F ( E ) | id

  • : A A1 | A2 | . . . | Am | 1 | 2 | . . . | n i -A i -

    " :A 1A | 2 A | . . . | nAA 1A | 2A | . . . | mA |

  • G ( A A) ( A ) ( , ASU ' 4.20 -4.22) A1, A2, . . . , An Ai i [1 .. n] Aj j [1 .. i-1] P Ai Aj Aj 1 | 2 | . . . | k P Ai 1 | 2 | . . . | n Ai

    +

  • (Left Factoring)stmt if expr then stmt else stmt | if expr then stmt if : A 1 | 2 : A AA 1 | 2 1 -2

  • : A: A A A 1 | 2 | . . . | n | 1 | 2 | . . . | m i " :A A | 1 | 2 | . . . | mA 1 | 2 | . . . | n A

  • Top Down Parsing

  • (Recursive Descent)

    : S S

  • V : P V : P V ( ) -V V ( ): () () 3V

  • : S cAdA ab a :cad

  • (Backtracking) , :

  • (Predictive Parser) (Predictive Parser) " stmt if expr then stmt else stmt while expr do stmt begin stmt_list endstmt_list stmt stmt_list " - ,

  • (Transition Diagrams) " " "

  • A: () A x1x2xn : x1, x2, xn

  • ::E T EE + T E | T F TT F T | F ( E ) | id

  • -$b+a M$ZYX -$ M[X,a] () X a ?

  • -: w M G: w L(G) w;

    append $ to w ;push ( $ );push ( S );i = 0;do { X = top () ; a = wi ; if ( X is terminal or $ ) { if ( X == a ) {pop () ;i ++ ; } else error () ; } else /* X is nonterminal */ if ( M[X, a] == X Y1Y2Yk ) { pop () ; push ( Yk, Yk-1, ... Y1 ) ; output X Y1 Y2 Yk ; } else error () ;} while ( X != $ ) ;

  • - -, - -, : X ( ) X

  • First -FollowFirst() - First()Follow(A) -A , : a Follow(A) S Aa S A $ Follow(A)

    S Aa S Aa a Follow(A) S A S A $ Follow(A)

  • First for each terminal tFirst (t) = { t };while ( ! done ) {if ( X is a production ) First (X) = { } ;if ( X Y1 Y2 Yk is a production ) {First (X) = First (Y1) - { };for ( i=1; i < k; i ++ ) if ( First (Yi) ) First (X) = First (Yi+1) - { }; elsebreak ;if ( i == k First (Yk)) First (X) = { } ; }} = -First - done = 0

    ( ) done = 1

  • First

    First () = First (X1) { } ;for ( i = 1; i < n; i ++ ) { if ( First (Xi) ) First () = First (Xi+1) { } else break ;if ( i == n First (Xn) ) First () = { } ; X1 X2 . . . Xn

  • FollowFollow (S) = { $ } ;while ( !done ) {if ( A B is a production ) Follow (B) = First () { } ;if ( A B is a production ( A B is a production First () ) ) Follow (B) = Follow (A); }done

  • E T EE + T E | T F TT F T | F ( E ) | idFirstFirst ( E ) = { ( , id }First (E ) = { + , }First ( T ) = { ( , id }First (T ) = { , }First ( F ) = { ( , id }FollowFollow ( E ) = { ) , $ }Follow (E ) = { ) , $ }Follow ( T ) = { + , ) , $ }Follow (T ) = { + , ) , $ }Follow ( F ) = { + , , ) , $ }

  • foreach ( production A ) {foreach ( terminal a First () ) M [A, a] += A ;if ( First () ) { foreach ( terminal b Follow (A) M [A, b] += A ; if ( $ Follow (A) ) M[A, $] += A ;} }

  • :S i E t S S | aS e S | E b:

    - " S eS S -else -then S e - -

  • LL(1) LL(1):L ( Left to right scan )L ( Left most derivation )1 ( 1 lookahead symbol ) LL(1) - LL(1) " A A a T : a a ( )if ( a ) then a Follow (A) LL(1)

  • (Bottom-Up Parsing)

  • S a A B eA A b c bB d a b b c d e "a b b c d ea A b c d ea A d e a A B e S rm rm rm rm

  • (Handler) (handler) A -A " - , S Aw w A w (w ) - - S w - - -A ( ) rm rm rm

  • "" S a A B eA A b c bB d a b b c d e "a b b c d ea A b c d ea A A c d e a A A c B e rm rm rm rmERROR

  • E E + EE E EE ( E )E idid1 + id2 id3

  • E E + EE E EE ( E )E idid1 + id2 id3 - :

  • " "(Shift-Reduce) 4 :shift reduce - ( ) accept error w$$ $$S

  • Shift-Reduce

  • shift/reduce shift reduce reduce/reduce

    LR(k) ( )

  • Shift/Reduce - LR stmt if expr then stmt if expr then stmt else stmt other . . . if expr then stmt else . . . $

    shift/reduce " " " -shift " -else -then

  • Reduce/Reduce : FUNC (A, B) ARR (I, J) id

  • : A(I, J) : id ( id , id ) . . . id (