LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-19-2006, 04:31 PM   #1
G00fy
Member
 
Registered: Jul 2004
Location: Herent, Belgium, Europe, Earth
Distribution: Ubuntu 7.04
Posts: 102

Rep: Reputation: 15
Question [sed] "Advanced" sed question(s)


Hi,


Basically what I try to do here is convert a slice-file (www.zeroc.com) to a C++ header file so DoxyGen can parse it without a glitch .
So I don't care about layouting in sed... As it will be interpreted by a machine anyway .


I have 3 questions for sed...

Let me first show the input file:
Code:
#ifndef TEST1
#define TEST1


module main
{
  local interface test1
        extends   ext1,
                  ext2
  {
                bool function( );
    idempotent  bool function( );

    nonmutating bool function( );
    nonmutating bool function( ) throws RangeError;
    nonmutating bool function( ) throws RangeError, NormalError;
    nonmutating bool function( ) throws RangeError,
                                        NormalError;
    nonmutating bool function( ) throws RangeError
                                        , NormalError;
  };
};


#endif // TEST1
Now what it has to look like:
Code:
#ifndef TEST1
#define TEST1


namespace main
{
  class test1
        : public ext1, public
                  ext2
  {
                bool function( );
    bool function( );

    bool function( ) const;
    bool function( ) const throw(RangeError);
    bool function( ) const throw(RangeError, NormalError);
    bool function( ) const throw (RangeError,
                                        NormalError);
    bool function( ) const throw (RangeError
                                        , NormalError);
  };
};


#endif // TEST1

I am trying to do this with 'sed'... Now I managed to get this script:
Code:
s/local//g
s/interface/class/g
s/extends/: public/g
s/module/namespace/g
s/sequence/vector/g
s/dictionary/map/g
/class/,/{/s/{/{public:/g
s/idempotent//g
s/nonmutating\(.*\);/\1 const;/g
now the problems I have are those:
  1. throws ...; -> throw(...); with support for multiple errors on multiple lines
  2. extends ..., ..., ... -> : public ..., public ..., public ...
  3. nonmutating ... () throws... -> ... () const throw...;


I have been looking around for something called 'N', but that seems like only running once, and what I need is something that even runs on multiple lines...


For (1) I need everything from 'throws' -> ';'. And then change the keyword 'throws' -> 'throw(', and the keyword ';' -> ');'. That would do this trick, but I don't know how to tell in sed.

For (2) I need to convert the extends <class>[, <class>] into : public <class>[, public <class]...

(3) is rather simple if I know how to do (1) in fact... So never mind about that one


Could someone help me with this?


Thanks!
 
Old 03-19-2006, 07:19 PM   #2
Frodak
LQ Newbie
 
Registered: Jul 2003
Distribution: Redhat 9
Posts: 12

Rep: Reputation: 0
you need to use multiple line addressing and stuff... this is kind of clunky, but I'm not a sed guru


Code:
#!/usr/bin/sed -f

s/module/namespace/

#parse local interface
/local interface/ , /{/    {
    /local interface/ {
        #change local interface to class
        s/local interface\(.*\)/class \1:/
        #branch to the end of script - do nothing else for local interface
        b
    }
#delete extends
    s/extends//
#add public before every keyword, but not {
    s/\([^ {]\+\)/public \1/g

}

s/nonmutating\(.*)\)/\1 const/

/throws.*;/ {
    s/throws/throw(/
    s/;/);/
    #print the line
    p
    #delete pattern space, start next cycle
    #don't process multi-line throws
    d
} 

/throws/,/;/ {
    s/throws/throw(/
    s/;/);/
}

$ cat temp.lst 
#ifndef TEST1
#define TEST1


module main
{
  local interface test1
        extends   ext1,
                  ext2
  {
                bool function( );
    idempotent  bool function( );

    nonmutating bool function( );
    nonmutating bool function( ) throws RangeError;
    nonmutating bool function( ) throws RangeError, NormalError;
    nonmutating bool function( ) throws RangeError,
                                        NormalError;
    nonmutating bool function( ) throws RangeError
                                        , NormalError;
  };
};


#endif // TEST1

e728075@NM75DTJYT4M81 ~
$ ./parse_file temp.lst 
#ifndef TEST1
#define TEST1


namespace main
{
  class  test1:
           public ext1,
                  public ext2
  {
                bool function( );
    idempotent  bool function( );

     bool function( ) const;
     bool function( ) const throw( RangeError);
     bool function( ) const throw( RangeError, NormalError);
     bool function( ) const throw( RangeError,
                                        NormalError);
     bool function( ) const throw( RangeError
                                        , NormalError);
  };
};


#endif // TEST1


Frodak
 
Old 03-20-2006, 12:34 AM   #3
G00fy
Member
 
Registered: Jul 2004
Location: Herent, Belgium, Europe, Earth
Distribution: Ubuntu 7.04
Posts: 102

Original Poster
Rep: Reputation: 15
Thanks a lot! To me you're a Guru anyway .


What I came up with so far (after your great help):
Code:
#!/usr/bin/sed -f

# Skip C-style commenting
/\/\*/ , /\*\//b
/\/\/\(.*\)/b

s/module/namespace/
s/local//
s/idempotent//
s/dictionary/map/
s/sequence/vector/

#parse local interface
/interface/ , /{/    {
    /interface/ {
        #change interface to class
        s/interface\(.*\)/class \1/
    }
#change extends
    s/extends/: public/
#add public before every keyword, but not {
    /{/!s/,\(.*\)/, public \1/g
    s/{/{public:/
}

s/nonmutating\(.*)\)/\1 const/

/throws/,/;/ {
    s/throws/throw(/
    s/;/);/
}
This changes this file:
Code:
#ifndef TEST1
#define TEST1

/**
  module
  interface
  nonmutating
*/

// local interface extends
module main
{
  interface test2 extends ext2
  {
  };
  
  local interface test1
        extends   ext1,
                  ext2
  {
                bool function( );
    idempotent  bool function( );

    nonmutating bool function( );
    nonmutating bool function( ) throws RangeError;
    nonmutating bool function( ) throws RangeError, NormalError;
    nonmutating bool function( ) throws RangeError,
                                        NormalError;
    nonmutating bool function( ) throws RangeError
                                        , NormalError;
  };
};


#endif // TEST1
Into this output:
Code:
#ifndef TEST1
#define TEST1

/**
  module
  interface
  nonmutating
*/

// local interface extends
namespace main
{
  class  test2 : public ext2
  {public:
  };

   class  test1
        : public   ext1, public
                  ext2
  {public:
                bool function( );
      bool function( );

     bool function( ) const;
     bool function( ) const throw( RangeError);
     bool function( ) const throw( RangeError, NormalError);
     bool function( ) const throw( RangeError,
                                        NormalError);
     bool function( ) const throw( RangeError
                                        , NormalError);
  };
};


#endif // TEST1
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
sed of "Solaris 9 Software companion CD" - problem with installation Tinkster Solaris / OpenSolaris 1 12-05-2005 09:35 PM
sed error: "parentheses not balanced" - But they are! Travis86 Programming 2 08-12-2004 07:08 PM
bash Problem with "sed" command blubbfish Linux - Newbie 3 06-01-2004 10:14 AM
can I replace text with the result of "wc" using sed? BrianK Linux - General 1 04-21-2004 01:15 PM


All times are GMT -5. The time now is 08:46 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration