最新问题!
问题在于这条产生式:
statement : block
| sentence
| ifstmt
| whilestmt
| forstmt;
当加入了ifstmt,whilestmt,forstmt后出现了冲突!
可是,如果不加的话就不支持如下的语法:
if(a>0)
for(a=1 to 10) do
b = 10;
而一定要象这样
if(a>0)
begin
for(a=1 to 10) do
b=10;
end
很不爽啊! 怎么办!
以下是bnf文法:
%lextest
{
%skip : <\s| |//[^\r\n]+>
IF : <if>
ELSE : <else>
THEN : <then>
FOR : <for>
TO : <to>
DO : <do>
DOWNTO : <downto>
WHILE : <while>
BREAK : <break>
LOOP : <loop>
BEGIN : <begin>
END : <end>
';' : <;>
'(' : <\(>
')' : <\)>
'*' : <\*>
'/' : <\/>
'+' : <\+>
'-' : <\->
'=' : <=>
ISEQUAL : <==>
ISNOTEQUAL : <!=>
GREATER : <\>>
GREATEROREQUAL : <>=>
LESSER : <<>
LESSEROREQUAL : <<=>
NOT : <Not>
AND : <And>
OR : <Or>
NUMBER : <[0-9]+(\.[0-9])*>
VARIABLENAME : <[a-zA-Z][a-zA-Z0-9]*>
STRING : <"[^\r\n]+">
}
%syntax
{
%terminator_type {int}
program : ifstmt
|forstmt
|sentences
|whilestmt;
whilestmt : WHILE logic_exp DO circlestatement ;
forstmt : FOR assignment_exp TO logic_exp DO circlestatement
| FOR assignment_exp DOWNTO logic_exp DO circlestatement ;
circlestatement : circleblock
| circlesentence;
circleblock : BEGIN circlesentences END;
circlesentences : circlesentences circlesentence
| circlesentence ;
circlesentence : logic_exp ';'
| LOOP ';'
| BREAK ';';
ifstmt : IF '(' logic_exp ')' THEN statement
| IF '(' logic_exp ')' THEN statement ELSE statement
| IF '(' logic_exp ')' THEN statement ELSE ifstmt ;
statement : block
| sentence
| ifstmt
| whilestmt
| forstmt;
block : BEGIN sentences END;
sentences : sentences sentence
| sentence;
sentence : logic_exp ';'
| assignment_exp ';';
assignment_exp : VARIABLENAME '=' logic_exp;
logic_exp : logic_exp OR logic_and_term
| logic_and_term;
logic_and_term : logic_and_term AND logic_not_term
| logic_not_term;
logic_not_term : NOT compare_exp
| compare_exp;
compare_exp : compare_exp ISEQUAL arithmetic_exp
| compare_exp ISNOTEQUAL arithmetic_exp
| compare_exp GREATER arithmetic_exp
| compare_exp GREATEROREQUAL arithmetic_exp
| compare_exp LESSER arithmetic_exp
| compare_exp LESSEROREQUAL arithmetic_exp
| arithmetic_exp;
arithmetic_exp : arithmetic_exp '+' arithmetic_term
| arithmetic_exp '-' arithmetic_term
| arithmetic_term;
arithmetic_term : arithmetic_term '*' arithmetic_factor
| arithmetic_term '/' arithmetic_factor
| arithmetic_factor;
arithmetic_factor : '(' logic_exp ')'
| NUMBER
| STRING
| VARIABLENAME ;
}
回复 更多评论