%option lex-compat
%option noyywrap
%option yylineno
%{
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
%}
%{
int INDENT=0,DEDENT=0,line=0;
%}
HASH "#"
NEWL [\n]
SPACE " "
MULTS """
COMP "e^"
LETTER ([a-zA-Z])
HEXL ([a-fA-F1-9])
ZERO "0"
EXP "10^"
COLON ":"
DOT "."
LPAREN "("
RPAREN ")"
PLUS "+"
MINUS "-"
SIGN ({PLUS}|{MINUS})
MULT "*"
DIV "/"
ASSIGN "="
EQUAL "=="
MORE_THAN ">"
LESS_THAN "<"
OR "or"
AND "and"
NOT "not"
IF "if"
ELSE "else"
LAMBDA "lambda"
FOR "for"
PASS "pass"
H ({"h"|"H"})
WHITESPACE {[\t\n]}
NUM_NZ [1-9]
NUM [0-9]
INTEGER ({SIGN}?({ZERO}|({ZERO}+/({NUM_NZ}+))))
REAL ({INTEGER}{DOT}{NUM}|{INTEGER}{DOT}{NUM}{EXP}{INTEGER})
HEX ({PLUS}?(ZERO}|{HEXL}+{H}))
COMPLEX ({PLUS}?{REAL}+{COMP}{SIGN}?(({REAL}/"i")|"i"))
ID ("'"{.}+/{LETTER}+"'")
STRING ("'"{.}+"'")
MULTISTRING ({MULTS}({.}+)|{NEWL}+{MULTS})
COMMENT ({.}+/{HASH})
%%
{SPACE} {INDENT++; };
{NEWL}
{
if(INDENT>DEDENT) printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"INDENT",yytext);
if(INDENT<DEDENT) printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"DEDENT",yytext);
DEDENT=INDENT; line++;};
{INTEGER} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"INTEGER",yytext);
{REAL} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"REAL",yytext);
{HEX} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"HEX",yytext);
{COMPLEX} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"COMPLEX",yytext);
{STRING} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"STRING",yytext);
{MULTISTRING} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"MULTISTRING",yytext);
{IF} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"IF",yytext);
{ELSE} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"ELSE",yytext);
LAMBDA} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"LAMBDA",yytext);
{FOR} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"FOR",yytext);
{PASS} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"PASS",yytext);
{ID} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"ID",yytext);
{COLON} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"COLON",yytext);
{LPAREN} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"LPAREN",yytext);
{RAREN} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"RPAREN",yytext);
{ARITH_OP} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"ARITH_OP",yytext);
{REL_OP} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"REL_OP",yytext);
{LOGIC_OP} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"LOGIC_OP",yytext);
{NOT} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"NOT",yytext);
{ASSIGN} printf("Line %d: Found token %s (lexeme: '%s').\n",yylineno,"ASSIGN",yytext);
. printf("Line %d: Invalid token ('%s').\n",yylineno,yytext);
%%
int main(int argc,char* argv[])
{
yylex();
}
我在第 102 行有一个无法识别的规则,其中包含 yylex();
. 我做错了什么,错误在哪里?我找不到它了。
hw1.text , line 102: unrecognized rule
当我尝试编译它时,我得到了这个错误。我正在写flex hw1.text
,文件和文件flex
在同一个文件夹中。
根据已删除的未回答添加的评论
我做了你让我做的事,出于某种原因,我收到了“printf”行的错误。
我已经添加 :
ARITH_OP ({PLUS}|{MINUS}|{DIV}|{MULT})
REL_OP ({MORE_THAN}|{LESS_THAN}|{EQUAL})
LOGIC_OP ({AND}|{OR})
可能是什么问题?