문제

I have this grammar in EBNF for a sub-language with arithmetic & logical expressions, variables assignment and printing.

start   ::= (print | assign)*
print   ::= print expr ;
assign  ::= ID = expr ;
expr    ::= andExpr (|| andExpr)*
andExpr ::= relExpr (&& relExpr)*
relExpr ::= addExpr ( == addExpr | != addExpr | <= addExpr | >= addExpr | < addExpr | > addExpr)?
addExpr ::= mulExpr (+ mulExpr | - mulExpr)*
mulExpr ::= unExpr (* hunExpri | / hunExpr)*
unExpr  ::= + unExpr | - unExpr | ! unExpr | primary
primary ::= ( expr ) | ID | NUM | true | false

unfortunately I just can't figure out what these two rules:
unExpr ::= + unExpr
unExpr ::= - unExpr

actually do, or why I should need them, since I seem to be able to derive every phrase of the language without applying them. Any idea?
thanks a lot :-)

도움이 되었습니까?

해결책

If you are planning no expressions like:

a=-1

(where "a" is an ID and "1" is a NUM) in your language than you don't need those two rules. Otherwise you have to implement them.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top