English 中文(简体)
Bison的行动命令
原标题:Actions order in Bison

I m试图在C++中利用Bison生成一个 par子。 图表是细微的,但我对这些行动有一些迅速的麻烦。 这里有一个简单的样本:

statements
: statement
| statements statement;

我知道,这是很正常的事情。 我提出的问题首先产生。 例如,如果我有这样的投入,那么我会这样做。

statement statement statement statement

Bison称我的行动

statement (statement (statement (statement))))

(((statement) statement) statement) statement

I m trying to construct a linked list of the rules invoked here, and I want to keep the list in the same 或der as was input. Right now, I ve got

statements
: statement
{ 
  $$ = $1; 
}
| statements statement
{ 
  dynamic_cast<ParsedFile::Statement*>($1)->Next = dynamic_cast<ParsedFile::Statement*>($2);
  $$ = $1;
};

Edit: OK,因此我可以做这样的事情:

switch_statement
: SWITCH  (  expression  ) 
{ 
  auto Switch = p.Make<ParsedFile::SwitchStatement>();
  Switch->Test = dynamic_cast<ParsedFile::Expression*>($3);
  p.NewScope();
  $$ = Switch;
}
 {  case_statements  } 
{
  auto Switch = dynamic_cast<ParsedFile::SwitchStatement*>($5);
  Switch->Cases = p.statements.top();
  p.PopScope();
  p.AddToCurrentScope(Switch);
};

default_statement
: DEFAULT  :  
{
  auto Default = p.Make<ParsedFile::DefaultStatement>();
  p.NewScope();
  $$ = Default;
}
statements
{
  auto Default = dynamic_cast<ParsedFile::DefaultStatement*>($3);
  Default->Statements = p.statements.top();
  p.PopScope();
  p.AddToCurrentScope(Default);
};

case_statement
: CASE expression 
{
  auto case = p.Make<ParsedFile::CaseStatement>();
  p->Value = dynamic_cast<ParsedFile::Expression*>($2);
  p.NewScope();
  $$ = case;
}
DOUBLE_COLON statements
{
  auto Case = dynamic_cast<ParsedFile::CaseStatement*>($3);
  Case->Statements = p.statements.top();
  p.PopScope();
  p.AddToCurrentScope(Case); 
};

case_statements
: case_statement
| case_statements case_statement
| case_statements default_statement;
最佳回答

它指左边,即左边。

(((statement) statement) statement) statement

You can tell that this is the only possibility because this can be reduced to statements statement, which is one of your productions. The other option

statement (statement (statement (statement))))

页: 1 然而,如果你想要的是正确的联系,你就可以这样做。

你的法典不会产生一个相互关联的清单,因为在与他人一起发言之后,你回到了first<>>>>> /em> 声明的点子上,因此,在你重写第1号声明的时,下一个声明就在座。

改变正确联系的次序应解决这一问题,但指出这需要在发言数目中留出线性平线空间。 因此,如果你希望能发表许多声明,你就应考虑以相反的方式制定相关名单。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?