Esempio n. 1
0
static inline struct marpa_r *
create_recce (struct marpa_g *g)
{
  struct marpa_r *r = marpa_r_new (g);
  if (!r)
    {
      puts (marpa_g_error (g));
      exit (1);
    }
  if (!marpa_start_input (r))
    {
      puts (marpa_g_error (g));
      exit (1);
    }
  return r;
}
Esempio n. 2
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;
}
Esempio n. 3
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;
}
Esempio n. 4
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);
    }
}
Esempio n. 5
0
int
main (int argc, char *argv[])
{
  int rc;

  Marpa_Config marpa_configuration;

  Marpa_Grammar g;
  Marpa_Recognizer r;

  plan(20);

  marpa_c_init (&marpa_configuration);
  g = marpa_g_simple_new(&marpa_configuration);

  /* for marpa_g_error() in marpa_m_test() */
  marpa_m_grammar_set(g);

  marpa_g_simple_precompute(g, S_top);
  ok(1, "precomputation succeeded");

  r = marpa_r_new (g);
  if (!r)
    fail("marpa_r_new", g);

  Marpa_Symbol_ID S_token = S_A2;
  marpa_m_test("marpa_r_alternative", r, S_token, 0, 0,
    MARPA_ERR_RECCE_NOT_ACCEPTING_INPUT, "before marpa_r_start_input()");

  rc = marpa_r_start_input (r);
  if (!rc)
    fail("marpa_r_start_input", g);

  Marpa_Symbol_ID S_expected = S_A2;
  int value = 1;
  marpa_m_test("marpa_r_expected_symbol_event_set", r, S_expected, value, value);

  /* recognizer reading methods on invalid and missing symbols */
  marpa_m_test("marpa_r_alternative", r, S_invalid, 0, 0, MARPA_ERR_INVALID_SYMBOL_ID,
    "invalid token symbol is checked before no-such");
  marpa_m_test("marpa_r_alternative", r, S_no_such, 0, 0, MARPA_ERR_NO_SUCH_SYMBOL_ID,
    "no such token symbol");
  marpa_m_test("marpa_r_alternative", r, S_token, 0, 0,
    MARPA_ERR_TOKEN_LENGTH_LE_ZERO, marpa_m_error_message(MARPA_ERR_TOKEN_LENGTH_LE_ZERO));
  marpa_m_test("marpa_r_earleme_complete", r, -2, MARPA_ERR_PARSE_EXHAUSTED);

  /* re-create the recce and try some input */
  r = marpa_r_new (g);
  if (!r)
    fail("marpa_r_new", g);

  rc = marpa_r_start_input (r);
  if (!rc)
    fail("marpa_r_start_input", g);

  marpa_m_test("marpa_r_alternative", r, S_C1, 1, 1, MARPA_ERR_NONE);
  marpa_m_test("marpa_r_earleme_complete", r, 1);

  /* marpa_o_high_rank_only_* */
  Marpa_Bocage b = marpa_b_new(r, marpa_r_current_earleme(r));
  if(!b)
    fail("marpa_b_new", g);

  marpa_m_test("marpa_b_ambiguity_metric", b, 1);
  marpa_m_test("marpa_b_is_null", b, 0);

  Marpa_Order o = marpa_o_new (b);
  ok(o != NULL, "marpa_o_new(): ordering at earleme 0");

  int flag = 1;
  marpa_m_test("marpa_o_high_rank_only_set", o, flag, flag);
  marpa_m_test("marpa_o_high_rank_only", o, flag);

  marpa_m_test("marpa_o_ambiguity_metric", o, 1);
  marpa_m_test("marpa_o_is_null", o, 0);

  marpa_m_test("marpa_o_high_rank_only_set", o, flag, -2, MARPA_ERR_ORDER_FROZEN);
  marpa_m_test("marpa_o_high_rank_only", o, flag);

  return 0;
}