This presentation is the property of its rightful owner.
Sponsored Links
1 / 47

第三章 词法分析 PowerPoint PPT Presentation


  • 92 Views
  • Uploaded on
  • Presentation posted in: General

第三章 词法分析. 概述 词法分析是编译过程的第一步,其主要任务是对源程序进行扫描,产生一个个的单词符号,把作为字符串的源程序改造成为单词符号串的中间程序。 因此,词法分析是编译的基础。执行词法分析是编译的基础。 词法分析:根据词法规则识别及组合单词,进行词法检查。 对数字常数完成数字字符串到(二进制)数值的转换。 删去空格字符和注解。. 主要内容 : 3.1 对于词法分析器的要求 3.2 词法分析器的设计 3.3 正规表达式与有限自动机 3.4 词法分析器的自动产生. 3.1 对于词法分析器的要求. 任务

Download Presentation

第三章 词法分析

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


5057597


5057597

:

  • 3.1

  • 3.2

  • 3.3

  • 3.4


5057597

3.1

    • .

    • ,.

    • .


5057597

1)

  • : Pascalbeginendif,while

  • :,:,.

  • : ,,.

  • : + - * / .

  • :,,,/*, */.

    ,,,,.


5057597

2)

  • :

    (, )

  • :

    • .

    • 1 ,2 .

    • . 1 BEGIN,2 END.


