使用FLEX工具生成SNL词法分析代码(吉林大学)

本文详细介绍了如何使用FLEX工具来生成SNL(符号串语言)的词法分析代码,包括定义词法规则、处理保留字、识别不同类型的符号等。此外,还提供了部分代码示例,帮助读者理解和应用。
摘要由CSDN通过智能技术生成

char *yytext;
#line 1 “my6.l”
#line 3 “my6.l”
#include “string.h”
#include “stdio.h”

typedef enum
{
ENDFILE, ERROR,
PROGRAM,PROCEDURE,TYPE,VAR,IF,THEN, ELSE,FI,WHILE,DO,ENDWH,BEGIN1,END1,READ,
WRITE,ARRAY,OF,RECORD,RETURN1,
INTEGER, CHAR1,
ID, INTC, CHARC,
ASSIGN,EQ,LT,PLUS,MINUS,TIMES,OVER,LPAREN,RPAREN,DOT,COLON,SEMI,COMMA,LMIDPAREN,
RMIDPAREN,UNDERANGE
} LexType;

#define MAXRESERVED 21
static struct
{char* str;
LexType tok;
} reservedWords[MAXRESERVED]
={ {“program”,PROGRAM}, {“type”,TYPE}, {“var”,VAR},
{“procedure”,PROCEDURE}, {“begin”,BEGIN1}, {“end”,END1},
{“array”,ARRAY}, {“of”,OF}, {“record”,RECORD}, {“if”,IF}, {“then”,THEN},
{“else”,ELSE}, {“fi”,FI}, {“while”,WHILE}, {“do”,DO}, {“endwh”,ENDWH},
{“read”,READ}, {“write”,WRITE}, {“return”,RETURN1},
{“integer”,INTEGER}, {“char”,CHAR1} };

LexType reservedLookup (char * s);
char yylval[20];
int flag=0;
#line 525 “lex.yy.c”

#define INITIAL 0

#ifndef YY_NO_UNISTD_H
/* Special case for “unistd.h”, since it is non-ANSI. We include it way
* down here because we want the user’s section 1 to have been scanned first.
* The user has a chance to override it with an option.
*/
#include <unistd.h>
#endif

#ifndef YY_EXTRA_TYPE
#define YY_EXTRA_TYPE void *
#endif

static int yy_init_globals (void );

/* Accessor methods to globals.
These are made visible to non-reentrant scanners for convenience. */

int yylex_destroy (void );

int yyget_debug (void );

void yyset_debug (int debug_flag );

YY_EXTRA_TYPE yyget_extra (void );

void yyset_extra (YY_EXTRA_TYPE user_defined );

FILE *yyget_in (void );

void yyset_in (FILE * in_str );

FILE *yyget_out (void );

void yyset_out (FILE * out_str );

yy_size_t yyget_leng (void );

char *yyget_text (void );

int yyget_lineno (void );

void yyset_lineno (int line_number );

/* Macros after this point can all be overridden by user definitions in
* section 1.
*/

#ifndef YY_SKIP_YYWRAP
#ifdef __cplusplus
extern “C” int yywrap (void );
#else
extern int yywrap (void );
#endif
#endif

static void yyunput (int c,char \*buf_ptr  );

#ifndef yytext_ptr
static void yy_flex_strncpy (char *,yyconst char *,int );
#endif

#ifdef YY_NEED_STRLEN
static int yy_flex_strlen (yyconst char * );
#endif

#ifndef YY_NO_INPUT

#ifdef __cplusplus
static int yyinput (void );
#else
static int input (void );
#endif

#endif

/* Amount of stuff to slurp up with each read. */
#ifndef YY_READ_BUF_SIZE
#define YY_READ_BUF_SIZE 8192
#endif

/* Copy whatever the last rule matched to the standard output. */
#ifndef ECHO
/* This used to be an fputs(), but since the string might contain NUL’s,
* we now use fwrite().
*/
#define ECHO do { if (fwrite( yytext, yyleng, 1, yyout )) {} } while (0)
#endif

