Re: Simple question on lex/yacc specifications

russell kym horsell <kym@svalbard.freeshell.org>
Sun, 15 Mar 2009 02:59:22 +0000 (UTC)

          From comp.compilers

Related articles
Simple question on lex/yacc specifications eric.fowler@gmail.com (Eric Fowler) (2009-03-13)
Re: Simple question on lex/yacc specifications eric.fowler@gmail.com (Eric Fowler) (2009-03-14)
Re: Simple question on lex/yacc specifications kym@svalbard.freeshell.org (russell kym horsell) (2009-03-15)
Re: Simple question on lex/yacc specifications max@gustavus.edu (Max Hailperin) (2009-03-15)
Re: Simple question on lex/yacc specifications eric.fowler@gmail.com (Eric Fowler) (2009-03-15)
| List of all articles for this month |

From: russell kym horsell <kym@svalbard.freeshell.org>
Newsgroups: comp.compilers
Date: Sun, 15 Mar 2009 02:59:22 +0000 (UTC)
Organization: Central Iowa (Model) Railroad, Plano, TX, USA
References: 09-03-058
Keywords: yacc, parse
Posted-Date: 15 Mar 2009 16:57:28 EDT

Eric Fowler <eric.fowler@gmail.com> wrote:
> This should be pretty easy: I am a relative newbie to lex & yacc, and
> I am writing a parser for NMEA strings as a toy project. NMEA strings
> are output by marine electronic equipment, and the ones I care about
> now look like this:


> !AIVDM,1,1,,B,15N@wP0P00o?
> ruLK?UMMbOw>04KH,0*31
> !AIVDM,1,1,,B,15Mj2u001vo?tV8K?<ub>8;@0D1<,0*17
> !AIVDM,2,1,3,B,55P5TL01VIaAL@7WKO@mBplU@<PDhh000000001S;AJ::4A80?4i@E53,0*3E
> !AIVDM,2,2,3,B,1@0000000000000,2*55
> !AIVDM,1,1,,B,15N:bCPP01G?jPfKADGUvww>2<17,0*4D


> So they are pretty simple, and I have a pretty good handle on parsing them.


"Parsing" is probably overkill for what looks strictly like text processing.


You can write a regular expression for the language you've shown examples of,
with the "field" of some lines simply being an empty or integer value
with R.E. in flex/lex given by
[0-9]*


i.e. 0 or more digits.


A sometimes-overlooked feature of lex is ability to define common
sub-expressions in the first lex section like:


NUM [0-9]+
OPT_NUM {NUM}?
OTHER_CRAP [^,]*
REST .*


and then call them up in the 2nd section using:


"!AIVDM,"{OPT_NUM}\,{OPT_NUM}\,{OPT_NUM}{REST} {do_something_with(yytext);}


Of course, this means the "parsing" (processing) of the text string
starting with !AIVDM needs to use string stuff like strchr(s,',') to find
commas and such. But this is not as fiddly as having to write and manage
any yacc component of your project.



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.