help-flex
[Top][All Lists]
Advanced

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

Re: Look ahead question (trailing context)


From: Hans Aberg
Subject: Re: Look ahead question (trailing context)
Date: Fri, 2 Apr 2004 00:55:06 +0200

At 22:37 +0200 2004/04/01, Henrik Sorensen wrote:
>> >So if both the previously matched token and the token following
>> >the matched ')'  is known, it can be determined whether a matched keyword
>> >token, is really a variable name or a keyword.
>> This is very difficult to do in Flex, especially if the stuff within (...)
>> can be an expression. In principle, you can make a lookahead for the
>> matching ")", resolve it at the next token, and then put the stuff back in
>> the line. But that would require some work.
>> This has been put up as a suggestion, but Flex can't do that for now.
>
>Could it be done by implementing a special trailing context operator say '§'
>
>R(§)S: would match R( only if followed by )S,where the parenthesis must match.
>
>The stuff matched by § are only checked for pairs of '(' ')'

I do not know about your idea.

The idea that comes to my mind is to have a parenthesis depth variable, and
then setting a special context, which then finds the matching ")",
something like this:

int parenthesis_depth;

%%

%x find_display_match

%%

"DISPLAY" { parenthesis_depth = 0; BEGIN(find_display_match); }

<find_display_match>{
  "("  { ++parenthesis_depth; }
  ")"  { if (--parenthesis_depth == 0)  BEGIN(display_token); }
   .
}

<display_token>{
  "=" { put back stuff after initial "DISPLAY"; BEGIN(INITIAL);
        return VARNAME; }
  ";" { put back stuff after initial "DISPLAY"; BEGIN(INITIAL);
        return DISPLAY; }
}

Of course, this might not suffice in PL1, if the (...) part requires
similar wrestling with contexts.

  Hans Aberg






reply via email to

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