compile.awk
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:2k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. # to run: awk -f transit.awk transit.p0
  2. #
  3. BEGIN { "date" | getline
  4. enable_index = 1
  5. today = $0
  6. printf("n/* this file was generated on %s  */n", today )
  7. not_firstone = 0 # flag to avoid empty entry in 1st table
  8. fpe = 0 # entry tbl array fill pointer
  9. fpeo = 0 # entry tbl offset list fill pointer
  10. fpdef = 0  # define list fill pointer
  11. }
  12. ### /^;/ { } # line starting with a semicolon is comment 
  13. /^[A-Z]/ { # table name
  14. if ( $1 == "TABLE" ) {
  15.    tbl = $2 # get table name
  16.    newtbl( tbl )
  17. }
  18. else if ( $1 == "COMPILE" ) {
  19.    array_name = $2
  20.    if ( $3 == "NOINDEX" ) { enable_index = 0 }
  21. }
  22. else { # table entry
  23.    ec = ec +1
  24.    n = split( $0, fld, " " )
  25.    action = fld[ n-1 ]
  26.    newstate = fld[ n ]
  27.    store( action, newstate )
  28.    ecct = ecct +1
  29. }
  30. }
  31. END { store( action, newstate )
  32. if ( enable_index ) {
  33.     printf( "n/* index name #defines: */nn",
  34.  ec, ecct )
  35.     for( ii = 1; ii <= fpeo; ii++ ){
  36.        printf( "#define %-12s %3dn", define[ ii ], ii -1 )
  37.     }
  38. }
  39. printf( "nn/* size of transition table is %d bytes */n",
  40.  fpe )
  41. if ( enable_index ) {
  42.     printf( "nstatic short int %s_offset [ ] ={", array_name )
  43.     for( ii = 1; ii <= fpeo; ii++ ){
  44.         if ( (ii % 10) == 1 ) printf("n  ")
  45.         printf( " %4d", entry_offset[ ii ] )
  46.         if ( ii < fpeo ) printf( "," )
  47.     }
  48.     printf(" };n")
  49. }
  50. printf( "nstatic char %s_entry [ ] = {", array_name )
  51. for( ii = 1; ii <= fpe; ii++ ){
  52.     if ( (ii % 6) == 1 ) printf("n  ")
  53.     printf( " %-14s", entry[ ii ] )
  54.     if ( ii < fpe ) printf( "," )
  55. }
  56. printf(" };n")
  57. }
  58. function store(  act, ns ){
  59. # printf( "%s %sn",  act, ns )
  60. entry[ ++fpe ] = act
  61. entry[ ++fpe ] = ns 
  62. }
  63. function newtbl( tbl ){
  64.    if ( not_firstone ) {
  65. store( action, newstate )
  66.    }
  67.    not_firstone = 1
  68.    entry_offset[ ++fpeo ] = fpe # entry tbl offset list
  69.    define[ ++fpdef ] = tbl # state name to define
  70. }