/* Gets input and stuffs it into “buf”. number of characters read, or YY_NULL,
* is returned in “result”.
*/
#ifndef YY_INPUT
#define YY_INPUT(buf,result,max_size)
if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive )
{
int c = ‘*’;
size_t n;
for ( n = 0; n < max_size &&
(c = getc( yyin )) != EOF && c != ‘\n’; ++n )
buf[n] = (char) c;
if ( c == ‘\n’ )
buf[n++] = (char) c;
if ( c == EOF && ferror( yyin ) )
YY_FATAL_ERROR( “input in flex scanner failed” );
result = n;
}
else
{
errno=0;
while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin))
{
if( errno != EINTR)
{
YY_FATAL_ERROR( “input in flex scanner failed” );
break;
}
errno=0;
clearerr(yyin);
}
}
\

#endif

/* No semi-colon after return; correct usage is to write “yyterminate();” -
* we don’t want an extra ‘;’ after the “return” because that will cause
* some compilers to complain about unreachable statements.
*/
#ifndef yyterminate
#define yyterminate() return YY_NULL
#endif

/* Number of entries by which start-condition stack grows. */
#ifndef YY_START_STACK_INCR
#define YY_START_STACK_INCR 25
#endif

/* Report a fatal error. */
#ifndef YY_FATAL_ERROR
#define YY_FATAL_ERROR(msg) yy_fatal_error( msg )
#endif

/* end tables serialization structures and prototypes */

/* Default declaration of generated scanner - a define so the user can
* easily add parameters.
*/
#ifndef YY_DECL
#define YY_DECL_IS_OURS 1

extern int yylex (void);

#define YY_DECL int yylex (void)
#endif /* !YY_DECL */

/* Code executed at the beginning of each rule, after yytext and yyleng
* have been set up.
*/
#ifndef YY_USER_ACTION
#define YY_USER_ACTION
#endif

/* Code executed at the end of each rule. */
#ifndef YY_BREAK
#define YY_BREAK break;
#endif

#define YY_RULE_SETUP
YY_USER_ACTION

/** The main scanner function which does all the work.
*/
YY_DECL
{
register yy_state_type yy_current_state;
register char *yy_cp, *yy_bp;
register int yy_act;

#line 35 “my6.l”

#line 709 “lex.yy.c”

if ( !(yy_init) )
	{
	(yy_init) = 1;

#ifdef YY_USER_INIT
YY_USER_INIT;
#endif

	if ( ! (yy_start) )
		(yy_start) = 1;	/\* first start state \*/

	if ( ! yyin )
		yyin = stdin;

	if ( ! yyout )
		yyout = stdout;

	if ( ! YY_CURRENT_BUFFER ) {
		yyensure\_buffer\_stack ();
		YY_CURRENT_BUFFER_LVALUE =
			yy\_create\_buffer(yyin,YY_BUF_SIZE );
	}

	yy\_load\_buffer\_state( );
	}

while ( 1 )		/\* loops until end-of-file is reached \*/
	{
	yy_cp = (yy_c_buf_p);

	/\* Support of yytext. \*/
	\*yy_cp = (yy_hold_char);

	/\* yy\_bp points to the position in yy\_ch\_buf of the start of

* the current run.
*/
yy_bp = yy_cp;

	yy_current_state = (yy_start);

yy_match:
do
{
register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)];
if ( yy_accept[yy_current_state] )
{
(yy_last_accepting_state) = yy_current_state;
(yy_last_accepting_cpos) = yy_cp;
}
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 36 )
yy_c = yy_meta[(unsigned int) yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
++yy_cp;
}
while ( yy_base[yy_current_state] != 41 );

yy_find_action:
yy_act = yy_accept[yy_current_state];
if ( yy_act == 0 )
{ /* have to back up */
yy_cp = (yy_last_accepting_cpos);
yy_current_state = (yy_last_accepting_state);
yy_act = yy_accept[yy_current_state];
}

	YY_DO_BEFORE_ACTION;

	if ( yy_act != YY_END_OF_BUFFER && yy_rule_can_match_eol[yy_act] )
		{
		yy_size_t yyl;
		for ( yyl = 0; yyl < yyleng; ++yyl )
			if ( yytext[yyl] == '\n' )
				
  • 9
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值