@q Copyright (c) 1990 The Regents of the University of California. @>
@q All rights reserved. @>
@q This code is derived from software contributed to Berkeley by @>
@q Vern Paxson. @>
@q The United States Government has rights in this work pursuant @>
@q to contract no. DE-AC03-76SF00098 between the United States @>
@q Department of Energy and the University of California. @>
@q This file is part of SPLinT. @>
@q Redistribution and use in source and binary forms, with or without @>
@q modification, are permitted provided that the following conditions @>
@q are met: @>
@q 1. Redistributions of source code must retain the above copyright @>
@q notice, this list of conditions and the following disclaimer. @>
@q 2. Redistributions in binary form must reproduce the above copyright @>
@q notice, this list of conditions and the following disclaimer in the @>
@q documentation and/or other materials provided with the distribution. @>
@q Neither the name of the University nor the names of its contributors @>
@q may be used to endorse or promote products derived from this software @>
@q without specific prior written permission. @>
@q THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR @>
@q IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED @>
@q WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR @>
@q PURPOSE. @>
@**The lexer for \ifx\flex\UNDEFINED\.{flex}\else\flex\ \fi syntax.
\ifx\flex\UNDEFINED
\input limbo.sty
\input yystype.sty
\input grabstates.sty
\immediate\openout\stlist=fil_states.h
\fi
The original lexer for \flex\ grammar relies on a few rules that use `trailing context'.
The lexing mechanism implemented by \splint\ cannot process such rules properly in general.
The rules used by \flex\ match fixed-length trailing context only, which makes it possible
to replace them with ordinary patterns and use |yyless()| in the actions.
@(fil.ll@>=
@G
%{@> @<Preamble for \flex\ lexer@> @=%}
@> @<Options for \flex\ input lexer@> @=
@> @<Output file for \flex\ input lexer@> @=
@> @<State definitions for \flex\ input lexer@> @=
@> @<Definitions for \flex\ input lexer@> @=
%%
@> @<Postamble for \flex\ input lexer@> @=
@> @<Common patterns for \flex\ lexer@> @=
@> @<Patterns for \flex\ lexer@> @=
%%
@> @<Auxilary code for \flex\ lexer@> @=
@g
@ Bootstrap lexer.
@(ssfs.ll@>=
@G
%{@> @<Preamble for \flex\ lexer@> @=%}
@> @<Options for \flex\ input lexer@> @=
@> @<Output file for the bootstrap \flex\ lexer@> @=
@> @<Definitions for \flex\ input lexer@> @=
%%
@> @<Common patterns for \flex\ lexer@> @=
@> @<Catchall rule for the bootstrap lexer@> @=
%%
@> @<Auxilary code for the bootstrap \flex\ lexer@> @=
@g
@ @<Preamble for \flex\ lexer@>=
@ There are a few options that are necessary to ensure that the lexer
functions properly. Some of them (like \.{caseless}) directly
affect the behavior of the scanner, others (e.g.~\.{noyy\_top\_state})
prevent generation of unnecessary code.
@<Options for \flex\ input lexer@>=
@G(fs1)
%option caseless nodefault stack noyy_top_state
%option nostdinit
%option bison-bridge
%option noyywrap nounput noinput reentrant
%option debug
%option stack
@ @<Output file for the bootstrap \flex\ lexer@>=
@G(fs1)
%option outfile="ssfs.c"
@g
@*1 Regular expression and state definitions.
The lexer uses a large number of states to control its operation. Both
section~1 and section~2 rules rely on the scanner being in the
appropriate state. Otherwise (see \.{symbols.sty} example) the lexer
may parse the same fragment in a wrong context.
@<State definitions for \flex\ input lexer@>=
@G(fs1)
%x SECT2 SECT2PROLOG SECT3 CODEBLOCK PICKUPDEF SC CARETISBOL NUM QUOTE
%x FIRSTCCL CCL ACTION RECOVER COMMENT ACTION_STRING PERCENT_BRACE_ACTION
%x OPTION LINEDIR CODEBLOCK_MATCH_BRACE
%x GROUP_WITH_PARAMS
%x GROUP_MINUS_PARAMS
%x EXTENDED_COMMENT
%x COMMENT_DISCARD
@ Somewhat counterintuitively, \flex\ definitions do not {\it always\/} have to be
fully formed regular expressions. For example, after
$$
\hbox to 3in{\flexrenstyle{BOGUS}\hfil\.{\^[a-}}
$$
one can form the following action:
$$
\hbox to 3in{\flexrenstyle{BOGUS}\.{t]}\hfil\.{;}}
$$
although without the `\.{\^}' in the definition of
`\flexrenstyle{BOGUS}' \flex\ would have put a `\.{)}' inside the
character class. We will assume such (rather counterproductive) tricks
are not used. If the definition is not a well-formed regular
expression the pretty printing will be suspended.
@<Definitions for \flex\ input lexer@>=
@G(fs1)
WS [[:blank:]]+
OPTWS [[:blank:]]*
NOT_WS [^[:blank:]\r\n]
NL \r?\n
NAME ([[:alpha:]_][[:alnum:]_-]*)
NOT_NAME [^[:alpha:]_*\n]+
^{NAME} {@> @<Copy the name and start a definition@> @=}
{SCNAME} @> @[TeX_( "/RETURNNAME" );@] @=
^{OPTWS}{NL} {@> @[TeX_( "/flinc@@linenum/yylexnext" );@]/* allows blank lines in section 1 */@=}
{OPTWS}{NL} {@> @[TeX_( "/flinc@@linenum/yylexnext" );@]/* maybe end of comment line */@=}
}
@ Ignore setting |brace_start_line| as it is only used internally to report errors.
@<Begin the \prodstyle{\%top} directive@>=
@[TeX_( "/flinc@@linenum" );@]@;
@[TeX_( "/def/flbrace@@depth{1}" );@]@;
@[TeX_( "/yypushstate{CODEBLOCK_MATCH_BRACE}/yylexnext" );@]@;
/* This is the same as \flexsnstyle{COMMENT}, but is discarded rather than output. */
<COMMENT_DISCARD>{
"*/" {@> @[TeX_( "/yypopstate/yylexnext" );@]@=}
"*" {@> @[TeX_( "/yylexnext" );@]@=}
[^*\n] {@> @[TeX_( "/yylexnext" );@]@=}
{NL} {@> @[TeX_( "/flinc@@linenum/yylexnext" );@]@=}
}
@ The \flexsnstyle{RECOVER} state is never used for typesetting and is only added for completeness.
@<Patterns for \flex\ lexer@>=
@G(fs2)
<RECOVER>.*{NL} {@> @[TeX_( "/flinc@@linenum/yyBEGIN{INITIAL}/yylexnext" );@] @=}
@ Like \bison, \flex\ allows insertion of \Cee\ code in the middle of the input file.
@<Patterns for \flex\ lexer@>=
@G(fs2)
<SECT2PROLOG>{
^"%{".* {@> @<Consume the brace and increment the brace level@> @=}
^"%}".* {@> @<Consume the brace and decrement the brace level@> @=}
@ A pattern below (for the character class processing) had to be broken into two lines.
A special symbol ($\odot$\gtextidx{$\odot$ separator, \flex}{0. separator, flex}{\flexidxdomain})
has been inserted to indicate that a break had occured.
The macros for \flex\
typesetting use a different mechanism from that of \bison\ macros and allow typographic
corrections to be applied to sections of the \flex\ code represented by various nonterminals.
These corrections can also be delayed. For the details, an interested reader may consult
\.{yyunion.sty}.
@<Patterns for \flex\ lexer@>=
@G(fs2)
<SECT2>{
^{OPTWS}{NL} {@> @[TeX_( "/flinc@@linenum/yylexnext" );@] /* allow blank lines in section 2 */ @=}
^{OPTWS}"%{" {@> @<Start braced code in section 2@> @=}
^{OPTWS}"<" {@> @[TeX_( "/ifflsf@@skip@@ws/else/yyBEGIN{SC}/fi/yylexreturnraw<" );@] @=}
^{OPTWS}"^" {@> @[TeX_( "/yylexreturnraw^" );@] @=}
\" {@> @[TeX_( "/yyBEGIN{QUOTE}/yylexreturnxchar/flquotechar" );@] @=}
"{"[[:digit:]] {@> @<Process a repeat pattern@> @=}
"$"([[:blank:]]|{NL}) {@> @[TeX_( "/yyless{1}/yylexreturnraw/$" );@] @=}
{WS}"%{" {@> @<Process braced code in the middle of section 2@> @=}
{WS}"|".*{NL} {@> @<Process a deferred action@> @=}
^{WS}"/*" {@> @<Process a comment inside a pattern@> @=}
^{WS} @> ; /* allow indented rules */@=
{WS} {@> @<Decide whether to start an action or skip whitespace inside a rule@> @=}
{OPTWS}{NL} {@> @<Finish the line and/or action@> @=}
"{"{NAME}"}"[[:space:]]? {@> @<Process a named expression after checking for whitespace at the end@> @=}
"/*" {@> @<Decide if this is a comment@> @=}
"(?#" {@> @<Determine if this is extended syntax or return a parenthesis@> @=}
"(?" {@> @<Determine if this is a parametric group or return a parenthesis@> @=}
"(" {@> @[TeX_( "/flsf@@push/yylexreturnraw/(" );@] @=}
")" {@> @[TeX_( "/flsf@@pop/yylexreturnraw/)" );@] @=}
@ @<Start processing a character class@>=
@[TeX_( "/edef/flnmstr{/the/yytext}" );@]@;
@[TeX_( "/yyless{1}" );@]@;
@[TeX_( "/yyBEGIN{FIRSTCCL}" );@]@;
@[TeX_( "/yylexreturnraw[" );@]@;
@ Return a special \prodstyle{CHAR} and return the whitespace back into the input.
The braces and the possible trailing whitespace will be dealt with by the typesetting code.
@<Process a named expression after checking for whitespace at the end@>=
@[TeX_( "/edef/flend@@ch{/the/yytextlastchar}" );@]@;
@[TeX_( "/ifnum/flend@@ch=`/}/relax" );@]@;
@[TeX_( " /flend@@is@@wsfalse" );@]@;
@[TeX_( "/else" );@]@;
@[TeX_( " /flend@@is@@wstrue" );@]@;
@[TeX_( "/fi" );@]@;
@[TeX_( "/toksa/expandafter{/astformat@@flnametok}" );@]@;
@[TeX_( "/let/astformat@@flnametok/empty" );@]@;
@[TeX_( "/edef/next{/yylval{{/nx/flnametok{/the/yytext}{/the/toksa}}{}{/the/yyfmark}{/the/yysmark}}}/next" );@]@;
@[TeX_( "/ifflend@@is@@ws" );@]@;
@[TeX_( " /unput{ }" );@]@;
@[TeX_( "/fi" );@]@;
@[TeX_( "/yylexreturn{CHAR}" );@]@;
@ @<Decide if this is a comment@>=
@[TeX_( "/ifflsf@@skip@@ws" );@]@;
@[TeX_( " /yypushstate{COMMENT_DISCARD}" );@]@;
@[TeX_( " /yylexnext" );@]@;
@[TeX_( "/else" );@]@;
@[TeX_( " /yyless{1}" );@]@;
@[TeX_( " /yylexreturnraw//" );@]@;
@[TeX_( "/fi" );@]@;
@ @<Determine if this is extended syntax or return a parenthesis@>=
@[TeX_( "/iffllex@@compat" );@]@;
@[TeX_( " /yybreak{/yyless{1}/flsf@@push/yylexreturnraw(}" );@]@;
@[TeX_( "/else" );@]@;
@[TeX_( " /ifflposix@@compat" );@]@;
@[TeX_( " /yybreak@@{/yyless{1}/flsf@@push/yylexreturnraw(}" );@]@;
@[TeX_( " /else" );@]@;
@[TeX_( " /yybreak@@{/yypushstate{EXTENDED_COMMENT}}" );@]@;
@[TeX_( " /fi" );@]@;
@[TeX_( "/yycontinue" );@]@;
@ @<Determine if this is a parametric group or return a parenthesis@>=
@[TeX_( "/flsf@@push" );@]@;
@[TeX_( "/iffllex@@compat" );@]@;
@[TeX_( " /yybreak{/yyless{1}}" );@]@;
@[TeX_( "/else" );@]@;
@[TeX_( " /ifflposix@@compat" );@]@;
@[TeX_( " /yybreak@@{/yyless{1}}" );@]@;
@[TeX_( " /else" );@]@;
@[TeX_( " /yybreak@@{/yyBEGIN{GROUP_WITH_PARAMS}}" );@]@;
@[TeX_( " /fi" );@]@;
@[TeX_( "/yycontinue" );@]@;
@[TeX_( "/yylexreturnraw(" );@]@;
@ @<Auxilary code for \flex\ lexer@>=
void define_all_states( void ) {
@<Collect state definitions for the \flex\ lexer@>@;
}
@ @<Collect state definitions for the \flex\ lexer@>=
#define _register_name( name ) @[Define_State( #name, name )@]
#include "fil_states.h"
#undef _register_name
@ @<Catchall rule for the bootstrap lexer@>=
@G(fs2)
<*>. {@> @[TeX_( "/yyerrterminate" );@] @=}
@ The drive expects this function to be defined but the bootstrap
lexer has no need for it. We leave it in to appease the compiler.
@<Auxilary code for the bootstrap \flex\ lexer@>=
void define_all_states( void ) {
@<Collect state definitions for the bootstrap \flex\ lexer@>@;
}
@ @<Collect state definitions for the bootstrap \flex\ lexer@>=
#define _register_name( name ) @[Define_State( #name, name )@]
/* The \flexsnstyle{INITIAL} state is generated automatically */
#undef _register_name