我想组成一个“经典”的基本语法与多一点自由格式的混合,例如:如何强制野牛即时编译
a=5:b=6:c=a+b // writing several instructrions into a single line
// using colon as separator
// but eliminating mandatory terminals (like ';' in c)
while (x < 3) { // condition has mandatory parentheses around, but
x=x+1:y=y+2 // body of 'while' may also have multi-statements line(s)
z=y+y // and may have several lines as well
}
为了实现它,我建立了下列规则。我的Flex确实职责,正确地消除了不必要的换行等:
^[ \t\r\n]*\n /*ignore empty line */
\/\/.*\n ; /* skip comment */
[ \t\r\n]+ ; /* ignore whitespace */
\{[ \t\r\n]*\} ; return EMPTY;
\{[ \t\r\n]* ; return '{';
\}[ \t\r\n]* ; return '}';
不幸的是,语句后面加上冒号不强制编译器做立即编译。 然而,在后面的代码被编译的过程中,往往预计会在后半部分。 这里是我的野牛档案的结构。
%type <nPtr> stmt stmt_list expr
%%
line:
line stmt_list ':' { // <-- this is my problem
ex($2); freeNode($2);
}
| line stmt_list '\n' {
ex($2); freeNode($2);
}
| /* NULL */
;
stmt: VARIABLE '=' expr { $$ = opr('=', 2, $3, id($1)); }
| PORT '=' expr { $$ = opr('=', 2, $3, id($1)); }
| .... etc.
| WHILE '(' expr ')' EMPTY { $$ = opr(WHILE, 1, $3); }
| WHILE '(' expr ')' stmt_list { $$ = opr(WHILE, 2, $3, $5); }
;
stmt_list:
stmt { $$ = $1; }
| '{' stmt_list '}' { $$ = $2; }
| '{' error '}' { errorflag=1; }
| error '\n' { errorflag=1; }
;
expr:
INTEGER { $$ = con($1); }
| VARIABLE { $$ = id($1); }
| .... etc.
| '(' expr ')' { $$ = $2; }
;
我该如何修改它以达到预期的行为?
“即时编译”是什么意思?预期的行为是什么以及发生了什么?尝试包括[sscce](http://sscce.org) –