我正在为独立研究创建一个.lex文件。我遇到了问题标题中所述的lex错误。它发生在第119行(线路编号)。我不知道为什么我会收到这个错误。谁能发现我可能做错了什么?我没有包含token.h文件,但如果需要,我会发布它。我的.lex文件名为scanner.lex,如下所示:
1 %{
2 #include <string.h>
3 #include "token.h"
4
5 int current_line_number = 1;
6
7 void newLine() {
8 current_line_number++;
9 }
10
11 int yywrap(void) {
12 return 1;
13 }
14
15 %}
16
17 %%
18 " " { }
19 \n {newLine();}
20 and {yylval.line_number = current_line_number;
21 return AND;}
22 array {yylval.line_number = current_line_number;
23 return ARRAY;}
24 begin {yylval.line_number = current_line_number;
25 return BEGIN;}
26 boolean {yylval.line_number = current_line_number;
27 return BOOLEAN;}
28 div {yylval.line_number = current_line_number;
29 return DIV;}
30 do {yylval.line_number = current_line_number;
31 return DO;}
32 else {yylval.line_number = current_line_number;
33 return ELSE;}
34 end {yylval.line_number = current_line_number;
35 return END;}
36 false {yylval.line_number = current_line_number;
37 return FALSE;}
38 for {yylval.line_number = current_line_number;
39 return FOR;}
40 goto {yylval.line_number = current_line_number;
41 return GOTO;}
42 if {yylval.line_number = current_line_number;
43 return IF;]
44 imply {yylval.line_number = current_line_number;
45 return IMPLY;}
46 integer {yylval.line_number = current_line_number;
47 return INTEGER;}
48 label {yylval.line_number = current_line_number;
49 return LABEL;}
50 not {yylval.line_number = current_line_number;
51 return NOT;}
52 or {yylval.line_number = current_line_number;
53 return OR;}
54 own {yylval.line_number = current_line_number;
55 return OWN;}
56 procedure {yylval.line_number = current_line_number;
57 return PROCEDURE;}
58 real {yylval.line_number = current_line_number;
59 return REAL;}
60 step {yylval.line_number = current_line_number;
61 return STEP;}
62 string {yylval.line_number = current_line_number;
63 return STRING;}
64 then {yylval.line_number = current_line_number;
65 return THEN;}
66 true {yylval.line_number = current_line_number;
67 return TRUE;}
68 until {yylval.line_number = current_line_number;
69 return UNTIL;}
70 value {yylval.line_number = current_line_number;
71 return VALUE;}
72 while {yylval.line_number = current_line_number;
73 return WHILE;}
74 [a-zA-Z][a-zA-Z0-9]* {yylval.string_value.line_number = current_line_number;
75 yylval.string_value.value = malloc(sizeof(char) *(strlen (yytext)+1));
76 return IDENTIFIER;}
77 [0-9]+ {yylval.integer_value.line_number = current_line_number;
78 yylval.integer_value.value = atoi(yytext);
79 return INT;}
80 [0-9][0-9]*\.[0-9][0-9]* {yylval.real_value.line_number = current_line_number;
81 yylval.real_value.value = atof(yytext);
82 return REAL;}
83 "+" {yylval.line_number = current_line_number;
84 return PLUS;}
85 "-" {yylval.line_number = current_line_number;
86 return MINUS;}
87 "*" {yylval.line_number = current_line_number;
88 return MULT;}
89 "/" {yylval.line_number = current_line_number;
90 return DIV;}
91 "<" {yylval.line_number = current_line_number;
92 return LESS;}
93 "<=" {yylval.line_number = current_line_number;
94 return LESSEQ;}
95 ">" {yylval.line_number = current_line_number;
96 return GREAT;}
97 ">=" {yylval.line_number = current_line_number;
98 return GREATEQ;}
99 "=" {yylval.line_number = current_line_number;
100 return EQ;}
101 "!=" {yylval.line_number = current_line_number;
102 return NOTEQ;}
103 "," {yylval.line_number = current_line_number;
104 return COMMA;}
105 ":" {yylval.line_number = current_line_number;
106 return COLON;}
107 ";" {yylval.line_number = current_line_number;
108 return SEMI;}
109 "(" {yylval.line_number = current_line_number;
110 return LPARAN;}
111 ")" {yylval.line_number = current_line_number;
112 return RPARAN;}
113 "[" {yylval.line_number = current_line_number;
114 return LBRAK;}
115 "]" {yylval.line_number = current_line_number;
116 return RBRAK;}
117 ":=" {yylval.line_number = current_line_number;
118 return ASSIGN;}
119 . {printf("Found other data \"%s\"\n", yytext);
120 return 1;}
121
122 %%
以下是实际的错误消息:
cs368@admiral:~/cs4880$ lex scanner.lex
"scanner.lex":line 119: Error: Action does not terminate
另外,我会感谢其他一些看似不对的建议,欢迎提出建议。谢谢大家。
答案 0 :(得分:1)
发现错误。在第43行,我输入了一个“]”而不是“}”如果你看到任何可以改进的东西,我会很感激。