Question

In general terms, how can I call this? I don't know if it's a statement or a unary expression or what else. My guess it's an unary expression because they need only one thing: its arguments to work, right? I think I'm looking for a compiler-term but I can't find that myself.

Was it helpful?

Solution

A function call is a function call. It is a separate production rule in the formal grammar of the language.

The production rule usually looks something like:

function_call := identifier '(' argument_list ')'
argument_list := expression ( ',' argument_list )*

which implies that there is just one rule for all function calls no matter how many arguments.

Since function call returns a value, the whole thing is one kind of expression. But not "unary expression", that's not something common to different languages. The production rules for expression look somewhat like this:

expression := literal
expression := '(' expression ')'
expression := function_call
...

I am oversimplifying a bit with the expression. The expression rule generally falls apart to many different levels by operator precedence. E.g. the argument_list rule would contain the expression with precedence higher than comma and the rule with parenthesis would have the lowest-precedence expression inside and produce the highest-precedence one. But that's the general idea.

The highest precedence expression is in some cases called "unary expression", in which case a function call would be unary expression. It may be called something else, depends on the grammar definition of particular language.

Note that the above applies to languages where function calls have arbitrary number of arguments. There are some languages, like Haskell where function calls may only have one argument (multi-argument functions are implemented via currying).

OTHER TIPS

In many languages, the typical grammar rules are a bit more general than those given by Jan Hudec in the other answer:

function_call ::= expression '(' argument_list ')'
argument_list ::= expression ( ',' argument_list )*
expression ::= ... | identifier | function_call

Notice that a function call starts with an expression and is followed by a construction that starts and ends with a terminal symbol. This can easily be seen as a unary expression with a postfix operator (namely, the parentheses and all the arguments in between them). For example, C and C++ do it like this.

Some languages, such as Haskell, treat a function call as a binary operator expression, with the binary "operator" being an empty sequence of symbols:

function_call ::= expression argument_expression
expression ::= ... | function_call | argument_expression

Here, argument_expression includes all expressions that can syntactically be a function argument (and includes things like parenthesized expressions and tuples).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top