Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cwjcc!gatech!udel!mmdf From: MROBINSON@wash-vax.bbn.com Newsgroups: comp.sys.amiga Subject: Re: C scanf question Message-ID: <20201@louie.udel.EDU> Date: 21 Jul 89 20:14:40 GMT Sender: mmdf@udel.EDU Lines: 37 [Charles Brown made a comment that the scanf family was ill-suited to parsing lines] First, I can't see anything wrong with the original code. Then again, I don't know much about Lattice C. I use scanf a bit, though, and wanted to pass on some knowledge. My reference book on C is Harbison and Steele's "C: A Reference Manual", mainly because it is the most useful book on C that I've ever seen. Many of the people on my hall at work stop by to borrow my book on a daily basis, it's very irritating. Please go get your own copy. Anyway. The versions of C that I use support formats of the following type: fscanf(f,"%c%c%s%*[^\n]\n",&c1,&c2,%str); I'll explain the %*[^\n] format in steps. The * says "whatever is read in for this format, throw it away". That means we're skipping something in the input. The [] mean "match any string made of characters found within these braces". The ^ means "oh, I mean all the characters EXCEPT the ones I list". Thus, this format means "find all the characters before the end of the line, and throw them away". I use this format when I want to allow comments at the end of the line. Now, about parsing lines. If your line format is pretty simple, the scanf family can handle it no problem. If it's as complicated as the general case of regular expressions, forget scanf, and make a real token-oriented parser. If it's an LL or LR grammar, use yacc or Snobol or Prolog's definite clause grammars. If it's context sensitive to any real degree, use Prolog DCGs, and take some smart pills. Your example looked like it fell into the pretty simple category, so take some time to learn scanf. If any of you out there are seriously into parsing, I strongly encourage you to look into Quintus or BIM Prolog and Warren and Pereira's Definite Clause Grammars. DCGs are way cool. I heard a guy from Berkeley give a talk once about using DCGs to translate among four hardware description languages. I've also used them myself for some medium-sized projects, with success and pleasure. --Max Robinson, mrobinson@wash-vax.bbn.com