www.webdeveloper.com
Results 1 to 5 of 5

Thread: Need help in parsing

  1. #1
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373

    Need help in parsing

    I am trying to convert data from one format to another. Starting with
    Code:
    X:1
    T:Speed the Plow
    M:4/4
    C:Trad.
    K:D
    |:GABc dedB|dedB dedB|c2ec B2dB|c2A2 A2BA|
      GABc dedB|dedB dedB|c2ec B2dB|A2F2 G4:|
    |:g2gf gdBd|g2f2 e2d2|c2ec B2dB|c2A2 A2df|
      g2gf g2Bd|g2f2 e2d2|c2ec B2dB|A2F2 G4:|
    I am trying to convert it to
    Code:
    ...
    |SongInfo|Title:"Speed the Plow"|Author:"Trad."|Lyricist:""|Copyright1:""|Copyright2:""
    ...
    |Clef|Type:Treble
    |Key|Signature:F#,C#
    |TimeSig|Signature:4/4
    |Bar|Style:MasterRepeatOpen
    |Note|Dur:8th|Pos:-2|Opts:Stem=Up,Beam=First
    |Note|Dur:8th|Pos:-1|Opts:Stem=Up,Beam
    etc.
    I am assuming I will have two textareas in a form, the data are copied from a clipboard to one of them and split into an array on newline characters. On the last few lines the letters A-G or a-g may or may not be preceded by _, =, or ^ and may or may not be followed by a comma or apostrophe then a (or not) number. The lack of a space after a letter indicates a grouping (beaming).

    Does anyone have an example of some parsing code? Just something that looks at the input byte by byte building a token, putting a character back if it belongs to the next token and handling the end of the line and end of data. If someone has a parser for anything, I think I could adapt it.

    Thanks in advance.

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Sounds confusing. Any parsing process is based on an algorithm. Set that algorithm for your particular case, and you have solved 90% of the problem. And the method is nothing but an equation, after all. What example do you need? Of an algorithm?

    I am not sure what example to give... Maybe parsing a RGB value of a color to its Hexadecimal correspondent. Here's a simplified version of a color picker which output RGB to Hexa:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Color picker</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    //Color picker
    //For safe (216) web colors
    //Genuine code 2006 by Corneliu Lucian "Kor" Rusu corneliulucian[at]gmail[dot].com
    onload = function(){
    var cel = document.getElementById('tab').getElementsByTagName('td');
    var R=0;var G=0;var B=0;
    for(var i=0;i<cel.length;i++){
    cel[i].style.backgroundColor='rgb('+R+','+G+','+B+')';
    cel[i].r=(R!=0)?R.toString(16):R.toString(16)+R.toString(16);
    cel[i].g=(G!=0)?G.toString(16):G.toString(16)+G.toString(16);
    cel[i].b=(B!=0)?B.toString(16):B.toString(16)+B.toString(16);
    cel[i].onclick=function(){
    document.getElementById('inp').value='#'+this.r+this.g+this.b;
    //optional, the below line sets the document's background color as well
    document.getElementsByTagName('body')[0].style.backgroundColor='#'+this.r+this.g+this.b;
    }
    G+=51;if(G>255){G=0;R+=51;if(R>255){R=0;B+=51;}}}
    }
    </script>
    </head>
    <body>
    <table id="tab" width="363" border="1" cellpadding="0" cellspacing="1" bordercolor="#666633">
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>
    <br>
    <br>
    <input id="inp" type="text">
    </body>
    </html>
    But this one uses a native method of JavaScript toString(base) which converts decimal numbers into any base, up to 36. Your case is strongly particular, thus you have to create your own "table of correspondence" between n pairs of values and your own algorithm which will be able reproduce that relationship even for n+1 pairs.
    Last edited by Kor; 10-21-2010 at 12:41 PM.

  3. #3
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    My only reference was to a compiler class I took back in 1994. The final product would look at a line such as
    Code:
    xrad= x * PI / 180.0;
    to fetch the variables x and PI, multiply them, then divide by a constant in order to place the result in xrad, creating the commands to do each of these things. If reading a variable it continued until it found a character which could not be in a variable, or a space, or end-of-line. (xrad is different from x, so it has to check the byte(s) after the x to see if there is more to the name).

    In this case c may stand alone or ^c'2/3 may be one token. A "|" means one thing by itself and "|:" does something different. If someone had some type of model I could use it and it would be much easier to modify later than if I had to do it from scratch.

    Thanks for looking at it. My data is from abcnotation.com.

  4. #4
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    Quote Originally Posted by Fang View Post
    Code:
    var needle = /con-(\d+)_(\d+)$/i;
    Thanks. I think I am ready to tackle this project in earnest.

  5. #5
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    Finally finished it (or at least got to a good stopping point). It has numerous examples of regular expressions and of the match command.

    abcnwc.htm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles