Help with this BNF

May 15, 2012 at 1:22 PM

I have the following BNF

<cause> ::= “{“<text>|[<parameter>]”}”
<effect> ::= “{“<text>|[<parameter>]”}”
<text> ::= [<char>]
<parameter> ::= “[“<table-parameter-value>|<other-parameter-value>”]”
<table-parameter-value> ::= [<alphanumeric>].<numeric><numeric><numeric>
<other-parameter-value> ::= <text>
<char> ::= <alpha>|<numeric>|<TMSL-symbol>
<alpha> ::= “A”-“Z” “a”-“z” “ “
<numeric> ::= “0”-“9”

I am having difficulty converting this in Irony. I am currently using a StringLiteral but the problem is that the "parameters" are not get parsed correctly. Sample texts are also given below. Any help is greatly appreciated.


{His/Her occupation in table [OCCLST01.013] and age is [>] [21]}
{Modify Volatile field use class to [PH1]}

NonTerminal text = new NonTerminal(TXT);
NonTerminal action = new NonTerminal(ACTION);
NonTerminal statement = new NonTerminal(STATEMENT);
NonTerminal for_cover = new NonTerminal(FOR_COVER_TYPES);
NonTerminal for_each_driver = new NonTerminal(FOR_EACH_DRIVER);
NonTerminal cover_list = new NonTerminal(COVER_LIST);
NonTerminal cover_type = new NonTerminal(COVER_TYPE);
NonTerminal if_do_endif = new NonTerminal(IF_DO_ENDIF);
NonTerminal if_else_endif = new NonTerminal(IF_ELSE_ENDIF);
NonTerminal condition = new NonTerminal(CONDITION);
NonTerminal logic_operator = new NonTerminal(LOGIC_OPERATOR);
NonTerminal cause = new NonTerminal(CAUSE);
NonTerminal effect = new NonTerminal(EFFECT);

NonTerminal not_condition = new NonTerminal(NOT_CONDITION);
NonTerminal logical_condition = new NonTerminal(LOGICAL_CONDITION);
NonTerminal cause_condition = new NonTerminal(CAUSE_CONDITION);

NumberLiteral numeric = new NumberLiteral("numeric");
StringLiteral alpha = new StringLiteral("alpha");
NonTerminal alpha_numeric = new NonTerminal("alpha_numeric");
NonTerminal chars = new NonTerminal("chars");
NonTerminal parameter = new NonTerminal("parameter");
NonTerminal other_parameter = new NonTerminal("other_parameter");
NonTerminal table_parameter = new NonTerminal("table_parameter");
//RegexBasedTerminal alpha = new RegexBasedTerminal("alpha", @"[a-zA-Z0-9 ]");

alpha.AddStartEnd("{", "}", StringOptions.NoEscapes);

alpha_numeric.Rule = alpha;
chars.Rule =  alpha;
text.Rule = chars;
other_parameter.Rule = text;
table_parameter.Rule = alpha_numeric + ToTerm(".") + numeric + numeric + numeric;
parameter.Rule = ToTerm("[") + other_parameter | table_parameter + ToTerm("]");

// <statement> ::= <FOR-statement>|<IF-DO-statement>|<IF-ELSE-statement>|<effect>|{}
statement.Rule = for_cover | for_each_driver | if_do_endif | if_else_endif | effect | Empty;

for_cover.Rule = ToTerm("FOR COVER TYPES") + "[" + cover_list + "]" + "DO" +
                                statement +
                             ToTerm("ENDFOR");

for_each_driver.Rule = ToTerm("FOR EACH DRIVER DO") +
                                        statement +
                                    ToTerm("ENDFOR");

//<cover-type> ::= COMP|TPFT|TPO
cover_type.Rule = ToTerm("COMP") | "TPFT" | "TPO";

//<cover-list> ::= {<cover-type>}{;<cover-type>}
cover_list.Rule = MakePlusRule(cover_list, ToTerm(";"), cover_type) | Empty;

// <IF-DO-statement> ::= IF	<condition>	DO <action> ENDIF
            if_do_endif.Rule = "IF" + condition + "DO" + action + "ENDIF";

//<IF-ELSE-statement>	::= IF <condition> THEN <action> ELSE <action> ENDIF

if_else_endif.Rule = "IF" + condition + "THEN" + action + "ELSE" + action + "ENDIF";

// <condition> ::= <cause>|( NOT <condition> )|( <condition><logic-operator> <condition>} )|()
logical_condition.Rule = "(" + condition + logic_operator + condition + ")";
not_condition.Rule = "(" + ToTerm("NOT") + condition + ")";
cause_condition.Rule = cause;
condition.Rule = cause_condition | not_condition | logical_condition | Empty;

//<logic-operator> ::= AND|OR
logic_operator.Rule = ToTerm("AND") | ToTerm("OR");

//<cause> ::= "{"<text>|[<parameter>]"}"
cause.Rule = (text | parameter);
effect.Rule = (text | parameter);

action.Rule = MakePlusRule(action, statement);

this.Root = action;

 



Thanks,

Muffadal J.

Coordinator
May 17, 2012 at 5:36 PM

hi

Parameter is probably defined wrong:

parameter.Rule = ToTerm("[") + other_parameter | table_parameter + ToTerm("]");

what you meant is probably

parameter.Rule = ToTerm("[") + (other_parameter | table_parameter) + ToTerm("]");

many other things do not seem right. Keep in mind, the source BNF you show specifies lexical rules (how tokens are composed) together with syntax rules - that's the usual case for BNF to mix them together. For ex: <char>, <numeric> are lex definitions.

But in Irony (as in any other parser) these are completely different things. Lexical rules are expressed thru selection of proper Terminals, with appropriate settings. So please read some introductory stuff about lexical analysis and syntax (even Wikipedia will be enough), then try to express the grammar in Irony (its intent, meaning, not symbol-by-symbol blind translation).

Roman