代码之家  ›  专栏  ›  技术社区  ›  varnie

振奋精神,推进宣言议题

  •  3
  • varnie  · 技术社区  · 16 年前

    foo(123);//<-- our parser targets here. we estimate we have a function 
             //    invocation, but we have no idea about foo declaration/prototype,
             //     so we can't be sure that "foo" takes one integer argument.   
    
    
    void foo(int i){
    //...
    }
    

    AST

    3 回复  |  直到 16 年前
        1
  •  3
  •   MSalters    16 年前

    [第二个答案,关于语义]

    模板

        2
  •  2
  •   BatchyX    16 年前

    [functionCall(name, args)]
    [functionDef(name, args, body)]
    

    它们将被定义为这样(不是正确的精神语法,但你明白了)

    functionCall(string name, vector<string> args)
    {
      if (!first_pass) {
        // check args for validity
        // whatever else you need to do
      }
    } 
    
    functionDef(string name, vector<string> args, ... body)
    {
      if (first_pass)
        // Add function decleration to symbol table
      else
        // define function
    }
    
        3
  •  1
  •   MSalters    16 年前

    我认为你做出了毫无根据的假设。例如,“很明显,我们必须至少有两次传球”。不,它不是。如果语法是这样的 foo(123) function-name "(" expression ")" ,那么一次传球就足够了。

    因此,我建议为明确的解析设计语法。避免不能单独解析的构造,例如避免依赖于此处的声明。