示例#1
0
int
main (int argc, char *argv[])
{

  Marpa_Config marpa_configuration;
  Marpa_Grammar g;

  Marpa_Symbol_ID S_lhs, S_rhs;
  Marpa_Symbol_ID rhs[1];

  Marpa_Rule_ID R_new;

  int rc;

  plan(4);

  marpa_c_init (&marpa_configuration);
  g = marpa_g_new (&marpa_configuration);
  if (!g)
    {
      Marpa_Error_Code errcode =
        marpa_c_error (&marpa_configuration, NULL);
      printf ("marpa_g_new: error %d", errcode);
      exit (1);
    }
  
  // symbols
  ((S_lhs = marpa_g_symbol_new (g)) >= 0) || err ("marpa_g_symbol_new", g);
  ((S_rhs = marpa_g_symbol_new (g)) >= 0) || err ("marpa_g_symbol_new", g);
  
  // rule
  rhs[0] = S_rhs;
  (((R_new = marpa_g_rule_new (g, S_lhs, rhs, 1)) >= 0)
    || err ("marpa_g_rule_new", g));
  ok ((R_new == 0),"marpa_g_rule_new returns 0");
  
  // precompute
  rc = marpa_g_start_symbol_set(g, S_lhs);
  ((rc >= 0) || err("marpa_g_start_symbol_set", g));

  rc = marpa_g_precompute(g);
  ok ((rc == 0), "marpa_g_precompute returned 0");
  ((rc >= 0) || err("marpa_g_sequence_separator", g));
  
  rc = marpa_g_is_precomputed(g);
  ok ((rc == 1), "marpa_g_is_precomputed returned 1");
  ((rc >= 0) || err("marpa_g_is_precomputed", g));

  // rule accessors
  rc = marpa_g_rule_lhs (g, R_new); 
  ok ((rc == 0), "marpa_g_rule_lhs(%d) returned 0", R_new);
  ((rc >= 0) || err("marpa_g_rule_lhs", g));
  
  return 0;
}
示例#2
0
static Marpa_Grammar
marpa_g_simple_new(Marpa_Config *config)
{
  Marpa_Grammar g;
  g = marpa_g_new (config);
  if (!g)
    {
      Marpa_Error_Code errcode = marpa_c_error (config, NULL);
      printf ("marpa_g_new returned %d", errcode);
      exit (1);
    }

  ((S_top = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_A1 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_A2 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_B1 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_B2 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_C1 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_C2 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);

  rhs[0] = S_A1;
  ((R_top_1 = marpa_g_rule_new (g, S_top, rhs, 1)) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_A2;
  ((R_top_2 = marpa_g_rule_new (g, S_top, rhs, 1)) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_B1;
  (marpa_g_rule_new (g, S_A1, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_B2;
  (marpa_g_rule_new (g, S_A2, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_C1;
  (marpa_g_rule_new (g, S_B1, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_C2;
  (marpa_g_rule_new (g, S_B2, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);

  return g;
}
示例#3
0
int
main (int argc, char *argv[])
{
  const unsigned char *p, *eof;
  int i;
  int rc;
  const char *error_string;
  struct stat sb;

  Marpa_Config marpa_configuration;

  Marpa_Grammar g;
  Marpa_Recognizer r;
  /* Longest rule is <= 4 symbols */
  Marpa_Symbol_ID rhs[4];

  plan(13);

  marpa_c_init (&marpa_configuration);
  g = marpa_g_new (&marpa_configuration);
  if (!g)
    {
      Marpa_Error_Code errcode =
	marpa_c_error (&marpa_configuration, &error_string);
      printf ("marpa_g_new returned %d: %s", errcode, error_string);
      exit (1);
    }

  ((S_top = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((marpa_g_symbol_is_nulled_event_set(g, S_top, 1)) >= 0)
    || fail ("marpa_g_symbol_is_nulled_event_set", g);
  ((S_A1 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((marpa_g_symbol_is_nulled_event_set(g, S_A1, 1)) >= 0)
    || fail ("marpa_g_symbol_is_nulled_event_set", g);
  ((S_A2 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((marpa_g_symbol_is_nulled_event_set(g, S_A2, 1)) >= 0)
    || fail ("marpa_g_symbol_is_nulled_event_set", g);
  ((S_B1 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((marpa_g_symbol_is_nulled_event_set(g, S_B1, 1)) >= 0)
    || fail ("marpa_g_symbol_is_nulled_event_set", g);
  ((S_B2 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((marpa_g_symbol_is_nulled_event_set(g, S_B2, 1)) >= 0)
    || fail ("marpa_g_symbol_is_nulled_event_set", g);
  ((S_C1 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((marpa_g_symbol_is_nulled_event_set(g, S_C1, 1)) >= 0)
    || fail ("marpa_g_symbol_is_nulled_event_set", g);
  ((S_C2 = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((marpa_g_symbol_is_nulled_event_set(g, S_C2, 1)) >= 0)
    || fail ("marpa_g_symbol_is_nulled_event_set", g);

  rhs[0] = S_A1;
  (marpa_g_rule_new (g, S_top, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_A2;
  (marpa_g_rule_new (g, S_top, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_B1;
  (marpa_g_rule_new (g, S_A1, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_B2;
  (marpa_g_rule_new (g, S_A2, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_C1;
  (marpa_g_rule_new (g, S_B1, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_C2;
  (marpa_g_rule_new (g, S_B2, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  (marpa_g_rule_new (g, S_C1, rhs, 0) >= 0)
    || fail ("marpa_g_rule_new", g);
  (marpa_g_rule_new (g, S_C2, rhs, 0) >= 0)
    || fail ("marpa_g_rule_new", g);

  (marpa_g_start_symbol_set (g, S_top) >= 0)
    || fail ("marpa_g_start_symbol_set", g);
  if (marpa_g_precompute (g) < 0)
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }
  ok(1, "precomputation succeeded");
  r = marpa_r_new (g);
  if (!r)
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }
  rc = marpa_r_start_input (r);
  if (!rc)
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }
  ok((marpa_r_is_exhausted(r)), "exhausted at earleme 0");

  {
    Marpa_Event event;
    const int highest_symbol_id = marpa_g_highest_symbol_id (g);
    int exhausted_event_triggered = 0;
    int spurious_events = 0;
    int spurious_nulled_events = 0;
    int event_ix;
    const int event_count = marpa_g_event_count (g);
    int *nulled_symbols = calloc ((highest_symbol_id + 1), sizeof (int));
    if (!nulled_symbols) abort();
    ok ((event_count == 8), "event count at earleme 0 is %ld",
	(long) event_count);
    for (event_ix = 0; event_ix < event_count; event_ix++)
      {
	int event_type = marpa_g_event (g, &event, event_ix);
	if (event_type == MARPA_EVENT_SYMBOL_NULLED)
	  {
	    const Marpa_Symbol_ID event_symbol_id = marpa_g_event_value(&event);
	    nulled_symbols[event_symbol_id]++;
	    continue;
	  }
	if (event_type == MARPA_EVENT_EXHAUSTED)
	  {
	    exhausted_event_triggered++;
	    continue;
	  }
	printf ("event type is %ld\n", (long) event_type);
	spurious_events++;
      }
    ok ((spurious_events == 0),
	"spurious events triggered: %ld", (long) spurious_events);
    ok (exhausted_event_triggered, "exhausted event triggered");
    for (i = 0; i <= highest_symbol_id; i++)
      {
	if (is_nullable (i))
	  {
	    ok (1, "nulled event triggered for %s", symbol_name (i));
	    continue;
	  }
	spurious_nulled_events++;
      }
    ok ((spurious_nulled_events == 0), "spurious nulled events triggered = %ld",
	(long) spurious_nulled_events);
    free (nulled_symbols);
  }

  return 0;
}
示例#4
0
int
main (int argc, char *argv[])
{
  const unsigned char *p, *eof;
  int i;
  const char *error_string;
  struct stat sb;

  Marpa_Config marpa_configuration;

  Marpa_Grammar g;
  Marpa_Recognizer r;
  /* Longest rule is 4 symbols */
  Marpa_Symbol_ID rhs[4];

  int fd = open (argv[1], O_RDONLY);
  //initialize a stat for getting the filesize
  if (fstat (fd, &sb) == -1)
    {
      perror ("fstat");
      return 1;
    }
  //do the actual mmap, and keep pointer to the first element
  p = (unsigned char *) mmap (0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  //something went wrong
  if (p == MAP_FAILED)
    {
      perror ("mmap");
      return 1;
    }

  marpa_c_init (&marpa_configuration);
  g = marpa_g_new (&marpa_configuration);
  if (!g)
    {
      Marpa_Error_Code errcode =
        marpa_c_error (&marpa_configuration, &error_string);
      printf ("marpa_g_new returned %d: %s", errcode, error_string);
      exit (1);
    }

  ((S_begin_array = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_begin_object = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_end_array = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_end_object = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_name_separator = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_value_separator = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_member = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_value = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((S_false = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((S_null = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((S_true = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((S_object = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_array = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((S_number = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_string = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_object_contents = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);
  ((S_array_contents = marpa_g_symbol_new (g)) >= 0)
    || fail ("marpa_g_symbol_new", g);

  rhs[0] = S_false;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_null;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_true;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_object;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_array;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_number;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);
  rhs[0] = S_string;
  (marpa_g_rule_new (g, S_value, rhs, 1) >= 0)
    || fail ("marpa_g_rule_new", g);

  rhs[0] = S_begin_array;
  rhs[1] = S_array_contents;
  rhs[2] = S_end_array;
  (marpa_g_rule_new (g, S_array, rhs, 3) >= 0)
    || fail ("marpa_g_rule_new", g);

  rhs[0] = S_begin_object;
  rhs[1] = S_object_contents;
  rhs[2] = S_end_object;
  (marpa_g_rule_new (g, S_object, rhs, 3) >= 0)
    || fail ("marpa_g_rule_new", g);

  (marpa_g_sequence_new
   (g, S_array_contents, S_value, S_value_separator, 0,
    MARPA_PROPER_SEPARATION) >= 0) || fail ("marpa_g_sequence_new", g);
  (marpa_g_sequence_new
   (g, S_object_contents, S_member, S_value_separator, 0,
    MARPA_PROPER_SEPARATION) >= 0) || fail ("marpa_g_sequence_new", g);

  rhs[0] = S_string;
  rhs[1] = S_name_separator;
  rhs[2] = S_value;
  (marpa_g_rule_new (g, S_member, rhs, 3) >= 0)
    || fail ("marpa_g_rule_new", g);

  if (0)
    {
      (marpa_g_symbol_is_terminal_set (g, S_value_separator, 1) >= 0) ||
        fail ("marpa_g_symbol_is_terminal", g);
    }

  (marpa_g_start_symbol_set (g, S_value) >= 0)
    || fail ("marpa_g_start_symbol_set", g);
  if (marpa_g_precompute (g) < 0)
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }
  r = marpa_r_new (g);
  if (!r)
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }
  if (!marpa_r_start_input (r))
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }

  i = 0;
  eof = p + sb.st_size;
  while (p + i < eof)
    {
      Marpa_Symbol_ID token;
      const int start_of_token = i;

      switch (p[i])
        {
        case '-':
        case '+':
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
          i = scan_number (p + i, eof) - p;
          token = S_number;
          break;
        case '"':
          i = scan_string (p + i, eof) - p;
          token = S_string;
          break;
        case '[':
          token = S_begin_array;
          i++;
          break;
        case ']':
          token = S_end_array;
          i++;
          break;
        case '{':
          token = S_begin_object;
          i++;
          break;
        case '}':
          token = S_end_object;
          i++;
          break;
        case ',':
          token = S_value_separator;
          i++;
          break;
        case ':':
          token = S_name_separator;
          i++;
          break;
        case 'n':
          i = scan_constant ("null", (p + i), eof) - p;
          token = S_null;
          break;
        case ' ':
        case 0x09:
        case 0x0A:
        case 0x0D:
          i++;
          goto NEXT_TOKEN;
        default:
          marpa_g_error (g, &error_string);
          printf ("lexer failed at char %d: '%c'", i, p[i]);
          exit (1);
        }
      /* Token value of zero is not allowed, so we add one */
      if (0)
        fprintf (stderr, "reading token %ld, %s\n",
                 (long) token, symbol_name (token));
      int status = marpa_r_alternative (r, token, start_of_token + 1, 1);
      if (status != MARPA_ERR_NONE)
        {
          Marpa_Symbol_ID expected[20];
          int count_of_expected = marpa_r_terminals_expected (r, expected);
          int i;
          for (i = 0; i < count_of_expected; i++)
            {
              fprintf (stderr, "expecting symbol %ld, %s\n",
                       (long) i, symbol_name (expected[i]));
            }
          marpa_g_error (g, &error_string);
          fprintf (stderr,
                   "marpa_alternative(%p,%ld,%s,%ld,1) returned %d: %s", r,
                   (long) token, symbol_name (token),
                   (long) (start_of_token + 1), status, error_string);
          exit (1);
        }
      status = marpa_r_earleme_complete (r);
      if (status < 0)
        {
          marpa_g_error (g, &error_string);
          printf ("marpa_earleme_complete returned %d: %s", status,
                  error_string);
          exit (1);
        }
    NEXT_TOKEN:;
    }

  {
    Marpa_Bocage bocage;
    Marpa_Order order;
    Marpa_Tree tree;
    bocage = marpa_b_new (r, -1);
    if (!bocage)
      {
        int errcode = marpa_g_error (g, &error_string);
        printf ("marpa_bocage_new returned %d: %s", errcode, error_string);
        exit (1);
      }
    order = marpa_o_new (bocage);
    if (!order)
      {
        int errcode = marpa_g_error (g, &error_string);
        printf ("marpa_order_new returned %d: %s", errcode, error_string);
        exit (1);
      }
    tree = marpa_t_new (order);
    if (!tree)
      {
        Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
        printf ("marpa_t_new returned %d: %s", errcode, error_string);
        exit (1);
      }
    {
      Marpa_Value value = NULL;
      int column = 0;
      int tree_status;
      tree_status = marpa_t_next (tree);
      if (tree_status <= -1)
        {
          Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
          printf ("marpa_t_next returned %d: %s", errcode, error_string);
          exit (1);
        }

      value = marpa_v_new (tree);
      if (!value)
        {
          Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
          printf ("marpa_v_new returned %d: %s", errcode, error_string);
          exit (1);
        }
      while (1)
        {
          Marpa_Step_Type step_type = marpa_v_step (value);
          Marpa_Symbol_ID token;
          if (step_type < 0)
            {
              Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
              printf ("marpa_v_event returned %d: %s", errcode, error_string);
              exit (1);
            }
          if (step_type == MARPA_STEP_INACTIVE)
            {
              if (0)
                printf ("No more events\n");
              break;
            }
          if (step_type != MARPA_STEP_TOKEN)
            continue;
          token = marpa_v_token (value);
          if (1)
            {
              if (column > 60)
                {
                  putchar ('\n');
                  column = 0;
                }
              if (token == S_begin_array)
                {
                  putchar ('[');
                  column++;
                  continue;
                }
              if (token == S_end_array)
                {
                  putchar (']');
                  column++;
                  continue;
                }
              if (token == S_begin_object)
                {
                  putchar ('{');
                  column++;
                  continue;
                }
              if (token == S_end_object)
                {
                  putchar ('}');
                  column++;
                  continue;
                }
              if (token == S_name_separator)
                {
                  putchar (':');
                  column++;
                  continue;
                }
              if (token == S_value_separator)
                {
                  putchar (',');
                  column++;
                  continue;
                }
              if (token == S_null)
                {
                  fputs ("undef", stdout);
                  column += 5;
                  continue;
                }
              if (token == S_true)
                {
                  putchar ('1');
                  column++;
                  continue;
                }
              if (token == S_false)
                {
                  putchar ('0');
                  column++;
                  continue;
                }
              if (token == S_number)
                {
                  /* We added one to avoid zero
                   * Now we must subtract it
                   */
                  int i;
                  const int start_of_number = marpa_v_token_value (value) - 1;
                  const int end_of_number =
                    scan_number (p + start_of_number, eof) - p;
                  column += 2 + (end_of_number - start_of_number);

                  /* We output numbers as Perl strings */
                  putchar ('"');
                  for (i = start_of_number; i < end_of_number; i++)
                    {
                      putchar (p[i]);
                    }
                  putchar ('"');
                  continue;
                }
              if (token == S_string)
                {
                  /* We added one to avoid zero
                   * Now we must subtract it, but we also
                   * add one for the initial double quote
                   */
                  int i;
                  const int start_of_string = marpa_v_token_value (value);
                  /* Subtract one for the final double quote */
                  const int end_of_string =
                    (scan_string (p + start_of_string, eof) - p) - 1;

                  /* We add back the inital and final double quotes,
                   * and increment the column accordingly.
                   */
                  column += 2;
                  putchar ('"');
                  i = start_of_string;
                  while (i < end_of_string)
                    {
                      const unsigned char ch0 = p[i++];
                      if (ch0 == '\\')
                        {
                          const unsigned char ch1 = p[i++];
                          switch (ch1)
                            {
                            case '\\':
                            case '/':
                            case '"':
                            case 'b':
                            case 'f':
                            case 'n':
                            case 'r':
                            case 't':
                              /* explicit non-hex JSON escapes are the same
                               * as the Perl escapes */
                              column += 2;
                              putchar ('\\');
                              putchar (ch1);
                              continue;
                            case 'u':
                              {
                                int digit;
                                putchar ('x');
                                putchar ('{');
                                for (digit = 0; digit < 4; digit++)
                                  {
                                    const unsigned char hex_ch = p[i + digit];
                                    if ((hex_ch >= 'a' && hex_ch <= 'f')
                                        || (hex_ch >= 'A' && hex_ch <= 'F')
                                        || (hex_ch >= '0' && hex_ch <= '9'))
                                      {
                                        printf
                                          ("illegal char in JSON hex number at location %d (0x%x): '%c' ",
                                           i, hex_ch, hex_ch);
                                        exit (1);
                                      }
                                    putchar (hex_ch);
                                  }
                                putchar ('}');
                                column += 7;
                                i += 4;
                              }
                              continue;
                            default:
                              printf
                                ("illegal escaped char in JSON input (0x%x):'%c' ",
                                 i, p[i]);
                              exit (1);
                            }
                        }

                      /* An unescaped JSON char, one that does not need Perl escaping */
                      if (ch0 == '_' || (ch0 >= '0' && ch0 <= '9')
                          || (ch0 >= 'a' && ch0 <= 'z') || (ch0 >= 'A'
                                                            && ch0 <= 'Z'))
                        {
                          putchar (ch0);
                          column++;
                          continue;
                        }
                      /* An unescaped JSON char,
                       * but one which quotemeta would escape for Perl */
                      putchar ('\\');
                      putchar (ch0);
                      column += 2;
                      continue;
                    }
                  putchar ('"');
                  continue;
                }
              fprintf (stderr, "Unknown symbol %s at %d",
                       symbol_name (token), marpa_v_token_value (value) - 1);
              exit (1);
            }
        }
      if (column > 60)
        {
          putchar ('\n');
          column = 0;
        }
    }
  }

  return 0;
}
示例#5
0
int
main (int argc, char **argv)
{
  int was_result_written = 0;
  int pass;
  /* This was to move gslice area out of the
     tree of Marpa calls during memory debugging */
  /* void* dummy = g_slice_alloc(42); */
  /* g_slice_free1(42, dummy); */
  GError *error = NULL;
  GOptionContext *context;
  char *test_string;

  context = g_option_context_new ("- test balanced parens");
  g_option_context_add_main_entries (context, entries, NULL);
  if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      g_print ("option parsing failed: %s\n", error->message);
      exit (1);
    }

  if (string)
    {
      /* Never freed */
      test_string = string;
      string_length = strlen (test_string);
      printf ("Target is \"%s\", length=%d\n", test_string, string_length);
    }
  else if (string_length < 10)
    {
      fprintf (stderr, "String length is %d, must be at least 10\n",
	       string_length);
      exit (1);
    }
  else
    {
      /* Never freed */
      test_string = gen_example_string (string_length);
      printf ("Target at end, length=%d\n", string_length);
    }

  for (pass = 0; pass < repeats; pass++)
    {
      int location;
      int start_of_match = -1;
      int end_of_match = -1;
      Marpa_Symbol_ID s_top, s_prefix, s_first_balanced;
      Marpa_Symbol_ID s_prefix_char, s_balanced;
      Marpa_Symbol_ID s_balanced_sequence;
      struct marpa_g *g;
      struct marpa_r *r;
      void *result;
      /* Longest rule is 4 symbols */
      Marpa_Symbol_ID rhs[4];

      g = marpa_g_new ();
      s_top = marpa_symbol_new (g);
      s_prefix = marpa_symbol_new (g);
      s_first_balanced = marpa_symbol_new (g);
      s_prefix_char = marpa_symbol_new (g);
      s_balanced = marpa_symbol_new (g);
      s_lparen = marpa_symbol_new (g);
      s_rparen = marpa_symbol_new (g);
      s_balanced_sequence = marpa_symbol_new (g);
      rhs[0] = s_prefix;
      rhs[1] = s_first_balanced;
      marpa_rule_new (g, s_top, rhs, 2);
      marpa_sequence_new (g, s_prefix, s_prefix_char, -1, 0, 0);
      rhs[0] = s_balanced;
      marpa_rule_new (g, s_first_balanced, rhs, 1);
      rhs[0] = s_lparen;
      rhs[1] = s_rparen;
      marpa_rule_new (g, s_balanced, rhs, 2);
      rhs[0] = s_lparen;
      rhs[1] = s_balanced_sequence;
      rhs[2] = s_rparen;
      marpa_rule_new (g, s_balanced, rhs, 3);
      marpa_sequence_new (g, s_balanced_sequence, s_balanced, -1, 1, 0);
      marpa_symbol_is_terminal_set (g, s_prefix_char, 1);
      marpa_symbol_is_terminal_set (g, s_lparen, 1);
      marpa_symbol_is_terminal_set (g, s_rparen, 1);
      marpa_start_symbol_set (g, s_top);
      result = marpa_precompute (g);
      if (!result)
	{
	  puts (marpa_g_error (g));
	  exit (1);
	}
      {
	int AHFA_state_count = marpa_AHFA_state_count (g);
	int ahfa_id;
	first_balanced_completion = -1;
	for (ahfa_id = 0; ahfa_id < AHFA_state_count; ahfa_id++)
	  {
	    unsigned int aim_ix;
	    unsigned int aim_count = marpa_AHFA_state_item_count (g, ahfa_id);
	    for (aim_ix = 0; aim_ix < aim_count; aim_ix++)
	      {
		int aim_id = marpa_AHFA_state_item (g, ahfa_id, aim_ix);
		int position = marpa_AHFA_item_position (g, aim_id);
		if (position == -1)
		  {
		    Marpa_Rule_ID rule = marpa_AHFA_item_rule (g, aim_id);
		    Marpa_Symbol_ID lhs = marpa_rule_lhs (g, rule);
		    if (lhs == s_first_balanced)
		      {
			if (first_balanced_completion != -1)
			  {
			    fprintf (stderr,
				     "First balanced completion is not unique");
			    exit (1);
			  }
			first_balanced_completion = ahfa_id;
			break;
		      }
		  }
	      }
	  }
      }
      r = create_recce (g);
      for (location = 0; location <= string_length; location++)
	{
	  int origin, status;
	  Marpa_Symbol_ID paren_token =
	    test_string[location] == '(' ? s_lparen : s_rparen;
	  status = marpa_alternative (r, paren_token, 0, 1);
	  if (status < -1)
	    fatal_r_error ("marpa alternative", r, status);
	  status = marpa_alternative (r, s_prefix_char, 0, 1);
	  if (status < -1)
	    fatal_r_error ("marpa alternative", r, status);
	  status = marpa_earleme_complete (r);
	  if (status < -1)
	    fatal_r_error ("marpa_earleme_complete", r, status);
	  /* If none of the alternatives were accepted, we are done */
	  origin = at_first_balanced_completion (r, location + 1);
	  if (origin >= 0)
	    {
	      start_of_match = origin;
	      end_of_match = location + 1;
	      break;
	    }
	}
      if (start_of_match < 0)
	{
	  printf ("No balanced parens\n");
	}
      while (++location < string_length)
	{
	  int origin, status, earleme_complete_status;
	  Marpa_Symbol_ID paren_token =
	    test_string[location] == '(' ? s_lparen : s_rparen;
	  status = marpa_alternative (r, paren_token, 0, 1);
	  if (status == -1)
	    break;
	  if (status < -1)
	    fatal_r_error ("marpa alternative", r, status);
	  earleme_complete_status = marpa_earleme_complete (r);
	  if (earleme_complete_status < -1)
	    fatal_r_error ("marpa_earleme_complete", r,
			   earleme_complete_status);
	  origin = at_first_balanced_completion (r, location + 1);
	  if (origin >= 0 && origin < start_of_match)
	    {
	      start_of_match = origin;
	      end_of_match = location + 1;
	      break;
	    }
	  if (earleme_complete_status == 0)
	    break;
	}
      if (!was_result_written)
	{
	  printf ("Match at %d-%d\n", start_of_match, end_of_match);
	  was_result_written++;
	}
      marpa_r_free (r);
      marpa_g_free (g);
      g = NULL;
    }
  exit (0);
}
示例#6
0
int
main (int argc, char **argv)
{
  int i;
  const char *error_string;
  Marpa_Config marpa_configuration;
  Marpa_Symbol_ID S, A, a, E;
  Marpa_Grammar g;
  Marpa_Recognizer r;
  /* Longest rule is 4 symbols */
  Marpa_Symbol_ID rhs[4];
  int initial_sleep = 0;
  if (argc >= 2)
    {
      initial_sleep = atoi (argv[1]);
    }
  await_input ();

  marpa_c_init (&marpa_configuration);
  g = marpa_g_new (&marpa_configuration);
  if (!g) {
    Marpa_Error_Code errcode = marpa_c_error (&marpa_configuration, &error_string);
    printf ("marpa_g_new returned %d: %s", errcode, error_string);
    exit (1);
  }
  ((S = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((A = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((a = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  ((E = marpa_g_symbol_new (g)) >= 0) || fail ("marpa_g_symbol_new", g);
  for (i = initial_sleep; i > 0; i--)
    {
      sleep (2);
      fputs ("-", stderr);
    }

  fputs ("\n", stderr);
  rhs[0] = A;
  rhs[1] = A;
  rhs[2] = A;
  rhs[3] = A;
  (marpa_g_rule_new (g, S, rhs, 4) >= 0) || fail ("marpa_g_rule_new", g);
  rhs[0] = a;
  (marpa_g_rule_new (g, A, rhs, 1) >= 0) || fail ("marpa_g_rule_new", g);
  rhs[0] = E;
  (marpa_g_rule_new (g, A, rhs, 1) >= 0) || fail ("marpa_g_rule_new", g);
  (marpa_g_rule_new (g, E, rhs, 0) >= 0) || fail ("marpa_g_rule_new", g);
  (marpa_g_symbol_is_terminal_set (g, a, 1) >= 0) ||
    fail ("marpa_g_rule_new", g);
  (marpa_g_start_symbol_set (g, S) >= 0) || fail ("marpa_g_rule_new", g);
  if (marpa_g_precompute (g) < 0)
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }
  r = marpa_r_new (g);
  if (!r)
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }
  if (!marpa_r_start_input (r))
    {
      marpa_g_error (g, &error_string);
      puts (error_string);
      exit (1);
    }
  for (i = 0; i < 4; i++)
    {
      int status = marpa_r_alternative (r, a, 42, 1);
      if (status != MARPA_ERR_NONE)
	{
	  marpa_g_error (g, &error_string);
	  printf ("marpa_alternative returned %d: %s", status, error_string);
	  exit (1);
	}
      status = marpa_r_earleme_complete (r);
      if (status < 0)
	{
	  marpa_g_error (g, &error_string);
	  printf ("marpa_earleme_complete returned %d: %s", status,
		  error_string);
	  exit (1);
	}
    }
  await_input ();
  for (i = 0; i <= 4; i++)
    {
      Marpa_Bocage bocage;
      Marpa_Order order;
      Marpa_Tree tree;
      int tree_ordinal = 0;
      bocage = marpa_b_new (r, i);
      if (!bocage)
	{
	  int errcode = marpa_g_error (g, &error_string);
	  printf ("marpa_bocage_new returned %d: %s", errcode, error_string);
	  exit (1);
	}
      order = marpa_o_new (bocage);
      if (!order)
	{
	  int errcode = marpa_g_error (g, &error_string);
	  printf ("marpa_order_new returned %d: %s", errcode, error_string);
	  exit (1);
	}
      tree = marpa_t_new (order);
      if (!tree)
	{
	  Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
	  printf ("marpa_t_new returned %d: %s", errcode, error_string);
	  exit (1);
	}
      while (++tree_ordinal)
	{
	  Marpa_Value value = NULL;
	  int tree_status;
	  tree_status = marpa_t_next (tree);
	  if (tree_status < -1)
	    {
	      Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
	      printf ("marpa_v_event returned %d: %s", errcode, error_string);
	      exit (1);
	    }
	  if (tree_status == -1)
	    break;
	  fprintf (stdout, "Tree #%d for length %d\n", tree_ordinal, i);
	  value = marpa_v_new (tree);
	  if (!value)
	    {
	      Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
	      printf ("marpa_v_new returned %d: %s", errcode, error_string);
	      exit (1);
	    }
	  while (1)
	    {
	      Marpa_Step_Type step_type = marpa_v_step (value);
	      if (step_type < 0)
		{
		  Marpa_Error_Code errcode = marpa_g_error (g, &error_string);
		  printf ("marpa_v_event returned %d: %s", errcode,
			  error_string);
		  exit (1);
		}
	      if (step_type == MARPA_STEP_INACTIVE)
		{
		  printf ("No more events\n");
		  break;
		}
	      fprintf (stdout, "Step: %d %d %d %d %d\n",
		       marpa_v_token (value),
		       marpa_v_token_value (value),
		       marpa_v_rule (value),
		       marpa_v_arg_0 (value), marpa_v_arg_n (value));
	    }
	  if (value)
	    marpa_v_unref (value);
	}
      marpa_t_unref (tree);
      marpa_o_unref (order);
      marpa_b_unref (bocage);
    }
  marpa_r_unref (r);
  marpa_g_unref (g);
  g = NULL;
  while (1)
    {
      putc ('.', stderr);
      sleep (10);
    }
}