Grammar for a boolean calculator language
I am writing a grammar for a boolean calculator language. Programmes written in this language will consist of atmost one statement whose result will be boolean.
An example statement of such language is given below:
( A + B >= C ) AND ( D == 4 )
The grammar that I have come up with so far is given below:
E =开发者_StackOverflow中文版> T COP T | EXPR BOP EXPR
T => VAR | EXPR
VAR => A | B | C | D
EXPR => AEXPR | CEXPR
AEXPR => "(" VAR AOP VAR ")"
CEXPR => VAR COP AEXPR | AEXPR COP AEXPR
AOP => + | - | * | / | %
BOP => AND | OR
COP => == | != | < | > | <= | >=
The main thing to consider here is that no two VARs can be joined in a binary operation BOP
and that the statement must return a boolean.
I want to know if the above grammar satisfies this criteria or am I missing something?
Any help will be appereciated.
Thanks
The main thing to consider here is that no two VARs can be joined in a binary operation BOP and that the statement must return a boolean.
The above requirement fails in following case:
E = EXPR BOP EXPR
EXPR = AEXPR
=> E = AEXPR BOP AEXPR
I am assuming here that (VAR AOP VAR)
yields a VAR
.
A more standardized form of your required grammar is given below:
E = T COP T | EXPR BOP EXPR;
T = VAR | AEXPR;
VAR = "A" | "B" | "C" | "D";
AEXPR = "(" VAR AOP VAR ")" | "(" VAR AOP AEXPR ")";
EXPR = VAR COP AEXPR | AEXPR COP AEXPR;
AOP = "+" | "-" | "*" | "/" | "%";
BOP = "AND" | "OR";
COP = "==" | "!=" | "<" | ">" | "<=" | ">=";
精彩评论