help-flex
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to negate definitions?


From: Hans Aberg
Subject: Re: how to negate definitions?
Date: Tue, 4 Dec 2001 20:57:27 +0100

At 06:06 -0800 2001/12/04, Crni Gorac wrote:
>I would like to have something like this in my Lex
>file:
>
>whtspc [ \t\n]
>spcl [#\\]
>%%
>[^{whtspc}{spcl}] { do_something(); }
>%%
>
>But this of course doesn't work because curlies are
>not special characters in character class. So I had to
>instead use:
>
>%%
>[^ \t\n#\\] { do_something(); }
>%%
>
>What would be proper way to implement former? Thanks.

You do not say exactly what you want to do with your rules. If the intent
is to zip out whiespace and enable comments, here is what I used in one
project:

%{

#include "pg_parser.h"

#define yylval pg_parserlval

#include "pg_parser.tab.h"

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

#define YYERRCODE       256

#define get_text(pos, red) yylval.text = std::string(yytext + (pos), yyleng
- (pos + red))

int pg_parserlineno0 = 0;

%}

%option yylineno

%x comment

comment_begin "**"
comment_end   "##"

identifier    [[:alpha:]][[:alnum:]_-]*
terminal      [[:graph:]]*

%%

[[:space:]]+     { /* Skip white-space. */ }

"?"              { return help_key; }
"??"             { return grammar_key; }
"%bye"|"%quit"   { return quit_key; }

"%delete"        { get_text(0, 0); return delete_key; }
"%start"         { get_text(0, 0); return sentence_key; }
"%sentence"      { get_text(0, 0); return sentence_key; }
"%rule"          { get_text(0, 0); return rule_key; }
"%rules"         { get_text(0, 0); return rule_key; }
":"              { get_text(0, 0); return production_symbol; }
"->"             { get_text(0, 0); return production_symbol; }

"0"               { get_text(0, 0); return grammar_zero_string; }
"`"{terminal}"'"  { get_text(1, 1); return terminal_name; }
{identifier}      { get_text(0, 0); return nonterminal_name; }

<INITIAL>.  { return (unsigned char)yytext[0]; }
<<EOF>>     { return EOF; }

{comment_begin}  { BEGIN(comment); pg_parserlineno0 = yylineno; }
<comment>{
  [^#]+          { }
  "#"[^#]+       { }
  {comment_end}  { BEGIN(INITIAL); }
}

"//".*\n        { }

%%

In the above, whitespace separates tokens, the exclusive start condition
<comment> zips out multiline comments, "//".*\n  { } zips out single line
comments. Other rules can be entered quite freely, since Flex, as is
usually expected, identifies the longest rule applicable. And one gets a
line count, which is useful for generating helpful error messages.

  Hans Aberg





reply via email to

[Prev in Thread] Current Thread [Next in Thread]