我想从字符串而不是文件解析.我知道v可以使用yy_scan_string fn来做它.但对我来说它不能正常工作所以请帮助我
lex和yacc应该一起使用.
扫描仪是哪一个,解析器是哪一个?
哪一个创建扫描仪,哪一个创建解析器?
我刚开始弯曲.
使用flex时出现构建错误.也就是说,我使用flex生成了一个.c文件,并且在运行它时遇到了这个错误:
1>lextest.obj : error LNK2001: unresolved external symbol "int __cdecl isatty(int)" (?isatty@@YAHH@Z)
1>C:\...\lextest.exe : fatal error LNK1120: 1 unresolved externals
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的lex文件(从这里抓取):
/*** Definition section ***/
%{
/* C code to be copied verbatim */
#include <stdio.h>
%}
/* This tells flex to read only one input file */
%option noyywrap
%%
/*** Rules section ***/
/* [0-9]+ matches a string of one or more digits */
[0-9]+ {
/* yytext is a string containing the matched text. …Run Code Online (Sandbox Code Playgroud) 如何匹配aa的数量应该是10分钟的aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ...
我的意思是我知道这种方式:
[a][a][a][a][a][a][a][a][a][a][a][a][a]a*b
Run Code Online (Sandbox Code Playgroud)
但是必须有一个更好的优雅方法,如果我的最小数量变为100 ...
它是什么?我试图匹配(a ^ n)b类似于n可以是任何东西的东西
编辑:
我忘了提到这是使用lex和yacc来完成的...其中lex必须将一个令牌返回给yacc.
%{
#include "y.tab.h"
%}
%%
aaaaaaaaaa[a]*b {return ok;}
\n {return '\n';}
. {return 0;}
%%
Run Code Online (Sandbox Code Playgroud) 在下面跳过注释的代码中, 的含义是什么BEGIN(INITIAL)?
%x C_COMMENT
“/*”{ 开始(C_COMMENT); }
“*/”{ 开始(初始);}
。{ }
在较大的程序中,我给出了以下(flex/bison)
在flex中:
pn [\+|\-]
dig [0-9]+
exp [e|E]{dig}+
Run Code Online (Sandbox Code Playgroud)
.
.
.
"+" {printf("+ detected\n");
return PLUS_SIGN;}
{pn}?{dig}+ { printf("digit detected - %s\n",yytext);
sscanf(yytext, "%d", (int*)&yylval);
return TYPE_INT;}
Run Code Online (Sandbox Code Playgroud)
在野牛:
expr:
expr PLUS_SIGN expr
{
$$ = $1 + $3;
printf(" $$=%f\n",$$);
}
| TYPE_INT
{
$$ = (int)$1;
printf(" $$=%f\n",$$);
}
;
Run Code Online (Sandbox Code Playgroud)
问题是:
当我给2 + 2时,它识别2和+2而不是2,+,2
我怎么能让它做这个添加?
我想使用flex为我的词法分析器制作一个make文件,我已经尝试过很多make文件的模板,但它没有用,所以请帮我构建一个这里是编译代码的行:
lex -t lexical.l > lexical.c
cc -c -o lexical.o lexical.c
cc -o lexy lexical.o -ll
Run Code Online (Sandbox Code Playgroud) 我正在做这个程序来理解词法分析器的正常功能.但是,当我正在编译此程序时,它显示错误消息::
"bl:25:在行动中遇到EOF".
我不明白为什么,因为我在Google上搜索理由而且它说像一些额外的关闭括号可能在行动部分,但事实并非如此.
%option noyywrap
%{
%}
%%
#include<.*> {fprintf(yyout,"\nPREPROCESSOR: %s",yytext);}
main {fprintf(yyout, "\nMAIN:%s",yytext);}
"void"|"int"|"for"|"if"|"return" {fprintf(yyout, "\n KEYWORD:%s",yytext);}
"{"|"}"|"("|")"|";"|"," {fprintf(yyout,"\n SPECIAL SYMBOLS:%s",yytext);}
"<"|">"|"=="|"<="|">=" {fprintf(yyout,"\nCONDITIONAL OPERATORS:%s",yytext);}
"==" {fprinf(yyout,"\nASSIGNMENT OPERATORS:%s",yytext);}
"++"|"--" {fprintf(yyout,"\nINC-DECR OPERATORS:%s",yytext);}
"printf".*|"scanf".* {fprintf(yyout,"\nINBUILT FUNCTIONS:"%s",yytext);}
[0-9]* {fprintf(yyout,"\nNUMBERS:%s",yytext);}
[a-zA-Z] [a-zA-Z0-9_]* {fprintf(yyout,"\nIDENTIFIERS:%s",yytext);}
%%
int main(int argc,char*argv[])
{
yyin = fopen(argv[1],"r");
yyout = fopen(argv[2],"w");
yylex();
fclose(yyin);
fclose(yyout);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个 lex 程序如下。我遇到错误
在动作 LEX 程序中遇到 EOF
%{
#include<stdio.h>
#include<math.h>
#include "y.tab.h"
%}
%%
[ \t]+ ;
[0-9]+ {yylval = atoi(yytext);
return INTEGER;}
[-+*/] {return *yytext;}
"(" {return *yytext;}
")" {return *yytext;}
\n {return *yytext;}
. {char msg[25];
sprintf(msg,"%s <%s>","invalid character",yytext);
yyerror(msg);}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我吗?
这里给出了完整的例子:
import ply.lex as lex
import Property
# List of token names. This is always required
tokens = [
'CheckupInformation',
'Introduction',
'Information',
'perfect',
'sick',
'LPAREN',
'RPAREN',
'CHAR',
'NUMBER'
]
def t_CheckupInformation(t) : 'CheckupInformation' ; return t
def t_Introduction(t) : 'Introduction' ; return t
def t_Information(t) : 'Information' ; return t
def t_perfect(t): 'perfect'; return t
def t_sick(t) : 'sick'; return t
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_CHAR = r'[a-zA-Z_][a-zA-Z0-9_\-]*'
t_ignore = " \t"
# Define a rule so we …Run Code Online (Sandbox Code Playgroud)