ROSE Compiler Framework/Processing Pragmas

From Wikibooks, open books for an open world
Jump to navigation Jump to search

overview[edit | edit source]

It is often useful to use pragmas to guide a translator.

A set of parser building functions are provided to help create recursive descent parsers:

Once you include the header AstFromString.h (located in src/frontend/SageIII/astFromString), you can access the variables and functions defined in the namespace.

There is an example project doing pragma parsing and saving the results into AST attributes. https://github.com/rose-compiler/rose-develop/tree/master/projects/pragmaParsing

Parsing recursive rules[edit | edit source]

left recursion

recursion rules are converted into repetition rules:

  /* YACC left recursion rule
     argument_expression_list
     : assignment_expression
     | argument_expression_list ',' assignment_expression


     Or in ANTLR as repetition
     argument_expression_list
     : assignment_expression (',' assignment_expression)*
     ;
*/
  bool afs_match_argument_expression_list()
  {

    bool result =false;
    const char* old_char = c_char;

    SgExprListExp* parameters = NULL;
    if (afs_match_assignment_expression())
    {
      SgExpression* arg1 = isSgExpression(c_parsed_node);
      assert (arg1 != NULL);
      parameters = buildExprListExp(arg1);
      c_parsed_node = parameters;
      result = true;
    }
    else
    { // immediate return false when first required term is not matched
      c_char = old_char;
      return false;
    }

    // match optional additional expressions
    old_char = c_char; // set rollback point
    while (afs_match_char(','))
    {
      if (afs_match_assignment_expression())
      {
        SgExpression* argx = isSgExpression(c_parsed_node);
        assert(argx != NULL);
        appendExpression(parameters, argx);
        c_parsed_node = parameters; // must set it again since it was rewritten in match_assignment_expression()
      }
      else
      {
        c_char = old_char;  // optional match fails, rollback
        //    printf("error. afs_match_argument_expression_list() expects assignment_expression after matching ','\n");
        //    assert (0);
        break;             // and break out
      }
      // prepare next round
      old_char = c_char;
    }

    assert (parameters != NULL);
    //    c_parsed_node = parameters; // this is necessary since the while loop may rewrite c_parsed_node
    assert (c_parsed_node == parameters);

    return true;
  }