[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ifelse Production



Hi Prof. Etienne;

 

I am writing the grammar for a language I’m working on(Mitrion-C). Using a reference for my work, joos ( part of Prof. Hendren’s work)

 

1)       AS can be seen from the grammar below of joos the ifelse production has no short_if in it. This is done to clear ambiguities about if stms.

           

stm =

            {simple} simplestm

                  {-> simplestm.stm} |

            {decl}   type identifier_list semicolon

                  {-> New stm.decl_first(type,

                                  [identifier_list.identifier]) } |

            {if}     if l_par exp r_par stm

                  {-> New stm.if(exp, stm)} |

            {ifelse} if l_par exp r_par stm_no_short_if else stm

                  {-> New stm.ifelse(exp, stm_no_short_if.stm, stm) } |

            {while}  while l_par exp r_par stm

                  {-> New stm.while(exp, stm)};

 

simplestm {-> stm} =

            {skip}   semicolon

                  {-> New stm.skip()} |

            {block}  l_brace stm* r_brace

                   {-> New stm.block([stm])} |

            {exp}    stm_exp semicolon

                  {-> New stm.exp(stm_exp.exp)} |

            {return} return exp? semicolon

                  {-> New stm.return(exp)} ;

 

stm_no_short_if {-> stm} =

            {simple}     simplestm

               {-> simplestm.stm} |

            {tmp_ifelse} if l_par exp r_par

               [then_stm_no_short_if]:stm_no_short_if else

               [else_stm_no_short_if]:stm_no_short_if

               {-> New stm.ifelse(exp, then_stm_no_short_if.stm,

                                  else_stm_no_short_if.stm)} |

            {tmp_while}  while l_par exp r_par stm_no_short_if

               {-> New stm.while(exp, stm_no_short_if.stm)};

 

 

 

2)       How do I integrate this in my grammar shown below?

 

stm{->stm?} =

            {variable_declaration} type identifier semicolon {->New stm.variable_declaration(type, identifier)}|

      {declare_assign} type identifier assign assignment_body semicolon {-> New stm.declare_assign(type,identifier, assignment_body)} |

            {exp} exp semicolon {-> New stm.exp(exp)};

     

 

assignment_body{-> assignment_body} =

            {block_exp} block_exp {-> New assignment_body.block_exp(block_exp)}|

      {exp} exp {-> New assignment_body.exp(exp)};

 

 

      block_exp {->block_exp} =

{for} T.for l_par [element_declaration]:identifiers T.in [collection_expression]:identifiers r_par l_brace stm+ r_brace [return_value]:identifiers

                  {->New block_exp.for([element_declaration.identifier],[collection_expression.identifier],[stm],[return_value.identifier])}  |

{foreach} T.foreach l_par [element_declaration]:identifiers T.in [collection_expression]:identifiers r_par l_brace stm+ r_brace  [return_value]:identifiers 

                  {->New block_exp.foreach([element_declaration.identifier],[collection_expression.identifier],[stm],[return_value.identifier])}  |

            {while} T.while l_par exp r_par iteration intconst l_brace stm+ r_brace identifiers

                  {->New block_exp.while(exp,intconst,[stm],[identifiers.identifier])}  |

            {if}  T.if l_par exp r_par l_brace stm+ r_brace identifiers {->New block_exp.if(exp,[stm],[identifiers.identifier])} |

            {ifelse} T.if l_par exp r_par [firstl]:l_brace [if_stm]:stm+ [firstr]:r_brace T.else [secondl]:l_brace [else_stm]stm+ [secondr]:r_brace identifiers

               {-> New block_exp.ifelse(exp,[if_stm.stm], [else_stm.stm], [identifiers.identifier])} ;

 

 

Notice that in my case the ifelse doesn’t ‘call’ block_exp (as is the case with joos) but goes up my tree back to the stm production(through if_stm.stm, else_stm.stm). I tried using another stm_noif, assignment_body_noif and block_exp_noif . All are added to the AST tree as stm, assignment_body and block_exp respectively. However this caused a reduce/reduce problem.  I also tried adding those three productions as being new elements in the tree(i.e {-> stm_noif}, {->assignment_body_noif},{->block_exp_noif} ), again the same reduce/reduce problem?

 

reduce/reduce conflict in state [stack: PMitrionStm PTypes TIdentifier TLPar PArguments TRPar TLBrace PIdentifiers TAssign TIf TLPar PExp TRPar TLBrace PExp TSemicolon *] on TInt in {

                    [ P$Stm = PExp TSemicolon * ] followed by TInt (reduce),

  [ P$StmNoif = PExp TSemicolon * ] followed by TInt (reduce)

}

           

            Any Ideas please?

 

 

 

Many thanks,

Ashraf Haddad