compiler file

Upload: kunal-arora

Post on 06-Jul-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/17/2019 Compiler File

    1/18

    //calc.yacc

    %{#include int regs[26];int base;

    %}%start list%token !"! $&%le't ()(%le't (*(%le't (+( (,(%le't (-( (/( (%(%le't !01 /-sulies recedence 'or unary 3inus -/%% /- beginning o' rules section -/list4 /-e3ty -/  )  list stat (5n(  )  list error (5n(  {  yyerrok;  }  ;stat4 er  {  rint'78%d5n89:;  }  )  $& (=( er  {  regs[:] = :;  }  ;er4 (7( er ((  {  :: = :2;  }  )  er (-( er  {  :: = : - :;  }  )  er (/( er  {

      :: = : / :;  }  )  er (%( er  {  :: = : % :;  }  )  er (+( er

  • 8/17/2019 Compiler File

    2/18

      {  :: = : + :;  }  )  er (,( er  {

      :: = : , :;  }  )  er (*( er  {  :: = : * :;  }  )  er ()( er  {  :: = : ) :;  }  )  (,( er %rec !01  {  :: = ,:2;  }  )  $&  {  :: = regs[:];  }  )  nu3ber  ;nu3ber4 !"!  {  :: = :;  base = 7:==? @ A 4 ?;  } )  nu3ber !"!  {  :: = base - : + :2;  }  ;%%3ain7{ return7yyarse7;}

    yyerror7schar -s;{  'rint'7stderr9 8%s5n89s;}yyBra7{  return7;}

  • 8/17/2019 Compiler File

    3/18

    //calc.le

    %{ #include

    #include 8y.tab.h8int c;etern int yylCal;%}%%8 8 ;[a,D] {  c = yytet[?];  yylCal = c , (a(;  return7$&;  }[?,E] {  c = yytet[?];

      yylCal = c , (?(;  return7!"!;  }[Fa,D?,E5b] {  c = yytet[?];  return7c;  }

    //!nost.l

    %{

  • 8/17/2019 Compiler File

    4/18

    #include

    #include "y.tab.h"

    %}

    %%

    [a-zA-Z] { strcpyyyl!al.syytet$

      return &'

      }

    [()-*] { return yytet[+]}

    [+-,] { return yytet[+]}

    [n] { return +}

    """$" { return yytet[+]}

    . {}

    %%

    //inots.y

    %{

    #include

    #include

    !oid yyerrorconst char )str${

     0print0stderr"error1 %sn"str$

    }

    int yy2rap$

    {

     return 3

    }

    4ain$

    {

     yyparse$

    }

    %}

    %le0t 5(5 5-5

    %le0t 5)5 5*5

    %nonassoc 67&869

    %union {

    char s[:+]

      }

    %to;en &' 867

    %type

    %%

     91 {print0"%sn"=3$} 

    15(5 {

      strcat=3=$

      strcat=3"("$

      strcpy===3$

      }

     5)5 {

      strcat=3=$

      strcat=3")"$

  • 8/17/2019 Compiler File

    5/18

      strcpy===3$

    }

     5*5 { strcat=3=$

      strcat=3"*"$

      strcpy===3$

     

    }

     5-5 {

      strcat=3=$

      strcat=3"-"$

      strcpy===3$

      }

     5-5 %prec 67&869 {

      strcat=:"67&869"$

      strcpy===:$

      }

     &' {strcpy===3$}

     867 {strcpy===3$}

     555$5 {strcpy===:$}

     

    %%*)!oid 4ain$

    {

    yyparse$

    })*

    *)yyerror$ {

      print0"rror."$

    })*

    //leanlDr.l

    letter [a-zA-Z]

    di/it[+-,]

    %%

    {di/it}("""(""-"$?{di/it}($? print0"n%stis real nu4ber"yytet$

    {di/it}("."{di/it}("""(""-"$?{di/it}($? print0"n%st is 0loatin/ pt no

    "yytet$

    "i0""else""int""char""scan0""print0""s2itch""return""struct""do""2hi

    le""!oid""0or""0loat" print0"n%st is ;ey2ords"yytet$

    "a""n""b""t""t""b""a" print0"n%stis scape

    se@uences"yytet$

  • 8/17/2019 Compiler File

    6/18

    {letter}{letter}{di/it}$) print0"n%stis identi0ier"yytet$

    """""B""B""(""-""?"")""*""%""""" print0"n

    %stoperator "yytet$

    "{""}""[""]""""$""#""5""."""""""""" print0"n%st is a

    special character"yytet$

    "%d""%s""%c""%0""%e" print0"n%stis a 0or4at speci0ier"yytet$

    %%

    int yy2rap$

    {

    return 3

    }

    int 4ainint ar/cchar )ar/![]$

    {

    yyinB0openar/![3]"r"$

    yyle$

    0closeyyin$

    return +

    }

    //sy3bolable

    #include

    #include

    #include

    struct table{

    char !ar[3+]

    int !alue

    }

    struct table tbl[:+]

    int iCn

    !oid create$

    !oid 4odi0y$

    int searchchar !ariable[]int n$

  • 8/17/2019 Compiler File

    7/18

    !oid insert$

    !oid display$

    !oid 4ain$

    {

    int chresultB+

    char ![3+]

    do

    {

    print0"nter ur

    choice1n3.Dreaten:.&nsertn.7odi0ynE.9earchnF.'isplaynG.it"$

    scan0"%d"ch$

    s2itchch$

    {

    case 31

    create$

    brea;

    case :1

    insert$

    brea;

    case 1

    4odi0y$brea;

    case E1

    print0"nter the !ariabe to be searchedn"$

    scan0"%s"!$

    resultBsearch!n$

    i0resultBB+$

    print0"Hhe !ariable does not belon/ to the tablen"$

    else

    print0"Hhe location o0 !ariable is %d. Hhe !alue o0 %s is %d"

    resulttbl[result].!artbl[result].!alue$

    brea;

    case F1

    display$

    brea;

    case G1

    eit3$

    }

    }

    2hilechIBG$

    }

    !oid create$

    {

    print0"nter the nu4ber o0 entriesn"$

    scan0"%d"n$

    print0"nter the !ariable and the !alue1n"$

    0oriB3iB5+5 tbl[i].!ar[+]

  • 8/17/2019 Compiler File

    8/18

    chec;31

    0orCB3CB:+$

    print0"Dannotinsert. Hable is 0ull"$

    else

    {

    n((print0"nter the !ariable and !aluen"$

    scan0"%s%d"tbl[n].!artbl[n].!alue$

    chec;1

    i0tbl[i].!ar[+]>B5+5 tbl[i].!ar[+]

  • 8/17/2019 Compiler File

    9/18

    print0"Hhe current !alue o0 the !ariable%s is %d nter the ne2 !ariable and

    its !alue"tbl[result].!artbl[result].!alue$

    scan0"%s%d"tbl[result].!artbl[result].!alue$

    chec;1

    i0tbl[i].!ar[+]>B5+5 tbl[i].!ar[+]

  • 8/17/2019 Compiler File

    10/18

    //d'a.c

    #include

    #include

    #include

    #include

    typede0 lon/ lon/ int KK

    usin/ na4espace std

    int 4ain$

    {

    KK i C n t 4 su4 B + ans B + count B + p ;

    strin/ input9tr

    cout

  • 8/17/2019 Compiler File

    11/18

    stateB:

    }

    else{

    stateB:

    }

    }

    i0stateBB${

    i0input9tr[i]BB5+5${

    stateB

    }

    else{

    stateB

    }

    }

    }

    i0stateBB${

    cout

  • 8/17/2019 Compiler File

    12/18

  • 8/17/2019 Compiler File

    13/18

  • 8/17/2019 Compiler File

    14/18

  • 8/17/2019 Compiler File

    15/18

  • 8/17/2019 Compiler File

    16/18

  • 8/17/2019 Compiler File

    17/18

    Enter ur choice:1.Create2.Insert3.Modify4.Search5.Display6.Eit

    1Enter the nu!"er of entries2Enter the #aria"le and the #alue:

     $ 26% 42&he ta"le after creation is'aria"le #alue

     $ 26% 42Enter ur choice:1.Create2.Insert3.Modify4.Search

    5.Display6.Eit2Enter the #aria"le and #alueD 1(&he ta"le after insertion is'aria"le #alue

     $ 26% 42D 1(Enter ur choice:1.Create2.Insert3.Modify4.Search

    5.Display6.Eit3Enter the #aria"le to "e !odifiedD&he current #alue of the #aria"leD is 1() Enter the ne* #aria"le and its #alueC2(&he ta"le after !odification is'aria"le #alue

     $ 26% 42C 2(Enter ur choice:1.Create2.Insert3.Modify4.Search5.Display6.Eit4Enter the #aria"e to "e searched

     $ &he location of #aria"le is 1. &he #alue of $is 26Enter ur choice:

  • 8/17/2019 Compiler File

    18/18

    1.Create2.Insert3.Modify4.Search5.Display6.Eit5

    'aria"le #alue $ 26% 42C 2(Enter ur choice:1.Create2.Insert3.Modify4.Search5.Display6.Eit