Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Best language for text parsing?

Best language for text parsing?
Thread Tools
calumr
Forum Regular
Join Date: Sep 2000
Location: UK
Status: Offline
Reply With Quote
Jun 13, 2001, 06:19 AM
 
I am looking for the best language to parse the output of terminal commands (eg. ping) and turn it into a more structured form, defined by a script of some sort.

I have had little experience with interpreted languages like perl, and was wondering which one offers the best (ie. fairly simple to start with advanced features optional) text processing.
     
Millennium
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Jun 13, 2001, 07:51 AM
 
If you want something like that, I would go with lex and yacc (or flex and bison, the versions Apple includes in their developer tools) myself. Both languages (they're meant to be used together, and are very similar) compile down to C code, which you can use in a Carbon or Cocoa app with no trouble.

O'Reilly has a great book out on the two languages; I'd start there if you want to learn them.

[ 06-13-2001: Message edited by: Millennium ]
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
Dalgo
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Jun 13, 2001, 09:20 AM
 
If you want to use an interpreted language then I'd say that perl is probably the way to go. It looks pretty cryptic but it is also pretty powerful in terms of text processing.
     
gralem
Dedicated MacNNer
Join Date: Nov 2000
Location: Malaysia
Status: Offline
Reply With Quote
Jun 13, 2001, 09:24 AM
 
Originally posted by Dalgo:
<STRONG>If you want to use an interpreted language then I'd say that perl is probably the way to go. It looks pretty cryptic but it is also pretty powerful in terms of text processing.</STRONG>
For that matter, you could use my favorite: sed. Always there, always seems to get the job done.

---gralem
     
sambeau
Mac Elite
Join Date: Jun 2001
Location: Dundee, Scotland
Status: Offline
Reply With Quote
Jun 13, 2001, 09:25 AM
 
I agree. Lex & Yacc without a doubt.

They take a bit of getting used to but once you 'get it' they are fantastic (and fantastically fast). Plus they come free with OSX (Flex & Bison).

The O'Reilly book is a very good start.

I have just written such a language using them & it storms along on OSX.

Just remember to link to -ll rather than -lfl on OSX.
     
parallax
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Jun 13, 2001, 10:25 AM
 
Depending on your goals, I'd go with either lex/yacc or Perl.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
tie
Professional Poster
Join Date: Feb 2001
Status: Offline
Reply With Quote
Jun 13, 2001, 07:45 PM
 
Is there an example somewhere of how to use lex/yacc inside Cocoa apps? How easily do they integrate? Also, is the integration fairly portable -- that is, is integrating into C/C++/Java about the same as integrating in ObjC?

For perl, is the easiest way to integrate to have a script in the Resources folder, and then start an NSProcess with that script, then read and parse it's output?

What are some concrete differences between perl and lex/yacc?
The 4 o'clock train will be a bus.
It will depart at 20 minutes to 5.
     
Kristoff
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Jun 13, 2001, 08:21 PM
 
What are you trying to do? Because Java's java.util.StringTokenizer makes it really easy to parse text in Java.
signatures are a waste of bandwidth
especially ones with political tripe in them.
     
sadie
Senior User
Join Date: Feb 2001
Location: Rochester, uk
Status: Offline
Reply With Quote
Jun 14, 2001, 06:22 AM
 
Java may have tokenisation and other standard string tools, but it doesn't have regular expressions unless you buy them in from somewhere. Perl does it naturally, and is a whole lot more flexible for string stuff. I haven't used lex or yacc, but i'm guessing they're similar to Perl on this.

I know what it's like to be entusiastic about a language - sometimes i even love Java - but you have to accept when better alternatives exist.
All words are lies. Including these ones.
     
Millennium
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Jun 14, 2001, 08:19 AM
 
Is there an example somewhere of how to use lex/yacc inside Cocoa apps? How easily do they integrate? Also, is the integration fairly portable -- that is, is integrating into C/C++/Java about the same as integrating in ObjC?
Lex and yacc are converted into stardard C code by their respective "compilers" (flex and bison on OSX). From there, getting it into C, C++, or Objective-C is a simple cut/paste job, or you could just include the files into the project like any other file and not even have to worry about cutting and pasting. You might want to wrap the code in a class for C++ or Objective-C, but isn't strictly necessary to do this.

Integrating it into Java is the same as integrating any other C code. Unfortunately, I don't know how to do that.
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
Kristoff
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Jun 14, 2001, 09:52 AM
 
Originally posted by sadie:
<STRONG>Java may have tokenisation and other standard string tools, but it doesn't have regular expressions unless you buy them in from somewhere. Perl does it naturally, and is a whole lot more flexible for string stuff. I haven't used lex or yacc, but i'm guessing they're similar to Perl on this.

I know what it's like to be entusiastic about a language - sometimes i even love Java - but you have to accept when better alternatives exist.</STRONG>
That's why I was asking what the intention was. There IS a gnu.regexp package for Java which is free. But, the original poster needs to be more specific on what they are trying to accomplish.

PERL is my first choice for text parsing that requires heavy regular expressions, as that is what the language was intended for (PRACTICAL EXTRACTION and REPORTING LANGUAGE).

It is simple to write a PERL script that can be called from a Java program by using the Runtime.exec() method. Check the docs for the java.lang package for the exact usage of Runtime.exec().
signatures are a waste of bandwidth
especially ones with political tripe in them.
     
calumr  (op)
Forum Regular
Join Date: Sep 2000
Location: UK
Status: Offline
Reply With Quote
Jun 14, 2001, 01:55 PM
 
I don't think lex and yacc apply here. I want to run a script (or whatever) specified in another file. lex and yacc would require me to compile (C?) code as well as run it. The scripts must be editable by the user.

Kristoff, FYI the purpose of this is to turn the output of a command like ping or traceroute into nice formatted text.

I will probably go with perl. It seems to be everywhere else, so why not in my app

(On a side note - isn't Cocoa fantastic? I was typing this post and I thought 'Is that how you spell editable?' - then I realised that it wasn't underlined and therefore spelt correctly. Although 'realised' is underlined - stupid American dictionary .
     
Millennium
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Jun 14, 2001, 02:14 PM
 
I don't think lex and yacc apply here. I want to run a script (or whatever) specified in another file. lex and yacc would require me to compile (C?)code as well as run it. The scripts must be editable by the user.
I'm not sure I understand what you're trying to say.

It sounds to me like you're writing a program in Cocoa. This program parses the text in some other file (which you said might be a script of some kind) and does something with that text. The file must be user-editable. If this is the case, then lex and yacc are not only perfect for the job, it's literally what they were made for.

Do I understand what you're trying to do here, or have I interpreted something wrong?
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
calumr  (op)
Forum Regular
Join Date: Sep 2000
Location: UK
Status: Offline
Reply With Quote
Jun 15, 2001, 02:54 PM
 
What I want to happen is this:
1. User writes/downloads a script themselves
2. User runs unix command via my app (eg. ping)
3. Output of unix app is passed to script
4. Script parses useful information from ouput and passes it back to my app in a more structured form (possibly HTML)
5. User gets nice display of information

I think you understood it from your last post, but there it is.

I wish Apple would write an NSHTMLView of some sort. Carbon has an HTML library, and I hope its only a matter of time before Cocoa does too. I looked at embedded Mozilla, but that's a long way off yet.
     
parallax
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Jun 15, 2001, 07:06 PM
 
Originally posted by calumr:
<STRONG>I wish Apple would write an NSHTMLView of some sort. Carbon has an HTML library, and I hope its only a matter of time before Cocoa does too. I looked at embedded Mozilla, but that's a long way off yet.</STRONG>
You can embed HTML in NSTextViews (but I forgot the method to activate the rendering).
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
   
 
Forum Links
Forum 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 On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 02:49 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,