سؤال

I'm trying to test the ANSI-C grammar provided on the GOLD Parser website. I can't seem to even completly parse the smallest of the C file.

Example:

int test_inc1(void)
{
  int t;
  t = 2 + 2;
  return 0;
}

It find int as a type, then test_inc1 as an Id, then parantheses correctly but after the second ), it is expecting a ; instead of a {. So it throws a syntax error. I'm very new into all this grammar funkyness. I'd simply like to parse my code into an AST :(

هل كانت مفيدة؟

المحلول

According to the grammar, the first line could be a <Func Proto>, if it was terminated by a semicolon:

<Func Proto> ::= <Func ID> '(' <Types>  ')' ';'
               | <Func ID> '(' <Params> ')' ';'
               | <Func ID> '(' ')' ';'

For parsing a function declaration, this production from the quoted grammar should have matched the part between the parentheses:

<Param>      ::= const <Type> ID
               |       <Type> ID

void was OK for the <Type>, but the ID that the grammar asks for just is not there.

But the grammar also contains this hint:

! Note: This is an ad hoc version of the language. If there are any flaws, 
! please visit the contact page and tell me.

so it should probably not be taken too seriously.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top