1. 程式人生 > >C++輕量級文法分析器更新,程式碼+DEMO×3(下載)

C++輕量級文法分析器更新,程式碼+DEMO×3(下載)

最新問題!
問題在於這條產生式:
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 ;
}



  
回覆
  更多評論