我遇到了Flex和野牛集成的问题,特别是与"stray
“in program”and
“expected”;“before '{' token."
这样的错误相关的问题。我已经检查了代码,但无法确定确切的问题。
错误:
lab4.l: In function 'yylex':
lab4.l:10:18: error: expected ';' before '{' token
"#include" { return INCLUDE; }
^
lab4.l:29:13: error: stray '\' in program
[-]?[0-9]+(\.[0-9]+)? { return CONST; }
^
lab4.l:52:3: error: stray '\' in program
[\n] { line++; }
^
字符串
lex code:
%{
#include "lab4.tab.h"
int line = 1;
%}
%option noyywrap
%%
"#include" { return INCLUDE; }
"cin" { return CIN; }
"cout" { return COUT; }
"main()" { return MAIN; }
"namespace" { return NAMESPACE; }
"std" { return STD; }
"int" { return INT; }
"void" { return VOID; }
"double" { return DOUBLE; }
"<iostream>" { return IOSTREAM; }
"<fstream>" { return IOSTREAM; }
"using" { return USING; }
"if" { return IF; }
"else" { return ELSE; }
"while" { return WHILE; }
"struct" { return STRUCT; }
[1-9]* { return DIGIT; }
[a-zA-Z][1-9]* {0, 250} { return ID; }
[-]?[0-9]+(\.[0-9]+)? { return CONST; }
[1-9]* { return NZD; }
[a-zA-Z]* { return CHAR }
"{" { return LBR; }
"}" { return RBR; }
"(" { return LPR; }
")" { return RPR; }
">>" { return RS; }
"<<" { return LS; }
"<" { return LT; }
">" { return GT; }
">=" { return GTE; }
"<=" { return LTE; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return MUL; }
"/" { return DIV; }
";" { return SEMICOLON; }
"=" { return ASSIGN; }
"!=" { return DIFF; }
"." { return DOT; }
[\n] { line++; }
%%
型
野牛代码:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int yylex();
extern int yyparse();
extern FILE* yyin;
extern int line;
void yyerror();
%}
%token DIGIT CONST ID CHAR NZD
%token INCLUDE USING NAMESPACE STD IOSTREAM FSTREAM
%token INT MAIN DOUBLE VOID STRUCT
%token LPR RPR LBR RBR
%token CIN COUT
%token IF WHILE SEMICOLON DOT
%left ELSE
%left ASSIGN DIFF LS RS
%left LT GT LTE GTE
%left PLUS MINUS MUL DIV
%%
program : headers_block main_block
;
headers_block : headers_list USING NAMESPACE STD SEMICOLON
;
headers_list : header headers_list
| header
header : INCLUDE library
;
library : IOSTREAM
| FSTREAM
;
main_block : type MAIN LBR instr_list RBR
;
type : VOID
| INT
| DOUBLE
| triple
| point
;
triple : NZD DOT DIGIT DIGIT DIGIT
;
point : STRUCT LBR INT ID SEMICOLON INT ID SEMICOLON RBR ID SEMICOLON
;
instr_list : instr instr_list
| instr
;
instr : atribuire
| read
| write
| if_instr
| while_instr
;
atribuire : type ID SEMICOLON
| ID ASSIGN expr SEMICOLON
;
expr : ID
| CONST
| operand expr
;
operand : ID
| CONST
| CONST operator ID
| id_expr
;
id_expr : ID operator ID
;
operator : PLUS
| MINUS
| DIV
| MUL
| LT
| GT
| DIFF
| LTE
| GTE
| ASSIGN
| RS
| LS
;
read : CIN RS ID SEMICOLON
;
write : COUT LS ID SEMICOLON
;
if_instr : IF LPR expr RPR LBR atribuire RBR else_instr
| IF LPR expr RPR LBR atribuire RBR
;
else_instr : ELSE LBR atribuire RBR
;
while_instr : WHILE LPR expr RPR LBR instr RBR
;
%%
int main(int argc, char* argv[]){
++argv, --argc;
if(argc > 0){
yyin = fopen(argv[0], "r");
} else {
yyin = stdin;
}
while(!feof(yyin)){
yyparse();
}
printf("Result: OK\n");
return 0;
}
void yyerror(){
printf("Error at line %d!\n", line);
exit(1);
}
型
如何在Flex和野牛交互的上下文中解决这些错误?
2条答案
按热度按时间t30tvxxf1#
您的Flex输入有几个问题,其中包括:
字符串
[1-9]*
,返回不同的令牌类型。型
删除图案中的空格。
bpzcxfmw2#
不要使用反斜杠来转义模式中的特殊字符;将它们用双引号引起来:
字符串