5057597

  • :

    • ..


  • 5057597

    • 1

    1

    2

    3

    4

    5

    6

    7

    0 1

    ()


    5057597

    • 2

    0 1

    -

    -

    -

    -

    ..

    -

    -

    -

    -

    --

    ()

    IF

    THEN

    ELSE

    FOR

    :

    +

    *

    ,

    (

    1

    2

    3

    4

    5

    6

    7

    8

    9

    .

    20

    21

    22

    23

    .


    5057597

    • C++

      while (i>=j) i--;

      <while, ->

      <(,->

      <id,i>

      <>=,->

      < id,j>

      <),->

      <id,i>

      <--,->

      <;,->


    3 1 2

    3.1.2

    :

    1.

    S.P.

    ()

    S.P.

    ()

    2.

    S.P.

    ()


    5057597

    3.2

    • 3.2.1

      1.

      2.

      3.

      4.


    5057597


    3 2 2

    3.2.2

    • P39


    5057597

    • FORTRAN

    • 1 DO99K=1,10

    • 2 IF(5.EQ.M) I=10

    • 3 DO99K=1.10

    • 4 IF(5)=55

    • 13 1DO3

    • 242IF4


    3 2 3

    3.2.3

    1

    • .,,. ,.


    5057597

    (2)()


    5057597

    (3)

    U->aW|a)

    • Z, U->aUZa


    5057597

    (3)

    • U->aWUWa


    5057597

    (3)

    U->Wa|a)

    • S,U->aSUa


    5057597

    (3)

    • U->WaWUa


    5057597

    1:

    • (a)1,X,X,2.Y,Y,3.

    • (b)0,,,1.1,,,1.,12.

    • (c).

    • *


    5057597

    2:

    : ( OCT )

    • oct o ( 0|...|7 ) ( 0|...|7 )*

      : ( DEC )

    • dec 0 | ( 1|...|9 ) ( 0|...|9 )*


    5057597

    1/2

    0-7

    o

    1

    2

    3

    4

    0-7

    0-9

    1

    5

    6

    1-9

    0


    5057597

    • 12312


    5057597

    3:

    $


    5057597

    • 3

      1IFWHILE

      2

      3

      013


    5057597

    3


    3 2 4

    3.2.4

    • 1ch

      2strToken

      3GetChar ch

      4GetBC chGetCharch

      5Concat chstrTokenstrTokenABchCconcatstrTokenABC

      6IsLetterIsDigitch

      7Reserve strToken0

      8Retractch

      9InsertId strToken

      10InsertConst strToken


    5057597

    j

    i

    k

    /

    l

    switchifthenelse

    Getchar();

    If (isLetter()){j;}

    Else if(isDigit()){k}

    Else if (ch=/){l}

    Else {}

    I


    5057597

    i

    j

    whileif

    GetChar();

    While(isLetter() or isDigit())

    GetChar();

    j

    return(code,value)codevaluereturn*Retract


    5057597

    3.3

    3

    int code,value;

    strToken :=; /*strToken */

    GetChar(); GetBC(); /**/

    if (IsLetter())

    begin

    while(IsLetter() or IsDigit())

    begin

    Concat(); GetChar(); /**/

    end


    5057597

    Retract(); /**/

    code:=Reserve(); /**/

    if (code=0) /**/

    begin

    value:=InsertId(strToken);

    return($ID,value); /**/

    end

    else

    return(code,-);

    end


    5057597

    else if(IsDigit())

    begin

    while(IsDigit())

    begin

    Concat(); GetChar();

    end

    Retract();

    value:=InsertConst(strToken);

    return($INT,value);

    end

    else if(ch== = ) return ($ASSIGN, -);

    else if(ch== + ) return ($PLUS, -);


    5057597

    else if(ch= * )

    begin

    GetChar();

    if (ch = *) return ($POWER,-);

    Retract();return($SATR,-);

    end

    else if(ch= ; ) return ($SEMICOLON, -);

    else if(ch= ( ) return ($LPAR, -);

    else if(ch= ) ) return ($RPAR, -);

    else if(ch= { ) return ($LBRACE, -);

    else if(ch= } ) return ($RBRACE, -);

    else ProcError();


    5057597

    4

    G[Z]:

    Z->U0|V1

    U->Z1|1

    V->Z0|0

    100101 100111


    5057597

    S

    100101S,U,Z,V,Z,V,Z,Z.. 10011110011

    1

    S

    U

    0

    1

    0

    1

    V

    Z

    0


    5057597

    3.3

    ,:

    • ,{} ;

    • a, a,{ a } ;

    • UV,L(U)L(V),,(U|V),(U*V),(U)*,L(U)L(V),L(U)L(V)()(L(U))* ()


    5057597

    ={a,b},:

    ba* ba

    a(a|b)* a

    ={A,B,0,1}, :

    (A|B)(A|B|0|1)*

    (0|1)(0|1)*


    5057597

    U V U=V.

    b(ab)*=(ba)*b

    (a|b)*=(a*b*)*


    5057597

    (1)a*b* (2) (a|b)* (3)(ab)* (4)a*|b* .

    a

    a

    b

    1

    2

    1

    a

    a*b*

    b

    1

    (a|b)*

    1

    x

    y

    a

    2

    b

    2

    b

    a*|b*

    (ab)*


    5057597

    :U,V W

    (1) U|V=V|U (

    (2U|V|W=U|V|W (

    (3UVW=UVW (

    (4) UV|W=UV|UW (

    V|WU =VU|WU (

    (5) U=U=U


    5057597


    5057597

    • =(T,N,,).T= , N

    • r, S S r, S G

    • xy , A xy A xB, B y, B BN


    5057597

    • Ax*y,

      A xB

      A y

      B xB

      B y B.

    • A x|y

      A x , A y


    5057597

    • R=a(a|d)*SS a(a|d)*,S aAA(a|d)*

      S aA, A (a|d)B, A->, B (a|d)B B ->

      S aA, A aB, A dB, B aB, B dB, A-> B ->


    5057597

    • ,


    5057597

    • G[S]

      S aA,S a,A aA,A dA,A a,A d

      S= aA|a A=(aA|dA)|(a|d)

      AA=(a|d)A|(a|d), 2A=(a|d)*|(a|d), AS S=a(a|d)*|(a(a|d)|a

      S=a(a|d)* |(a|d)|

      S=a(a|d)* a(a|d)*


  • Login